Index: chrome/tools/profile_reset/jtl_compiler_unittest.cc |
diff --git a/chrome/tools/profile_reset/jtl_compiler_unittest.cc b/chrome/tools/profile_reset/jtl_compiler_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..92dae848fcb6be073ad3f827a13c0ee167f527e7 |
--- /dev/null |
+++ b/chrome/tools/profile_reset/jtl_compiler_unittest.cc |
@@ -0,0 +1,81 @@ |
+// Copyright 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/tools/profile_reset/jtl_compiler.h" |
+ |
+#include <string> |
+ |
+#include "base/json/json_writer.h" |
+#include "base/memory/scoped_ptr.h" |
+#include "base/values.h" |
+#include "chrome/browser/profile_resetter/jtl_foundation.h" |
+#include "chrome/browser/profile_resetter/jtl_instructions.h" |
+#include "testing/gmock/include/gmock/gmock-matchers.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace { |
+ |
+const char kHashSeed[] = "test-hash-seed"; |
+ |
+// Helpers ------------------------------------------------------------------- |
+ |
+std::string GetHash(const std::string& input) { |
+ return jtl_foundation::Hasher(kHashSeed).GetHash(input); |
+} |
+ |
+// Tests --------------------------------------------------------------------- |
+ |
+TEST(JtlCompiler, CompileSingleInstructions) { |
+ struct TestCase { |
+ std::string source_code; |
+ std::string expected_bytecode; |
+ } cases[] = { |
+ {"node(\"foo\")/", OP_NAVIGATE(GetHash("foo"))}, |
+ {"node(\"bar\")/", OP_NAVIGATE(GetHash("bar"))}, |
+ {"any/", OP_NAVIGATE_ANY}, {"back/", OP_NAVIGATE_BACK}, |
battre
2013/09/27 15:00:01
nit: new line after ,
engedy
2013/10/01 10:48:10
Done.
|
+ {"store_bool(\"name\", true)/", |
+ OP_STORE_BOOL(GetHash("name"), VALUE_TRUE)}, |
+ {"compare_stored_bool(\"name\", true, false)/", |
+ OP_COMPARE_STORED_BOOL(GetHash("name"), VALUE_TRUE, VALUE_FALSE)}, |
+ {"store_hash(\"name\", \"value\")/", |
+ OP_STORE_HASH(GetHash("name"), GetHash("value"))}, |
+ {"compare_stored_hash(\"name\", \"value\", \"default\")/", |
+ OP_COMPARE_STORED_HASH( |
+ GetHash("name"), GetHash("value"), GetHash("default"))}, |
+ {"compare_bool(false)/", OP_COMPARE_NODE_BOOL(VALUE_FALSE)}, |
+ {"compare_bool(true)/", OP_COMPARE_NODE_BOOL(VALUE_TRUE)}, |
+ {"compare_hash(\"foo\")/", OP_COMPARE_NODE_HASH(GetHash("foo"))}, |
+ {"compare_hash(\"bar\")/", OP_COMPARE_NODE_HASH(GetHash("bar"))}, |
+ {"break/", OP_STOP_EXECUTING_SENTENCE}, |
+ {"break;", OP_STOP_EXECUTING_SENTENCE + OP_END_OF_SENTENCE}}; |
+ |
+ for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
+ SCOPED_TRACE(cases[i].source_code); |
+ std::string bytecode; |
+ EXPECT_TRUE( |
+ JtlCompiler::Compile(cases[i].source_code, kHashSeed, &bytecode, NULL)); |
+ EXPECT_EQ(cases[i].expected_bytecode, bytecode); |
+ } |
+} |
+ |
+TEST(JtlCompiler, CompileEntireProgram) { |
+ const char kSourceCode[] = |
+ "// Store \"x\"=true if path is found.\n" |
+ "node(\"foo\")/node(\"bar\")/store_bool(\"x\", true);\n" |
+ "// ...\n" |
+ "// Store \"y\"=\"1\" if above value is set.\n" |
+ "compare_stored_bool(\"x\", true, false)/store_hash(\"y\", \"1\");\n"; |
+ |
+ std::string expected_bytecode = |
+ OP_NAVIGATE(GetHash("foo")) + OP_NAVIGATE(GetHash("bar")) + |
+ OP_STORE_BOOL(GetHash("x"), VALUE_TRUE) + OP_END_OF_SENTENCE + |
+ OP_COMPARE_STORED_BOOL(GetHash("x"), VALUE_TRUE, VALUE_FALSE) + |
+ OP_STORE_HASH(GetHash("y"), GetHash("1")) + OP_END_OF_SENTENCE; |
+ |
+ std::string bytecode; |
+ EXPECT_TRUE(JtlCompiler::Compile(kSourceCode, kHashSeed, &bytecode, NULL)); |
+ EXPECT_EQ(expected_bytecode, bytecode); |
+} |
+ |
+} // namespace |