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