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

Side by Side Diff: test/JsValue.cpp

Issue 10184021: Some refactoring of global JavaScript objects (Closed)
Patch Set: Created April 15, 2013, 2:54 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') | no next file » | 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 <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 }
(...skipping 11 matching lines...) Expand all
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->Call()); 33 ASSERT_ANY_THROW(value->Call());
33 } 34 }
34 35
35 TEST(JsValueTest, NullValue) 36 TEST(JsValueTest, NullValue)
36 { 37 {
37 ThrowingErrorCallback errorCallback; 38 ThrowingErrorCallback errorCallback;
38 AdblockPlus::JsEngine jsEngine(0, 0, &errorCallback); 39 AdblockPlus::JsEngine jsEngine(0, 0, &errorCallback);
39 AdblockPlus::JsValuePtr value = jsEngine.Evaluate("null"); 40 AdblockPlus::JsValuePtr value = jsEngine.Evaluate("null");
40 ASSERT_FALSE(value->IsUndefined()); 41 ASSERT_FALSE(value->IsUndefined());
41 ASSERT_TRUE(value->IsNull()); 42 ASSERT_TRUE(value->IsNull());
42 ASSERT_FALSE(value->IsString()); 43 ASSERT_FALSE(value->IsString());
43 ASSERT_FALSE(value->IsBool()); 44 ASSERT_FALSE(value->IsBool());
44 ASSERT_FALSE(value->IsNumber()); 45 ASSERT_FALSE(value->IsNumber());
45 ASSERT_FALSE(value->IsObject()); 46 ASSERT_FALSE(value->IsObject());
46 ASSERT_FALSE(value->IsArray()); 47 ASSERT_FALSE(value->IsArray());
47 ASSERT_FALSE(value->IsFunction()); 48 ASSERT_FALSE(value->IsFunction());
48 ASSERT_EQ("null", value->AsString()); 49 ASSERT_EQ("null", value->AsString());
49 ASSERT_FALSE(value->AsBool()); 50 ASSERT_FALSE(value->AsBool());
50 ASSERT_ANY_THROW(value->AsList()); 51 ASSERT_ANY_THROW(value->AsList());
51 ASSERT_ANY_THROW(value->GetProperty("foo")); 52 ASSERT_ANY_THROW(value->GetProperty("foo"));
52 ASSERT_ANY_THROW(value->SetProperty("foo", false)); 53 ASSERT_ANY_THROW(value->SetProperty("foo", false));
53 ASSERT_ANY_THROW(value->GetClassName()); 54 ASSERT_ANY_THROW(value->GetClassName());
55 ASSERT_ANY_THROW(value->GetOwnPropertyNames());
54 ASSERT_ANY_THROW(value->Call()); 56 ASSERT_ANY_THROW(value->Call());
55 } 57 }
56 58
57 TEST(JsValueTest, StringValue) 59 TEST(JsValueTest, StringValue)
58 { 60 {
59 ThrowingErrorCallback errorCallback; 61 ThrowingErrorCallback errorCallback;
60 AdblockPlus::JsEngine jsEngine(0, 0, &errorCallback); 62 AdblockPlus::JsEngine jsEngine(0, 0, &errorCallback);
61 AdblockPlus::JsValuePtr value = jsEngine.Evaluate("'123'"); 63 AdblockPlus::JsValuePtr value = jsEngine.Evaluate("'123'");
62 ASSERT_FALSE(value->IsUndefined()); 64 ASSERT_FALSE(value->IsUndefined());
63 ASSERT_FALSE(value->IsNull()); 65 ASSERT_FALSE(value->IsNull());
64 ASSERT_TRUE(value->IsString()); 66 ASSERT_TRUE(value->IsString());
65 ASSERT_FALSE(value->IsBool()); 67 ASSERT_FALSE(value->IsBool());
66 ASSERT_FALSE(value->IsNumber()); 68 ASSERT_FALSE(value->IsNumber());
67 ASSERT_FALSE(value->IsObject()); 69 ASSERT_FALSE(value->IsObject());
68 ASSERT_FALSE(value->IsArray()); 70 ASSERT_FALSE(value->IsArray());
69 ASSERT_FALSE(value->IsFunction()); 71 ASSERT_FALSE(value->IsFunction());
70 ASSERT_EQ("123", value->AsString()); 72 ASSERT_EQ("123", value->AsString());
71 ASSERT_EQ(123, value->AsInt()); 73 ASSERT_EQ(123, value->AsInt());
72 ASSERT_TRUE(value->AsBool()); 74 ASSERT_TRUE(value->AsBool());
73 ASSERT_ANY_THROW(value->AsList()); 75 ASSERT_ANY_THROW(value->AsList());
74 ASSERT_ANY_THROW(value->GetProperty("foo")); 76 ASSERT_ANY_THROW(value->GetProperty("foo"));
75 ASSERT_ANY_THROW(value->SetProperty("foo", false)); 77 ASSERT_ANY_THROW(value->SetProperty("foo", false));
76 ASSERT_ANY_THROW(value->GetClassName()); 78 ASSERT_ANY_THROW(value->GetClassName());
79 ASSERT_ANY_THROW(value->GetOwnPropertyNames());
77 ASSERT_ANY_THROW(value->Call()); 80 ASSERT_ANY_THROW(value->Call());
78 } 81 }
79 82
80 TEST(JsValueTest, IntValue) 83 TEST(JsValueTest, IntValue)
81 { 84 {
82 ThrowingErrorCallback errorCallback; 85 ThrowingErrorCallback errorCallback;
83 AdblockPlus::JsEngine jsEngine(0, 0, &errorCallback); 86 AdblockPlus::JsEngine jsEngine(0, 0, &errorCallback);
84 AdblockPlus::JsValuePtr value = jsEngine.Evaluate("123"); 87 AdblockPlus::JsValuePtr value = jsEngine.Evaluate("123");
85 ASSERT_FALSE(value->IsUndefined()); 88 ASSERT_FALSE(value->IsUndefined());
86 ASSERT_FALSE(value->IsNull()); 89 ASSERT_FALSE(value->IsNull());
87 ASSERT_FALSE(value->IsString()); 90 ASSERT_FALSE(value->IsString());
88 ASSERT_FALSE(value->IsBool()); 91 ASSERT_FALSE(value->IsBool());
89 ASSERT_TRUE(value->IsNumber()); 92 ASSERT_TRUE(value->IsNumber());
90 ASSERT_FALSE(value->IsObject()); 93 ASSERT_FALSE(value->IsObject());
91 ASSERT_FALSE(value->IsArray()); 94 ASSERT_FALSE(value->IsArray());
92 ASSERT_FALSE(value->IsFunction()); 95 ASSERT_FALSE(value->IsFunction());
93 ASSERT_EQ("123", value->AsString()); 96 ASSERT_EQ("123", value->AsString());
94 ASSERT_EQ(123, value->AsInt()); 97 ASSERT_EQ(123, value->AsInt());
95 ASSERT_TRUE(value->AsBool()); 98 ASSERT_TRUE(value->AsBool());
96 ASSERT_ANY_THROW(value->AsList()); 99 ASSERT_ANY_THROW(value->AsList());
97 ASSERT_ANY_THROW(value->GetProperty("foo")); 100 ASSERT_ANY_THROW(value->GetProperty("foo"));
98 ASSERT_ANY_THROW(value->SetProperty("foo", false)); 101 ASSERT_ANY_THROW(value->SetProperty("foo", false));
99 ASSERT_ANY_THROW(value->GetClassName()); 102 ASSERT_ANY_THROW(value->GetClassName());
103 ASSERT_ANY_THROW(value->GetOwnPropertyNames());
100 ASSERT_ANY_THROW(value->Call()); 104 ASSERT_ANY_THROW(value->Call());
101 } 105 }
102 106
103 TEST(JsValueTest, BoolValue) 107 TEST(JsValueTest, BoolValue)
104 { 108 {
105 ThrowingErrorCallback errorCallback; 109 ThrowingErrorCallback errorCallback;
106 AdblockPlus::JsEngine jsEngine(0, 0, &errorCallback); 110 AdblockPlus::JsEngine jsEngine(0, 0, &errorCallback);
107 AdblockPlus::JsValuePtr value = jsEngine.Evaluate("true"); 111 AdblockPlus::JsValuePtr value = jsEngine.Evaluate("true");
108 ASSERT_FALSE(value->IsUndefined()); 112 ASSERT_FALSE(value->IsUndefined());
109 ASSERT_FALSE(value->IsNull()); 113 ASSERT_FALSE(value->IsNull());
110 ASSERT_FALSE(value->IsString()); 114 ASSERT_FALSE(value->IsString());
111 ASSERT_TRUE(value->IsBool()); 115 ASSERT_TRUE(value->IsBool());
112 ASSERT_FALSE(value->IsNumber()); 116 ASSERT_FALSE(value->IsNumber());
113 ASSERT_FALSE(value->IsObject()); 117 ASSERT_FALSE(value->IsObject());
114 ASSERT_FALSE(value->IsArray()); 118 ASSERT_FALSE(value->IsArray());
115 ASSERT_FALSE(value->IsFunction()); 119 ASSERT_FALSE(value->IsFunction());
116 ASSERT_EQ("true", value->AsString()); 120 ASSERT_EQ("true", value->AsString());
117 ASSERT_TRUE(value->AsBool()); 121 ASSERT_TRUE(value->AsBool());
118 ASSERT_ANY_THROW(value->AsList()); 122 ASSERT_ANY_THROW(value->AsList());
119 ASSERT_ANY_THROW(value->GetProperty("foo")); 123 ASSERT_ANY_THROW(value->GetProperty("foo"));
120 ASSERT_ANY_THROW(value->SetProperty("foo", false)); 124 ASSERT_ANY_THROW(value->SetProperty("foo", false));
121 ASSERT_ANY_THROW(value->GetClassName()); 125 ASSERT_ANY_THROW(value->GetClassName());
126 ASSERT_ANY_THROW(value->GetOwnPropertyNames());
122 ASSERT_ANY_THROW(value->Call()); 127 ASSERT_ANY_THROW(value->Call());
123 } 128 }
124 129
125 TEST(JsValueTest, ObjectValue) 130 TEST(JsValueTest, ObjectValue)
126 { 131 {
127 ThrowingErrorCallback errorCallback; 132 ThrowingErrorCallback errorCallback;
128 AdblockPlus::JsEngine jsEngine(0, 0, &errorCallback); 133 AdblockPlus::JsEngine jsEngine(0, 0, &errorCallback);
129 const std::string source("\ 134 const std::string source("\
130 function Foo() {\ 135 function Foo() {\
131 this.x = 2;\ 136 this.x = 2;\
(...skipping 10 matching lines...) Expand all
142 ASSERT_TRUE(value->IsObject()); 147 ASSERT_TRUE(value->IsObject());
143 ASSERT_FALSE(value->IsArray()); 148 ASSERT_FALSE(value->IsArray());
144 ASSERT_FALSE(value->IsFunction()); 149 ASSERT_FALSE(value->IsFunction());
145 ASSERT_EQ("foo", value->AsString()); 150 ASSERT_EQ("foo", value->AsString());
146 ASSERT_EQ(123, value->AsInt()); 151 ASSERT_EQ(123, value->AsInt());
147 ASSERT_TRUE(value->AsBool()); 152 ASSERT_TRUE(value->AsBool());
148 ASSERT_ANY_THROW(value->AsList()); 153 ASSERT_ANY_THROW(value->AsList());
149 ASSERT_EQ(2, value->GetProperty("x")->AsInt()); 154 ASSERT_EQ(2, value->GetProperty("x")->AsInt());
150 value->SetProperty("x", 12); 155 value->SetProperty("x", 12);
151 ASSERT_EQ(12, value->GetProperty("x")->AsInt()); 156 ASSERT_EQ(12, value->GetProperty("x")->AsInt());
157 value->SetProperty("x", jsEngine.NewValue(15));
158 ASSERT_EQ(15, value->GetProperty("x")->AsInt());
152 ASSERT_EQ("Foo", value->GetClassName()); 159 ASSERT_EQ("Foo", value->GetClassName());
160 ASSERT_EQ(3u, value->GetOwnPropertyNames().size());
153 ASSERT_ANY_THROW(value->Call()); 161 ASSERT_ANY_THROW(value->Call());
154 } 162 }
155 163
156 TEST(JsValueTest, ArrayValue) 164 TEST(JsValueTest, ArrayValue)
157 { 165 {
158 ThrowingErrorCallback errorCallback; 166 ThrowingErrorCallback errorCallback;
159 AdblockPlus::JsEngine jsEngine(0, 0, &errorCallback); 167 AdblockPlus::JsEngine jsEngine(0, 0, &errorCallback);
160 AdblockPlus::JsValuePtr value = jsEngine.Evaluate("[5,8,12]"); 168 AdblockPlus::JsValuePtr value = jsEngine.Evaluate("[5,8,12]");
161 ASSERT_FALSE(value->IsUndefined()); 169 ASSERT_FALSE(value->IsUndefined());
162 ASSERT_FALSE(value->IsNull()); 170 ASSERT_FALSE(value->IsNull());
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
206 const std::string source("\ 214 const std::string source("\
207 function Foo() {\ 215 function Foo() {\
208 this.toString = function() {throw 'test1';};\ 216 this.toString = function() {throw 'test1';};\
209 this.valueOf = function() {throw 'test2';};\ 217 this.valueOf = function() {throw 'test2';};\
210 };\ 218 };\
211 new Foo()"); 219 new Foo()");
212 AdblockPlus::JsValuePtr value = jsEngine.Evaluate(source); 220 AdblockPlus::JsValuePtr value = jsEngine.Evaluate(source);
213 ASSERT_EQ("", value->AsString()); 221 ASSERT_EQ("", value->AsString());
214 ASSERT_EQ(0, value->AsInt()); 222 ASSERT_EQ(0, value->AsInt());
215 } 223 }
OLDNEW
« no previous file with comments | « test/JsEngine.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld