jscript: Added String.valueOf implementation.
[wine] / dlls / jscript / tests / api.js
1 /*
2  * Copyright 2008 Jacek Caban for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 var tmp;
20
21 tmp = "" + new Object();
22 ok(tmp === "[object Object]", "'' + new Object() = " + tmp);
23
24 ok("".length === 0, "\"\".length = " + "".length);
25 ok(getVT("".length) == "VT_I4", "\"\".length = " + "".length);
26 ok("abc".length === 3, "\"abc\".length = " + "abc".length);
27 ok(String.prototype.length === 0, "String.prototype.length = " + String.prototype.length);
28
29 tmp = "".toString();
30 ok(tmp === "", "''.toString() = " + tmp);
31 tmp = "test".toString();
32 ok(tmp === "test", "''.toString() = " + tmp);
33 tmp = "test".toString(3);
34 ok(tmp === "test", "''.toString(3) = " + tmp);
35
36 tmp = "".valueOf();
37 ok(tmp === "", "''.valueOf() = " + tmp);
38 tmp = "test".valueOf();
39 ok(tmp === "test", "''.valueOf() = " + tmp);
40 tmp = "test".valueOf(3);
41 ok(tmp === "test", "''.valueOf(3) = " + tmp);
42
43 tmp = "abc".charAt(0);
44 ok(tmp === "a", "'abc',charAt(0) = " + tmp);
45 tmp = "abc".charAt(1);
46 ok(tmp === "b", "'abc',charAt(1) = " + tmp);
47 tmp = "abc".charAt(2);
48 ok(tmp === "c", "'abc',charAt(2) = " + tmp);
49 tmp = "abc".charAt(3);
50 ok(tmp === "", "'abc',charAt(3) = " + tmp);
51 tmp = "abc".charAt(4);
52 ok(tmp === "", "'abc',charAt(4) = " + tmp);
53 tmp = "abc".charAt();
54 ok(tmp === "a", "'abc',charAt() = " + tmp);
55 tmp = "abc".charAt(-1);
56 ok(tmp === "", "'abc',charAt(-1) = " + tmp);
57 tmp = "abc".charAt(0,2);
58 ok(tmp === "a", "'abc',charAt(0.2) = " + tmp);
59
60 tmp = "abcd".substring(1,3);
61 ok(tmp === "bc", "'abcd'.substring(1,3) = " + tmp);
62 tmp = "abcd".substring(-1,3);
63 ok(tmp === "abc", "'abcd'.substring(-1,3) = " + tmp);
64 tmp = "abcd".substring(1,6);
65 ok(tmp === "bcd", "'abcd'.substring(1,6) = " + tmp);
66 tmp = "abcd".substring(3,1);
67 ok(tmp === "bc", "'abcd'.substring(3,1) = " + tmp);
68 tmp = "abcd".substring(2,2);
69 ok(tmp === "", "'abcd'.substring(2,2) = " + tmp);
70 tmp = "abcd".substring(true,"3");
71 ok(tmp === "bc", "'abcd'.substring(true,'3') = " + tmp);
72 tmp = "abcd".substring(1,3,2);
73 ok(tmp === "bc", "'abcd'.substring(1,3,2) = " + tmp);
74 tmp = "abcd".substring();
75 ok(tmp === "abcd", "'abcd'.substring() = " + tmp);
76
77 var arr = new Array();
78 ok(typeof(arr) === "object", "arr () is not object");
79 ok((arr.length === 0), "arr.length is not 0");
80 ok(arr["0"] === undefined, "arr[0] is not undefined");
81
82 var arr = new Array(1, 2, "test");
83 ok(typeof(arr) === "object", "arr (1,2,test) is not object");
84 ok((arr.length === 3), "arr.length is not 3");
85 ok(arr["0"] === 1, "arr[0] is not 1");
86 ok(arr["1"] === 2, "arr[1] is not 2");
87 ok(arr["2"] === "test", "arr[2] is not \"test\"");
88
89 arr["7"] = true;
90 ok((arr.length === 8), "arr.length is not 8");
91
92 tmp = "" + [];
93 ok(tmp === "", "'' + [] = " + tmp);
94 tmp = "" + [1,true];
95 ok(tmp === "1,true", "'' + [1,true] = " + tmp);
96
97 var arr = new Array(6);
98 ok(typeof(arr) === "object", "arr (6) is not object");
99 ok((arr.length === 6), "arr.length is not 6");
100 ok(arr["0"] === undefined, "arr[0] is not undefined");
101
102 ok(arr.push() === 6, "arr.push() !== 6");
103 ok(arr.push(1) === 7, "arr.push(1) !== 7");
104 ok(arr[6] === 1, "arr[6] != 1");
105 ok(arr.length === 7, "arr.length != 10");
106 ok(arr.push(true, 'b', false) === 10, "arr.push(true, 'b', false) !== 10");
107 ok(arr[8] === "b", "arr[8] != 'b'");
108 ok(arr.length === 10, "arr.length != 10");
109
110 arr = [1,2,null,false,undefined,,"a"];
111
112 tmp = arr.join();
113 ok(tmp === "1,2,,false,,,a", "arr.join() = " + tmp);
114 tmp = arr.join(";");
115 ok(tmp === "1;2;;false;;;a", "arr.join(';') = " + tmp);
116 tmp = arr.join(";","test");
117 ok(tmp === "1;2;;false;;;a", "arr.join(';') = " + tmp);
118 tmp = arr.join("");
119 ok(tmp === "12falsea", "arr.join('') = " + tmp);
120
121 tmp = arr.toString();
122 ok(tmp === "1,2,,false,,,a", "arr.toString() = " + tmp);
123 tmp = arr.toString("test");
124 ok(tmp === "1,2,,false,,,a", "arr.toString() = " + tmp);
125
126 arr = [5,true,2,-1,3,false,"2.5"];
127 tmp = arr.sort(function(x,y) { return y-x; });
128 ok(tmp === arr, "tmp !== arr");
129 tmp = [5,3,"2.5",2,true,false,-1];
130 for(var i=0; i < arr.length; i++)
131     ok(arr[i] === tmp[i], "arr[" + i + "] = " + arr[i] + " expected " + tmp[i]);
132
133 arr = [5,false,2,0,"abc",3,"a",-1];
134 tmp = arr.sort();
135 ok(tmp === arr, "tmp !== arr");
136 tmp = [-1,0,2,3,5,"a","abc",false];
137 for(var i=0; i < arr.length; i++)
138     ok(arr[i] === tmp[i], "arr[" + i + "] = " + arr[i] + " expected " + tmp[i]);
139
140 arr = ["a", "b", "ab"];
141 tmp = ["a", "ab", "b"];
142 ok(arr.sort() === arr, "arr.sort() !== arr");
143 for(var i=0; i < arr.length; i++)
144     ok(arr[i] === tmp[i], "arr[" + i + "] = " + arr[i] + " expected " + tmp[i]);
145
146 var num = new Number(6);
147 arr = [0,1,2];
148 tmp = arr.concat(3, [4,5], num);
149 ok(tmp !== arr, "tmp === arr");
150 for(var i=0; i<6; i++)
151     ok(tmp[i] === i, "tmp[" + i + "] = " + tmp[i]);
152 ok(tmp[6] === num, "tmp[6] !== num");
153 ok(tmp.length === 7, "tmp.length = " + tmp.length);
154
155 arr = [].concat();
156 ok(arr.length === 0, "arr.length = " + arr.lrngth);
157
158 arr = [1,];
159 tmp = arr.concat([2]);
160 ok(tmp.length === 3, "tmp.length = " + tmp.length);
161 ok(tmp[1] === undefined, "tmp[1] = " + tmp[1]);
162
163 var num = new Number(2);
164 ok(num.toString() === "2", "num(2).toString !== 2");
165 var num = new Number();
166 ok(num.toString() === "0", "num().toString !== 0");
167
168 ok(Number() === 0, "Number() = " + Number());
169 ok(Number(false) === 0, "Number(false) = " + Number(false));
170 ok(Number("43") === 43, "Number('43') = " + Number("43"));
171
172 reportSuccess();