Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: libadblockplus-android-tests/src/org/adblockplus/libadblockplus/tests/JsTest.java

Issue 29344967: Issue 4031 - Implement tests for libadblockplus-android (Closed)
Patch Set: fix typo ".. file[s]" Created Sept. 15, 2016, 10:44 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2016 Eyeo GmbH
4 *
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
7 * published by the Free Software Foundation.
8 *
9 * Adblock Plus 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
12 * GNU General Public License for more details.
13 *
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/>.
16 */
17
18 package org.adblockplus.libadblockplus.tests;
19
20 import org.adblockplus.libadblockplus.AdblockPlusException;
21 import org.adblockplus.libadblockplus.JsValue;
22
23 import org.junit.Test;
24
25 import java.util.ArrayList;
26 import java.util.List;
27
28 public class JsTest extends BaseJsTest
29 {
30 @Test
31 public void testUndefinedValue()
32 {
33 final JsValue value = jsEngine.evaluate("undefined");
34 assertTrue(value.isUndefined());
35 assertFalse(value.isNull());
36 assertFalse(value.isString());
37 assertFalse(value.isBoolean());
38 assertFalse(value.isNumber());
39 assertFalse(value.isObject());
40 assertFalse(value.isArray());
41 assertEquals("undefined", value.asString());
42 try
43 {
44 value.asList();
45 fail(AdblockPlusException.class.getSimpleName() + " is expected to be thro wn");
46 }
47 catch (AdblockPlusException e)
48 {
49 // ignored
50 }
51 try
52 {
53 value.getProperty("foo");
54 fail(AdblockPlusException.class.getSimpleName() + " is expected to be thro wn");
55 }
56 catch (AdblockPlusException e)
57 {
58 // ignored
59 }
60 }
61
62 @Test
63 public void testNullValue()
64 {
65 final JsValue value = jsEngine.evaluate("null");
66 assertFalse(value.isUndefined());
67 assertTrue(value.isNull());
68 assertFalse(value.isString());
69 assertFalse(value.isBoolean());
70 assertFalse(value.isNumber());
71 assertFalse(value.isObject());
72 assertFalse(value.isArray());
73 assertFalse(value.isFunction());
74 assertEquals("null", value.asString());
75 assertFalse(value.asBoolean());
76 try
77 {
78 value.asList();
79 fail(AdblockPlusException.class.getSimpleName() + " is expected to be thro wn");
80 }
81 catch (AdblockPlusException e)
82 {
83 // ignored
84 }
85 try
86 {
87 value.getProperty("foo");
88 fail(AdblockPlusException.class.getSimpleName() + " is expected to be thro wn");
89 }
90 catch (AdblockPlusException e)
91 {
92 // ignored
93 }
94 }
95
96 @Test
97 public void testStringValue()
98 {
99 JsValue value = jsEngine.evaluate("'123'");
100 assertFalse(value.isUndefined());
101 assertFalse(value.isNull());
102 assertTrue(value.isString());
103 assertFalse(value.isBoolean());
104 assertFalse(value.isNumber());
105 assertFalse(value.isObject());
106 assertFalse(value.isArray());
107 assertFalse(value.isFunction());
108 assertEquals("123", value.asString());
109 assertEquals(123l, value.asLong());
110 assertTrue(value.asBoolean());
111 try
112 {
113 value.asList();
114 fail(AdblockPlusException.class.getSimpleName() + " is expected to be thro wn");
115 }
116 catch (AdblockPlusException e)
117 {
118 // ignored
119 }
120 try
121 {
122 value.getProperty("foo");
123 fail(AdblockPlusException.class.getSimpleName() + " is expected to be thro wn");
124 }
125 catch (AdblockPlusException e)
126 {
127 // ignored
128 }
129 }
130
131 @Test
132 public void testLongValue()
133 {
134 JsValue value = jsEngine.evaluate("12345678901234");
135 assertFalse(value.isUndefined());
136 assertFalse(value.isNull());
137 assertFalse(value.isString());
138 assertFalse(value.isBoolean());
139 assertTrue(value.isNumber());
140 assertFalse(value.isObject());
141 assertFalse(value.isArray());
142 assertFalse(value.isFunction());
143 assertEquals("12345678901234", value.asString());
144 assertEquals(12345678901234l, value.asLong());
145 assertTrue(value.asBoolean());
146 try
147 {
148 value.asList();
149 fail(AdblockPlusException.class.getSimpleName() + " is expected to be thro wn");
150 }
151 catch (AdblockPlusException e)
152 {
153 // ignored
154 }
155 try
156 {
157 value.getProperty("foo");
158 fail(AdblockPlusException.class.getSimpleName() + " is expected to be thro wn");
159 }
160 catch (AdblockPlusException e)
161 {
162 // ignored
163 }
164 }
165
166 @Test
167 public void testBoolValue()
168 {
169 JsValue value = jsEngine.evaluate("true");
170 assertFalse(value.isUndefined());
171 assertFalse(value.isNull());
172 assertFalse(value.isString());
173 assertTrue(value.isBoolean());
174 assertFalse(value.isNumber());
175 assertFalse(value.isObject());
176 assertFalse(value.isArray());
177 assertFalse(value.isFunction());
178 assertEquals("true", value.asString());
179 assertTrue(value.asBoolean());
180 try
181 {
182 value.asList();
183 fail(AdblockPlusException.class.getSimpleName() + " is expected to be thro wn");
184 }
185 catch (AdblockPlusException e)
186 {
187 // ignored
188 }
189 try
190 {
191 value.getProperty("foo");
192 fail(AdblockPlusException.class.getSimpleName() + " is expected to be thro wn");
193 }
194 catch (AdblockPlusException e)
195 {
196 // ignored
197 }
198 }
199
200 @Test
201 public void testObjectValue()
202 {
203 final String source =
204 "function Foo() {\n" +
205 " this.x = 2;\n" +
206 " this.toString = function() {return 'foo';};\n" +
207 " this.valueOf = function() {return 123;};\n" +
208 "};\n" +
209 "new Foo()";
210 JsValue value = jsEngine.evaluate(source);
211 assertFalse(value.isUndefined());
212 assertFalse(value.isNull());
213 assertFalse(value.isString());
214 assertFalse(value.isBoolean());
215 assertFalse(value.isNumber());
216 assertTrue(value.isObject());
217 assertFalse(value.isArray());
218 assertFalse(value.isFunction());
219 assertEquals("foo", value.asString());
220 assertEquals(123l, value.asLong());
221 assertTrue(value.asBoolean());
222 try
223 {
224 value.asList();
225 fail(AdblockPlusException.class.getSimpleName() + " is expected to be thro wn");
226 }
227 catch (AdblockPlusException e)
228 {
229 // ignored
230 }
231 assertEquals(2l, value.getProperty("x").asLong());
232 }
233
234 @Test
235 public void testArrayValue()
236 {
237 JsValue value = jsEngine.evaluate("[5,8,12]");
238 assertFalse(value.isUndefined());
239 assertFalse(value.isNull());
240 assertFalse(value.isString());
241 assertFalse(value.isBoolean());
242 assertFalse(value.isNumber());
243 assertTrue(value.isObject());
244 assertTrue(value.isArray());
245 assertEquals("5,8,12", value.asString());
246 assertTrue(value.asBoolean());
247 assertEquals(3l, value.asList().size());
248 assertEquals(5l, value.asList().get(0).asLong());
249 assertEquals(8l, value.asList().get(1).asLong());
250 assertEquals(12l, value.asList().get(2).asLong());
251 assertEquals(3l, value.getProperty("length").asLong());
252 }
253
254 @Test
255 public void testThrowingCoversion()
256 {
257 final String source =
258 "function Foo() {\n" +
259 " this.toString = function() {throw 'test1';};\n" +
260 " this.valueOf = function() {throw 'test2';};\n" +
261 "};\n" +
262 "new Foo()";
263
264 JsValue value = jsEngine.evaluate(source);
265 assertEquals("", value.asString());
266 assertEquals(0l, value.asLong());
267 }
268 }
OLDNEW

Powered by Google App Engine
This is Rietveld