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

Unified Diff: test/FileSystemJsObject.cpp

Issue 29449592: Issue 5183 - Provide async interface for FileSystem (Closed) Base URL: https://hg.adblockplus.org/libadblockplus/
Patch Set: Updated implementation. Created June 2, 2017, 3:49 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: test/FileSystemJsObject.cpp
===================================================================
--- a/test/FileSystemJsObject.cpp
+++ b/test/FileSystemJsObject.cpp
@@ -16,17 +16,17 @@
*/
#include <sstream>
#include "BaseJsTest.h"
#include "../src/Thread.h"
namespace
{
- class MockFileSystem : public AdblockPlus::FileSystem
+ class MockFileSystem : public AdblockPlus::IFileSystem
{
public:
bool success;
std::string contentToRead;
std::string lastWrittenPath;
std::string lastWrittenContent;
std::string movedFrom;
std::string movedTo;
@@ -36,63 +36,84 @@
bool statIsDirectory;
bool statIsFile;
int statLastModified;
MockFileSystem() : success(true)
{
}
- std::shared_ptr<std::istream> Read(const std::string& path) const
+ void Read(const std::string& path, const ReadCallback& callback) const
{
if (!success)
- throw std::runtime_error("Unable to read " + path);
- std::stringstream* const stream = new std::stringstream;
- *stream << contentToRead;
- return std::shared_ptr<std::istream>(stream);
+ {
+ callback(std::move(std::string()),
+ std::string("Unable to read " + path));
+ return;
+ }
+ callback(std::move(std::string(contentToRead)), "");
}
- void Write(const std::string& path, std::istream& data)
+ void Write(const std::string& path, std::shared_ptr<std::istream> data,
+ const Callback& callback)
{
if (!success)
- throw std::runtime_error("Unable to write to " + path);
+ {
+ callback(std::string("Unable to write to " + path));
+ return;
+ }
lastWrittenPath = path;
std::stringstream content;
- content << data.rdbuf();
+ content << data->rdbuf();
lastWrittenContent = content.str();
+ callback("");
}
- void Move(const std::string& fromPath, const std::string& toPath)
+ void Move(const std::string& fromPath, const std::string& toPath,
+ const Callback& callback)
{
if (!success)
- throw std::runtime_error("Unable to move " + fromPath + " to "
- + toPath);
+ {
+ callback(std::string("Unable to move " + fromPath + " to "
+ + toPath));
+ return;
+ }
movedFrom = fromPath;
movedTo = toPath;
+ callback("");
}
- void Remove(const std::string& path)
+ void Remove(const std::string& path, const Callback& callback)
{
if (!success)
- throw std::runtime_error("Unable to remove " + path);
+ {
+ callback(std::string("Unable to remove " + path));
+ return;
+ }
removedPath = path;
+ callback("");
}
- StatResult Stat(const std::string& path) const
+ void Stat(const std::string& path,
+ const StatCallback& callback) const
{
- if (!success)
- throw std::runtime_error("Unable to stat " + path);
- statPath = path;
StatResult result;
- result.exists = statExists;
- result.isDirectory = statIsDirectory;
- result.isFile = statIsFile;
- result.lastModified = statLastModified;
- return result;
+ std::string error;
+ if (!success)
+ error = std::string("Unable to stat " + path);
+ else
+ {
+ statPath = path;
+ result.exists = statExists;
+ result.isDirectory = statIsDirectory;
+ result.isFile = statIsFile;
+ result.lastModified = statLastModified;
+ }
+ callback(result, error);
}
std::string Resolve(const std::string& path) const
{
if (!success)
throw std::runtime_error("Unable to stat " + path);
return path;
}
@@ -111,19 +132,20 @@
class FileSystemJsObjectTest : public BaseJsTest
{
protected:
MockFileSystemPtr mockFileSystem;
void SetUp()
{
- BaseJsTest::SetUp();
mockFileSystem = MockFileSystemPtr(new MockFileSystem);
- jsEngine->SetFileSystem(mockFileSystem);
+ JsEngineCreationParameters params;
+ params.fileSystem = mockFileSystem;
+ jsEngine = CreateJsEngine(std::move(params));
}
};
}
TEST_F(FileSystemJsObjectTest, Read)
{
mockFileSystem->contentToRead = "foo";
std::string content;
« lib/init.js ('K') | « test/DefaultFileSystem.cpp ('k') | test/FilterEngine.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld