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 | |
7 { | |
8 void Write(const std::string& path, | |
9 std::tr1::shared_ptr<std::ostream> content) | |
10 { | |
11 throw std::runtime_error("Write is not implemented"); | |
12 } | |
13 | |
14 void Move(const std::string& fromPath, const std::string& toPath) | |
15 { | |
16 throw std::runtime_error("Move is not implemented"); | |
17 } | |
18 | |
19 void Remove(const std::string& path) | |
20 { | |
21 throw std::runtime_error("Remove is not implemented"); | |
22 } | |
23 | |
24 StatResult Stat(const std::string& path) const | |
25 { | |
26 throw std::runtime_error("Stat is not implemented"); | |
27 } | |
28 }; | |
29 | |
30 class ThrowingErrorCallback : public AdblockPlus::ErrorCallback | 6 class ThrowingErrorCallback : public AdblockPlus::ErrorCallback |
31 { | 7 { |
32 public: | 8 public: |
33 void operator()(const std::string& message) | 9 void operator()(const std::string& message) |
34 { | 10 { |
35 throw std::runtime_error("Unexpected error: " + message); | 11 throw std::runtime_error("Unexpected error: " + message); |
36 } | 12 } |
37 }; | 13 }; |
38 | 14 |
39 class StubFileSystem : public BaseFileSystem | 15 TEST(JsEngineTest, Evaluate) |
40 { | |
41 public: | |
42 std::tr1::shared_ptr<std::istream> Read(const std::string& path) const | |
43 { | |
44 std::stringstream* const source = new std::stringstream; | |
45 *source << "function hello() { return 'Hello'; }"; | |
46 return std::tr1::shared_ptr<std::istream>(source); | |
47 } | |
48 }; | |
49 | |
50 class BadFileSystem : public BaseFileSystem | |
51 { | |
52 public: | |
53 std::tr1::shared_ptr<std::istream> Read(const std::string& path) const | |
54 { | |
55 std::ifstream* const file = new std::ifstream; | |
56 file->open(""); | |
57 return std::tr1::shared_ptr<std::istream>(file); | |
58 } | |
59 | |
60 void Write(const std::string& path, | |
61 std::tr1::shared_ptr<std::ostream> content) | |
62 { | |
63 throw std::runtime_error("No writing"); | |
64 } | |
65 }; | |
66 | |
67 TEST(JsEngineTest, EvaluateAndCall) | |
68 { | 16 { |
69 AdblockPlus::JsEngine jsEngine; | 17 AdblockPlus::JsEngine jsEngine; |
70 jsEngine.SetErrorCallback(AdblockPlus::ErrorCallbackPtr(new ThrowingErrorCallb
ack())); | 18 jsEngine.SetErrorCallback(AdblockPlus::ErrorCallbackPtr(new ThrowingErrorCallb
ack())); |
71 const std::string source = "function hello() { return 'Hello'; }"; | 19 jsEngine.Evaluate("function hello() { return 'Hello'; }"); |
72 jsEngine.Evaluate(source); | |
73 AdblockPlus::JsValuePtr result = jsEngine.Evaluate("hello()"); | 20 AdblockPlus::JsValuePtr result = jsEngine.Evaluate("hello()"); |
74 ASSERT_TRUE(result->IsString()); | 21 ASSERT_TRUE(result->IsString()); |
75 ASSERT_EQ("Hello", result->AsString()); | 22 ASSERT_EQ("Hello", result->AsString()); |
76 } | 23 } |
77 | 24 |
78 TEST(JsEngineTest, LoadAndCall) | |
79 { | |
80 AdblockPlus::JsEngine jsEngine; | |
81 jsEngine.SetErrorCallback(AdblockPlus::ErrorCallbackPtr(new ThrowingErrorCallb
ack())); | |
82 jsEngine.SetFileSystem(AdblockPlus::FileSystemPtr(new StubFileSystem())); | |
83 jsEngine.Load("hello.js"); | |
84 AdblockPlus::JsValuePtr result = jsEngine.Evaluate("hello()"); | |
85 ASSERT_TRUE(result->IsString()); | |
86 ASSERT_EQ("Hello", result->AsString()); | |
87 } | |
88 | |
89 TEST(JsEngineTest, LoadBadStreamFails) | |
90 { | |
91 AdblockPlus::JsEngine jsEngine; | |
92 jsEngine.SetErrorCallback(AdblockPlus::ErrorCallbackPtr(new ThrowingErrorCallb
ack())); | |
93 jsEngine.SetFileSystem(AdblockPlus::FileSystemPtr(new BadFileSystem())); | |
94 ASSERT_ANY_THROW(jsEngine.Load("hello.js")); | |
95 } | |
96 | |
97 TEST(JsEngineTest, RuntimeExceptionIsThrown) | 25 TEST(JsEngineTest, RuntimeExceptionIsThrown) |
98 { | 26 { |
99 AdblockPlus::JsEngine jsEngine; | 27 AdblockPlus::JsEngine jsEngine; |
100 jsEngine.SetErrorCallback(AdblockPlus::ErrorCallbackPtr(new ThrowingErrorCallb
ack())); | 28 jsEngine.SetErrorCallback(AdblockPlus::ErrorCallbackPtr(new ThrowingErrorCallb
ack())); |
101 ASSERT_THROW(jsEngine.Evaluate("doesnotexist()"), AdblockPlus::JsError); | 29 ASSERT_THROW(jsEngine.Evaluate("doesnotexist()"), AdblockPlus::JsError); |
102 } | 30 } |
103 | 31 |
104 TEST(JsEngineTest, CompileTimeExceptionIsThrown) | 32 TEST(JsEngineTest, CompileTimeExceptionIsThrown) |
105 { | 33 { |
106 AdblockPlus::JsEngine jsEngine; | 34 AdblockPlus::JsEngine jsEngine; |
(...skipping 16 matching lines...) Expand all Loading... |
123 ASSERT_EQ(12, value->AsInt()); | 51 ASSERT_EQ(12, value->AsInt()); |
124 | 52 |
125 value = jsEngine.NewValue(true); | 53 value = jsEngine.NewValue(true); |
126 ASSERT_TRUE(value->IsBool()); | 54 ASSERT_TRUE(value->IsBool()); |
127 ASSERT_TRUE(value->AsBool()); | 55 ASSERT_TRUE(value->AsBool()); |
128 | 56 |
129 value = jsEngine.NewObject(); | 57 value = jsEngine.NewObject(); |
130 ASSERT_TRUE(value->IsObject()); | 58 ASSERT_TRUE(value->IsObject()); |
131 ASSERT_EQ(0u, value->GetOwnPropertyNames().size()); | 59 ASSERT_EQ(0u, value->GetOwnPropertyNames().size()); |
132 } | 60 } |
OLD | NEW |