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

Side by Side Diff: test/JsEngine.cpp

Issue 10213003: Make JsEngine::Evaluate() return a wrapper for v8::Value to accessdifferent variable types easily (Closed)
Patch Set: Added tests, resolved type conversion ambiguities, implemented some missing API calls Created April 15, 2013, 6:23 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
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 ThrowingFileReader : public AdblockPlus::FileReader
7 { 7 {
8 public: 8 public:
9 std::auto_ptr<std::istream> Read(const std::string& path) const 9 std::auto_ptr<std::istream> Read(const std::string& path) const
10 { 10 {
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
43 } 43 }
44 }; 44 };
45 45
46 TEST(JsEngineTest, EvaluateAndCall) 46 TEST(JsEngineTest, EvaluateAndCall)
47 { 47 {
48 ThrowingFileReader fileReader; 48 ThrowingFileReader fileReader;
49 ThrowingErrorCallback errorCallback; 49 ThrowingErrorCallback errorCallback;
50 AdblockPlus::JsEngine jsEngine(&fileReader, 0, &errorCallback); 50 AdblockPlus::JsEngine jsEngine(&fileReader, 0, &errorCallback);
51 const std::string source = "function hello() { return 'Hello'; }"; 51 const std::string source = "function hello() { return 'Hello'; }";
52 jsEngine.Evaluate(source); 52 jsEngine.Evaluate(source);
53 const std::string result = jsEngine.Evaluate("hello()"); 53 AdblockPlus::JsValuePtr result = jsEngine.Evaluate("hello()");
54 ASSERT_EQ("Hello", result); 54 ASSERT_TRUE(result->IsString());
55 ASSERT_EQ("Hello", result->AsString());
55 } 56 }
56 57
57 TEST(JsEngineTest, LoadAndCall) 58 TEST(JsEngineTest, LoadAndCall)
58 { 59 {
59 StubFileReader fileReader; 60 StubFileReader fileReader;
60 ThrowingErrorCallback errorCallback; 61 ThrowingErrorCallback errorCallback;
61 AdblockPlus::JsEngine jsEngine(&fileReader, 0, &errorCallback); 62 AdblockPlus::JsEngine jsEngine(&fileReader, 0, &errorCallback);
62 jsEngine.Load("hello.js"); 63 jsEngine.Load("hello.js");
63 const std::string result = jsEngine.Evaluate("hello()"); 64 AdblockPlus::JsValuePtr result = jsEngine.Evaluate("hello()");
64 ASSERT_EQ("Hello", result); 65 ASSERT_TRUE(result->IsString());
66 ASSERT_EQ("Hello", result->AsString());
65 } 67 }
66 68
67 TEST(JsEngineTest, LoadBadStreamFails) 69 TEST(JsEngineTest, LoadBadStreamFails)
68 { 70 {
69 BadFileReader fileReader; 71 BadFileReader fileReader;
70 ThrowingErrorCallback errorCallback; 72 ThrowingErrorCallback errorCallback;
71 AdblockPlus::JsEngine jsEngine(&fileReader, 0, &errorCallback); 73 AdblockPlus::JsEngine jsEngine(&fileReader, 0, &errorCallback);
72 ASSERT_ANY_THROW(jsEngine.Load("hello.js")); 74 ASSERT_ANY_THROW(jsEngine.Load("hello.js"));
73 } 75 }
74 76
75 TEST(JsEngineTest, RuntimeExceptionIsThrown) 77 TEST(JsEngineTest, RuntimeExceptionIsThrown)
76 { 78 {
77 ThrowingFileReader fileReader; 79 ThrowingFileReader fileReader;
78 ThrowingErrorCallback errorCallback; 80 ThrowingErrorCallback errorCallback;
79 AdblockPlus::JsEngine jsEngine(&fileReader, 0, &errorCallback); 81 AdblockPlus::JsEngine jsEngine(&fileReader, 0, &errorCallback);
80 ASSERT_THROW(jsEngine.Evaluate("doesnotexist()"), AdblockPlus::JsError); 82 ASSERT_THROW(jsEngine.Evaluate("doesnotexist()"), AdblockPlus::JsError);
81 } 83 }
82 84
83 TEST(JsEngineTest, CompileTimeExceptionIsThrown) 85 TEST(JsEngineTest, CompileTimeExceptionIsThrown)
84 { 86 {
85 ThrowingFileReader fileReader; 87 ThrowingFileReader fileReader;
86 ThrowingErrorCallback errorCallback; 88 ThrowingErrorCallback errorCallback;
87 AdblockPlus::JsEngine jsEngine(&fileReader, 0, &errorCallback); 89 AdblockPlus::JsEngine jsEngine(&fileReader, 0, &errorCallback);
88 ASSERT_THROW(jsEngine.Evaluate("'foo'bar'"), AdblockPlus::JsError); 90 ASSERT_THROW(jsEngine.Evaluate("'foo'bar'"), AdblockPlus::JsError);
89 } 91 }
92
93 TEST(JsEngineTest, ValueCreation)
94 {
95 ThrowingErrorCallback errorCallback;
96 AdblockPlus::JsEngine jsEngine(0, 0, &errorCallback);
97 AdblockPlus::JsValuePtr value;
98
99 value = jsEngine.NewValue("foo");
100 ASSERT_TRUE(value->IsString());
101 ASSERT_EQ("foo", value->AsString());
102
103 value = jsEngine.NewValue(12);
104 ASSERT_TRUE(value->IsNumber());
105 ASSERT_EQ(12, value->AsInt());
106
107 value = jsEngine.NewValue(true);
108 ASSERT_TRUE(value->IsBool());
109 ASSERT_TRUE(value->AsBool());
110 }
OLDNEW
« src/FilterEngine.cpp ('K') | « test/GlobalJsObject.cpp ('k') | test/JsValue.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld