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-2017 eyeo GmbH | 3 * Copyright (C) 2006-2017 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 |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 ASSERT_FALSE(value->IsString()); | 146 ASSERT_FALSE(value->IsString()); |
147 ASSERT_FALSE(value->IsBool()); | 147 ASSERT_FALSE(value->IsBool()); |
148 ASSERT_FALSE(value->IsNumber()); | 148 ASSERT_FALSE(value->IsNumber()); |
149 ASSERT_TRUE(value->IsObject()); | 149 ASSERT_TRUE(value->IsObject()); |
150 ASSERT_FALSE(value->IsArray()); | 150 ASSERT_FALSE(value->IsArray()); |
151 ASSERT_FALSE(value->IsFunction()); | 151 ASSERT_FALSE(value->IsFunction()); |
152 ASSERT_EQ("foo", value->AsString()); | 152 ASSERT_EQ("foo", value->AsString()); |
153 ASSERT_EQ(123, value->AsInt()); | 153 ASSERT_EQ(123, value->AsInt()); |
154 ASSERT_TRUE(value->AsBool()); | 154 ASSERT_TRUE(value->AsBool()); |
155 ASSERT_ANY_THROW(value->AsList()); | 155 ASSERT_ANY_THROW(value->AsList()); |
156 ASSERT_EQ(2, value->GetProperty("x")->AsInt()); | 156 ASSERT_EQ(2, value->GetProperty("x").AsInt()); |
157 value->SetProperty("x", 12); | 157 value->SetProperty("x", 12); |
158 ASSERT_EQ(12, value->GetProperty("x")->AsInt()); | 158 ASSERT_EQ(12, value->GetProperty("x").AsInt()); |
159 value->SetProperty("x", jsEngine->NewValue(15)); | 159 value->SetProperty("x", *jsEngine->NewValue(15)); |
160 ASSERT_EQ(15, value->GetProperty("x")->AsInt()); | 160 ASSERT_EQ(15, value->GetProperty("x").AsInt()); |
161 ASSERT_EQ("Foo", value->GetClass()); | 161 ASSERT_EQ("Foo", value->GetClass()); |
162 ASSERT_EQ(3u, value->GetOwnPropertyNames().size()); | 162 ASSERT_EQ(3u, value->GetOwnPropertyNames().size()); |
163 ASSERT_ANY_THROW(value->Call()); | 163 ASSERT_ANY_THROW(value->Call()); |
164 } | 164 } |
165 | 165 |
166 TEST_F(JsValueTest, ArrayValue) | 166 TEST_F(JsValueTest, ArrayValue) |
167 { | 167 { |
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->GetClass()); | 182 ASSERT_EQ("Array", value->GetClass()); |
183 ASSERT_ANY_THROW(value->Call()); | 183 ASSERT_ANY_THROW(value->Call()); |
184 } | 184 } |
185 | 185 |
186 TEST_F(JsValueTest, FunctionValue) | 186 TEST_F(JsValueTest, FunctionValue) |
187 { | 187 { |
188 AdblockPlus::JsValuePtr value = jsEngine->Evaluate("(function(foo, bar) {retur
n this.x + '/' + foo + '/' + bar;})"); | 188 AdblockPlus::JsValuePtr value = jsEngine->Evaluate("(function(foo, bar) {retur
n this.x + '/' + foo + '/' + bar;})"); |
189 ASSERT_FALSE(value->IsUndefined()); | 189 ASSERT_FALSE(value->IsUndefined()); |
190 ASSERT_FALSE(value->IsNull()); | 190 ASSERT_FALSE(value->IsNull()); |
191 ASSERT_FALSE(value->IsString()); | 191 ASSERT_FALSE(value->IsString()); |
192 ASSERT_FALSE(value->IsBool()); | 192 ASSERT_FALSE(value->IsBool()); |
193 ASSERT_FALSE(value->IsNumber()); | 193 ASSERT_FALSE(value->IsNumber()); |
194 ASSERT_TRUE(value->IsObject()); | 194 ASSERT_TRUE(value->IsObject()); |
195 ASSERT_FALSE(value->IsArray()); | 195 ASSERT_FALSE(value->IsArray()); |
196 ASSERT_TRUE(value->IsFunction()); | 196 ASSERT_TRUE(value->IsFunction()); |
197 ASSERT_TRUE(value->AsBool()); | 197 ASSERT_TRUE(value->AsBool()); |
198 ASSERT_ANY_THROW(value->AsList()); | 198 ASSERT_ANY_THROW(value->AsList()); |
199 ASSERT_EQ(2, value->GetProperty("length")->AsInt()); | 199 ASSERT_EQ(2, value->GetProperty("length").AsInt()); |
200 | 200 |
201 AdblockPlus::JsValuePtr thisPtr = jsEngine->Evaluate("({x:2})"); | 201 AdblockPlus::JsValuePtr thisPtr = jsEngine->Evaluate("({x:2})"); |
202 AdblockPlus::JsConstValueList params; | 202 AdblockPlus::JsConstValueList params; |
203 params.push_back(jsEngine->NewValue(5)); | 203 params.push_back(jsEngine->NewValue(5)); |
204 params.push_back(jsEngine->NewValue("xyz")); | 204 params.push_back(jsEngine->NewValue("xyz")); |
205 ASSERT_EQ("2/5/xyz", value->Call(params, thisPtr).AsString()); | 205 ASSERT_EQ("2/5/xyz", value->Call(params, thisPtr).AsString()); |
206 } | 206 } |
207 | 207 |
208 TEST_F(JsValueTest, JsValueCallSingleArg) | 208 TEST_F(JsValueTest, JsValueCallSingleArg) |
209 { | 209 { |
210 auto func = jsEngine->Evaluate("(function(arg) {return arg * 2;})"); | 210 auto func = jsEngine->Evaluate("(function(arg) {return arg * 2;})"); |
211 EXPECT_EQ(10, func->Call(*jsEngine->NewValue(5)).AsInt()); | 211 EXPECT_EQ(10, func->Call(*jsEngine->NewValue(5)).AsInt()); |
212 } | 212 } |
213 | 213 |
214 TEST_F(JsValueTest, ThrowingCoversion) | 214 TEST_F(JsValueTest, ThrowingCoversion) |
215 { | 215 { |
216 const std::string source("\ | 216 const std::string source("\ |
217 function Foo() {\ | 217 function Foo() {\ |
218 this.toString = function() {throw 'test1';};\ | 218 this.toString = function() {throw 'test1';};\ |
219 this.valueOf = function() {throw 'test2';};\ | 219 this.valueOf = function() {throw 'test2';};\ |
220 };\ | 220 };\ |
221 new Foo()"); | 221 new Foo()"); |
222 AdblockPlus::JsValuePtr value = jsEngine->Evaluate(source); | 222 AdblockPlus::JsValuePtr value = jsEngine->Evaluate(source); |
223 ASSERT_EQ("", value->AsString()); | 223 ASSERT_EQ("", value->AsString()); |
224 ASSERT_EQ(0, value->AsInt()); | 224 ASSERT_EQ(0, value->AsInt()); |
225 } | 225 } |
OLD | NEW |