OLD | NEW |
1 #include <AdblockPlus.h> | 1 #include <AdblockPlus.h> |
2 #include <fstream> | 2 #include <fstream> |
3 #include <gtest/gtest.h> | 3 #include <gtest/gtest.h> |
4 #include <sstream> | 4 #include <sstream> |
5 | 5 |
6 class BaseFileSystem : public AdblockPlus::FileSystem | 6 class BaseFileSystem : public AdblockPlus::FileSystem |
7 { | 7 { |
8 void Write(const std::string& path, | 8 void Write(const std::string& path, |
9 std::tr1::shared_ptr<std::ostream> content) | 9 std::tr1::shared_ptr<std::ostream> content) |
10 { | 10 { |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 void Write(const std::string& path, | 60 void Write(const std::string& path, |
61 std::tr1::shared_ptr<std::ostream> content) | 61 std::tr1::shared_ptr<std::ostream> content) |
62 { | 62 { |
63 throw std::runtime_error("No writing"); | 63 throw std::runtime_error("No writing"); |
64 } | 64 } |
65 }; | 65 }; |
66 | 66 |
67 TEST(JsEngineTest, EvaluateAndCall) | 67 TEST(JsEngineTest, EvaluateAndCall) |
68 { | 68 { |
69 ThrowingErrorCallback errorCallback; | 69 ThrowingErrorCallback errorCallback; |
70 AdblockPlus::JsEngine jsEngine(0, 0, &errorCallback); | 70 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), 0, 0, &errorCallback); |
71 const std::string source = "function hello() { return 'Hello'; }"; | 71 const std::string source = "function hello() { return 'Hello'; }"; |
72 jsEngine.Evaluate(source); | 72 jsEngine.Evaluate(source); |
73 AdblockPlus::JsValuePtr result = jsEngine.Evaluate("hello()"); | 73 AdblockPlus::JsValuePtr result = jsEngine.Evaluate("hello()"); |
74 ASSERT_TRUE(result->IsString()); | 74 ASSERT_TRUE(result->IsString()); |
75 ASSERT_EQ("Hello", result->AsString()); | 75 ASSERT_EQ("Hello", result->AsString()); |
76 } | 76 } |
77 | 77 |
78 TEST(JsEngineTest, LoadAndCall) | 78 TEST(JsEngineTest, LoadAndCall) |
79 { | 79 { |
80 StubFileSystem fileSystem; | 80 StubFileSystem fileSystem; |
81 ThrowingErrorCallback errorCallback; | 81 ThrowingErrorCallback errorCallback; |
82 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, &errorCallback); | 82 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), &fileSystem, 0, |
| 83 &errorCallback); |
83 jsEngine.Load("hello.js"); | 84 jsEngine.Load("hello.js"); |
84 AdblockPlus::JsValuePtr result = jsEngine.Evaluate("hello()"); | 85 AdblockPlus::JsValuePtr result = jsEngine.Evaluate("hello()"); |
85 ASSERT_TRUE(result->IsString()); | 86 ASSERT_TRUE(result->IsString()); |
86 ASSERT_EQ("Hello", result->AsString()); | 87 ASSERT_EQ("Hello", result->AsString()); |
87 } | 88 } |
88 | 89 |
89 TEST(JsEngineTest, LoadBadStreamFails) | 90 TEST(JsEngineTest, LoadBadStreamFails) |
90 { | 91 { |
91 BadFileSystem fileSystem; | 92 BadFileSystem fileSystem; |
92 ThrowingErrorCallback errorCallback; | 93 ThrowingErrorCallback errorCallback; |
93 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, &errorCallback); | 94 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), &fileSystem, 0, |
| 95 &errorCallback); |
94 ASSERT_ANY_THROW(jsEngine.Load("hello.js")); | 96 ASSERT_ANY_THROW(jsEngine.Load("hello.js")); |
95 } | 97 } |
96 | 98 |
97 TEST(JsEngineTest, RuntimeExceptionIsThrown) | 99 TEST(JsEngineTest, RuntimeExceptionIsThrown) |
98 { | 100 { |
99 ThrowingErrorCallback errorCallback; | 101 ThrowingErrorCallback errorCallback; |
100 AdblockPlus::JsEngine jsEngine(0, 0, &errorCallback); | 102 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), 0, 0, &errorCallback); |
101 ASSERT_THROW(jsEngine.Evaluate("doesnotexist()"), AdblockPlus::JsError); | 103 ASSERT_THROW(jsEngine.Evaluate("doesnotexist()"), AdblockPlus::JsError); |
102 } | 104 } |
103 | 105 |
104 TEST(JsEngineTest, CompileTimeExceptionIsThrown) | 106 TEST(JsEngineTest, CompileTimeExceptionIsThrown) |
105 { | 107 { |
106 ThrowingErrorCallback errorCallback; | 108 ThrowingErrorCallback errorCallback; |
107 AdblockPlus::JsEngine jsEngine(0, 0, &errorCallback); | 109 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), 0, 0, &errorCallback); |
108 ASSERT_THROW(jsEngine.Evaluate("'foo'bar'"), AdblockPlus::JsError); | 110 ASSERT_THROW(jsEngine.Evaluate("'foo'bar'"), AdblockPlus::JsError); |
109 } | 111 } |
110 | 112 |
111 TEST(JsEngineTest, ValueCreation) | 113 TEST(JsEngineTest, ValueCreation) |
112 { | 114 { |
113 ThrowingErrorCallback errorCallback; | 115 ThrowingErrorCallback errorCallback; |
114 AdblockPlus::JsEngine jsEngine(0, 0, &errorCallback); | 116 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), 0, 0, &errorCallback); |
115 AdblockPlus::JsValuePtr value; | 117 AdblockPlus::JsValuePtr value; |
116 | 118 |
117 value = jsEngine.NewValue("foo"); | 119 value = jsEngine.NewValue("foo"); |
118 ASSERT_TRUE(value->IsString()); | 120 ASSERT_TRUE(value->IsString()); |
119 ASSERT_EQ("foo", value->AsString()); | 121 ASSERT_EQ("foo", value->AsString()); |
120 | 122 |
121 value = jsEngine.NewValue(12); | 123 value = jsEngine.NewValue(12); |
122 ASSERT_TRUE(value->IsNumber()); | 124 ASSERT_TRUE(value->IsNumber()); |
123 ASSERT_EQ(12, value->AsInt()); | 125 ASSERT_EQ(12, value->AsInt()); |
124 | 126 |
125 value = jsEngine.NewValue(true); | 127 value = jsEngine.NewValue(true); |
126 ASSERT_TRUE(value->IsBool()); | 128 ASSERT_TRUE(value->IsBool()); |
127 ASSERT_TRUE(value->AsBool()); | 129 ASSERT_TRUE(value->AsBool()); |
128 | 130 |
129 value = jsEngine.NewObject(); | 131 value = jsEngine.NewObject(); |
130 ASSERT_TRUE(value->IsObject()); | 132 ASSERT_TRUE(value->IsObject()); |
131 ASSERT_EQ(0u, value->GetOwnPropertyNames().size()); | 133 ASSERT_EQ(0u, value->GetOwnPropertyNames().size()); |
132 } | 134 } |
OLD | NEW |