jscript: Added String.toString 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 = "abc".charAt(0);
37 ok(tmp === "a", "'abc',charAt(0) = " + tmp);
38 tmp = "abc".charAt(1);
39 ok(tmp === "b", "'abc',charAt(1) = " + tmp);
40 tmp = "abc".charAt(2);
41 ok(tmp === "c", "'abc',charAt(2) = " + tmp);
42 tmp = "abc".charAt(3);
43 ok(tmp === "", "'abc',charAt(3) = " + tmp);
44 tmp = "abc".charAt(4);
45 ok(tmp === "", "'abc',charAt(4) = " + tmp);
46 tmp = "abc".charAt();
47 ok(tmp === "a", "'abc',charAt() = " + tmp);
48 tmp = "abc".charAt(-1);
49 ok(tmp === "", "'abc',charAt(-1) = " + tmp);
50 tmp = "abc".charAt(0,2);
51 ok(tmp === "a", "'abc',charAt(0.2) = " + tmp);
52
53 tmp = "abcd".substring(1,3);
54 ok(tmp === "bc", "'abcd'.substring(1,3) = " + tmp);
55 tmp = "abcd".substring(-1,3);
56 ok(tmp === "abc", "'abcd'.substring(-1,3) = " + tmp);
57 tmp = "abcd".substring(1,6);
58 ok(tmp === "bcd", "'abcd'.substring(1,6) = " + tmp);
59 tmp = "abcd".substring(3,1);
60 ok(tmp === "bc", "'abcd'.substring(3,1) = " + tmp);
61 tmp = "abcd".substring(2,2);
62 ok(tmp === "", "'abcd'.substring(2,2) = " + tmp);
63 tmp = "abcd".substring(true,"3");
64 ok(tmp === "bc", "'abcd'.substring(true,'3') = " + tmp);
65 tmp = "abcd".substring(1,3,2);
66 ok(tmp === "bc", "'abcd'.substring(1,3,2) = " + tmp);
67 tmp = "abcd".substring();
68 ok(tmp === "abcd", "'abcd'.substring() = " + tmp);
69
70 var arr = new Array();
71 ok(typeof(arr) === "object", "arr () is not object");
72 ok((arr.length === 0), "arr.length is not 0");
73 ok(arr["0"] === undefined, "arr[0] is not undefined");
74
75 var arr = new Array(1, 2, "test");
76 ok(typeof(arr) === "object", "arr (1,2,test) is not object");
77 ok((arr.length === 3), "arr.length is not 3");
78 ok(arr["0"] === 1, "arr[0] is not 1");
79 ok(arr["1"] === 2, "arr[1] is not 2");
80 ok(arr["2"] === "test", "arr[2] is not \"test\"");
81
82 arr["7"] = true;
83 ok((arr.length === 8), "arr.length is not 8");
84
85 tmp = "" + [];
86 ok(tmp === "", "'' + [] = " + tmp);
87 tmp = "" + [1,true];
88 ok(tmp === "1,true", "'' + [1,true] = " + tmp);
89
90 var arr = new Array(6);
91 ok(typeof(arr) === "object", "arr (6) is not object");
92 ok((arr.length === 6), "arr.length is not 6");
93 ok(arr["0"] === undefined, "arr[0] is not undefined");
94
95 ok(arr.push() === 6, "arr.push() !== 6");
96 ok(arr.push(1) === 7, "arr.push(1) !== 7");
97 ok(arr[6] === 1, "arr[6] != 1");
98 ok(arr.length === 7, "arr.length != 10");
99 ok(arr.push(true, 'b', false) === 10, "arr.push(true, 'b', false) !== 10");
100 ok(arr[8] === "b", "arr[8] != 'b'");
101 ok(arr.length === 10, "arr.length != 10");
102
103 arr = [1,2,null,false,undefined,,"a"];
104
105 tmp = arr.join();
106 ok(tmp === "1,2,,false,,,a", "arr.join() = " + tmp);
107 tmp = arr.join(";");
108 ok(tmp === "1;2;;false;;;a", "arr.join(';') = " + tmp);
109 tmp = arr.join(";","test");
110 ok(tmp === "1;2;;false;;;a", "arr.join(';') = " + tmp);
111 tmp = arr.join("");
112 ok(tmp === "12falsea", "arr.join('') = " + tmp);
113
114 tmp = arr.toString();
115 ok(tmp === "1,2,,false,,,a", "arr.toString() = " + tmp);
116 tmp = arr.toString("test");
117 ok(tmp === "1,2,,false,,,a", "arr.toString() = " + tmp);
118
119 arr = [5,true,2,-1,3,false,"2.5"];
120 tmp = arr.sort(function(x,y) { return y-x; });
121 ok(tmp === arr, "tmp !== arr");
122 tmp = [5,3,"2.5",2,true,false,-1];
123 for(var i=0; i < arr.length; i++)
124     ok(arr[i] === tmp[i], "arr[" + i + "] = " + arr[i] + " expected " + tmp[i]);
125
126 arr = [5,false,2,0,"abc",3,"a",-1];
127 tmp = arr.sort();
128 ok(tmp === arr, "tmp !== arr");
129 tmp = [-1,0,2,3,5,"a","abc",false];
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 = ["a", "b", "ab"];
134 tmp = ["a", "ab", "b"];
135 ok(arr.sort() === arr, "arr.sort() !== arr");
136 for(var i=0; i < arr.length; i++)
137     ok(arr[i] === tmp[i], "arr[" + i + "] = " + arr[i] + " expected " + tmp[i]);
138
139 var num = new Number(6);
140 arr = [0,1,2];
141 tmp = arr.concat(3, [4,5], num);
142 ok(tmp !== arr, "tmp === arr");
143 for(var i=0; i<6; i++)
144     ok(tmp[i] === i, "tmp[" + i + "] = " + tmp[i]);
145 ok(tmp[6] === num, "tmp[6] !== num");
146 ok(tmp.length === 7, "tmp.length = " + tmp.length);
147
148 arr = [].concat();
149 ok(arr.length === 0, "arr.length = " + arr.lrngth);
150
151 arr = [1,];
152 tmp = arr.concat([2]);
153 ok(tmp.length === 3, "tmp.length = " + tmp.length);
154 ok(tmp[1] === undefined, "tmp[1] = " + tmp[1]);
155
156 var num = new Number(2);
157 ok(num.toString() === "2", "num(2).toString !== 2");
158 var num = new Number();
159 ok(num.toString() === "0", "num().toString !== 0");
160
161 ok(Number() === 0, "Number() = " + Number());
162 ok(Number(false) === 0, "Number(false) = " + Number(false));
163 ok(Number("43") === 43, "Number('43') = " + Number("43"));
164
165 reportSuccess();