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