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 ThrowingFileReader : public AdblockPlus::FileReader | 6 class BaseFileSystem : public AdblockPlus::FileSystem |
| 7 { |
| 8 void Write(const std::string& path, const std::string& content) |
| 9 { |
| 10 throw std::runtime_error("Write is not implemented"); |
| 11 } |
| 12 |
| 13 void Move(const std::string& fromPath, const std::string& toPath) |
| 14 { |
| 15 throw std::runtime_error("Move is not implemented"); |
| 16 } |
| 17 |
| 18 void Remove(const std::string& path) |
| 19 { |
| 20 throw std::runtime_error("Remove is not implemented"); |
| 21 } |
| 22 |
| 23 StatResult Stat(const std::string& path) const |
| 24 { |
| 25 throw std::runtime_error("Stat is not implemented"); |
| 26 } |
| 27 }; |
| 28 |
| 29 class ThrowingFileSystem : public BaseFileSystem |
7 { | 30 { |
8 public: | 31 public: |
9 std::auto_ptr<std::istream> Read(const std::string& path) const | 32 std::auto_ptr<std::istream> Read(const std::string& path) const |
10 { | 33 { |
11 throw std::runtime_error("Unexpected read of file: " + path); | 34 throw std::runtime_error("Unexpected read of file: " + path); |
12 } | 35 } |
13 }; | 36 }; |
14 | 37 |
15 class ThrowingErrorCallback : public AdblockPlus::ErrorCallback | 38 class ThrowingErrorCallback : public AdblockPlus::ErrorCallback |
16 { | 39 { |
17 public: | 40 public: |
18 void operator()(const std::string& message) | 41 void operator()(const std::string& message) |
19 { | 42 { |
20 throw std::runtime_error("Unexpected error: " + message); | 43 throw std::runtime_error("Unexpected error: " + message); |
21 } | 44 } |
22 }; | 45 }; |
23 | 46 |
24 class StubFileReader : public AdblockPlus::FileReader | 47 class StubFileSystem : public BaseFileSystem |
25 { | 48 { |
26 public: | 49 public: |
27 std::auto_ptr<std::istream> Read(const std::string& path) const | 50 std::auto_ptr<std::istream> Read(const std::string& path) const |
28 { | 51 { |
29 std::stringstream* const source = new std::stringstream; | 52 std::stringstream* const source = new std::stringstream; |
30 *source << "function hello() { return 'Hello'; }"; | 53 *source << "function hello() { return 'Hello'; }"; |
31 return std::auto_ptr<std::istream>(source); | 54 return std::auto_ptr<std::istream>(source); |
32 } | 55 } |
33 }; | 56 }; |
34 | 57 |
35 class BadFileReader : public AdblockPlus::FileReader | 58 class BadFileSystem : public BaseFileSystem |
36 { | 59 { |
37 public: | 60 public: |
38 std::auto_ptr<std::istream> Read(const std::string& path) const | 61 std::auto_ptr<std::istream> Read(const std::string& path) const |
39 { | 62 { |
40 std::ifstream* const file = new std::ifstream; | 63 std::ifstream* const file = new std::ifstream; |
41 file->open(""); | 64 file->open(""); |
42 return std::auto_ptr<std::istream>(file); | 65 return std::auto_ptr<std::istream>(file); |
43 } | 66 } |
44 }; | 67 }; |
45 | 68 |
46 TEST(JsEngineTest, EvaluateAndCall) | 69 TEST(JsEngineTest, EvaluateAndCall) |
47 { | 70 { |
48 ThrowingFileReader fileReader; | 71 ThrowingFileSystem fileSystem; |
49 ThrowingErrorCallback errorCallback; | 72 ThrowingErrorCallback errorCallback; |
50 AdblockPlus::JsEngine jsEngine(&fileReader, 0, &errorCallback); | 73 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, &errorCallback); |
51 const std::string source = "function hello() { return 'Hello'; }"; | 74 const std::string source = "function hello() { return 'Hello'; }"; |
52 jsEngine.Evaluate(source); | 75 jsEngine.Evaluate(source); |
53 const std::string result = jsEngine.Evaluate("hello()"); | 76 const std::string result = jsEngine.Evaluate("hello()"); |
54 ASSERT_EQ("Hello", result); | 77 ASSERT_EQ("Hello", result); |
55 } | 78 } |
56 | 79 |
57 TEST(JsEngineTest, LoadAndCall) | 80 TEST(JsEngineTest, LoadAndCall) |
58 { | 81 { |
59 StubFileReader fileReader; | 82 StubFileSystem fileSystem; |
60 ThrowingErrorCallback errorCallback; | 83 ThrowingErrorCallback errorCallback; |
61 AdblockPlus::JsEngine jsEngine(&fileReader, 0, &errorCallback); | 84 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, &errorCallback); |
62 jsEngine.Load("hello.js"); | 85 jsEngine.Load("hello.js"); |
63 const std::string result = jsEngine.Evaluate("hello()"); | 86 const std::string result = jsEngine.Evaluate("hello()"); |
64 ASSERT_EQ("Hello", result); | 87 ASSERT_EQ("Hello", result); |
65 } | 88 } |
66 | 89 |
67 TEST(JsEngineTest, LoadBadStreamFails) | 90 TEST(JsEngineTest, LoadBadStreamFails) |
68 { | 91 { |
69 BadFileReader fileReader; | 92 BadFileSystem fileSystem; |
70 ThrowingErrorCallback errorCallback; | 93 ThrowingErrorCallback errorCallback; |
71 AdblockPlus::JsEngine jsEngine(&fileReader, 0, &errorCallback); | 94 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, &errorCallback); |
72 ASSERT_ANY_THROW(jsEngine.Load("hello.js")); | 95 ASSERT_ANY_THROW(jsEngine.Load("hello.js")); |
73 } | 96 } |
74 | 97 |
75 TEST(JsEngineTest, RuntimeExceptionIsThrown) | 98 TEST(JsEngineTest, RuntimeExceptionIsThrown) |
76 { | 99 { |
77 ThrowingFileReader fileReader; | 100 ThrowingFileSystem fileSystem; |
78 ThrowingErrorCallback errorCallback; | 101 ThrowingErrorCallback errorCallback; |
79 AdblockPlus::JsEngine jsEngine(&fileReader, 0, &errorCallback); | 102 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, &errorCallback); |
80 ASSERT_THROW(jsEngine.Evaluate("doesnotexist()"), AdblockPlus::JsError); | 103 ASSERT_THROW(jsEngine.Evaluate("doesnotexist()"), AdblockPlus::JsError); |
81 } | 104 } |
82 | 105 |
83 TEST(JsEngineTest, CompileTimeExceptionIsThrown) | 106 TEST(JsEngineTest, CompileTimeExceptionIsThrown) |
84 { | 107 { |
85 ThrowingFileReader fileReader; | 108 ThrowingFileSystem fileSystem; |
86 ThrowingErrorCallback errorCallback; | 109 ThrowingErrorCallback errorCallback; |
87 AdblockPlus::JsEngine jsEngine(&fileReader, 0, &errorCallback); | 110 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, &errorCallback); |
88 ASSERT_THROW(jsEngine.Evaluate("'foo'bar'"), AdblockPlus::JsError); | 111 ASSERT_THROW(jsEngine.Evaluate("'foo'bar'"), AdblockPlus::JsError); |
89 } | 112 } |
OLD | NEW |