1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2 <html>
3  <head>
4<!--
5  $Id: StringTests.html 108 2006-11-15 12:30:32Z wingedfox $
6  $HeadURL: https://svn.debugger.ru/repos/jslibs/BrowserExtensions/tags/BrowserExtensions.001/tests/StringTests.html $
7
8  Automated tests for the StringExtensions module
9  @author Ilya Lebedev <ilya@lebedev.net>
10-->
11   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
12   <title>BrowserExtensions/StringExtensions tests</title>
13   <link rel="stylesheet" type="text/css" href="../css/jsUnitStyle.css">
14   <script language="JavaScript" type="text/javascript" src="jsUnitCore.js"></script>
15   <script language="JavaScript" type="text/javascript" src="../helpers.js"></script>
16   <script language="JavaScript" type="text/javascript" src="../stringextensions.js"></script>
17   <script language="JavaScript" type="text/javascript" src="../numberextensions.js"></script>
18   <script language="JavaScript" type="text/javascript" src="../regexpextensions.js"></script>
19   <script language="JavaScript" type="text/javascript">
20
21function testLtrim() {
22    assertEquals("Ltrim strips whitespaces, when no parameter supplied", "test", "   test".ltrim());
23    assertEquals("Ltrim strips a char, when char parameter supplied", "test", "1111test".ltrim('1'));
24    assertEquals("Ltrim strips a sequentional substring, when string parameter supplied", "test", "rereretest".ltrim('re'));
25}
26function testRtrim() {
27    assertEquals("Rtrim strips whitespaces, when no parameter supplied", "test", "test    ".rtrim());
28    assertEquals("Rtrim strips a char, when char parameter supplied", "test", "test11111".rtrim('1'));
29    assertEquals("Rtrim strips a sequentional substring, when string parameter supplied", "test", "testrerere".rtrim('re'));
30}
31function testTrim() {
32    assertEquals("Trim strips whitespaces, when no parameter supplied", "test", "    test    ".trim());
33    assertEquals("Trim strips a char, when char parameter supplied", "2test", "11112test11111".trim('1'));
34    assertEquals("Trim strips a sequentional substring, when string parameter supplied", "test", "rereretestrerere".trim('re'));
35}
36function testDup() {
37    assertEquals("Dup correctly duplicates the string", "testtest", "test".dup());
38}
39function testPadding() {
40    assertEquals("Padding does not pad, when no number supplied", "test", "test".padding());
41    assertEquals("Padding uses space by default", "test   ", "test".padding(7));
42    assertEquals("Padding applies padding at the left side, when negative number supplied", "   test", "test".padding(-7));
43    assertEquals("Padding does use supplied fill char", "000test", "test".padding(-7,'0'));
44    assertEquals("Padding does use only first char from the supplied string", "000test", "test".padding(-7,'01234'));
45}
46function testPadLeft() {
47    assertEquals("PadLeft is equal to padding with negative counter", "test".padding(-7), "test".padLeft(7));
48    assertEquals("PadLeft does use specified fill char", "111test", "test".padLeft(7, '1'));
49}
50function testPadRight() {
51    assertEquals("PadRight is equal to padding with positive counter", "test".padding(7), "test".padRight(7));
52    assertEquals("PadRight does use specified fill char", "test111", "test".padRight(7, '1'));
53}
54function testRepeat() {
55    assertEquals("Repeat does not repeat, when no number specified", "", "test".repeat());
56    assertEquals("Repeat does not repeat, when negative number specified", "", "test".repeat(-2));
57    assertEquals("Repeat return the same string, when 1 specified", "test", "test".repeat(1));
58    assertEquals("Repeat does repeat string exact number of times, as specified", "testtesttest", "test".repeat(3));
59}
60function testSprintfCommon() {
61    assertEquals("%c should print the char from supplied code", "\"", "%c".sprintf(34));
62    assertEquals("%d should print trunced floats", "34", "%d".sprintf(34.41));
63    assertEquals("%u should trunc vars and print absolute value", "34", "%u".sprintf(-34.41));
64    assertEquals("%2$d %d %0$d", "3 1 1", "%2$d %d %0$d".sprintf(1, 2, 3));
65    assertEquals("<% d>", "< 12>", "<% d>".sprintf(12));
66    assertEquals("<%+d>", "<+12>", "<%+d>".sprintf(12));
67    assertEquals("<%6s>", "<    12>", "<%6s>".sprintf(12));
68    assertEquals("<%-6s>", "<12    >", "<%-6s>".sprintf(12));
69    assertEquals("<%#x>", "<0xc>", "<%#x>".sprintf(12));
70}
71function testSprintfFlags() {
72    assertEquals("<%06s>", "<000012>", "<%06s>".sprintf(12));
73    assertEquals("<%+.6d>", "<+000012>", "<%+.6d>".sprintf(12));
74    assertEquals("<%+.6d>", "<-000012>", "<%+.6d>".sprintf(-12));
75    assertEquals("<% .6d>", "< 000012>", "<% .6d>".sprintf(12));
76    assertEquals("<% .6d>", "<-000012>", "<% .6d>".sprintf(-12));
77}
78function testSprintfMinwidth() {
79    // minimum width
80    assertEquals("<%03d>", "<001>", "<%03d>".sprintf(1));
81    assertEquals("<%+08.3d>", "<    +001>", "<%+08.3d>".sprintf(1));
82    assertEquals("<%+08.3d>", "<    -001>", "<%+08.3d>".sprintf(-1));
83    assertEquals("<%s>", "<a>", "<%s>".sprintf('a'));
84    assertEquals("<%6s>", "<     a>", "<%6s>".sprintf('a'));
85    assertEquals("<%*s>", "<     a>", "<%*s>".sprintf(6,'a'));
86    assertEquals("<%*1$s>", "<     a>", "<%*1$s>".sprintf('a',6));
87    assertEquals("<%2s>", "<long>", "<%2s>".sprintf('long'));
88}
89function testSprintfPrecision() {
90    // precision
91    assertEquals("<%f>", "<1.000000>", "<%f>".sprintf(1));
92    assertEquals("<%.1f>", "<1.0>", "<%.1f>".sprintf(1));
93    assertEquals("<%.0f>", "<1>", "<%.0f>".sprintf(1));
94    assertEquals("<%e>", "<1.000000e+1>", "<%e>".sprintf(10));
95    assertEquals("<%.1e>", "<1.0e+1>", "<%.1e>".sprintf(10));
96
97    assertEquals("<%.6x>", "<000001>", "<%.6x>".sprintf(1));
98    assertEquals("<%#.6x>", "<0x000001>", "<%#.6x>".sprintf(1));
99    assertEquals("<%-10.6x>", "<000001    >", "<%-10.6x>".sprintf(1));
100
101    assertEquals("<%.5s>", "<trunc>", "<%.5s>".sprintf('truncated'));
102    assertEquals("<%10.5s>", "<     trunc>", "<%10.5s>".sprintf('truncated'));
103
104    assertEquals("<%.*x>", "<000001>", "<%.*x>".sprintf(6,1));
105    assertEquals("<%.*1$x>", "<000001>", "<%.*1$x>".sprintf(1,6));
106}
107function testSprintfArgOrder() {
108    // precision
109    assertEquals("<%*.*s>", "<te   >", "<%*.*s>".sprintf(-5,2,'test'));
110    assertEquals("<%-*.*s>", "<te   >", "<%-*.*s>".sprintf(5,2,'test'));
111    assertEquals("<%*.*s>", "<   te>", "<%*.*s>".sprintf(5,2,'test'));
112
113    assertEquals("<%*0$.*s>", "<te>", "<%*0$.*s>".sprintf(2,'test'));
114    assertEquals("<%-*0$.*s>", "<te>", "<%-*0$.*s>".sprintf(-2,'test'));
115    assertEquals("<%*0$.*s>", "<te>", "<%*0$.*s>".sprintf(2,'test'));
116
117    assertEquals("%1$d %d\n", "34 12\n", "%1$d %d\n".sprintf(12, 34));
118    assertEquals("%1$d %d %d\n", "34 12 34\n", "%1$d %d %d\n".sprintf(12, 34));
119    assertEquals("%2$d %d %d\n", "56 12 34\n", "%2$d %d %d\n".sprintf(12, 34, 56));
120    assertEquals("%1$*2$d %d\n", " 34 12\n", "%1$*2$d %d\n".sprintf(12, 34, 3));
121}
122</script>
123</head>
124
125<body>
126<h1>Tests for the String extensions</h1>
127
128<p>This page contains tests for the String.prototype extension methods. To see them, take a look at the source.</p>
129</body>
130</html>
131