OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-2016 Eyeo GmbH | 3 * Copyright (C) 2006-2016 Eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 package org.adblockplus.libadblockplus.tests; | 18 package org.adblockplus.libadblockplus.tests; |
19 | 19 |
20 import org.adblockplus.libadblockplus.AdblockPlusException; | 20 import org.adblockplus.libadblockplus.AdblockPlusException; |
21 import org.adblockplus.libadblockplus.JsValue; | 21 import org.adblockplus.libadblockplus.JsValue; |
22 | 22 |
23 import org.junit.Test; | 23 import org.junit.Test; |
24 | 24 |
25 import java.util.ArrayList; | 25 import java.util.ArrayList; |
26 import java.util.List; | 26 import java.util.List; |
27 | 27 |
28 public class JsTest extends BaseJsTest | 28 public class JsTest extends BaseJsTest |
29 { | 29 { |
30 @Test | 30 @Test |
31 public void testUndefinedValue() | 31 public void testUndefinedValue() |
32 { | 32 { |
33 final JsValue value = jsEngine.evaluate("undefined"); | 33 final JsValue value = jsEngine.evaluate("undefined"); |
34 assertTrue(value.isUndefined()); | 34 assertTrue(value.isUndefined()); |
35 assertFalse(value.isNull()); | 35 assertFalse(value.isNull()); |
36 assertFalse(value.isString()); | 36 assertFalse(value.isString()); |
37 assertFalse(value.isBoolean()); | 37 assertFalse(value.isBoolean()); |
38 assertFalse(value.isNumber()); | 38 assertFalse(value.isNumber()); |
39 assertFalse(value.isObject()); | 39 assertFalse(value.isObject()); |
40 assertFalse(value.isArray()); | 40 assertFalse(value.isArray()); |
41 assertEquals("undefined", value.asString()); | 41 assertEquals("undefined", value.asString()); |
42 try | 42 try |
43 { | 43 { |
44 value.asList(); | 44 value.asList(); |
45 fail(AdblockPlusException.class.getSimpleName() + " is expected to b
e thrown"); | 45 fail(AdblockPlusException.class.getSimpleName() + " is expected to be thro
wn"); |
46 } catch (AdblockPlusException e) | 46 } catch (AdblockPlusException e) |
47 { | 47 { |
48 // ignored | 48 // ignored |
49 } | 49 } |
50 try | 50 try |
51 { | 51 { |
52 value.getProperty("foo"); | 52 value.getProperty("foo"); |
53 fail(AdblockPlusException.class.getSimpleName() + " is expected to b
e thrown"); | 53 fail(AdblockPlusException.class.getSimpleName() + " is expected to be thro
wn"); |
54 } catch (AdblockPlusException e) | 54 } catch (AdblockPlusException e) |
55 { | 55 { |
56 // ignored | 56 // ignored |
57 } | 57 } |
58 } | 58 } |
59 | 59 |
60 @Test | 60 @Test |
61 public void testNullValue() | 61 public void testNullValue() |
62 { | 62 { |
63 final JsValue value = jsEngine.evaluate("null"); | 63 final JsValue value = jsEngine.evaluate("null"); |
64 assertFalse(value.isUndefined()); | 64 assertFalse(value.isUndefined()); |
65 assertTrue(value.isNull()); | 65 assertTrue(value.isNull()); |
66 assertFalse(value.isString()); | 66 assertFalse(value.isString()); |
67 assertFalse(value.isBoolean()); | 67 assertFalse(value.isBoolean()); |
68 assertFalse(value.isNumber()); | 68 assertFalse(value.isNumber()); |
69 assertFalse(value.isObject()); | 69 assertFalse(value.isObject()); |
70 assertFalse(value.isArray()); | 70 assertFalse(value.isArray()); |
71 assertFalse(value.isFunction()); | 71 assertFalse(value.isFunction()); |
72 assertEquals("null", value.asString()); | 72 assertEquals("null", value.asString()); |
73 assertFalse(value.asBoolean()); | 73 assertFalse(value.asBoolean()); |
74 try | 74 try |
75 { | 75 { |
76 value.asList(); | 76 value.asList(); |
77 fail(AdblockPlusException.class.getSimpleName() + " is expected to b
e thrown"); | 77 fail(AdblockPlusException.class.getSimpleName() + " is expected to be thro
wn"); |
78 } catch (AdblockPlusException e) | 78 } catch (AdblockPlusException e) |
79 { | 79 { |
80 // ignored | 80 // ignored |
81 } | 81 } |
82 try | 82 try |
83 { | 83 { |
84 value.getProperty("foo"); | 84 value.getProperty("foo"); |
85 fail(AdblockPlusException.class.getSimpleName() + " is expected to b
e thrown"); | 85 fail(AdblockPlusException.class.getSimpleName() + " is expected to be thro
wn"); |
86 } catch (AdblockPlusException e) | 86 } catch (AdblockPlusException e) |
87 { | 87 { |
88 // ignored | 88 // ignored |
89 } | 89 } |
90 } | 90 } |
91 | 91 |
92 @Test | 92 @Test |
93 public void testStringValue() | 93 public void testStringValue() |
94 { | 94 { |
95 JsValue value = jsEngine.evaluate("'123'"); | 95 JsValue value = jsEngine.evaluate("'123'"); |
96 assertFalse(value.isUndefined()); | 96 assertFalse(value.isUndefined()); |
97 assertFalse(value.isNull()); | 97 assertFalse(value.isNull()); |
98 assertTrue(value.isString()); | 98 assertTrue(value.isString()); |
99 assertFalse(value.isBoolean()); | 99 assertFalse(value.isBoolean()); |
100 assertFalse(value.isNumber()); | 100 assertFalse(value.isNumber()); |
101 assertFalse(value.isObject()); | 101 assertFalse(value.isObject()); |
102 assertFalse(value.isArray()); | 102 assertFalse(value.isArray()); |
103 assertFalse(value.isFunction()); | 103 assertFalse(value.isFunction()); |
104 assertEquals("123", value.asString()); | 104 assertEquals("123", value.asString()); |
105 assertEquals(123l, value.asLong()); | 105 assertEquals(123l, value.asLong()); |
106 assertTrue(value.asBoolean()); | 106 assertTrue(value.asBoolean()); |
107 try | 107 try |
108 { | 108 { |
109 value.asList(); | 109 value.asList(); |
110 fail(AdblockPlusException.class.getSimpleName() + " is expected to b
e thrown"); | 110 fail(AdblockPlusException.class.getSimpleName() + " is expected to be thro
wn"); |
111 } catch (AdblockPlusException e) | 111 } catch (AdblockPlusException e) |
112 { | 112 { |
113 // ignored | 113 // ignored |
114 } | 114 } |
115 try | 115 try |
116 { | 116 { |
117 value.getProperty("foo"); | 117 value.getProperty("foo"); |
118 fail(AdblockPlusException.class.getSimpleName() + " is expected to b
e thrown"); | 118 fail(AdblockPlusException.class.getSimpleName() + " is expected to be thro
wn"); |
119 } catch (AdblockPlusException e) | 119 } catch (AdblockPlusException e) |
120 { | 120 { |
121 // ignored | 121 // ignored |
122 } | 122 } |
123 } | 123 } |
124 | 124 |
125 @Test | 125 @Test |
126 public void testLongValue() | 126 public void testLongValue() |
127 { | 127 { |
128 JsValue value = jsEngine.evaluate("12345678901234"); | 128 JsValue value = jsEngine.evaluate("12345678901234"); |
129 assertFalse(value.isUndefined()); | 129 assertFalse(value.isUndefined()); |
130 assertFalse(value.isNull()); | 130 assertFalse(value.isNull()); |
131 assertFalse(value.isString()); | 131 assertFalse(value.isString()); |
132 assertFalse(value.isBoolean()); | 132 assertFalse(value.isBoolean()); |
133 assertTrue(value.isNumber()); | 133 assertTrue(value.isNumber()); |
134 assertFalse(value.isObject()); | 134 assertFalse(value.isObject()); |
135 assertFalse(value.isArray()); | 135 assertFalse(value.isArray()); |
136 assertFalse(value.isFunction()); | 136 assertFalse(value.isFunction()); |
137 assertEquals("12345678901234", value.asString()); | 137 assertEquals("12345678901234", value.asString()); |
138 assertEquals(12345678901234l, value.asLong()); | 138 assertEquals(12345678901234l, value.asLong()); |
139 assertTrue(value.asBoolean()); | 139 assertTrue(value.asBoolean()); |
140 try | 140 try |
141 { | 141 { |
142 value.asList(); | 142 value.asList(); |
143 fail(AdblockPlusException.class.getSimpleName() + " is expected to b
e thrown"); | 143 fail(AdblockPlusException.class.getSimpleName() + " is expected to be thro
wn"); |
144 } catch (AdblockPlusException e) | 144 } catch (AdblockPlusException e) |
145 { | 145 { |
146 // ignored | 146 // ignored |
147 } | 147 } |
148 try { | 148 try |
149 value.getProperty("foo"); | 149 { |
150 fail(AdblockPlusException.class.getSimpleName() + " is expected to b
e thrown"); | 150 value.getProperty("foo"); |
151 } catch (AdblockPlusException e) | 151 fail(AdblockPlusException.class.getSimpleName() + " is expected to be thro
wn"); |
152 { | 152 } catch (AdblockPlusException e) |
153 // ignored | 153 { |
154 } | 154 // ignored |
155 } | 155 } |
156 | 156 } |
157 @Test | 157 |
158 public void testBoolValue() | 158 @Test |
159 { | 159 public void testBoolValue() |
160 JsValue value = jsEngine.evaluate("true"); | 160 { |
161 assertFalse(value.isUndefined()); | 161 JsValue value = jsEngine.evaluate("true"); |
162 assertFalse(value.isNull()); | 162 assertFalse(value.isUndefined()); |
163 assertFalse(value.isString()); | 163 assertFalse(value.isNull()); |
164 assertTrue(value.isBoolean()); | 164 assertFalse(value.isString()); |
165 assertFalse(value.isNumber()); | 165 assertTrue(value.isBoolean()); |
166 assertFalse(value.isObject()); | 166 assertFalse(value.isNumber()); |
167 assertFalse(value.isArray()); | 167 assertFalse(value.isObject()); |
168 assertFalse(value.isFunction()); | 168 assertFalse(value.isArray()); |
169 assertEquals("true", value.asString()); | 169 assertFalse(value.isFunction()); |
170 assertTrue(value.asBoolean()); | 170 assertEquals("true", value.asString()); |
171 try | 171 assertTrue(value.asBoolean()); |
172 { | 172 try |
173 value.asList(); | 173 { |
174 fail(AdblockPlusException.class.getSimpleName() + " is expected to b
e thrown"); | 174 value.asList(); |
175 } catch (AdblockPlusException e) | 175 fail(AdblockPlusException.class.getSimpleName() + " is expected to be thro
wn"); |
176 { | 176 } catch (AdblockPlusException e) |
177 // ignored | 177 { |
178 } | 178 // ignored |
179 try | 179 } |
180 { | 180 try |
181 value.getProperty("foo"); | 181 { |
182 fail(AdblockPlusException.class.getSimpleName() + " is expected to b
e thrown"); | 182 value.getProperty("foo"); |
183 } catch (AdblockPlusException e) | 183 fail(AdblockPlusException.class.getSimpleName() + " is expected to be thro
wn"); |
184 { | 184 } catch (AdblockPlusException e) |
185 // ignored | 185 { |
186 } | 186 // ignored |
187 } | 187 } |
188 | 188 } |
189 @Test | 189 |
190 public void testObjectValue() | 190 @Test |
191 { | 191 public void testObjectValue() |
192 final String source = | 192 { |
193 "function Foo() {\n" + | 193 final String source = |
194 " this.x = 2;\n" + | 194 "function Foo() {\n" + |
195 " this.toString = function() {return 'foo';};\n" + | 195 " this.x = 2;\n" + |
196 " this.valueOf = function() {return 123;};\n" + | 196 " this.toString = function() {return 'foo';};\n" + |
197 "};\n" + | 197 " this.valueOf = function() {return 123;};\n" + |
198 "new Foo()"; | 198 "};\n" + |
199 JsValue value = jsEngine.evaluate(source); | 199 "new Foo()"; |
200 assertFalse(value.isUndefined()); | 200 JsValue value = jsEngine.evaluate(source); |
201 assertFalse(value.isNull()); | 201 assertFalse(value.isUndefined()); |
202 assertFalse(value.isString()); | 202 assertFalse(value.isNull()); |
203 assertFalse(value.isBoolean()); | 203 assertFalse(value.isString()); |
204 assertFalse(value.isNumber()); | 204 assertFalse(value.isBoolean()); |
205 assertTrue(value.isObject()); | 205 assertFalse(value.isNumber()); |
206 assertFalse(value.isArray()); | 206 assertTrue(value.isObject()); |
207 assertFalse(value.isFunction()); | 207 assertFalse(value.isArray()); |
208 assertEquals("foo", value.asString()); | 208 assertFalse(value.isFunction()); |
209 assertEquals(123l, value.asLong()); | 209 assertEquals("foo", value.asString()); |
210 assertTrue(value.asBoolean()); | 210 assertEquals(123l, value.asLong()); |
211 try | 211 assertTrue(value.asBoolean()); |
212 { | 212 try |
213 value.asList(); | 213 { |
214 fail(AdblockPlusException.class.getSimpleName() + " is expected to b
e thrown"); | 214 value.asList(); |
215 } catch (AdblockPlusException e) | 215 fail(AdblockPlusException.class.getSimpleName() + " is expected to be thro
wn"); |
216 { | 216 } catch (AdblockPlusException e) |
217 // ignored | 217 { |
218 } | 218 // ignored |
219 assertEquals(2l, value.getProperty("x").asLong()); | 219 } |
220 } | 220 assertEquals(2l, value.getProperty("x").asLong()); |
221 | 221 } |
222 @Test | 222 |
223 public void testArrayValue() | 223 @Test |
224 { | 224 public void testArrayValue() |
225 JsValue value = jsEngine.evaluate("[5,8,12]"); | 225 { |
226 assertFalse(value.isUndefined()); | 226 JsValue value = jsEngine.evaluate("[5,8,12]"); |
227 assertFalse(value.isNull()); | 227 assertFalse(value.isUndefined()); |
228 assertFalse(value.isString()); | 228 assertFalse(value.isNull()); |
229 assertFalse(value.isBoolean()); | 229 assertFalse(value.isString()); |
230 assertFalse(value.isNumber()); | 230 assertFalse(value.isBoolean()); |
231 assertTrue(value.isObject()); | 231 assertFalse(value.isNumber()); |
232 assertTrue(value.isArray()); | 232 assertTrue(value.isObject()); |
233 assertEquals("5,8,12", value.asString()); | 233 assertTrue(value.isArray()); |
234 assertTrue(value.asBoolean()); | 234 assertEquals("5,8,12", value.asString()); |
235 assertEquals(3l, value.asList().size()); | 235 assertTrue(value.asBoolean()); |
236 assertEquals(5l, value.asList().get(0).asLong()); | 236 assertEquals(3l, value.asList().size()); |
237 assertEquals(8l, value.asList().get(1).asLong()); | 237 assertEquals(5l, value.asList().get(0).asLong()); |
238 assertEquals(12l, value.asList().get(2).asLong()); | 238 assertEquals(8l, value.asList().get(1).asLong()); |
239 assertEquals(3l, value.getProperty("length").asLong()); | 239 assertEquals(12l, value.asList().get(2).asLong()); |
240 } | 240 assertEquals(3l, value.getProperty("length").asLong()); |
241 | 241 } |
242 @Test | 242 |
243 public void testThrowingCoversion() | 243 @Test |
244 { | 244 public void testThrowingCoversion() |
245 final String source = | 245 { |
246 "function Foo() {\n" + | 246 final String source = |
247 " this.toString = function() {throw 'test1';};\n" + | 247 "function Foo() {\n" + |
248 " this.valueOf = function() {throw 'test2';};\n" + | 248 " this.toString = function() {throw 'test1';};\n" + |
249 "};\n" + | 249 " this.valueOf = function() {throw 'test2';};\n" + |
250 "new Foo()"; | 250 "};\n" + |
251 | 251 "new Foo()"; |
252 JsValue value = jsEngine.evaluate(source); | 252 |
253 assertEquals("", value.asString()); | 253 JsValue value = jsEngine.evaluate(source); |
254 assertEquals(0l, value.asLong()); | 254 assertEquals("", value.asString()); |
255 } | 255 assertEquals(0l, value.asLong()); |
| 256 } |
256 } | 257 } |
OLD | NEW |