Index: test/FileSystemJsObject.cpp |
=================================================================== |
--- a/test/FileSystemJsObject.cpp |
+++ b/test/FileSystemJsObject.cpp |
@@ -16,83 +16,100 @@ |
*/ |
#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::vector<char> contentToRead; |
std::string lastWrittenPath; |
- std::string lastWrittenContent; |
+ std::vector<char> lastWrittenContent; |
std::string movedFrom; |
std::string movedTo; |
std::string removedPath; |
mutable std::string statPath; |
bool statExists; |
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::vector<char>(), "Unable to read " + path); |
+ return; |
+ } |
+ callback(std::vector<char>(contentToRead), ""); |
} |
- void Write(const std::string& path, std::istream& data) |
+ void Write(const std::string& path, const std::vector<char>& data, |
+ const Callback& callback) |
{ |
if (!success) |
- throw std::runtime_error("Unable to write to " + path); |
+ { |
+ callback("Unable to write to " + path); |
+ return; |
+ } |
lastWrittenPath = path; |
- std::stringstream content; |
- content << data.rdbuf(); |
- lastWrittenContent = content.str(); |
+ lastWrittenContent = data; |
+ 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("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("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 = "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,31 +128,32 @@ |
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"; |
+ mockFileSystem->contentToRead = std::vector<char>{'f', 'o', 'o'}; |
std::string content; |
std::string error; |
ReadFile(jsEngine, content, error); |
ASSERT_EQ("foo", content); |
- ASSERT_EQ("", error); |
+ ASSERT_EQ("undefined", error); |
} |
TEST_F(FileSystemJsObjectTest, ReadIllegalArguments) |
{ |
ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.read()")); |
ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.read('', '')")); |
} |
@@ -149,18 +167,19 @@ |
ASSERT_EQ("", content); |
} |
TEST_F(FileSystemJsObjectTest, Write) |
{ |
jsEngine->Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e})"); |
AdblockPlus::Sleep(50); |
ASSERT_EQ("foo", mockFileSystem->lastWrittenPath); |
- ASSERT_EQ("bar", mockFileSystem->lastWrittenContent); |
- ASSERT_EQ("", jsEngine->Evaluate("error").AsString()); |
+ ASSERT_EQ((std::vector<char>{'b', 'a', 'r'}), |
+ mockFileSystem->lastWrittenContent); |
+ ASSERT_TRUE(jsEngine->Evaluate("error").IsUndefined()); |
} |
TEST_F(FileSystemJsObjectTest, WriteIllegalArguments) |
{ |
ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.write()")); |
ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.write('', '', '')")); |
} |
@@ -173,39 +192,39 @@ |
} |
TEST_F(FileSystemJsObjectTest, Move) |
{ |
jsEngine->Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e})"); |
AdblockPlus::Sleep(50); |
ASSERT_EQ("foo", mockFileSystem->movedFrom); |
ASSERT_EQ("bar", mockFileSystem->movedTo); |
- ASSERT_EQ("", jsEngine->Evaluate("error").AsString()); |
+ ASSERT_TRUE(jsEngine->Evaluate("error").IsUndefined()); |
} |
TEST_F(FileSystemJsObjectTest, MoveIllegalArguments) |
{ |
ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.move()")); |
ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.move('', '', '')")); |
} |
TEST_F(FileSystemJsObjectTest, MoveError) |
{ |
mockFileSystem->success = false; |
jsEngine->Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e})"); |
AdblockPlus::Sleep(50); |
- ASSERT_NE("", jsEngine->Evaluate("error").AsString()); |
+ ASSERT_FALSE(jsEngine->Evaluate("error").IsUndefined()); |
} |
TEST_F(FileSystemJsObjectTest, Remove) |
{ |
jsEngine->Evaluate("_fileSystem.remove('foo', function(e) {error = e})"); |
AdblockPlus::Sleep(50); |
ASSERT_EQ("foo", mockFileSystem->removedPath); |
- ASSERT_EQ("", jsEngine->Evaluate("error").AsString()); |
+ ASSERT_TRUE(jsEngine->Evaluate("error").IsUndefined()); |
} |
TEST_F(FileSystemJsObjectTest, RemoveIllegalArguments) |
{ |
ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.remove()")); |
ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.remove('', '')")); |
} |
@@ -221,17 +240,17 @@ |
{ |
mockFileSystem->statExists = true; |
mockFileSystem->statIsDirectory= false; |
mockFileSystem->statIsFile = true; |
mockFileSystem->statLastModified = 1337; |
jsEngine->Evaluate("_fileSystem.stat('foo', function(r) {result = r})"); |
AdblockPlus::Sleep(50); |
ASSERT_EQ("foo", mockFileSystem->statPath); |
- ASSERT_EQ("", jsEngine->Evaluate("result.error").AsString()); |
+ ASSERT_TRUE(jsEngine->Evaluate("result.error").IsUndefined()); |
ASSERT_TRUE(jsEngine->Evaluate("result.exists").AsBool()); |
ASSERT_FALSE(jsEngine->Evaluate("result.isDirectory").AsBool()); |
ASSERT_TRUE(jsEngine->Evaluate("result.isFile").AsBool()); |
ASSERT_EQ(1337, jsEngine->Evaluate("result.lastModified").AsInt()); |
} |
TEST_F(FileSystemJsObjectTest, StatIllegalArguments) |
{ |
@@ -239,10 +258,10 @@ |
ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.stat('', '')")); |
} |
TEST_F(FileSystemJsObjectTest, StatError) |
{ |
mockFileSystem->success = false; |
jsEngine->Evaluate("_fileSystem.stat('foo', function(r) {result = r})"); |
AdblockPlus::Sleep(50); |
- ASSERT_NE("", jsEngine->Evaluate("result.error").AsString()); |
+ ASSERT_FALSE(jsEngine->Evaluate("result.error").IsUndefined()); |
} |