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

Side by Side Diff: test/FileSystemJsObject.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 | « src/JsValue.cpp ('k') | test/FilterEngineStubs.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 <gtest/gtest.h> 2 #include <gtest/gtest.h>
3 3
4 #include "../src/Thread.h" 4 #include "../src/Thread.h"
5 #include "../src/Utils.h" 5 #include "../src/Utils.h"
6 6
7 namespace 7 namespace
8 { 8 {
9 class MockFileSystem : public AdblockPlus::FileSystem 9 class MockFileSystem : public AdblockPlus::FileSystem
10 { 10 {
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
71 result.lastModified = statLastModified; 71 result.lastModified = statLastModified;
72 return result; 72 return result;
73 } 73 }
74 }; 74 };
75 75
76 void ReadFile(AdblockPlus::JsEngine& jsEngine, std::string& content, 76 void ReadFile(AdblockPlus::JsEngine& jsEngine, std::string& content,
77 std::string& error) 77 std::string& error)
78 { 78 {
79 jsEngine.Evaluate("_fileSystem.read('', function(r) {result = r})"); 79 jsEngine.Evaluate("_fileSystem.read('', function(r) {result = r})");
80 AdblockPlus::Sleep(10); 80 AdblockPlus::Sleep(10);
81 content = jsEngine.Evaluate("result.content"); 81 content = jsEngine.Evaluate("result.content")->AsString();
82 error = jsEngine.Evaluate("result.error"); 82 error = jsEngine.Evaluate("result.error")->AsString();
83 } 83 }
84 } 84 }
85 85
86 TEST(FileSystemJsObjectTest, Read) 86 TEST(FileSystemJsObjectTest, Read)
87 { 87 {
88 MockFileSystem fileSystem; 88 MockFileSystem fileSystem;
89 fileSystem.contentToRead = "foo"; 89 fileSystem.contentToRead = "foo";
90 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0); 90 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0);
91 std::string content; 91 std::string content;
92 std::string error; 92 std::string error;
(...skipping 22 matching lines...) Expand all
115 } 115 }
116 116
117 TEST(FileSystemJsObjectTest, Write) 117 TEST(FileSystemJsObjectTest, Write)
118 { 118 {
119 MockFileSystem fileSystem; 119 MockFileSystem fileSystem;
120 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0); 120 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0);
121 jsEngine.Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e})"); 121 jsEngine.Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e})");
122 AdblockPlus::Sleep(10); 122 AdblockPlus::Sleep(10);
123 ASSERT_EQ("foo", fileSystem.lastWrittenPath); 123 ASSERT_EQ("foo", fileSystem.lastWrittenPath);
124 ASSERT_EQ("bar", fileSystem.lastWrittenContent); 124 ASSERT_EQ("bar", fileSystem.lastWrittenContent);
125 ASSERT_EQ("", jsEngine.Evaluate("error")); 125 ASSERT_EQ("", jsEngine.Evaluate("error")->AsString());
126 } 126 }
127 127
128 TEST(FileSystemJsObjectTest, WriteIllegalArguments) 128 TEST(FileSystemJsObjectTest, WriteIllegalArguments)
129 { 129 {
130 AdblockPlus::JsEngine jsEngine(0, 0, 0); 130 AdblockPlus::JsEngine jsEngine(0, 0, 0);
131 ASSERT_ANY_THROW(jsEngine.Evaluate("_fileSystem.write()")); 131 ASSERT_ANY_THROW(jsEngine.Evaluate("_fileSystem.write()"));
132 ASSERT_ANY_THROW(jsEngine.Evaluate("_fileSystem.write('', '', '')")); 132 ASSERT_ANY_THROW(jsEngine.Evaluate("_fileSystem.write('', '', '')"));
133 } 133 }
134 134
135 TEST(FileSystemJsObjectTest, WriteError) 135 TEST(FileSystemJsObjectTest, WriteError)
136 { 136 {
137 MockFileSystem fileSystem; 137 MockFileSystem fileSystem;
138 fileSystem.success = false; 138 fileSystem.success = false;
139 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0); 139 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0);
140 jsEngine.Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e})"); 140 jsEngine.Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e})");
141 AdblockPlus::Sleep(10); 141 AdblockPlus::Sleep(10);
142 ASSERT_NE("", jsEngine.Evaluate("error")); 142 ASSERT_NE("", jsEngine.Evaluate("error")->AsString());
143 } 143 }
144 144
145 TEST(FileSystemJsObjectTest, Move) 145 TEST(FileSystemJsObjectTest, Move)
146 { 146 {
147 MockFileSystem fileSystem; 147 MockFileSystem fileSystem;
148 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0); 148 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0);
149 jsEngine.Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e})"); 149 jsEngine.Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e})");
150 AdblockPlus::Sleep(10); 150 AdblockPlus::Sleep(10);
151 ASSERT_EQ("foo", fileSystem.movedFrom); 151 ASSERT_EQ("foo", fileSystem.movedFrom);
152 ASSERT_EQ("bar", fileSystem.movedTo); 152 ASSERT_EQ("bar", fileSystem.movedTo);
153 ASSERT_EQ("", jsEngine.Evaluate("error")); 153 ASSERT_EQ("", jsEngine.Evaluate("error")->AsString());
154 } 154 }
155 155
156 TEST(FileSystemJsObjectTest, MoveIllegalArguments) 156 TEST(FileSystemJsObjectTest, MoveIllegalArguments)
157 { 157 {
158 AdblockPlus::JsEngine jsEngine(0, 0, 0); 158 AdblockPlus::JsEngine jsEngine(0, 0, 0);
159 ASSERT_ANY_THROW(jsEngine.Evaluate("_fileSystem.move()")); 159 ASSERT_ANY_THROW(jsEngine.Evaluate("_fileSystem.move()"));
160 ASSERT_ANY_THROW(jsEngine.Evaluate("_fileSystem.move('', '', '')")); 160 ASSERT_ANY_THROW(jsEngine.Evaluate("_fileSystem.move('', '', '')"));
161 } 161 }
162 162
163 TEST(FileSystemJsObjectTest, MoveError) 163 TEST(FileSystemJsObjectTest, MoveError)
164 { 164 {
165 MockFileSystem fileSystem; 165 MockFileSystem fileSystem;
166 fileSystem.success = false; 166 fileSystem.success = false;
167 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0); 167 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0);
168 jsEngine.Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e})"); 168 jsEngine.Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e})");
169 AdblockPlus::Sleep(10); 169 AdblockPlus::Sleep(10);
170 ASSERT_NE("", jsEngine.Evaluate("error")); 170 ASSERT_NE("", jsEngine.Evaluate("error")->AsString());
171 } 171 }
172 172
173 TEST(FileSystemJsObjectTest, Remove) 173 TEST(FileSystemJsObjectTest, Remove)
174 { 174 {
175 MockFileSystem fileSystem; 175 MockFileSystem fileSystem;
176 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0); 176 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0);
177 jsEngine.Evaluate("_fileSystem.remove('foo', function(e) {error = e})"); 177 jsEngine.Evaluate("_fileSystem.remove('foo', function(e) {error = e})");
178 AdblockPlus::Sleep(10); 178 AdblockPlus::Sleep(10);
179 ASSERT_EQ("foo", fileSystem.removedPath); 179 ASSERT_EQ("foo", fileSystem.removedPath);
180 ASSERT_EQ("", jsEngine.Evaluate("error")); 180 ASSERT_EQ("", jsEngine.Evaluate("error")->AsString());
181 } 181 }
182 182
183 TEST(FileSystemJsObjectTest, RemoveIllegalArguments) 183 TEST(FileSystemJsObjectTest, RemoveIllegalArguments)
184 { 184 {
185 AdblockPlus::JsEngine jsEngine(0, 0, 0); 185 AdblockPlus::JsEngine jsEngine(0, 0, 0);
186 ASSERT_ANY_THROW(jsEngine.Evaluate("_fileSystem.remove()")); 186 ASSERT_ANY_THROW(jsEngine.Evaluate("_fileSystem.remove()"));
187 ASSERT_ANY_THROW(jsEngine.Evaluate("_fileSystem.remove('', '')")); 187 ASSERT_ANY_THROW(jsEngine.Evaluate("_fileSystem.remove('', '')"));
188 } 188 }
189 189
190 TEST(FileSystemJsObjectTest, RemoveError) 190 TEST(FileSystemJsObjectTest, RemoveError)
191 { 191 {
192 MockFileSystem fileSystem; 192 MockFileSystem fileSystem;
193 fileSystem.success = false; 193 fileSystem.success = false;
194 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0); 194 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0);
195 jsEngine.Evaluate("_fileSystem.remove('foo', function(e) {error = e})"); 195 jsEngine.Evaluate("_fileSystem.remove('foo', function(e) {error = e})");
196 AdblockPlus::Sleep(10); 196 AdblockPlus::Sleep(10);
197 ASSERT_NE("", jsEngine.Evaluate("error")); 197 ASSERT_NE("", jsEngine.Evaluate("error")->AsString());
198 } 198 }
199 199
200 TEST(FileSystemJsObjectTest, Stat) 200 TEST(FileSystemJsObjectTest, Stat)
201 { 201 {
202 MockFileSystem fileSystem; 202 MockFileSystem fileSystem;
203 fileSystem.statExists = true; 203 fileSystem.statExists = true;
204 fileSystem.statIsDirectory= false; 204 fileSystem.statIsDirectory= false;
205 fileSystem.statIsFile = true; 205 fileSystem.statIsFile = true;
206 fileSystem.statLastModified = 1337; 206 fileSystem.statLastModified = 1337;
207 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0); 207 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0);
208 jsEngine.Evaluate("_fileSystem.stat('foo', function(r) {result = r})"); 208 jsEngine.Evaluate("_fileSystem.stat('foo', function(r) {result = r})");
209 AdblockPlus::Sleep(10); 209 AdblockPlus::Sleep(10);
210 ASSERT_EQ("foo", fileSystem.statPath); 210 ASSERT_EQ("foo", fileSystem.statPath);
211 ASSERT_EQ("", jsEngine.Evaluate("result.error")); 211 ASSERT_EQ("", jsEngine.Evaluate("result.error")->AsString());
212 ASSERT_EQ("true", jsEngine.Evaluate("result.exists")); 212 ASSERT_TRUE(jsEngine.Evaluate("result.exists")->AsBool());
213 ASSERT_EQ("false", jsEngine.Evaluate("result.isDirectory")); 213 ASSERT_FALSE(jsEngine.Evaluate("result.isDirectory")->AsBool());
214 ASSERT_EQ("true", jsEngine.Evaluate("result.isFile")); 214 ASSERT_TRUE(jsEngine.Evaluate("result.isFile")->AsBool());
215 ASSERT_EQ("1337", jsEngine.Evaluate("result.lastModified")); 215 ASSERT_EQ(1337, jsEngine.Evaluate("result.lastModified")->AsInt());
216 } 216 }
217 217
218 TEST(FileSystemJsObjectTest, StatIllegalArguments) 218 TEST(FileSystemJsObjectTest, StatIllegalArguments)
219 { 219 {
220 AdblockPlus::JsEngine jsEngine(0, 0, 0); 220 AdblockPlus::JsEngine jsEngine(0, 0, 0);
221 ASSERT_ANY_THROW(jsEngine.Evaluate("_fileSystem.stat()")); 221 ASSERT_ANY_THROW(jsEngine.Evaluate("_fileSystem.stat()"));
222 ASSERT_ANY_THROW(jsEngine.Evaluate("_fileSystem.stat('', '')")); 222 ASSERT_ANY_THROW(jsEngine.Evaluate("_fileSystem.stat('', '')"));
223 } 223 }
224 224
225 TEST(FileSystemJsObjectTest, StatError) 225 TEST(FileSystemJsObjectTest, StatError)
226 { 226 {
227 MockFileSystem fileSystem; 227 MockFileSystem fileSystem;
228 fileSystem.success = false; 228 fileSystem.success = false;
229 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0); 229 AdblockPlus::JsEngine jsEngine(&fileSystem, 0, 0);
230 jsEngine.Evaluate("_fileSystem.stat('foo', function(r) {result = r})"); 230 jsEngine.Evaluate("_fileSystem.stat('foo', function(r) {result = r})");
231 AdblockPlus::Sleep(10); 231 AdblockPlus::Sleep(10);
232 ASSERT_NE("", jsEngine.Evaluate("result.error")); 232 ASSERT_NE("", jsEngine.Evaluate("result.error")->AsString());
233 } 233 }
OLDNEW
« no previous file with comments | « src/JsValue.cpp ('k') | test/FilterEngineStubs.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld