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

Side by Side Diff: test/FileSystemJsObject.cpp

Issue 29531696: Issue 5568 - Improve string handling by splitting read strings into only ASCII and non-ASCII strings (Closed) Base URL: https://github.com/adblockplus/libadblockplus.git
Patch Set: address comments Created Aug. 31, 2017, 1:20 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 | « src/JsError.cpp ('k') | no next file » | 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-present eyeo GmbH 3 * Copyright (C) 2006-present 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 using namespace AdblockPlus;
23
22 namespace 24 namespace
23 { 25 {
24 class MockFileSystem : public AdblockPlus::IFileSystem 26 class MockFileSystem : public AdblockPlus::IFileSystem
25 { 27 {
26 public: 28 public:
27 bool success; 29 bool success;
28 IOBuffer contentToRead; 30 IOBuffer contentToRead;
29 std::string lastWrittenFile; 31 std::string lastWrittenFile;
30 IOBuffer lastWrittenContent; 32 IOBuffer lastWrittenContent;
31 std::string movedFrom; 33 std::string movedFrom;
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after
234 ASSERT_ANY_THROW(GetJsEngine().Evaluate("_fileSystem.stat()")); 236 ASSERT_ANY_THROW(GetJsEngine().Evaluate("_fileSystem.stat()"));
235 ASSERT_ANY_THROW(GetJsEngine().Evaluate("_fileSystem.stat('', '')")); 237 ASSERT_ANY_THROW(GetJsEngine().Evaluate("_fileSystem.stat('', '')"));
236 } 238 }
237 239
238 TEST_F(FileSystemJsObjectTest, StatError) 240 TEST_F(FileSystemJsObjectTest, StatError)
239 { 241 {
240 mockFileSystem->success = false; 242 mockFileSystem->success = false;
241 GetJsEngine().Evaluate("let result; _fileSystem.stat('foo', function(r) {resul t = r})"); 243 GetJsEngine().Evaluate("let result; _fileSystem.stat('foo', function(r) {resul t = r})");
242 ASSERT_FALSE(GetJsEngine().Evaluate("result.error").IsUndefined()); 244 ASSERT_FALSE(GetJsEngine().Evaluate("result.error").IsUndefined());
243 } 245 }
246
247 namespace
248 {
249 class FileSystemJsObject_ReadFromFileTest : public FileSystemJsObjectTest
250 {
251 public:
252 typedef std::function<void(const std::string&)> ReadFromFileCallback;
253 typedef std::vector<std::string> Lines;
254
255 void readFromFile(const ReadFromFileCallback& onLine)
256 {
257 ASSERT_TRUE(onLine);
258 auto& jsEngine = GetJsEngine();
259 bool isOnDoneCalled = false;
260 jsEngine.SetEventCallback("onLine", [onLine](JsValueList&& /*line*/jsArgs)
261 {
262 ASSERT_EQ(1u, jsArgs.size());
263 EXPECT_TRUE(jsArgs[0].IsString());
264 onLine(jsArgs[0].AsString());
265 });
266 jsEngine.SetEventCallback("onDone", [this, &isOnDoneCalled](JsValueList&& /*error*/jsArgs)
267 {
268 isOnDoneCalled = true;
269 if (this->mockFileSystem->success)
270 {
271 EXPECT_EQ(0u, jsArgs.size()) << jsArgs[0].AsString();
272 }
273 else
274 {
275 ASSERT_EQ(1u, jsArgs.size());
276 EXPECT_TRUE(jsArgs[0].IsString());
277 EXPECT_FALSE(jsArgs[0].AsString().empty());
278 }
279 });
280 jsEngine.Evaluate(R"js(_fileSystem.readFromFile("foo",
281 (line) => _triggerEvent("onLine", line),
282 (error) =>
283 {
284 if (error)
285 _triggerEvent("onDone", error);
286 else
287 _triggerEvent("onDone");
288 });)js");
289 EXPECT_TRUE(isOnDoneCalled);
290 }
291
292 void readFromFile_Lines(const std::string& content, const Lines& expected)
293 {
294 mockFileSystem->contentToRead.assign(content.begin(), content.end());
295 std::vector<std::string> readLines;
296 readFromFile([&readLines](const std::string& line)
297 {
298 readLines.emplace_back(line);
299 });
300
301 ASSERT_EQ(expected.size(), readLines.size());
302 for (Lines::size_type i = 0; i < expected.size(); ++i)
303 {
304 EXPECT_EQ(expected[i], readLines[i]);
305 }
306 }
307 };
308 }
309
310 TEST_F(FileSystemJsObject_ReadFromFileTest, NoFile)
311 {
312 bool isOnLineCalled = false;
313 mockFileSystem->success = false;
314 readFromFile([&isOnLineCalled](const std::string& line)
315 {
316 isOnLineCalled = true;
317 });
318 EXPECT_FALSE(isOnLineCalled);
319 }
320
321 TEST_F(FileSystemJsObject_ReadFromFileTest, Lines)
322 {
323 readFromFile_Lines({}, {""});
324 readFromFile_Lines({"\n"}, {""});
325 readFromFile_Lines({"\n\r"}, {""});
326 readFromFile_Lines({"\n\r\n"}, {""});
327
328 readFromFile_Lines({"line"}, {"line"});
329
330 readFromFile_Lines(
331 "first\n"
332 "second\r\n"
333 "third\r\n"
334 "\r\n"
335 "\n"
336 "last",
337 {"first", "second", "third", "last"});
338
339 readFromFile_Lines(
340 "first\n"
341 "second\r\n"
342 "third\n",
343 {"first", "second", "third"});
344
345 readFromFile_Lines(
346 "first\n"
347 "second\r\n"
348 "third\r\n",
349 {"first", "second", "third"});
350
351 readFromFile_Lines(
352 "first\n"
353 "second\r\n"
354 "third\r\n"
355 "\r\n"
356 "\n",
357 {"first", "second", "third"});
358
359 readFromFile_Lines(
360 "\n"
361 "first\n"
362 "second\r\n"
363 "third\r\n",
364 {"first", "second", "third"});
365 }
366
367 TEST_F(FileSystemJsObject_ReadFromFileTest, ProcessLineThrowsException)
368 {
369 std::string content = "1\n2\n3";
370 mockFileSystem->contentToRead.assign(content.begin(), content.end());
371
372 std::vector<std::string> readLines;
373 auto& jsEngine = GetJsEngine();
374 std::string error;
375 jsEngine.SetEventCallback("onLine", [&readLines](JsValueList&& /*line*/jsArgs)
376 {
377 ASSERT_EQ(1u, jsArgs.size());
378 EXPECT_TRUE(jsArgs[0].IsString());
379 readLines.emplace_back(jsArgs[0].AsString());
380 });
381 jsEngine.SetEventCallback("onDone", [this, &error](JsValueList&& /*error*/jsAr gs)
382 {
383 ASSERT_EQ(1u, jsArgs.size());
384 EXPECT_TRUE(jsArgs[0].IsString());
385 error = jsArgs[0].AsString();
386 });
387 jsEngine.Evaluate(R"js(
388 let onLineCounter = 0;
389 _fileSystem.readFromFile("foo",
390 (line) =>
391 {
392 if (onLineCounter++ == 2)
393 {
394 throw new Error("my-error");
395 }
396 _triggerEvent("onLine", line);
397 },
398 (error) =>
399 {
400 if (error)
401 _triggerEvent("onDone", error);
402 else
403 _triggerEvent("onDone");
404 });)js");
405 EXPECT_EQ(2u, readLines.size());
406 EXPECT_EQ("Error: my-error at undefined:8", error);
407 }
OLDNEW
« no previous file with comments | « src/JsError.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld