OLD | NEW |
1 #include <AdblockPlus.h> | 1 #include "BaseJsTest.h" |
2 #include <gtest/gtest.h> | |
3 | 2 |
4 class ThrowingErrorCallback : public AdblockPlus::ErrorCallback | 3 namespace |
5 { | 4 { |
6 public: | 5 class JsEngineTest : public BaseJsTest |
7 void operator()(const std::string& message) | |
8 { | 6 { |
9 throw std::runtime_error("Unexpected error: " + message); | 7 }; |
10 } | 8 } |
11 }; | |
12 | 9 |
13 TEST(JsEngineTest, Evaluate) | 10 TEST_F(JsEngineTest, Evaluate) |
14 { | 11 { |
15 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New()); | |
16 jsEngine->SetErrorCallback(AdblockPlus::ErrorCallbackPtr(new ThrowingErrorCall
back())); | |
17 jsEngine->Evaluate("function hello() { return 'Hello'; }"); | 12 jsEngine->Evaluate("function hello() { return 'Hello'; }"); |
18 AdblockPlus::JsValuePtr result = jsEngine->Evaluate("hello()"); | 13 AdblockPlus::JsValuePtr result = jsEngine->Evaluate("hello()"); |
19 ASSERT_TRUE(result->IsString()); | 14 ASSERT_TRUE(result->IsString()); |
20 ASSERT_EQ("Hello", result->AsString()); | 15 ASSERT_EQ("Hello", result->AsString()); |
21 } | 16 } |
22 | 17 |
23 TEST(JsEngineTest, RuntimeExceptionIsThrown) | 18 TEST_F(JsEngineTest, RuntimeExceptionIsThrown) |
24 { | 19 { |
25 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New()); | |
26 jsEngine->SetErrorCallback(AdblockPlus::ErrorCallbackPtr(new ThrowingErrorCall
back())); | |
27 ASSERT_THROW(jsEngine->Evaluate("doesnotexist()"), AdblockPlus::JsError); | 20 ASSERT_THROW(jsEngine->Evaluate("doesnotexist()"), AdblockPlus::JsError); |
28 } | 21 } |
29 | 22 |
30 TEST(JsEngineTest, CompileTimeExceptionIsThrown) | 23 TEST_F(JsEngineTest, CompileTimeExceptionIsThrown) |
31 { | 24 { |
32 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New()); | |
33 jsEngine->SetErrorCallback(AdblockPlus::ErrorCallbackPtr(new ThrowingErrorCall
back())); | |
34 ASSERT_THROW(jsEngine->Evaluate("'foo'bar'"), AdblockPlus::JsError); | 25 ASSERT_THROW(jsEngine->Evaluate("'foo'bar'"), AdblockPlus::JsError); |
35 } | 26 } |
36 | 27 |
37 TEST(JsEngineTest, ValueCreation) | 28 TEST_F(JsEngineTest, ValueCreation) |
38 { | 29 { |
39 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New()); | |
40 jsEngine->SetErrorCallback(AdblockPlus::ErrorCallbackPtr(new ThrowingErrorCall
back())); | |
41 AdblockPlus::JsValuePtr value; | 30 AdblockPlus::JsValuePtr value; |
42 | 31 |
43 value = jsEngine->NewValue("foo"); | 32 value = jsEngine->NewValue("foo"); |
44 ASSERT_TRUE(value->IsString()); | 33 ASSERT_TRUE(value->IsString()); |
45 ASSERT_EQ("foo", value->AsString()); | 34 ASSERT_EQ("foo", value->AsString()); |
46 | 35 |
47 value = jsEngine->NewValue(12345678901234); | 36 value = jsEngine->NewValue(12345678901234); |
48 ASSERT_TRUE(value->IsNumber()); | 37 ASSERT_TRUE(value->IsNumber()); |
49 ASSERT_EQ(12345678901234, value->AsInt()); | 38 ASSERT_EQ(12345678901234, value->AsInt()); |
50 | 39 |
51 value = jsEngine->NewValue(true); | 40 value = jsEngine->NewValue(true); |
52 ASSERT_TRUE(value->IsBool()); | 41 ASSERT_TRUE(value->IsBool()); |
53 ASSERT_TRUE(value->AsBool()); | 42 ASSERT_TRUE(value->AsBool()); |
54 | 43 |
55 value = jsEngine->NewObject(); | 44 value = jsEngine->NewObject(); |
56 ASSERT_TRUE(value->IsObject()); | 45 ASSERT_TRUE(value->IsObject()); |
57 ASSERT_EQ(0u, value->GetOwnPropertyNames().size()); | 46 ASSERT_EQ(0u, value->GetOwnPropertyNames().size()); |
58 } | 47 } |
59 | 48 |
60 TEST(JsEngineTest, CallbackGetSet) | 49 TEST(NewJsEngineTest, CallbackGetSet) |
61 { | 50 { |
62 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New()); | 51 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New()); |
63 | 52 |
64 ASSERT_TRUE(jsEngine->GetErrorCallback()); | 53 ASSERT_TRUE(jsEngine->GetErrorCallback()); |
65 ASSERT_ANY_THROW(jsEngine->SetErrorCallback(AdblockPlus::ErrorCallbackPtr())); | 54 ASSERT_ANY_THROW(jsEngine->SetErrorCallback(AdblockPlus::ErrorCallbackPtr())); |
66 AdblockPlus::ErrorCallbackPtr errorCallback(new AdblockPlus::DefaultErrorCallb
ack()); | 55 AdblockPlus::ErrorCallbackPtr errorCallback(new AdblockPlus::DefaultErrorCallb
ack()); |
67 jsEngine->SetErrorCallback(errorCallback); | 56 jsEngine->SetErrorCallback(errorCallback); |
68 ASSERT_EQ(errorCallback, jsEngine->GetErrorCallback()); | 57 ASSERT_EQ(errorCallback, jsEngine->GetErrorCallback()); |
69 | 58 |
70 ASSERT_TRUE(jsEngine->GetFileSystem()); | 59 ASSERT_TRUE(jsEngine->GetFileSystem()); |
71 ASSERT_ANY_THROW(jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr())); | 60 ASSERT_ANY_THROW(jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr())); |
72 AdblockPlus::FileSystemPtr fileSystem(new AdblockPlus::DefaultFileSystem()); | 61 AdblockPlus::FileSystemPtr fileSystem(new AdblockPlus::DefaultFileSystem()); |
73 jsEngine->SetFileSystem(fileSystem); | 62 jsEngine->SetFileSystem(fileSystem); |
74 ASSERT_EQ(fileSystem, jsEngine->GetFileSystem()); | 63 ASSERT_EQ(fileSystem, jsEngine->GetFileSystem()); |
75 | 64 |
76 ASSERT_TRUE(jsEngine->GetWebRequest()); | 65 ASSERT_TRUE(jsEngine->GetWebRequest()); |
77 ASSERT_ANY_THROW(jsEngine->SetWebRequest(AdblockPlus::WebRequestPtr())); | 66 ASSERT_ANY_THROW(jsEngine->SetWebRequest(AdblockPlus::WebRequestPtr())); |
78 AdblockPlus::WebRequestPtr webRequest(new AdblockPlus::DefaultWebRequest()); | 67 AdblockPlus::WebRequestPtr webRequest(new AdblockPlus::DefaultWebRequest()); |
79 jsEngine->SetWebRequest(webRequest); | 68 jsEngine->SetWebRequest(webRequest); |
80 ASSERT_EQ(webRequest, jsEngine->GetWebRequest()); | 69 ASSERT_EQ(webRequest, jsEngine->GetWebRequest()); |
81 } | 70 } |
OLD | NEW |