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: Remove a #include Utils.h from test. Created June 2, 2017, 7:38 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()),
sergei 2017/06/16 15:05:56 std::move is not necessary here
hub 2017/06/16 21:52:55 Done.
+ std::string("Unable to read " + path));
sergei 2017/06/16 15:05:56 Actually, here and below one can simply pass "some
hub 2017/06/16 21:52:56 Done.
+ return;
+ }
+ callback(std::move(std::string(contentToRead)), "");
sergei 2017/06/16 15:05:56 std::move is not necessary here
hub 2017/06/16 21:52:55 Done.
}
- 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;

Powered by Google App Engine
This is Rietveld