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

Side by Side Diff: test/JsValue.cpp

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