-
Notifications
You must be signed in to change notification settings - Fork 219
/
String_test.mxml
203 lines (162 loc) · 7.73 KB
/
String_test.mxml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?xml version="1.0"?>
<mx:Application name="lightspark_String_test"
xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute"
applicationComplete="appComplete();"
backgroundColor="white">
<mx:Script>
<![CDATA[
import Tests;
private var callbackArgs:Array;
private function appComplete():void
{
//Match tests
var str:String = new String("matchablemat");
var ret:Array = str.match("mat");
Tests.assertArrayEquals(["mat"], ret, "match(): normal string match", true);
var re:RegExp = new RegExp("m a* t | a[a-z]*e","x")
ret = str.match(re);
Tests.assertArrayEquals(["mat"], ret, "match(): non-global extended regexp match", true);
re = new RegExp("MA*T|a[a-z]*e","i")
ret = str.match(re);
Tests.assertArrayEquals(["mat"], ret, "match(): non-global case-insensitive regexp match", true);
re = new RegExp("ma*t|a[a-z]*e","")
ret = str.match(re);
Tests.assertArrayEquals(["mat"], ret, "match(): non-global regexp match", true);
re = new RegExp("ma*t|a[a-z]*e","g")
ret = str.match(re);
Tests.assertArrayEquals(["mat", "able", "mat"], ret, "match(): global regexp match", true);
re = new RegExp("nomatch", "g");
ret = str.match(re);
Tests.assertEquals(0, ret.length, "match(): no match");
//Replace tests
str = new String("replaceable");
var ret2:String = str.replace("l","ll");
Tests.assertEquals("repllaceable", ret2, "replace(): normal string replace with string", true);
re = new RegExp("(p | b)l", "x")
ret2 = str.replace(re,"ll");
Tests.assertEquals("rellaceable", ret2, "replace(): non-global extended regexp replace with string", true);
re = new RegExp("[PB]l", "i")
ret2 = str.replace(re,"ll");
Tests.assertEquals("rellaceable", ret2, "replace(): non-global case-insensetive regexp replace with string", true);
re = new RegExp("[pb]l")
ret2 = str.replace(re,"ll");
Tests.assertEquals("rellaceable", ret2, "replace(): non-global regexp replace with string", true);
re = new RegExp("[pb]l","g")
callbackArgs = new Array();
ret2 = str.replace(re,func1);
Tests.assertEquals("refuncaceafunce", ret2, "replace(): non-global regexp replace with function", true);
Tests.assertEquals(2, callbackArgs.length);
Tests.assertArrayEquals(["pl", 2, "replaceable"], callbackArgs[0],
"replace(): non-global regexp replace with function callback arguments", true);
Tests.assertArrayEquals(["bl", 8, "replaceable"], callbackArgs[1],
"replace(): non-global regexp replace with function callback arguments", true);
re = new RegExp("(([pb])l)","g");
callbackArgs = new Array();
ret2 = str.replace(re,func1);
Tests.assertEquals(2, callbackArgs.length);
Tests.assertEquals("refuncaceafunce", ret2, "replace(): non-global regexp replace with function", true);
Tests.assertArrayEquals(["pl","pl","p",2,"replaceable"], callbackArgs[0],
"replace(): global regexp replace with capturing groups with function callback arguments", false);
Tests.assertArrayEquals(["bl","bl","b",8,"replaceable"], callbackArgs[1],
"replace(): global regexp replace with capturing groups with function callback arguments", false);
ret2 = str.replace(/\s*/g, "-");
Tests.assertEquals("-r-e-p-l-a-c-e-a-b-l-e-", ret2, "replace(): whitespace", true);
//Split tests
str = new String("splittable");
var ret3:Array = str.split("l");
Tests.assertArrayEquals(["sp", "ittab", "e"], ret3, "split(): normal string split", true);
re = new RegExp("pl | bl","x")
ret3 = str.split(re);
Tests.assertArrayEquals(["s", "itta", "e"], ret3, "split(): non capturing extended regexp split", true);
re = new RegExp("[PB]l","i")
ret3 = str.split(re);
Tests.assertArrayEquals(["s", "itta", "e"], ret3, "split(): non capturing case-insensitive regexp split", true);
re = new RegExp("[pb]l","")
ret3 = str.split(re);
Tests.assertArrayEquals(["s", "itta", "e"], ret3, "split(): non capturing regexp split", true);
re = new RegExp("[pb](l)","")
ret3 = str.split(re);
Tests.assertArrayEquals(["s", "l", "itta", "l", "e"], ret3, "split(): capturing regexp split", true);
re = new RegExp("^$","")
ret3 = str.split(re);
Tests.assertArrayEquals(["splittable"], ret3, "split(): null matching regexp split", true);
ret3 = str.split("");
Tests.assertArrayEquals(["s", "p", "l", "i", "t", "t", "a", "b", "l", "e"], ret3, "split(): empty string split", true);
//Search tests
str = new String("searchable");
var ret4:int = str.search("e");
Tests.assertEquals(1, ret4, "search(): normal string search");
re = new RegExp("e | a", "x")
ret4 = str.search(re);
Tests.assertEquals(1, ret4, "search(): extended regexp search");
re = new RegExp("[EA]", "i")
ret4 = str.search(re);
Tests.assertEquals(1, ret4, "search(): case-insensitive regexp search");
re = new RegExp("[ea]")
ret4 = str.search(re);
Tests.assertEquals(1, ret4, "search(): regexp search");
re = new RegExp("[ea]", "g")
ret4 = str.search(re);
Tests.assertEquals(1, ret4, "search(): global regexp search");
re = new RegExp("([ea])")
ret4 = str.search(re);
Tests.assertEquals(1, ret4, "search(): capturing regexp search");
//Substring tests
str = new String("sliceable");
var ret5:String = str.substr(5);
Tests.assertEquals("able", ret5, "substr(): start", true);
ret5 = str.substr(5, 2);
Tests.assertEquals("ab", ret5, "substr(): start and len", true);
ret5 = str.substring(5);
Tests.assertEquals("able", ret5, "substring(): start", true);
ret5 = str.substring(5, 7);
Tests.assertEquals("ab", ret5, "substring(): start and end", true);
ret5 = str.slice(5, 7);
Tests.assertEquals("ab", ret5, "slice(): positive parameters", true);
ret5 = str.slice(-4, -2);
Tests.assertEquals("ab", ret5, "slice(): negative parameters", true);
//indexOf tests
str = "abc";
Tests.assertEquals(str.indexOf("b", -10), 1, "indexOf() negative offset");
Tests.assertEquals(str.indexOf("b", 3), -1, "indexOf() offset too big");
//lastIndexOf tests
var e:String="abcdb";
Tests.assertEquals(e.lastIndexOf("b"), 4, "lastIndexOf() no offset");
Tests.assertEquals(e.lastIndexOf("b", 1), 1, "lastIndexOf() positive offset");
Tests.assertEquals(e.lastIndexOf("b", 0), -1, "lastIndexOf() zero offset");
Tests.assertEquals(e.lastIndexOf("ab", -100), -1, "lastIndexOf() negative offset (1)");
Tests.assertEquals(e.lastIndexOf("bc", -100), -1, "lastIndexOf() negative offset (2)");
Tests.assertEquals(e.lastIndexOf("b", NaN), 4, "lastIndexOf() NaN offset");
Tests.assertEquals(e.lastIndexOf("b", undefined), 4, "lastIndexOf() undefined offset");
Tests.assertEquals(e.lastIndexOf("b", 20), 4, "lastIndexOf() too big");
//charCodeAt tests
e=String.fromCharCode(128);
Tests.assertEquals(0x80,e.charCodeAt(0),"charCodeAt");
//Type conversions
var mc:MovieClip = new MovieClip();
Tests.assertEquals(String(NaN), "NaN", "String(NaN)", true);
Tests.assertEquals(String(3.2), "3.2", "String(3.2)", true);
Tests.assertEquals(String(3.8), "3.8", "String(3.8)", true);
Tests.assertEquals(String(true), "true", "String(true)", true);
Tests.assertEquals(String(false), "false", "String(false)", true);
Tests.assertEquals(String(-4), "-4", "String(-4)", true);
Tests.assertEquals(String(null),"null","String(null)",true);
Tests.assertEquals(String(undefined),"undefined","String(undefined)",true);
Tests.assertEquals(String(mc),"[object MovieClip]","String(MovieClip)",true);
var array:Array = ["primary", "secondary", "tertiary"];
Tests.assertEquals(String(array),"primary,secondary,tertiary","String(Array)",true);
var str1:String = "";
var str2:String = str1.replace("", "ins");
Tests.assertEquals("ins", str2, "replace on empty string");
Tests.report(visual, this.name);
}
private function func1():String
{
callbackArgs.push(arguments);
return "func";
}
]]>
</mx:Script>
<mx:UIComponent id="visual" />
</mx:Application>