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

Delta Between Two Patch Sets: test/JsEngine.cpp

Issue 10198022: Pass application data into libadblockplus (Closed)
Left Patch Set: Created April 12, 2013, 1:23 p.m.
Right Patch Set: Merged upstream, addressed issues, renamed AppInfo fields Created April 17, 2013, 2:47 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
LEFTRIGHT
1 #include <AdblockPlus.h> 1 #include <AdblockPlus.h>
2 #include <fstream> 2 #include <fstream>
3 #include <gtest/gtest.h> 3 #include <gtest/gtest.h>
4 #include <sstream> 4 #include <sstream>
5 5
6 class ThrowingFileReader : public AdblockPlus::FileReader 6 class BaseFileSystem : public AdblockPlus::FileSystem
7 { 7 {
8 public: 8 void Write(const std::string& path,
9 std::auto_ptr<std::istream> Read(const std::string& path) const 9 std::tr1::shared_ptr<std::ostream> content)
10 { 10 {
11 throw std::runtime_error("Unexpected read of file: " + path); 11 throw std::runtime_error("Write is not implemented");
12 } 12 }
13
14 void Move(const std::string& fromPath, const std::string& toPath)
15 {
16 throw std::runtime_error("Move is not implemented");
17 }
18
19 void Remove(const std::string& path)
20 {
21 throw std::runtime_error("Remove is not implemented");
22 }
23
24 StatResult Stat(const std::string& path) const
25 {
26 throw std::runtime_error("Stat is not implemented");
27 }
13 }; 28 };
14 29
15 class ThrowingErrorCallback : public AdblockPlus::ErrorCallback 30 class ThrowingErrorCallback : public AdblockPlus::ErrorCallback
16 { 31 {
17 public: 32 public:
18 void operator()(const std::string& message) 33 void operator()(const std::string& message)
19 { 34 {
20 throw std::runtime_error("Unexpected error: " + message); 35 throw std::runtime_error("Unexpected error: " + message);
21 } 36 }
22 }; 37 };
23 38
24 class StubFileReader : public AdblockPlus::FileReader 39 class StubFileSystem : public BaseFileSystem
25 { 40 {
26 public: 41 public:
27 std::auto_ptr<std::istream> Read(const std::string& path) const 42 std::tr1::shared_ptr<std::istream> Read(const std::string& path) const
28 { 43 {
29 std::stringstream* const source = new std::stringstream; 44 std::stringstream* const source = new std::stringstream;
30 *source << "function hello() { return 'Hello'; }"; 45 *source << "function hello() { return 'Hello'; }";
31 return std::auto_ptr<std::istream>(source); 46 return std::tr1::shared_ptr<std::istream>(source);
32 } 47 }
33 }; 48 };
34 49
35 class BadFileReader : public AdblockPlus::FileReader 50 class BadFileSystem : public BaseFileSystem
36 { 51 {
37 public: 52 public:
38 std::auto_ptr<std::istream> Read(const std::string& path) const 53 std::tr1::shared_ptr<std::istream> Read(const std::string& path) const
39 { 54 {
40 std::ifstream* const file = new std::ifstream; 55 std::ifstream* const file = new std::ifstream;
41 file->open(""); 56 file->open("");
42 return std::auto_ptr<std::istream>(file); 57 return std::tr1::shared_ptr<std::istream>(file);
58 }
59
60 void Write(const std::string& path,
61 std::tr1::shared_ptr<std::ostream> content)
62 {
63 throw std::runtime_error("No writing");
43 } 64 }
44 }; 65 };
45 66
46 TEST(JsEngineTest, EvaluateAndCall) 67 TEST(JsEngineTest, EvaluateAndCall)
47 { 68 {
48 ThrowingFileReader fileReader;
49 ThrowingErrorCallback errorCallback; 69 ThrowingErrorCallback errorCallback;
50 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), &fileReader, 0, 70 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), 0, 0, &errorCallback);
51 &errorCallback);
52 const std::string source = "function hello() { return 'Hello'; }"; 71 const std::string source = "function hello() { return 'Hello'; }";
53 jsEngine.Evaluate(source); 72 jsEngine.Evaluate(source);
54 const std::string result = jsEngine.Evaluate("hello()"); 73 AdblockPlus::JsValuePtr result = jsEngine.Evaluate("hello()");
55 ASSERT_EQ("Hello", result); 74 ASSERT_TRUE(result->IsString());
75 ASSERT_EQ("Hello", result->AsString());
56 } 76 }
57 77
58 TEST(JsEngineTest, LoadAndCall) 78 TEST(JsEngineTest, LoadAndCall)
59 { 79 {
60 StubFileReader fileReader; 80 StubFileSystem fileSystem;
61 ThrowingErrorCallback errorCallback; 81 ThrowingErrorCallback errorCallback;
62 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), &fileReader, 0, 82 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), &fileSystem, 0,
63 &errorCallback); 83 &errorCallback);
64 jsEngine.Load("hello.js"); 84 jsEngine.Load("hello.js");
65 const std::string result = jsEngine.Evaluate("hello()"); 85 AdblockPlus::JsValuePtr result = jsEngine.Evaluate("hello()");
66 ASSERT_EQ("Hello", result); 86 ASSERT_TRUE(result->IsString());
87 ASSERT_EQ("Hello", result->AsString());
67 } 88 }
68 89
69 TEST(JsEngineTest, LoadBadStreamFails) 90 TEST(JsEngineTest, LoadBadStreamFails)
70 { 91 {
71 BadFileReader fileReader; 92 BadFileSystem fileSystem;
72 ThrowingErrorCallback errorCallback; 93 ThrowingErrorCallback errorCallback;
73 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), &fileReader, 0, 94 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), &fileSystem, 0,
74 &errorCallback); 95 &errorCallback);
75 ASSERT_ANY_THROW(jsEngine.Load("hello.js")); 96 ASSERT_ANY_THROW(jsEngine.Load("hello.js"));
76 } 97 }
77 98
78 TEST(JsEngineTest, RuntimeExceptionIsThrown) 99 TEST(JsEngineTest, RuntimeExceptionIsThrown)
79 { 100 {
80 ThrowingFileReader fileReader;
81 ThrowingErrorCallback errorCallback; 101 ThrowingErrorCallback errorCallback;
82 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), &fileReader, 0, 102 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), 0, 0, &errorCallback);
83 &errorCallback);
84 ASSERT_THROW(jsEngine.Evaluate("doesnotexist()"), AdblockPlus::JsError); 103 ASSERT_THROW(jsEngine.Evaluate("doesnotexist()"), AdblockPlus::JsError);
85 } 104 }
86 105
87 TEST(JsEngineTest, CompileTimeExceptionIsThrown) 106 TEST(JsEngineTest, CompileTimeExceptionIsThrown)
88 { 107 {
89 ThrowingFileReader fileReader;
90 ThrowingErrorCallback errorCallback; 108 ThrowingErrorCallback errorCallback;
91 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), &fileReader, 0, 109 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), 0, 0, &errorCallback);
92 &errorCallback);
93 ASSERT_THROW(jsEngine.Evaluate("'foo'bar'"), AdblockPlus::JsError); 110 ASSERT_THROW(jsEngine.Evaluate("'foo'bar'"), AdblockPlus::JsError);
94 } 111 }
112
113 TEST(JsEngineTest, ValueCreation)
114 {
115 ThrowingErrorCallback errorCallback;
116 AdblockPlus::JsEngine jsEngine(AdblockPlus::AppInfo(), 0, 0, &errorCallback);
117 AdblockPlus::JsValuePtr value;
118
119 value = jsEngine.NewValue("foo");
120 ASSERT_TRUE(value->IsString());
121 ASSERT_EQ("foo", value->AsString());
122
123 value = jsEngine.NewValue(12);
124 ASSERT_TRUE(value->IsNumber());
125 ASSERT_EQ(12, value->AsInt());
126
127 value = jsEngine.NewValue(true);
128 ASSERT_TRUE(value->IsBool());
129 ASSERT_TRUE(value->AsBool());
130
131 value = jsEngine.NewObject();
132 ASSERT_TRUE(value->IsObject());
133 ASSERT_EQ(0u, value->GetOwnPropertyNames().size());
134 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld