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

Side by Side Diff: test/JsEngine.cpp

Issue 29417605: Issue 5034 - Part 3: Create plain JsValue instead of JsValuePtr (Closed) Base URL: https://hg.adblockplus.org/libadblockplus/
Patch Set: Pass JsEngine by ref Created April 20, 2017, 1:01 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/FilterEngine.cpp ('k') | test/JsValue.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 /* 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
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 #include <stdexcept> 18 #include <stdexcept>
19 #include "BaseJsTest.h" 19 #include "BaseJsTest.h"
20 #include "../src/JsContext.h"
20 21
21 namespace 22 namespace
22 { 23 {
23 class JsEngineTest : public BaseJsTest 24 class JsEngineTest : public BaseJsTest
24 { 25 {
25 }; 26 };
26 } 27 }
27 28
28 TEST_F(JsEngineTest, Evaluate) 29 TEST_F(JsEngineTest, Evaluate)
29 { 30 {
30 jsEngine->Evaluate("function hello() { return 'Hello'; }"); 31 jsEngine->Evaluate("function hello() { return 'Hello'; }");
31 AdblockPlus::JsValuePtr result = jsEngine->Evaluate("hello()"); 32 AdblockPlus::JsValuePtr result = jsEngine->Evaluate("hello()");
32 ASSERT_TRUE(result->IsString()); 33 ASSERT_TRUE(result->IsString());
33 ASSERT_EQ("Hello", result->AsString()); 34 ASSERT_EQ("Hello", result->AsString());
34 } 35 }
35 36
36 TEST_F(JsEngineTest, RuntimeExceptionIsThrown) 37 TEST_F(JsEngineTest, RuntimeExceptionIsThrown)
37 { 38 {
38 ASSERT_THROW(jsEngine->Evaluate("doesnotexist()"), std::runtime_error); 39 ASSERT_THROW(jsEngine->Evaluate("doesnotexist()"), std::runtime_error);
39 } 40 }
40 41
41 TEST_F(JsEngineTest, CompileTimeExceptionIsThrown) 42 TEST_F(JsEngineTest, CompileTimeExceptionIsThrown)
42 { 43 {
43 ASSERT_THROW(jsEngine->Evaluate("'foo'bar'"), std::runtime_error); 44 ASSERT_THROW(jsEngine->Evaluate("'foo'bar'"), std::runtime_error);
44 } 45 }
45 46
46 TEST_F(JsEngineTest, ValueCreation) 47 TEST_F(JsEngineTest, ValueCreation)
47 { 48 {
48 AdblockPlus::JsValuePtr value; 49 auto value = jsEngine->NewValue("foo");
49 50 ASSERT_TRUE(value.IsString());
50 value = jsEngine->NewValue("foo"); 51 ASSERT_EQ("foo", value.AsString());
51 ASSERT_TRUE(value->IsString());
52 ASSERT_EQ("foo", value->AsString());
53 52
54 value = jsEngine->NewValue(12345678901234); 53 value = jsEngine->NewValue(12345678901234);
55 ASSERT_TRUE(value->IsNumber()); 54 ASSERT_TRUE(value.IsNumber());
56 ASSERT_EQ(12345678901234, value->AsInt()); 55 ASSERT_EQ(12345678901234, value.AsInt());
57 56
58 value = jsEngine->NewValue(true); 57 value = jsEngine->NewValue(true);
59 ASSERT_TRUE(value->IsBool()); 58 ASSERT_TRUE(value.IsBool());
60 ASSERT_TRUE(value->AsBool()); 59 ASSERT_TRUE(value.AsBool());
61 60
62 value = jsEngine->NewObject(); 61 value = jsEngine->NewObject();
63 ASSERT_TRUE(value->IsObject()); 62 ASSERT_TRUE(value.IsObject());
64 ASSERT_EQ(0u, value->GetOwnPropertyNames().size()); 63 ASSERT_EQ(0u, value.GetOwnPropertyNames().size());
64 }
65
66 namespace {
67
68 bool IsSame(AdblockPlus::JsEngine& jsEngine,
69 const AdblockPlus::JsValue& v1, const AdblockPlus::JsValue& v2)
70 {
71 AdblockPlus::JsValueList params;
72 params.push_back(v1);
73 params.push_back(v2);
74 return jsEngine.Evaluate("f = function(a, b) { return a == b };")->Call(para ms).AsBool();
75 }
76
77 }
78
79 TEST_F(JsEngineTest, ValueCopy)
80 {
81 {
82 auto value = jsEngine->NewValue("foo");
83 ASSERT_TRUE(value.IsString());
84 ASSERT_EQ("foo", value.AsString());
85
86 AdblockPlus::JsValue value2(value);
87 ASSERT_TRUE(value2.IsString());
88 ASSERT_EQ("foo", value2.AsString());
89
90 ASSERT_TRUE(IsSame(*jsEngine, value, value2));
91 }
92 {
93 auto value = jsEngine->NewValue(12345678901234);
94 ASSERT_TRUE(value.IsNumber());
95 ASSERT_EQ(12345678901234, value.AsInt());
96
97 AdblockPlus::JsValue value2(value);
98 ASSERT_TRUE(value2.IsNumber());
99 ASSERT_EQ(12345678901234, value2.AsInt());
100
101 ASSERT_TRUE(IsSame(*jsEngine, value, value2));
102 }
103 {
104 auto value = jsEngine->NewValue(true);
105 ASSERT_TRUE(value.IsBool());
106 ASSERT_TRUE(value.AsBool());
107
108 AdblockPlus::JsValue value2(value);
109 ASSERT_TRUE(value2.IsBool());
110 ASSERT_TRUE(value2.AsBool());
111
112 ASSERT_TRUE(IsSame(*jsEngine, value, value2));
113 }
114 {
115 auto value = jsEngine->NewObject();
116 ASSERT_TRUE(value.IsObject());
117 ASSERT_EQ(0u, value.GetOwnPropertyNames().size());
118
119 AdblockPlus::JsValue value2(value);
120 ASSERT_TRUE(value2.IsObject());
121 ASSERT_EQ(0u, value2.GetOwnPropertyNames().size());
122
123 ASSERT_TRUE(IsSame(*jsEngine, value, value2));
124 }
65 } 125 }
66 126
67 TEST_F(JsEngineTest, EventCallbacks) 127 TEST_F(JsEngineTest, EventCallbacks)
68 { 128 {
69 bool callbackCalled = false; 129 bool callbackCalled = false;
70 AdblockPlus::JsConstValueList callbackParams; 130 AdblockPlus::JsValueList callbackParams;
71 auto Callback = [&callbackCalled, & callbackParams]( 131 auto Callback = [&callbackCalled, & callbackParams](
72 const AdblockPlus::JsConstValueList& params) 132 const AdblockPlus::JsValueList& params)
73 { 133 {
74 callbackCalled = true; 134 callbackCalled = true;
75 callbackParams = params; 135 callbackParams = params;
76 }; 136 };
77 137
78 // Trigger event without a callback 138 // Trigger event without a callback
79 callbackCalled = false; 139 callbackCalled = false;
80 jsEngine->Evaluate("_triggerEvent('foobar')"); 140 jsEngine->Evaluate("_triggerEvent('foobar')");
81 ASSERT_FALSE(callbackCalled); 141 ASSERT_FALSE(callbackCalled);
82 142
83 // Set callback 143 // Set callback
84 jsEngine->SetEventCallback("foobar", Callback); 144 jsEngine->SetEventCallback("foobar", Callback);
85 callbackCalled = false; 145 callbackCalled = false;
86 jsEngine->Evaluate("_triggerEvent('foobar', 1, 'x', true)"); 146 jsEngine->Evaluate("_triggerEvent('foobar', 1, 'x', true)");
87 ASSERT_TRUE(callbackCalled); 147 ASSERT_TRUE(callbackCalled);
88 ASSERT_EQ(callbackParams.size(), 3u); 148 ASSERT_EQ(callbackParams.size(), 3u);
89 ASSERT_EQ(callbackParams[0]->AsInt(), 1); 149 ASSERT_EQ(callbackParams[0].AsInt(), 1);
90 ASSERT_EQ(callbackParams[1]->AsString(), "x"); 150 ASSERT_EQ(callbackParams[1].AsString(), "x");
91 ASSERT_TRUE(callbackParams[2]->AsBool()); 151 ASSERT_TRUE(callbackParams[2].AsBool());
92 152
93 // Trigger a different event 153 // Trigger a different event
94 callbackCalled = false; 154 callbackCalled = false;
95 jsEngine->Evaluate("_triggerEvent('barfoo')"); 155 jsEngine->Evaluate("_triggerEvent('barfoo')");
96 ASSERT_FALSE(callbackCalled); 156 ASSERT_FALSE(callbackCalled);
97 157
98 // Remove callback 158 // Remove callback
99 jsEngine->RemoveEventCallback("foobar"); 159 jsEngine->RemoveEventCallback("foobar");
100 callbackCalled = false; 160 callbackCalled = false;
101 jsEngine->Evaluate("_triggerEvent('foobar')"); 161 jsEngine->Evaluate("_triggerEvent('foobar')");
(...skipping 19 matching lines...) Expand all
121 ASSERT_TRUE(jsEngine->GetWebRequest()); 181 ASSERT_TRUE(jsEngine->GetWebRequest());
122 ASSERT_ANY_THROW(jsEngine->SetWebRequest(AdblockPlus::WebRequestPtr())); 182 ASSERT_ANY_THROW(jsEngine->SetWebRequest(AdblockPlus::WebRequestPtr()));
123 AdblockPlus::WebRequestPtr webRequest(new AdblockPlus::DefaultWebRequest()); 183 AdblockPlus::WebRequestPtr webRequest(new AdblockPlus::DefaultWebRequest());
124 jsEngine->SetWebRequest(webRequest); 184 jsEngine->SetWebRequest(webRequest);
125 ASSERT_EQ(webRequest, jsEngine->GetWebRequest()); 185 ASSERT_EQ(webRequest, jsEngine->GetWebRequest());
126 } 186 }
127 187
128 TEST(NewJsEngineTest, GlobalPropertyTest) 188 TEST(NewJsEngineTest, GlobalPropertyTest)
129 { 189 {
130 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New()); 190 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New());
131 jsEngine->SetGlobalProperty("foo", *jsEngine->NewValue("bar")); 191 jsEngine->SetGlobalProperty("foo", jsEngine->NewValue("bar"));
132 AdblockPlus::JsValuePtr foo = jsEngine->Evaluate("foo"); 192 AdblockPlus::JsValuePtr foo = jsEngine->Evaluate("foo");
133 ASSERT_TRUE(foo->IsString()); 193 ASSERT_TRUE(foo->IsString());
134 ASSERT_EQ(foo->AsString(), "bar"); 194 ASSERT_EQ(foo->AsString(), "bar");
135 } 195 }
136 196
OLDNEW
« no previous file with comments | « test/FilterEngine.cpp ('k') | test/JsValue.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld