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

Side by Side Diff: test/FileSystemJsObject.cpp

Issue 29508569: Issue 5450 - don't expose std::shared_ptr<JsEngine> (Closed) Base URL: https://github.com/adblockplus/libadblockplus.git
Patch Set: Created Aug. 7, 2017, 8:39 a.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/ConsoleJsObject.cpp ('k') | test/FilterEngine.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
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
107 } 107 }
108 108
109 std::string Resolve(const std::string& path) const override 109 std::string Resolve(const std::string& path) const override
110 { 110 {
111 if (!success) 111 if (!success)
112 throw std::runtime_error("Unable to stat " + path); 112 throw std::runtime_error("Unable to stat " + path);
113 return path; 113 return path;
114 } 114 }
115 }; 115 };
116 116
117 void ReadFile(AdblockPlus::JsEnginePtr jsEngine, std::string& content, 117 void ReadFile(AdblockPlus::JsEngine& jsEngine, std::string& content,
118 std::string& error) 118 std::string& error)
119 { 119 {
120 jsEngine->Evaluate("_fileSystem.read('', function(r) {result = r})"); 120 jsEngine.Evaluate("_fileSystem.read('', function(r) {result = r})");
121 content = jsEngine->Evaluate("result.content").AsString(); 121 content = jsEngine.Evaluate("result.content").AsString();
122 error = jsEngine->Evaluate("result.error").AsString(); 122 error = jsEngine.Evaluate("result.error").AsString();
123 } 123 }
124 124
125 typedef std::shared_ptr<MockFileSystem> MockFileSystemPtr; 125 typedef std::shared_ptr<MockFileSystem> MockFileSystemPtr;
126 126
127 class FileSystemJsObjectTest : public BaseJsTest 127 class FileSystemJsObjectTest : public BaseJsTest
128 { 128 {
129 protected: 129 protected:
130 MockFileSystemPtr mockFileSystem; 130 MockFileSystemPtr mockFileSystem;
131 AdblockPlus::JsEnginePtr jsEngine;
132 131
133 void SetUp() 132 void SetUp()
134 { 133 {
135 mockFileSystem = MockFileSystemPtr(new MockFileSystem()); 134 mockFileSystem = MockFileSystemPtr(new MockFileSystem());
136 ThrowingPlatformCreationParameters params; 135 ThrowingPlatformCreationParameters params;
137 params.fileSystem = mockFileSystem; 136 params.fileSystem = mockFileSystem;
138 platform.reset(new AdblockPlus::Platform(std::move(params))); 137 platform.reset(new AdblockPlus::Platform(std::move(params)));
139 jsEngine = platform->GetJsEngine();
140 } 138 }
141 }; 139 };
142 } 140 }
143 141
144 TEST_F(FileSystemJsObjectTest, Read) 142 TEST_F(FileSystemJsObjectTest, Read)
145 { 143 {
146 mockFileSystem->contentToRead = 144 mockFileSystem->contentToRead =
147 AdblockPlus::IFileSystem::IOBuffer{'f', 'o', 'o'}; 145 AdblockPlus::IFileSystem::IOBuffer{'f', 'o', 'o'};
148 std::string content; 146 std::string content;
149 std::string error; 147 std::string error;
150 ReadFile(jsEngine, content, error); 148 ReadFile(GetJsEngine(), content, error);
151 ASSERT_EQ("foo", content); 149 ASSERT_EQ("foo", content);
152 ASSERT_EQ("undefined", error); 150 ASSERT_EQ("undefined", error);
153 } 151 }
154 152
155 TEST_F(FileSystemJsObjectTest, ReadIllegalArguments) 153 TEST_F(FileSystemJsObjectTest, ReadIllegalArguments)
156 { 154 {
157 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.read()")); 155 ASSERT_ANY_THROW(GetJsEngine().Evaluate("_fileSystem.read()"));
158 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.read('', '')")); 156 ASSERT_ANY_THROW(GetJsEngine().Evaluate("_fileSystem.read('', '')"));
159 } 157 }
160 158
161 TEST_F(FileSystemJsObjectTest, ReadError) 159 TEST_F(FileSystemJsObjectTest, ReadError)
162 { 160 {
163 mockFileSystem->success = false; 161 mockFileSystem->success = false;
164 std::string content; 162 std::string content;
165 std::string error; 163 std::string error;
166 ReadFile(jsEngine, content, error); 164 ReadFile(GetJsEngine(), content, error);
167 ASSERT_NE("", error); 165 ASSERT_NE("", error);
168 ASSERT_EQ("", content); 166 ASSERT_EQ("", content);
169 } 167 }
170 168
171 TEST_F(FileSystemJsObjectTest, Write) 169 TEST_F(FileSystemJsObjectTest, Write)
172 { 170 {
173 jsEngine->Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e})") ; 171 GetJsEngine().Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e })");
174 ASSERT_EQ("foo", mockFileSystem->lastWrittenPath); 172 ASSERT_EQ("foo", mockFileSystem->lastWrittenPath);
175 ASSERT_EQ((AdblockPlus::IFileSystem::IOBuffer{'b', 'a', 'r'}), 173 ASSERT_EQ((AdblockPlus::IFileSystem::IOBuffer{'b', 'a', 'r'}),
176 mockFileSystem->lastWrittenContent); 174 mockFileSystem->lastWrittenContent);
177 ASSERT_TRUE(jsEngine->Evaluate("error").IsUndefined()); 175 ASSERT_TRUE(GetJsEngine().Evaluate("error").IsUndefined());
178 } 176 }
179 177
180 TEST_F(FileSystemJsObjectTest, WriteIllegalArguments) 178 TEST_F(FileSystemJsObjectTest, WriteIllegalArguments)
181 { 179 {
182 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.write()")); 180 ASSERT_ANY_THROW(GetJsEngine().Evaluate("_fileSystem.write()"));
183 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.write('', '', '')")); 181 ASSERT_ANY_THROW(GetJsEngine().Evaluate("_fileSystem.write('', '', '')"));
184 } 182 }
185 183
186 TEST_F(FileSystemJsObjectTest, WriteError) 184 TEST_F(FileSystemJsObjectTest, WriteError)
187 { 185 {
188 mockFileSystem->success = false; 186 mockFileSystem->success = false;
189 jsEngine->Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e})") ; 187 GetJsEngine().Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e })");
190 ASSERT_NE("", jsEngine->Evaluate("error").AsString()); 188 ASSERT_NE("", GetJsEngine().Evaluate("error").AsString());
191 } 189 }
192 190
193 TEST_F(FileSystemJsObjectTest, Move) 191 TEST_F(FileSystemJsObjectTest, Move)
194 { 192 {
195 jsEngine->Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e})"); 193 GetJsEngine().Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e} )");
196 ASSERT_EQ("foo", mockFileSystem->movedFrom); 194 ASSERT_EQ("foo", mockFileSystem->movedFrom);
197 ASSERT_EQ("bar", mockFileSystem->movedTo); 195 ASSERT_EQ("bar", mockFileSystem->movedTo);
198 ASSERT_TRUE(jsEngine->Evaluate("error").IsUndefined()); 196 ASSERT_TRUE(GetJsEngine().Evaluate("error").IsUndefined());
199 } 197 }
200 198
201 TEST_F(FileSystemJsObjectTest, MoveIllegalArguments) 199 TEST_F(FileSystemJsObjectTest, MoveIllegalArguments)
202 { 200 {
203 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.move()")); 201 ASSERT_ANY_THROW(GetJsEngine().Evaluate("_fileSystem.move()"));
204 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.move('', '', '')")); 202 ASSERT_ANY_THROW(GetJsEngine().Evaluate("_fileSystem.move('', '', '')"));
205 } 203 }
206 204
207 TEST_F(FileSystemJsObjectTest, MoveError) 205 TEST_F(FileSystemJsObjectTest, MoveError)
208 { 206 {
209 mockFileSystem->success = false; 207 mockFileSystem->success = false;
210 jsEngine->Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e})"); 208 GetJsEngine().Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e} )");
211 ASSERT_FALSE(jsEngine->Evaluate("error").IsUndefined()); 209 ASSERT_FALSE(GetJsEngine().Evaluate("error").IsUndefined());
212 } 210 }
213 211
214 TEST_F(FileSystemJsObjectTest, Remove) 212 TEST_F(FileSystemJsObjectTest, Remove)
215 { 213 {
216 jsEngine->Evaluate("_fileSystem.remove('foo', function(e) {error = e})"); 214 GetJsEngine().Evaluate("_fileSystem.remove('foo', function(e) {error = e})");
217 ASSERT_EQ("foo", mockFileSystem->removedPath); 215 ASSERT_EQ("foo", mockFileSystem->removedPath);
218 ASSERT_TRUE(jsEngine->Evaluate("error").IsUndefined()); 216 ASSERT_TRUE(GetJsEngine().Evaluate("error").IsUndefined());
219 } 217 }
220 218
221 TEST_F(FileSystemJsObjectTest, RemoveIllegalArguments) 219 TEST_F(FileSystemJsObjectTest, RemoveIllegalArguments)
222 { 220 {
223 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.remove()")); 221 ASSERT_ANY_THROW(GetJsEngine().Evaluate("_fileSystem.remove()"));
224 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.remove('', '')")); 222 ASSERT_ANY_THROW(GetJsEngine().Evaluate("_fileSystem.remove('', '')"));
225 } 223 }
226 224
227 TEST_F(FileSystemJsObjectTest, RemoveError) 225 TEST_F(FileSystemJsObjectTest, RemoveError)
228 { 226 {
229 mockFileSystem->success = false; 227 mockFileSystem->success = false;
230 jsEngine->Evaluate("_fileSystem.remove('foo', function(e) {error = e})"); 228 GetJsEngine().Evaluate("_fileSystem.remove('foo', function(e) {error = e})");
231 ASSERT_NE("", jsEngine->Evaluate("error").AsString()); 229 ASSERT_NE("", GetJsEngine().Evaluate("error").AsString());
232 } 230 }
233 231
234 TEST_F(FileSystemJsObjectTest, Stat) 232 TEST_F(FileSystemJsObjectTest, Stat)
235 { 233 {
236 mockFileSystem->statExists = true; 234 mockFileSystem->statExists = true;
237 mockFileSystem->statIsDirectory= false; 235 mockFileSystem->statIsDirectory= false;
238 mockFileSystem->statIsFile = true; 236 mockFileSystem->statIsFile = true;
239 mockFileSystem->statLastModified = 1337; 237 mockFileSystem->statLastModified = 1337;
240 jsEngine->Evaluate("_fileSystem.stat('foo', function(r) {result = r})"); 238 GetJsEngine().Evaluate("_fileSystem.stat('foo', function(r) {result = r})");
241 ASSERT_EQ("foo", mockFileSystem->statPath); 239 ASSERT_EQ("foo", mockFileSystem->statPath);
242 ASSERT_TRUE(jsEngine->Evaluate("result.error").IsUndefined()); 240 ASSERT_TRUE(GetJsEngine().Evaluate("result.error").IsUndefined());
243 ASSERT_TRUE(jsEngine->Evaluate("result.exists").AsBool()); 241 ASSERT_TRUE(GetJsEngine().Evaluate("result.exists").AsBool());
244 ASSERT_FALSE(jsEngine->Evaluate("result.isDirectory").AsBool()); 242 ASSERT_FALSE(GetJsEngine().Evaluate("result.isDirectory").AsBool());
245 ASSERT_TRUE(jsEngine->Evaluate("result.isFile").AsBool()); 243 ASSERT_TRUE(GetJsEngine().Evaluate("result.isFile").AsBool());
246 ASSERT_EQ(1337, jsEngine->Evaluate("result.lastModified").AsInt()); 244 ASSERT_EQ(1337, GetJsEngine().Evaluate("result.lastModified").AsInt());
247 } 245 }
248 246
249 TEST_F(FileSystemJsObjectTest, StatIllegalArguments) 247 TEST_F(FileSystemJsObjectTest, StatIllegalArguments)
250 { 248 {
251 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.stat()")); 249 ASSERT_ANY_THROW(GetJsEngine().Evaluate("_fileSystem.stat()"));
252 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.stat('', '')")); 250 ASSERT_ANY_THROW(GetJsEngine().Evaluate("_fileSystem.stat('', '')"));
253 } 251 }
254 252
255 TEST_F(FileSystemJsObjectTest, StatError) 253 TEST_F(FileSystemJsObjectTest, StatError)
256 { 254 {
257 mockFileSystem->success = false; 255 mockFileSystem->success = false;
258 jsEngine->Evaluate("_fileSystem.stat('foo', function(r) {result = r})"); 256 GetJsEngine().Evaluate("_fileSystem.stat('foo', function(r) {result = r})");
259 ASSERT_FALSE(jsEngine->Evaluate("result.error").IsUndefined()); 257 ASSERT_FALSE(GetJsEngine().Evaluate("result.error").IsUndefined());
260 } 258 }
OLDNEW
« no previous file with comments | « test/ConsoleJsObject.cpp ('k') | test/FilterEngine.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld