Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: test/FileSystemJsObject.cpp

Issue 29481704: Noissue - Use buffer for FileSystem IO (Closed) Base URL: https://hg.adblockplus.org/libadblockplus/
Patch Set: Nit addressed Created July 7, 2017, 12:50 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « test/DefaultFileSystem.cpp ('k') | test/Prefs.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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-2017 eyeo GmbH 3 * Copyright (C) 2006-2017 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
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 #include <sstream> 18 #include <sstream>
19 #include "BaseJsTest.h" 19 #include "BaseJsTest.h"
20 #include "../src/Thread.h" 20 #include "../src/Thread.h"
21 21
22 namespace 22 namespace
23 { 23 {
24 class MockFileSystem : public AdblockPlus::FileSystem 24 class MockFileSystem : public AdblockPlus::FileSystem
25 { 25 {
26 public: 26 public:
27 bool success; 27 bool success;
28 std::string contentToRead; 28 IOBuffer contentToRead;
29 std::string lastWrittenPath; 29 std::string lastWrittenPath;
30 std::string lastWrittenContent; 30 IOBuffer lastWrittenContent;
31 std::string movedFrom; 31 std::string movedFrom;
32 std::string movedTo; 32 std::string movedTo;
33 std::string removedPath; 33 std::string removedPath;
34 mutable std::string statPath; 34 mutable std::string statPath;
35 bool statExists; 35 bool statExists;
36 bool statIsDirectory; 36 bool statIsDirectory;
37 bool statIsFile; 37 bool statIsFile;
38 int statLastModified; 38 int statLastModified;
39 39
40 MockFileSystem() : success(true) 40 MockFileSystem() : success(true)
41 { 41 {
42 } 42 }
43 43
44 std::shared_ptr<std::istream> Read(const std::string& path) const 44 IOBuffer Read(const std::string& path) const
45 { 45 {
46 if (!success) 46 if (!success)
47 throw std::runtime_error("Unable to read " + path); 47 throw std::runtime_error("Unable to read " + path);
48 std::stringstream* const stream = new std::stringstream; 48 return contentToRead;
49 *stream << contentToRead;
50 return std::shared_ptr<std::istream>(stream);
51 } 49 }
52 50
53 void Write(const std::string& path, std::istream& data) 51 void Write(const std::string& path, const IOBuffer& data)
54 { 52 {
55 if (!success) 53 if (!success)
56 throw std::runtime_error("Unable to write to " + path); 54 throw std::runtime_error("Unable to write to " + path);
57 lastWrittenPath = path; 55 lastWrittenPath = path;
58 56 lastWrittenContent = data;
59 std::stringstream content;
60 content << data.rdbuf();
61 lastWrittenContent = content.str();
62 } 57 }
63 58
64 void Move(const std::string& fromPath, const std::string& toPath) 59 void Move(const std::string& fromPath, const std::string& toPath)
65 { 60 {
66 if (!success) 61 if (!success)
67 throw std::runtime_error("Unable to move " + fromPath + " to " 62 throw std::runtime_error("Unable to move " + fromPath + " to "
68 + toPath); 63 + toPath);
69 movedFrom = fromPath; 64 movedFrom = fromPath;
70 movedTo = toPath; 65 movedTo = toPath;
71 } 66 }
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
118 { 113 {
119 BaseJsTest::SetUp(); 114 BaseJsTest::SetUp();
120 mockFileSystem = MockFileSystemPtr(new MockFileSystem); 115 mockFileSystem = MockFileSystemPtr(new MockFileSystem);
121 jsEngine->SetFileSystem(mockFileSystem); 116 jsEngine->SetFileSystem(mockFileSystem);
122 } 117 }
123 }; 118 };
124 } 119 }
125 120
126 TEST_F(FileSystemJsObjectTest, Read) 121 TEST_F(FileSystemJsObjectTest, Read)
127 { 122 {
128 mockFileSystem->contentToRead = "foo"; 123 mockFileSystem->contentToRead =
124 AdblockPlus::FileSystem::IOBuffer{'f', 'o', 'o'};
129 std::string content; 125 std::string content;
130 std::string error; 126 std::string error;
131 ReadFile(jsEngine, content, error); 127 ReadFile(jsEngine, content, error);
132 ASSERT_EQ("foo", content); 128 ASSERT_EQ("foo", content);
133 ASSERT_EQ("", error); 129 ASSERT_EQ("", error);
134 } 130 }
135 131
136 TEST_F(FileSystemJsObjectTest, ReadIllegalArguments) 132 TEST_F(FileSystemJsObjectTest, ReadIllegalArguments)
137 { 133 {
138 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.read()")); 134 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.read()"));
139 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.read('', '')")); 135 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.read('', '')"));
140 } 136 }
141 137
142 TEST_F(FileSystemJsObjectTest, ReadError) 138 TEST_F(FileSystemJsObjectTest, ReadError)
143 { 139 {
144 mockFileSystem->success = false; 140 mockFileSystem->success = false;
145 std::string content; 141 std::string content;
146 std::string error; 142 std::string error;
147 ReadFile(jsEngine, content, error); 143 ReadFile(jsEngine, content, error);
148 ASSERT_NE("", error); 144 ASSERT_NE("", error);
149 ASSERT_EQ("", content); 145 ASSERT_EQ("", content);
150 } 146 }
151 147
152 TEST_F(FileSystemJsObjectTest, Write) 148 TEST_F(FileSystemJsObjectTest, Write)
153 { 149 {
154 jsEngine->Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e})") ; 150 jsEngine->Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e})") ;
155 AdblockPlus::Sleep(50); 151 AdblockPlus::Sleep(50);
156 ASSERT_EQ("foo", mockFileSystem->lastWrittenPath); 152 ASSERT_EQ("foo", mockFileSystem->lastWrittenPath);
157 ASSERT_EQ("bar", mockFileSystem->lastWrittenContent); 153 ASSERT_EQ((AdblockPlus::FileSystem::IOBuffer{'b', 'a', 'r'}),
154 mockFileSystem->lastWrittenContent);
158 ASSERT_EQ("", jsEngine->Evaluate("error").AsString()); 155 ASSERT_EQ("", jsEngine->Evaluate("error").AsString());
159 } 156 }
160 157
161 TEST_F(FileSystemJsObjectTest, WriteIllegalArguments) 158 TEST_F(FileSystemJsObjectTest, WriteIllegalArguments)
162 { 159 {
163 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.write()")); 160 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.write()"));
164 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.write('', '', '')")); 161 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.write('', '', '')"));
165 } 162 }
166 163
167 TEST_F(FileSystemJsObjectTest, WriteError) 164 TEST_F(FileSystemJsObjectTest, WriteError)
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
239 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.stat('', '')")); 236 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.stat('', '')"));
240 } 237 }
241 238
242 TEST_F(FileSystemJsObjectTest, StatError) 239 TEST_F(FileSystemJsObjectTest, StatError)
243 { 240 {
244 mockFileSystem->success = false; 241 mockFileSystem->success = false;
245 jsEngine->Evaluate("_fileSystem.stat('foo', function(r) {result = r})"); 242 jsEngine->Evaluate("_fileSystem.stat('foo', function(r) {result = r})");
246 AdblockPlus::Sleep(50); 243 AdblockPlus::Sleep(50);
247 ASSERT_NE("", jsEngine->Evaluate("result.error").AsString()); 244 ASSERT_NE("", jsEngine->Evaluate("result.error").AsString());
248 } 245 }
OLDNEW
« no previous file with comments | « test/DefaultFileSystem.cpp ('k') | test/Prefs.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld