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

Side by Side Diff: test/JsValue.cpp

Issue 10213003: Make JsEngine::Evaluate() return a wrapper for v8::Value to accessdifferent variable types easily (Closed)
Patch Set: Unbitrotted patch Created April 16, 2013, 3:30 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « test/JsEngine.cpp ('k') | test/WebRequest.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #include <AdblockPlus.h>
2 #include <gtest/gtest.h>
3
4 class ThrowingErrorCallback : public AdblockPlus::ErrorCallback
5 {
6 public:
7 void operator()(const std::string& message)
8 {
9 throw std::runtime_error("Unexpected error: " + message);
10 }
11 };
12
13 TEST(JsValueTest, UndefinedValue)
14 {
15 ThrowingErrorCallback errorCallback;
16 AdblockPlus::JsEngine jsEngine(0, 0, &errorCallback);
17 AdblockPlus::JsValuePtr value = jsEngine.Evaluate("undefined");
18 ASSERT_TRUE(value->IsUndefined());
19 ASSERT_FALSE(value->IsNull());
20 ASSERT_FALSE(value->IsString());
21 ASSERT_FALSE(value->IsBool());
22 ASSERT_FALSE(value->IsNumber());
23 ASSERT_FALSE(value->IsObject());
24 ASSERT_FALSE(value->IsArray());
25 ASSERT_FALSE(value->IsFunction());
26 ASSERT_EQ("undefined", value->AsString());
27 ASSERT_FALSE(value->AsBool());
28 ASSERT_ANY_THROW(value->AsList());
29 ASSERT_ANY_THROW(value->GetProperty("foo"));
30 ASSERT_ANY_THROW(value->SetProperty("foo", false));
31 ASSERT_ANY_THROW(value->GetClassName());
32 ASSERT_ANY_THROW(value->Call());
33 }
34
35 TEST(JsValueTest, NullValue)
36 {
37 ThrowingErrorCallback errorCallback;
38 AdblockPlus::JsEngine jsEngine(0, 0, &errorCallback);
39 AdblockPlus::JsValuePtr value = jsEngine.Evaluate("null");
40 ASSERT_FALSE(value->IsUndefined());
41 ASSERT_TRUE(value->IsNull());
42 ASSERT_FALSE(value->IsString());
43 ASSERT_FALSE(value->IsBool());
44 ASSERT_FALSE(value->IsNumber());
45 ASSERT_FALSE(value->IsObject());
46 ASSERT_FALSE(value->IsArray());
47 ASSERT_FALSE(value->IsFunction());
48 ASSERT_EQ("null", value->AsString());
49 ASSERT_FALSE(value->AsBool());
50 ASSERT_ANY_THROW(value->AsList());
51 ASSERT_ANY_THROW(value->GetProperty("foo"));
52 ASSERT_ANY_THROW(value->SetProperty("foo", false));
53 ASSERT_ANY_THROW(value->GetClassName());
54 ASSERT_ANY_THROW(value->Call());
55 }
56
57 TEST(JsValueTest, StringValue)
58 {
59 ThrowingErrorCallback errorCallback;
60 AdblockPlus::JsEngine jsEngine(0, 0, &errorCallback);
61 AdblockPlus::JsValuePtr value = jsEngine.Evaluate("'123'");
62 ASSERT_FALSE(value->IsUndefined());
63 ASSERT_FALSE(value->IsNull());
64 ASSERT_TRUE(value->IsString());
65 ASSERT_FALSE(value->IsBool());
66 ASSERT_FALSE(value->IsNumber());
67 ASSERT_FALSE(value->IsObject());
68 ASSERT_FALSE(value->IsArray());
69 ASSERT_FALSE(value->IsFunction());
70 ASSERT_EQ("123", value->AsString());
71 ASSERT_EQ(123, value->AsInt());
72 ASSERT_TRUE(value->AsBool());
73 ASSERT_ANY_THROW(value->AsList());
74 ASSERT_ANY_THROW(value->GetProperty("foo"));
75 ASSERT_ANY_THROW(value->SetProperty("foo", false));
76 ASSERT_ANY_THROW(value->GetClassName());
77 ASSERT_ANY_THROW(value->Call());
78 }
79
80 TEST(JsValueTest, IntValue)
81 {
82 ThrowingErrorCallback errorCallback;
83 AdblockPlus::JsEngine jsEngine(0, 0, &errorCallback);
84 AdblockPlus::JsValuePtr value = jsEngine.Evaluate("123");
85 ASSERT_FALSE(value->IsUndefined());
86 ASSERT_FALSE(value->IsNull());
87 ASSERT_FALSE(value->IsString());
88 ASSERT_FALSE(value->IsBool());
89 ASSERT_TRUE(value->IsNumber());
90 ASSERT_FALSE(value->IsObject());
91 ASSERT_FALSE(value->IsArray());
92 ASSERT_FALSE(value->IsFunction());
93 ASSERT_EQ("123", value->AsString());
94 ASSERT_EQ(123, value->AsInt());
95 ASSERT_TRUE(value->AsBool());
96 ASSERT_ANY_THROW(value->AsList());
97 ASSERT_ANY_THROW(value->GetProperty("foo"));
98 ASSERT_ANY_THROW(value->SetProperty("foo", false));
99 ASSERT_ANY_THROW(value->GetClassName());
100 ASSERT_ANY_THROW(value->Call());
101 }
102
103 TEST(JsValueTest, BoolValue)
104 {
105 ThrowingErrorCallback errorCallback;
106 AdblockPlus::JsEngine jsEngine(0, 0, &errorCallback);
107 AdblockPlus::JsValuePtr value = jsEngine.Evaluate("true");
108 ASSERT_FALSE(value->IsUndefined());
109 ASSERT_FALSE(value->IsNull());
110 ASSERT_FALSE(value->IsString());
111 ASSERT_TRUE(value->IsBool());
112 ASSERT_FALSE(value->IsNumber());
113 ASSERT_FALSE(value->IsObject());
114 ASSERT_FALSE(value->IsArray());
115 ASSERT_FALSE(value->IsFunction());
116 ASSERT_EQ("true", value->AsString());
117 ASSERT_TRUE(value->AsBool());
118 ASSERT_ANY_THROW(value->AsList());
119 ASSERT_ANY_THROW(value->GetProperty("foo"));
120 ASSERT_ANY_THROW(value->SetProperty("foo", false));
121 ASSERT_ANY_THROW(value->GetClassName());
122 ASSERT_ANY_THROW(value->Call());
123 }
124
125 TEST(JsValueTest, ObjectValue)
126 {
127 ThrowingErrorCallback errorCallback;
128 AdblockPlus::JsEngine jsEngine(0, 0, &errorCallback);
129 const std::string source("\
130 function Foo() {\
131 this.x = 2;\
132 this.toString = function() {return 'foo';};\
133 this.valueOf = function() {return 123;};\
134 };\
135 new Foo()");
136 AdblockPlus::JsValuePtr value = jsEngine.Evaluate(source);
137 ASSERT_FALSE(value->IsUndefined());
138 ASSERT_FALSE(value->IsNull());
139 ASSERT_FALSE(value->IsString());
140 ASSERT_FALSE(value->IsBool());
141 ASSERT_FALSE(value->IsNumber());
142 ASSERT_TRUE(value->IsObject());
143 ASSERT_FALSE(value->IsArray());
144 ASSERT_FALSE(value->IsFunction());
145 ASSERT_EQ("foo", value->AsString());
146 ASSERT_EQ(123, value->AsInt());
147 ASSERT_TRUE(value->AsBool());
148 ASSERT_ANY_THROW(value->AsList());
149 ASSERT_EQ(2, value->GetProperty("x")->AsInt());
150 value->SetProperty("x", 12);
151 ASSERT_EQ(12, value->GetProperty("x")->AsInt());
152 ASSERT_EQ("Foo", value->GetClassName());
153 ASSERT_ANY_THROW(value->Call());
154 }
155
156 TEST(JsValueTest, ArrayValue)
157 {
158 ThrowingErrorCallback errorCallback;
159 AdblockPlus::JsEngine jsEngine(0, 0, &errorCallback);
160 AdblockPlus::JsValuePtr value = jsEngine.Evaluate("[5,8,12]");
161 ASSERT_FALSE(value->IsUndefined());
162 ASSERT_FALSE(value->IsNull());
163 ASSERT_FALSE(value->IsString());
164 ASSERT_FALSE(value->IsBool());
165 ASSERT_FALSE(value->IsNumber());
166 ASSERT_TRUE(value->IsObject());
167 ASSERT_TRUE(value->IsArray());
168 ASSERT_FALSE(value->IsFunction());
169 ASSERT_EQ("5,8,12", value->AsString());
170 ASSERT_TRUE(value->AsBool());
171 ASSERT_EQ(3u, value->AsList().size());
172 ASSERT_EQ(8, value->AsList()[1]->AsInt());
173 ASSERT_EQ(3, value->GetProperty("length")->AsInt());
174 ASSERT_EQ("Array", value->GetClassName());
175 ASSERT_ANY_THROW(value->Call());
176 }
177
178 TEST(JsValueTest, FunctionValue)
179 {
180 ThrowingErrorCallback errorCallback;
181 AdblockPlus::JsEngine jsEngine(0, 0, &errorCallback);
182 AdblockPlus::JsValuePtr value = jsEngine.Evaluate("(function(foo, bar) {return this.x + '/' + foo + '/' + bar;})");
183 ASSERT_FALSE(value->IsUndefined());
184 ASSERT_FALSE(value->IsNull());
185 ASSERT_FALSE(value->IsString());
186 ASSERT_FALSE(value->IsBool());
187 ASSERT_FALSE(value->IsNumber());
188 ASSERT_TRUE(value->IsObject());
189 ASSERT_FALSE(value->IsArray());
190 ASSERT_TRUE(value->IsFunction());
191 ASSERT_TRUE(value->AsBool());
192 ASSERT_ANY_THROW(value->AsList());
193 ASSERT_EQ(2, value->GetProperty("length")->AsInt());
194
195 AdblockPlus::JsValuePtr thisPtr = jsEngine.Evaluate("({x:2})");
196 AdblockPlus::JsValueList params;
197 params.push_back(jsEngine.NewValue(5));
198 params.push_back(jsEngine.NewValue("xyz"));
199 ASSERT_EQ("2/5/xyz", value->Call(params, thisPtr)->AsString());
200 }
201
202 TEST(JsValueTest, ThrowingCoversion)
203 {
204 ThrowingErrorCallback errorCallback;
205 AdblockPlus::JsEngine jsEngine(0, 0, &errorCallback);
206 const std::string source("\
207 function Foo() {\
208 this.toString = function() {throw 'test1';};\
209 this.valueOf = function() {throw 'test2';};\
210 };\
211 new Foo()");
212 AdblockPlus::JsValuePtr value = jsEngine.Evaluate(source);
213 ASSERT_EQ("", value->AsString());
214 ASSERT_EQ(0, value->AsInt());
215 }
OLDNEW
« no previous file with comments | « test/JsEngine.cpp ('k') | test/WebRequest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld