LEFT | RIGHT |
(no file at all) | |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-2016 Eyeo GmbH | 3 * Copyright (C) 2006-2016 Eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
(...skipping 26 matching lines...) Expand all Loading... |
37 int statLastModified; | 37 int statLastModified; |
38 | 38 |
39 MockFileSystem() : success(true) | 39 MockFileSystem() : success(true) |
40 { | 40 { |
41 } | 41 } |
42 | 42 |
43 std::shared_ptr<std::istream> Read(const std::string& path) const | 43 std::shared_ptr<std::istream> Read(const std::string& path) const |
44 { | 44 { |
45 if (!success) | 45 if (!success) |
46 throw std::runtime_error("Unable to read " + path); | 46 throw std::runtime_error("Unable to read " + path); |
47 std::stringstream* const stream = new std::stringstream; | 47 const auto stream = std::make_shared<std::stringstream>(); |
48 *stream << contentToRead; | 48 *stream << contentToRead; |
49 return std::shared_ptr<std::istream>(stream); | 49 return stream; |
50 } | 50 } |
51 | 51 |
52 void Write(const std::string& path, std::shared_ptr<std::istream> data) | 52 void Write(const std::string& path, std::shared_ptr<std::istream> data) |
53 { | 53 { |
54 if (!success) | 54 if (!success) |
55 throw std::runtime_error("Unable to write to " + path); | 55 throw std::runtime_error("Unable to write to " + path); |
56 lastWrittenPath = path; | 56 lastWrittenPath = path; |
57 | 57 |
58 std::stringstream content; | 58 std::stringstream content; |
59 content << data->rdbuf(); | 59 content << data->rdbuf(); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 typedef std::shared_ptr<MockFileSystem> MockFileSystemPtr; | 109 typedef std::shared_ptr<MockFileSystem> MockFileSystemPtr; |
110 | 110 |
111 class FileSystemJsObjectTest : public BaseJsTest | 111 class FileSystemJsObjectTest : public BaseJsTest |
112 { | 112 { |
113 protected: | 113 protected: |
114 MockFileSystemPtr mockFileSystem; | 114 MockFileSystemPtr mockFileSystem; |
115 | 115 |
116 void SetUp() | 116 void SetUp() |
117 { | 117 { |
118 BaseJsTest::SetUp(); | 118 BaseJsTest::SetUp(); |
119 mockFileSystem = MockFileSystemPtr(new MockFileSystem); | 119 mockFileSystem = std::make_shared<MockFileSystem>(); |
120 jsEngine->SetFileSystem(mockFileSystem); | 120 jsEngine->SetFileSystem(mockFileSystem); |
| 121 } |
| 122 |
| 123 void TearDown() |
| 124 { |
| 125 mockFileSystem.reset(); |
| 126 BaseJsTest::TearDown(); |
121 } | 127 } |
122 }; | 128 }; |
123 } | 129 } |
124 | 130 |
125 TEST_F(FileSystemJsObjectTest, Read) | 131 TEST_F(FileSystemJsObjectTest, Read) |
126 { | 132 { |
127 mockFileSystem->contentToRead = "foo"; | 133 mockFileSystem->contentToRead = "foo"; |
128 std::string content; | 134 std::string content; |
129 std::string error; | 135 std::string error; |
130 ReadFile(jsEngine, content, error); | 136 ReadFile(jsEngine, content, error); |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.stat('', '')")); | 244 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.stat('', '')")); |
239 } | 245 } |
240 | 246 |
241 TEST_F(FileSystemJsObjectTest, StatError) | 247 TEST_F(FileSystemJsObjectTest, StatError) |
242 { | 248 { |
243 mockFileSystem->success = false; | 249 mockFileSystem->success = false; |
244 jsEngine->Evaluate("_fileSystem.stat('foo', function(r) {result = r})"); | 250 jsEngine->Evaluate("_fileSystem.stat('foo', function(r) {result = r})"); |
245 std::this_thread::sleep_for(std::chrono::milliseconds(50)); | 251 std::this_thread::sleep_for(std::chrono::milliseconds(50)); |
246 ASSERT_NE("", jsEngine->Evaluate("result.error")->AsString()); | 252 ASSERT_NE("", jsEngine->Evaluate("result.error")->AsString()); |
247 } | 253 } |
LEFT | RIGHT |