LEFT | RIGHT |
(no file at all) | |
1 #include <AdblockPlus.h> | 1 #include <AdblockPlus.h> |
2 #include <gtest/gtest.h> | 2 #include <gtest/gtest.h> |
3 | 3 |
4 class ThrowingErrorCallback : public AdblockPlus::ErrorCallback | 4 class ThrowingErrorCallback : public AdblockPlus::ErrorCallback |
5 { | 5 { |
6 public: | 6 public: |
7 void operator()(const std::string& message) | 7 void operator()(const std::string& message) |
8 { | 8 { |
9 throw std::runtime_error("Unexpected error: " + message); | 9 throw std::runtime_error("Unexpected error: " + message); |
10 } | 10 } |
11 }; | 11 }; |
12 | 12 |
13 TEST(JsValueTest, UndefinedValue) | 13 TEST(JsValueTest, UndefinedValue) |
14 { | 14 { |
15 ThrowingErrorCallback errorCallback; | 15 ThrowingErrorCallback errorCallback; |
16 AdblockPlus::JsEngine jsEngine(0, 0, &errorCallback); | 16 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), 0, 0, &errorCallback); |
17 AdblockPlus::JsValuePtr value = jsEngine.Evaluate("undefined"); | 17 AdblockPlus::JsValuePtr value = jsEngine.Evaluate("undefined"); |
18 ASSERT_TRUE(value->IsUndefined()); | 18 ASSERT_TRUE(value->IsUndefined()); |
19 ASSERT_FALSE(value->IsNull()); | 19 ASSERT_FALSE(value->IsNull()); |
20 ASSERT_FALSE(value->IsString()); | 20 ASSERT_FALSE(value->IsString()); |
21 ASSERT_FALSE(value->IsBool()); | 21 ASSERT_FALSE(value->IsBool()); |
22 ASSERT_FALSE(value->IsNumber()); | 22 ASSERT_FALSE(value->IsNumber()); |
23 ASSERT_FALSE(value->IsObject()); | 23 ASSERT_FALSE(value->IsObject()); |
24 ASSERT_FALSE(value->IsArray()); | 24 ASSERT_FALSE(value->IsArray()); |
25 ASSERT_FALSE(value->IsFunction()); | 25 ASSERT_FALSE(value->IsFunction()); |
26 ASSERT_EQ("undefined", value->AsString()); | 26 ASSERT_EQ("undefined", value->AsString()); |
27 ASSERT_FALSE(value->AsBool()); | 27 ASSERT_FALSE(value->AsBool()); |
28 ASSERT_ANY_THROW(value->AsList()); | 28 ASSERT_ANY_THROW(value->AsList()); |
29 ASSERT_ANY_THROW(value->GetProperty("foo")); | 29 ASSERT_ANY_THROW(value->GetProperty("foo")); |
30 ASSERT_ANY_THROW(value->SetProperty("foo", false)); | 30 ASSERT_ANY_THROW(value->SetProperty("foo", false)); |
31 ASSERT_ANY_THROW(value->GetClassName()); | 31 ASSERT_ANY_THROW(value->GetClassName()); |
32 ASSERT_ANY_THROW(value->GetOwnPropertyNames()); | 32 ASSERT_ANY_THROW(value->GetOwnPropertyNames()); |
33 ASSERT_ANY_THROW(value->Call()); | 33 ASSERT_ANY_THROW(value->Call()); |
34 } | 34 } |
35 | 35 |
36 TEST(JsValueTest, NullValue) | 36 TEST(JsValueTest, NullValue) |
37 { | 37 { |
38 ThrowingErrorCallback errorCallback; | 38 ThrowingErrorCallback errorCallback; |
39 AdblockPlus::JsEngine jsEngine(0, 0, &errorCallback); | 39 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), 0, 0, &errorCallback); |
40 AdblockPlus::JsValuePtr value = jsEngine.Evaluate("null"); | 40 AdblockPlus::JsValuePtr value = jsEngine.Evaluate("null"); |
41 ASSERT_FALSE(value->IsUndefined()); | 41 ASSERT_FALSE(value->IsUndefined()); |
42 ASSERT_TRUE(value->IsNull()); | 42 ASSERT_TRUE(value->IsNull()); |
43 ASSERT_FALSE(value->IsString()); | 43 ASSERT_FALSE(value->IsString()); |
44 ASSERT_FALSE(value->IsBool()); | 44 ASSERT_FALSE(value->IsBool()); |
45 ASSERT_FALSE(value->IsNumber()); | 45 ASSERT_FALSE(value->IsNumber()); |
46 ASSERT_FALSE(value->IsObject()); | 46 ASSERT_FALSE(value->IsObject()); |
47 ASSERT_FALSE(value->IsArray()); | 47 ASSERT_FALSE(value->IsArray()); |
48 ASSERT_FALSE(value->IsFunction()); | 48 ASSERT_FALSE(value->IsFunction()); |
49 ASSERT_EQ("null", value->AsString()); | 49 ASSERT_EQ("null", value->AsString()); |
50 ASSERT_FALSE(value->AsBool()); | 50 ASSERT_FALSE(value->AsBool()); |
51 ASSERT_ANY_THROW(value->AsList()); | 51 ASSERT_ANY_THROW(value->AsList()); |
52 ASSERT_ANY_THROW(value->GetProperty("foo")); | 52 ASSERT_ANY_THROW(value->GetProperty("foo")); |
53 ASSERT_ANY_THROW(value->SetProperty("foo", false)); | 53 ASSERT_ANY_THROW(value->SetProperty("foo", false)); |
54 ASSERT_ANY_THROW(value->GetClassName()); | 54 ASSERT_ANY_THROW(value->GetClassName()); |
55 ASSERT_ANY_THROW(value->GetOwnPropertyNames()); | 55 ASSERT_ANY_THROW(value->GetOwnPropertyNames()); |
56 ASSERT_ANY_THROW(value->Call()); | 56 ASSERT_ANY_THROW(value->Call()); |
57 } | 57 } |
58 | 58 |
59 TEST(JsValueTest, StringValue) | 59 TEST(JsValueTest, StringValue) |
60 { | 60 { |
61 ThrowingErrorCallback errorCallback; | 61 ThrowingErrorCallback errorCallback; |
62 AdblockPlus::JsEngine jsEngine(0, 0, &errorCallback); | 62 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), 0, 0, &errorCallback); |
63 AdblockPlus::JsValuePtr value = jsEngine.Evaluate("'123'"); | 63 AdblockPlus::JsValuePtr value = jsEngine.Evaluate("'123'"); |
64 ASSERT_FALSE(value->IsUndefined()); | 64 ASSERT_FALSE(value->IsUndefined()); |
65 ASSERT_FALSE(value->IsNull()); | 65 ASSERT_FALSE(value->IsNull()); |
66 ASSERT_TRUE(value->IsString()); | 66 ASSERT_TRUE(value->IsString()); |
67 ASSERT_FALSE(value->IsBool()); | 67 ASSERT_FALSE(value->IsBool()); |
68 ASSERT_FALSE(value->IsNumber()); | 68 ASSERT_FALSE(value->IsNumber()); |
69 ASSERT_FALSE(value->IsObject()); | 69 ASSERT_FALSE(value->IsObject()); |
70 ASSERT_FALSE(value->IsArray()); | 70 ASSERT_FALSE(value->IsArray()); |
71 ASSERT_FALSE(value->IsFunction()); | 71 ASSERT_FALSE(value->IsFunction()); |
72 ASSERT_EQ("123", value->AsString()); | 72 ASSERT_EQ("123", value->AsString()); |
73 ASSERT_EQ(123, value->AsInt()); | 73 ASSERT_EQ(123, value->AsInt()); |
74 ASSERT_TRUE(value->AsBool()); | 74 ASSERT_TRUE(value->AsBool()); |
75 ASSERT_ANY_THROW(value->AsList()); | 75 ASSERT_ANY_THROW(value->AsList()); |
76 ASSERT_ANY_THROW(value->GetProperty("foo")); | 76 ASSERT_ANY_THROW(value->GetProperty("foo")); |
77 ASSERT_ANY_THROW(value->SetProperty("foo", false)); | 77 ASSERT_ANY_THROW(value->SetProperty("foo", false)); |
78 ASSERT_ANY_THROW(value->GetClassName()); | 78 ASSERT_ANY_THROW(value->GetClassName()); |
79 ASSERT_ANY_THROW(value->GetOwnPropertyNames()); | 79 ASSERT_ANY_THROW(value->GetOwnPropertyNames()); |
80 ASSERT_ANY_THROW(value->Call()); | 80 ASSERT_ANY_THROW(value->Call()); |
81 } | 81 } |
82 | 82 |
83 TEST(JsValueTest, IntValue) | 83 TEST(JsValueTest, IntValue) |
84 { | 84 { |
85 ThrowingErrorCallback errorCallback; | 85 ThrowingErrorCallback errorCallback; |
86 AdblockPlus::JsEngine jsEngine(0, 0, &errorCallback); | 86 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), 0, 0, &errorCallback); |
87 AdblockPlus::JsValuePtr value = jsEngine.Evaluate("123"); | 87 AdblockPlus::JsValuePtr value = jsEngine.Evaluate("123"); |
88 ASSERT_FALSE(value->IsUndefined()); | 88 ASSERT_FALSE(value->IsUndefined()); |
89 ASSERT_FALSE(value->IsNull()); | 89 ASSERT_FALSE(value->IsNull()); |
90 ASSERT_FALSE(value->IsString()); | 90 ASSERT_FALSE(value->IsString()); |
91 ASSERT_FALSE(value->IsBool()); | 91 ASSERT_FALSE(value->IsBool()); |
92 ASSERT_TRUE(value->IsNumber()); | 92 ASSERT_TRUE(value->IsNumber()); |
93 ASSERT_FALSE(value->IsObject()); | 93 ASSERT_FALSE(value->IsObject()); |
94 ASSERT_FALSE(value->IsArray()); | 94 ASSERT_FALSE(value->IsArray()); |
95 ASSERT_FALSE(value->IsFunction()); | 95 ASSERT_FALSE(value->IsFunction()); |
96 ASSERT_EQ("123", value->AsString()); | 96 ASSERT_EQ("123", value->AsString()); |
97 ASSERT_EQ(123, value->AsInt()); | 97 ASSERT_EQ(123, value->AsInt()); |
98 ASSERT_TRUE(value->AsBool()); | 98 ASSERT_TRUE(value->AsBool()); |
99 ASSERT_ANY_THROW(value->AsList()); | 99 ASSERT_ANY_THROW(value->AsList()); |
100 ASSERT_ANY_THROW(value->GetProperty("foo")); | 100 ASSERT_ANY_THROW(value->GetProperty("foo")); |
101 ASSERT_ANY_THROW(value->SetProperty("foo", false)); | 101 ASSERT_ANY_THROW(value->SetProperty("foo", false)); |
102 ASSERT_ANY_THROW(value->GetClassName()); | 102 ASSERT_ANY_THROW(value->GetClassName()); |
103 ASSERT_ANY_THROW(value->GetOwnPropertyNames()); | 103 ASSERT_ANY_THROW(value->GetOwnPropertyNames()); |
104 ASSERT_ANY_THROW(value->Call()); | 104 ASSERT_ANY_THROW(value->Call()); |
105 } | 105 } |
106 | 106 |
107 TEST(JsValueTest, BoolValue) | 107 TEST(JsValueTest, BoolValue) |
108 { | 108 { |
109 ThrowingErrorCallback errorCallback; | 109 ThrowingErrorCallback errorCallback; |
110 AdblockPlus::JsEngine jsEngine(0, 0, &errorCallback); | 110 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), 0, 0, &errorCallback); |
111 AdblockPlus::JsValuePtr value = jsEngine.Evaluate("true"); | 111 AdblockPlus::JsValuePtr value = jsEngine.Evaluate("true"); |
112 ASSERT_FALSE(value->IsUndefined()); | 112 ASSERT_FALSE(value->IsUndefined()); |
113 ASSERT_FALSE(value->IsNull()); | 113 ASSERT_FALSE(value->IsNull()); |
114 ASSERT_FALSE(value->IsString()); | 114 ASSERT_FALSE(value->IsString()); |
115 ASSERT_TRUE(value->IsBool()); | 115 ASSERT_TRUE(value->IsBool()); |
116 ASSERT_FALSE(value->IsNumber()); | 116 ASSERT_FALSE(value->IsNumber()); |
117 ASSERT_FALSE(value->IsObject()); | 117 ASSERT_FALSE(value->IsObject()); |
118 ASSERT_FALSE(value->IsArray()); | 118 ASSERT_FALSE(value->IsArray()); |
119 ASSERT_FALSE(value->IsFunction()); | 119 ASSERT_FALSE(value->IsFunction()); |
120 ASSERT_EQ("true", value->AsString()); | 120 ASSERT_EQ("true", value->AsString()); |
121 ASSERT_TRUE(value->AsBool()); | 121 ASSERT_TRUE(value->AsBool()); |
122 ASSERT_ANY_THROW(value->AsList()); | 122 ASSERT_ANY_THROW(value->AsList()); |
123 ASSERT_ANY_THROW(value->GetProperty("foo")); | 123 ASSERT_ANY_THROW(value->GetProperty("foo")); |
124 ASSERT_ANY_THROW(value->SetProperty("foo", false)); | 124 ASSERT_ANY_THROW(value->SetProperty("foo", false)); |
125 ASSERT_ANY_THROW(value->GetClassName()); | 125 ASSERT_ANY_THROW(value->GetClassName()); |
126 ASSERT_ANY_THROW(value->GetOwnPropertyNames()); | 126 ASSERT_ANY_THROW(value->GetOwnPropertyNames()); |
127 ASSERT_ANY_THROW(value->Call()); | 127 ASSERT_ANY_THROW(value->Call()); |
128 } | 128 } |
129 | 129 |
130 TEST(JsValueTest, ObjectValue) | 130 TEST(JsValueTest, ObjectValue) |
131 { | 131 { |
132 ThrowingErrorCallback errorCallback; | 132 ThrowingErrorCallback errorCallback; |
133 AdblockPlus::JsEngine jsEngine(0, 0, &errorCallback); | 133 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), 0, 0, &errorCallback); |
134 const std::string source("\ | 134 const std::string source("\ |
135 function Foo() {\ | 135 function Foo() {\ |
136 this.x = 2;\ | 136 this.x = 2;\ |
137 this.toString = function() {return 'foo';};\ | 137 this.toString = function() {return 'foo';};\ |
138 this.valueOf = function() {return 123;};\ | 138 this.valueOf = function() {return 123;};\ |
139 };\ | 139 };\ |
140 new Foo()"); | 140 new Foo()"); |
141 AdblockPlus::JsValuePtr value = jsEngine.Evaluate(source); | 141 AdblockPlus::JsValuePtr value = jsEngine.Evaluate(source); |
142 ASSERT_FALSE(value->IsUndefined()); | 142 ASSERT_FALSE(value->IsUndefined()); |
143 ASSERT_FALSE(value->IsNull()); | 143 ASSERT_FALSE(value->IsNull()); |
(...skipping 13 matching lines...) Expand all Loading... |
157 value->SetProperty("x", jsEngine.NewValue(15)); | 157 value->SetProperty("x", jsEngine.NewValue(15)); |
158 ASSERT_EQ(15, value->GetProperty("x")->AsInt()); | 158 ASSERT_EQ(15, value->GetProperty("x")->AsInt()); |
159 ASSERT_EQ("Foo", value->GetClassName()); | 159 ASSERT_EQ("Foo", value->GetClassName()); |
160 ASSERT_EQ(3u, value->GetOwnPropertyNames().size()); | 160 ASSERT_EQ(3u, value->GetOwnPropertyNames().size()); |
161 ASSERT_ANY_THROW(value->Call()); | 161 ASSERT_ANY_THROW(value->Call()); |
162 } | 162 } |
163 | 163 |
164 TEST(JsValueTest, ArrayValue) | 164 TEST(JsValueTest, ArrayValue) |
165 { | 165 { |
166 ThrowingErrorCallback errorCallback; | 166 ThrowingErrorCallback errorCallback; |
167 AdblockPlus::JsEngine jsEngine(0, 0, &errorCallback); | 167 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), 0, 0, &errorCallback); |
168 AdblockPlus::JsValuePtr value = jsEngine.Evaluate("[5,8,12]"); | 168 AdblockPlus::JsValuePtr value = jsEngine.Evaluate("[5,8,12]"); |
169 ASSERT_FALSE(value->IsUndefined()); | 169 ASSERT_FALSE(value->IsUndefined()); |
170 ASSERT_FALSE(value->IsNull()); | 170 ASSERT_FALSE(value->IsNull()); |
171 ASSERT_FALSE(value->IsString()); | 171 ASSERT_FALSE(value->IsString()); |
172 ASSERT_FALSE(value->IsBool()); | 172 ASSERT_FALSE(value->IsBool()); |
173 ASSERT_FALSE(value->IsNumber()); | 173 ASSERT_FALSE(value->IsNumber()); |
174 ASSERT_TRUE(value->IsObject()); | 174 ASSERT_TRUE(value->IsObject()); |
175 ASSERT_TRUE(value->IsArray()); | 175 ASSERT_TRUE(value->IsArray()); |
176 ASSERT_FALSE(value->IsFunction()); | 176 ASSERT_FALSE(value->IsFunction()); |
177 ASSERT_EQ("5,8,12", value->AsString()); | 177 ASSERT_EQ("5,8,12", value->AsString()); |
178 ASSERT_TRUE(value->AsBool()); | 178 ASSERT_TRUE(value->AsBool()); |
179 ASSERT_EQ(3u, value->AsList().size()); | 179 ASSERT_EQ(3u, value->AsList().size()); |
180 ASSERT_EQ(8, value->AsList()[1]->AsInt()); | 180 ASSERT_EQ(8, value->AsList()[1]->AsInt()); |
181 ASSERT_EQ(3, value->GetProperty("length")->AsInt()); | 181 ASSERT_EQ(3, value->GetProperty("length")->AsInt()); |
182 ASSERT_EQ("Array", value->GetClassName()); | 182 ASSERT_EQ("Array", value->GetClassName()); |
183 ASSERT_ANY_THROW(value->Call()); | 183 ASSERT_ANY_THROW(value->Call()); |
184 } | 184 } |
185 | 185 |
186 TEST(JsValueTest, FunctionValue) | 186 TEST(JsValueTest, FunctionValue) |
187 { | 187 { |
188 ThrowingErrorCallback errorCallback; | 188 ThrowingErrorCallback errorCallback; |
189 AdblockPlus::JsEngine jsEngine(0, 0, &errorCallback); | 189 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), 0, 0, &errorCallback); |
190 AdblockPlus::JsValuePtr value = jsEngine.Evaluate("(function(foo, bar) {return
this.x + '/' + foo + '/' + bar;})"); | 190 AdblockPlus::JsValuePtr value = jsEngine.Evaluate("(function(foo, bar) {return
this.x + '/' + foo + '/' + bar;})"); |
191 ASSERT_FALSE(value->IsUndefined()); | 191 ASSERT_FALSE(value->IsUndefined()); |
192 ASSERT_FALSE(value->IsNull()); | 192 ASSERT_FALSE(value->IsNull()); |
193 ASSERT_FALSE(value->IsString()); | 193 ASSERT_FALSE(value->IsString()); |
194 ASSERT_FALSE(value->IsBool()); | 194 ASSERT_FALSE(value->IsBool()); |
195 ASSERT_FALSE(value->IsNumber()); | 195 ASSERT_FALSE(value->IsNumber()); |
196 ASSERT_TRUE(value->IsObject()); | 196 ASSERT_TRUE(value->IsObject()); |
197 ASSERT_FALSE(value->IsArray()); | 197 ASSERT_FALSE(value->IsArray()); |
198 ASSERT_TRUE(value->IsFunction()); | 198 ASSERT_TRUE(value->IsFunction()); |
199 ASSERT_TRUE(value->AsBool()); | 199 ASSERT_TRUE(value->AsBool()); |
200 ASSERT_ANY_THROW(value->AsList()); | 200 ASSERT_ANY_THROW(value->AsList()); |
201 ASSERT_EQ(2, value->GetProperty("length")->AsInt()); | 201 ASSERT_EQ(2, value->GetProperty("length")->AsInt()); |
202 | 202 |
203 AdblockPlus::JsValuePtr thisPtr = jsEngine.Evaluate("({x:2})"); | 203 AdblockPlus::JsValuePtr thisPtr = jsEngine.Evaluate("({x:2})"); |
204 AdblockPlus::JsValueList params; | 204 AdblockPlus::JsValueList params; |
205 params.push_back(jsEngine.NewValue(5)); | 205 params.push_back(jsEngine.NewValue(5)); |
206 params.push_back(jsEngine.NewValue("xyz")); | 206 params.push_back(jsEngine.NewValue("xyz")); |
207 ASSERT_EQ("2/5/xyz", value->Call(params, thisPtr)->AsString()); | 207 ASSERT_EQ("2/5/xyz", value->Call(params, thisPtr)->AsString()); |
208 } | 208 } |
209 | 209 |
210 TEST(JsValueTest, ThrowingCoversion) | 210 TEST(JsValueTest, ThrowingCoversion) |
211 { | 211 { |
212 ThrowingErrorCallback errorCallback; | 212 ThrowingErrorCallback errorCallback; |
213 AdblockPlus::JsEngine jsEngine(0, 0, &errorCallback); | 213 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), 0, 0, &errorCallback); |
214 const std::string source("\ | 214 const std::string source("\ |
215 function Foo() {\ | 215 function Foo() {\ |
216 this.toString = function() {throw 'test1';};\ | 216 this.toString = function() {throw 'test1';};\ |
217 this.valueOf = function() {throw 'test2';};\ | 217 this.valueOf = function() {throw 'test2';};\ |
218 };\ | 218 };\ |
219 new Foo()"); | 219 new Foo()"); |
220 AdblockPlus::JsValuePtr value = jsEngine.Evaluate(source); | 220 AdblockPlus::JsValuePtr value = jsEngine.Evaluate(source); |
221 ASSERT_EQ("", value->AsString()); | 221 ASSERT_EQ("", value->AsString()); |
222 ASSERT_EQ(0, value->AsInt()); | 222 ASSERT_EQ(0, value->AsInt()); |
223 } | 223 } |
LEFT | RIGHT |