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

Unified 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: Added value test for copy ctor Created April 20, 2017, 12:40 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « test/FilterEngine.cpp ('k') | test/JsValue.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: test/JsEngine.cpp
===================================================================
--- a/test/JsEngine.cpp
+++ b/test/JsEngine.cpp
@@ -12,16 +12,17 @@
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdexcept>
#include "BaseJsTest.h"
+#include "../src/JsContext.h"
namespace
{
class JsEngineTest : public BaseJsTest
{
};
}
@@ -40,60 +41,119 @@
TEST_F(JsEngineTest, CompileTimeExceptionIsThrown)
{
ASSERT_THROW(jsEngine->Evaluate("'foo'bar'"), std::runtime_error);
}
TEST_F(JsEngineTest, ValueCreation)
{
- AdblockPlus::JsValuePtr value;
-
- value = jsEngine->NewValue("foo");
- ASSERT_TRUE(value->IsString());
- ASSERT_EQ("foo", value->AsString());
+ auto value = jsEngine->NewValue("foo");
+ ASSERT_TRUE(value.IsString());
+ ASSERT_EQ("foo", value.AsString());
value = jsEngine->NewValue(12345678901234);
- ASSERT_TRUE(value->IsNumber());
- ASSERT_EQ(12345678901234, value->AsInt());
+ ASSERT_TRUE(value.IsNumber());
+ ASSERT_EQ(12345678901234, value.AsInt());
value = jsEngine->NewValue(true);
- ASSERT_TRUE(value->IsBool());
- ASSERT_TRUE(value->AsBool());
+ ASSERT_TRUE(value.IsBool());
+ ASSERT_TRUE(value.AsBool());
value = jsEngine->NewObject();
- ASSERT_TRUE(value->IsObject());
- ASSERT_EQ(0u, value->GetOwnPropertyNames().size());
+ ASSERT_TRUE(value.IsObject());
+ ASSERT_EQ(0u, value.GetOwnPropertyNames().size());
+}
+
+namespace {
+
+ bool IsSame(const AdblockPlus::JsEnginePtr& jsEngine,
sergei 2017/04/20 12:45:08 Can be just `JsEngine&` (without pointer).
hub 2017/04/20 13:01:46 ok
+ const AdblockPlus::JsValue& v1, const AdblockPlus::JsValue& v2)
+ {
+ AdblockPlus::JsValueList params;
+ params.push_back(v1);
+ params.push_back(v2);
+ return jsEngine->Evaluate("f = function(a, b) { return a == b };")->Call(params).AsBool();
+ }
+
+}
+
+TEST_F(JsEngineTest, ValueCopy)
+{
+ {
+ auto value = jsEngine->NewValue("foo");
+ ASSERT_TRUE(value.IsString());
+ ASSERT_EQ("foo", value.AsString());
+
+ AdblockPlus::JsValue value2(value);
+ ASSERT_TRUE(value2.IsString());
+ ASSERT_EQ("foo", value2.AsString());
+
+ ASSERT_TRUE(IsSame(jsEngine, value, value2));
sergei 2017/04/20 12:45:08 I think we should rather try to use EXPECT_ when i
hub 2017/04/20 13:01:46 I used ASSERT_ for consistency. EXPECT_ is fine to
+ }
+ {
+ auto value = jsEngine->NewValue(12345678901234);
+ ASSERT_TRUE(value.IsNumber());
+ ASSERT_EQ(12345678901234, value.AsInt());
+
+ AdblockPlus::JsValue value2(value);
+ ASSERT_TRUE(value2.IsNumber());
+ ASSERT_EQ(12345678901234, value2.AsInt());
+
+ ASSERT_TRUE(IsSame(jsEngine, value, value2));
+ }
+ {
+ auto value = jsEngine->NewValue(true);
+ ASSERT_TRUE(value.IsBool());
+ ASSERT_TRUE(value.AsBool());
+
+ AdblockPlus::JsValue value2(value);
+ ASSERT_TRUE(value2.IsBool());
+ ASSERT_TRUE(value2.AsBool());
+
+ ASSERT_TRUE(IsSame(jsEngine, value, value2));
+ }
+ {
+ auto value = jsEngine->NewObject();
+ ASSERT_TRUE(value.IsObject());
+ ASSERT_EQ(0u, value.GetOwnPropertyNames().size());
+
+ AdblockPlus::JsValue value2(value);
+ ASSERT_TRUE(value2.IsObject());
+ ASSERT_EQ(0u, value2.GetOwnPropertyNames().size());
+
+ ASSERT_TRUE(IsSame(jsEngine, value, value2));
+ }
}
TEST_F(JsEngineTest, EventCallbacks)
{
bool callbackCalled = false;
- AdblockPlus::JsConstValueList callbackParams;
+ AdblockPlus::JsValueList callbackParams;
auto Callback = [&callbackCalled, & callbackParams](
- const AdblockPlus::JsConstValueList& params)
+ const AdblockPlus::JsValueList& params)
{
callbackCalled = true;
callbackParams = params;
};
// Trigger event without a callback
callbackCalled = false;
jsEngine->Evaluate("_triggerEvent('foobar')");
ASSERT_FALSE(callbackCalled);
// Set callback
jsEngine->SetEventCallback("foobar", Callback);
callbackCalled = false;
jsEngine->Evaluate("_triggerEvent('foobar', 1, 'x', true)");
ASSERT_TRUE(callbackCalled);
ASSERT_EQ(callbackParams.size(), 3u);
- ASSERT_EQ(callbackParams[0]->AsInt(), 1);
- ASSERT_EQ(callbackParams[1]->AsString(), "x");
- ASSERT_TRUE(callbackParams[2]->AsBool());
+ ASSERT_EQ(callbackParams[0].AsInt(), 1);
+ ASSERT_EQ(callbackParams[1].AsString(), "x");
+ ASSERT_TRUE(callbackParams[2].AsBool());
// Trigger a different event
callbackCalled = false;
jsEngine->Evaluate("_triggerEvent('barfoo')");
ASSERT_FALSE(callbackCalled);
// Remove callback
jsEngine->RemoveEventCallback("foobar");
@@ -123,14 +183,14 @@
AdblockPlus::WebRequestPtr webRequest(new AdblockPlus::DefaultWebRequest());
jsEngine->SetWebRequest(webRequest);
ASSERT_EQ(webRequest, jsEngine->GetWebRequest());
}
TEST(NewJsEngineTest, GlobalPropertyTest)
{
AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New());
- jsEngine->SetGlobalProperty("foo", *jsEngine->NewValue("bar"));
+ jsEngine->SetGlobalProperty("foo", jsEngine->NewValue("bar"));
AdblockPlus::JsValuePtr foo = jsEngine->Evaluate("foo");
ASSERT_TRUE(foo->IsString());
ASSERT_EQ(foo->AsString(), "bar");
}
« 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