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

Delta Between Two Patch Sets: test/JsEngine.cpp

Issue 10213003: Make JsEngine::Evaluate() return a wrapper for v8::Value to accessdifferent variable types easily (Closed)
Left Patch Set: Extended JsValue API to support all FilterEngine functions Created April 13, 2013, 8:42 p.m.
Right Patch Set: Addressed review comments Created April 17, 2013, 7:56 a.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
« no previous file with change/comment | « test/GlobalJsObject.cpp ('k') | test/JsValue.cpp » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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(&fileReader, 0, &errorCallback); 70 AdblockPlus::JsEngine jsEngine(0, 0, &errorCallback);
51 const std::string source = "function hello() { return 'Hello'; }"; 71 const std::string source = "function hello() { return 'Hello'; }";
52 jsEngine.Evaluate(source); 72 jsEngine.Evaluate(source);
53 AdblockPlus::JsValuePtr result = jsEngine.Evaluate("hello()"); 73 AdblockPlus::JsValuePtr result = jsEngine.Evaluate("hello()");
54 ASSERT_EQ(true, result->IsString()); 74 ASSERT_TRUE(result->IsString());
55 ASSERT_EQ("Hello", result->AsString()); 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(&fileReader, 0, &errorCallback); 82 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, &errorCallback);
63 jsEngine.Load("hello.js"); 83 jsEngine.Load("hello.js");
64 AdblockPlus::JsValuePtr result = jsEngine.Evaluate("hello()"); 84 AdblockPlus::JsValuePtr result = jsEngine.Evaluate("hello()");
65 ASSERT_EQ(true, result->IsString()); 85 ASSERT_TRUE(result->IsString());
66 ASSERT_EQ("Hello", result->AsString()); 86 ASSERT_EQ("Hello", result->AsString());
67 } 87 }
68 88
69 TEST(JsEngineTest, LoadBadStreamFails) 89 TEST(JsEngineTest, LoadBadStreamFails)
70 { 90 {
71 BadFileReader fileReader; 91 BadFileSystem fileSystem;
72 ThrowingErrorCallback errorCallback; 92 ThrowingErrorCallback errorCallback;
73 AdblockPlus::JsEngine jsEngine(&fileReader, 0, &errorCallback); 93 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, &errorCallback);
74 ASSERT_ANY_THROW(jsEngine.Load("hello.js")); 94 ASSERT_ANY_THROW(jsEngine.Load("hello.js"));
75 } 95 }
76 96
77 TEST(JsEngineTest, RuntimeExceptionIsThrown) 97 TEST(JsEngineTest, RuntimeExceptionIsThrown)
78 { 98 {
79 ThrowingFileReader fileReader;
80 ThrowingErrorCallback errorCallback; 99 ThrowingErrorCallback errorCallback;
81 AdblockPlus::JsEngine jsEngine(&fileReader, 0, &errorCallback); 100 AdblockPlus::JsEngine jsEngine(0, 0, &errorCallback);
82 ASSERT_THROW(jsEngine.Evaluate("doesnotexist()"), AdblockPlus::JsError); 101 ASSERT_THROW(jsEngine.Evaluate("doesnotexist()"), AdblockPlus::JsError);
83 } 102 }
84 103
85 TEST(JsEngineTest, CompileTimeExceptionIsThrown) 104 TEST(JsEngineTest, CompileTimeExceptionIsThrown)
86 { 105 {
87 ThrowingFileReader fileReader;
88 ThrowingErrorCallback errorCallback; 106 ThrowingErrorCallback errorCallback;
89 AdblockPlus::JsEngine jsEngine(&fileReader, 0, &errorCallback); 107 AdblockPlus::JsEngine jsEngine(0, 0, &errorCallback);
90 ASSERT_THROW(jsEngine.Evaluate("'foo'bar'"), AdblockPlus::JsError); 108 ASSERT_THROW(jsEngine.Evaluate("'foo'bar'"), AdblockPlus::JsError);
91 } 109 }
110
111 TEST(JsEngineTest, ValueCreation)
112 {
113 ThrowingErrorCallback errorCallback;
114 AdblockPlus::JsEngine jsEngine(0, 0, &errorCallback);
115 AdblockPlus::JsValuePtr value;
116
117 value = jsEngine.NewValue("foo");
118 ASSERT_TRUE(value->IsString());
119 ASSERT_EQ("foo", value->AsString());
120
121 value = jsEngine.NewValue(12);
122 ASSERT_TRUE(value->IsNumber());
123 ASSERT_EQ(12, value->AsInt());
124
125 value = jsEngine.NewValue(true);
126 ASSERT_TRUE(value->IsBool());
127 ASSERT_TRUE(value->AsBool());
128 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld