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