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: 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:
View unified diff | Download patch
« no previous file with comments | « test/GlobalJsObject.cpp ('k') | test/JsValue.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 BaseFileSystem : public AdblockPlus::FileSystem 6 class BaseFileSystem : public AdblockPlus::FileSystem
7 { 7 {
8 void Write(const std::string& path, 8 void Write(const std::string& path,
9 std::tr1::shared_ptr<std::ostream> content) 9 std::tr1::shared_ptr<std::ostream> content)
10 { 10 {
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
63 throw std::runtime_error("No writing"); 63 throw std::runtime_error("No writing");
64 } 64 }
65 }; 65 };
66 66
67 TEST(JsEngineTest, EvaluateAndCall) 67 TEST(JsEngineTest, EvaluateAndCall)
68 { 68 {
69 ThrowingErrorCallback errorCallback; 69 ThrowingErrorCallback errorCallback;
70 AdblockPlus::JsEngine jsEngine(0, 0, &errorCallback); 70 AdblockPlus::JsEngine jsEngine(0, 0, &errorCallback);
71 const std::string source = "function hello() { return 'Hello'; }"; 71 const std::string source = "function hello() { return 'Hello'; }";
72 jsEngine.Evaluate(source); 72 jsEngine.Evaluate(source);
73 const std::string result = jsEngine.Evaluate("hello()"); 73 AdblockPlus::JsValuePtr result = jsEngine.Evaluate("hello()");
74 ASSERT_EQ("Hello", result); 74 ASSERT_TRUE(result->IsString());
75 ASSERT_EQ("Hello", result->AsString());
75 } 76 }
76 77
77 TEST(JsEngineTest, LoadAndCall) 78 TEST(JsEngineTest, LoadAndCall)
78 { 79 {
79 StubFileSystem fileSystem; 80 StubFileSystem fileSystem;
80 ThrowingErrorCallback errorCallback; 81 ThrowingErrorCallback errorCallback;
81 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, &errorCallback); 82 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, &errorCallback);
82 jsEngine.Load("hello.js"); 83 jsEngine.Load("hello.js");
83 const std::string result = jsEngine.Evaluate("hello()"); 84 AdblockPlus::JsValuePtr result = jsEngine.Evaluate("hello()");
84 ASSERT_EQ("Hello", result); 85 ASSERT_TRUE(result->IsString());
86 ASSERT_EQ("Hello", result->AsString());
85 } 87 }
86 88
87 TEST(JsEngineTest, LoadBadStreamFails) 89 TEST(JsEngineTest, LoadBadStreamFails)
88 { 90 {
89 BadFileSystem fileSystem; 91 BadFileSystem fileSystem;
90 ThrowingErrorCallback errorCallback; 92 ThrowingErrorCallback errorCallback;
91 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, &errorCallback); 93 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, &errorCallback);
92 ASSERT_ANY_THROW(jsEngine.Load("hello.js")); 94 ASSERT_ANY_THROW(jsEngine.Load("hello.js"));
93 } 95 }
94 96
95 TEST(JsEngineTest, RuntimeExceptionIsThrown) 97 TEST(JsEngineTest, RuntimeExceptionIsThrown)
96 { 98 {
97 ThrowingErrorCallback errorCallback; 99 ThrowingErrorCallback errorCallback;
98 AdblockPlus::JsEngine jsEngine(0, 0, &errorCallback); 100 AdblockPlus::JsEngine jsEngine(0, 0, &errorCallback);
99 ASSERT_THROW(jsEngine.Evaluate("doesnotexist()"), AdblockPlus::JsError); 101 ASSERT_THROW(jsEngine.Evaluate("doesnotexist()"), AdblockPlus::JsError);
100 } 102 }
101 103
102 TEST(JsEngineTest, CompileTimeExceptionIsThrown) 104 TEST(JsEngineTest, CompileTimeExceptionIsThrown)
103 { 105 {
104 ThrowingErrorCallback errorCallback; 106 ThrowingErrorCallback errorCallback;
105 AdblockPlus::JsEngine jsEngine(0, 0, &errorCallback); 107 AdblockPlus::JsEngine jsEngine(0, 0, &errorCallback);
106 ASSERT_THROW(jsEngine.Evaluate("'foo'bar'"), AdblockPlus::JsError); 108 ASSERT_THROW(jsEngine.Evaluate("'foo'bar'"), AdblockPlus::JsError);
107 } 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 }
OLDNEW
« no previous file with comments | « test/GlobalJsObject.cpp ('k') | test/JsValue.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld