Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/tools/profile_reset/jtl_compiler.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/json/json_writer.h" | |
| 10 #include "base/memory/scoped_ptr.h" | |
| 11 #include "base/values.h" | |
| 12 #include "chrome/browser/profile_resetter/jtl_foundation.h" | |
| 13 #include "chrome/browser/profile_resetter/jtl_instructions.h" | |
| 14 #include "testing/gmock/include/gmock/gmock-matchers.h" | |
| 15 #include "testing/gtest/include/gtest/gtest.h" | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 const char kHashSeed[] = "test-hash-seed"; | |
| 20 | |
| 21 // Helpers ------------------------------------------------------------------- | |
| 22 | |
| 23 std::string GetHash(const std::string& input) { | |
| 24 return jtl_foundation::Hasher(kHashSeed).GetHash(input); | |
| 25 } | |
| 26 | |
| 27 // Tests --------------------------------------------------------------------- | |
| 28 | |
| 29 TEST(JtlCompiler, CompileSingleInstructions) { | |
| 30 struct TestCase { | |
| 31 std::string source_code; | |
| 32 std::string expected_bytecode; | |
| 33 } cases[] = { | |
| 34 {"node(\"foo\")/", OP_NAVIGATE(GetHash("foo"))}, | |
| 35 {"node(\"bar\")/", OP_NAVIGATE(GetHash("bar"))}, | |
| 36 {"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.
| |
| 37 {"store_bool(\"name\", true)/", | |
| 38 OP_STORE_BOOL(GetHash("name"), VALUE_TRUE)}, | |
| 39 {"compare_stored_bool(\"name\", true, false)/", | |
| 40 OP_COMPARE_STORED_BOOL(GetHash("name"), VALUE_TRUE, VALUE_FALSE)}, | |
| 41 {"store_hash(\"name\", \"value\")/", | |
| 42 OP_STORE_HASH(GetHash("name"), GetHash("value"))}, | |
| 43 {"compare_stored_hash(\"name\", \"value\", \"default\")/", | |
| 44 OP_COMPARE_STORED_HASH( | |
| 45 GetHash("name"), GetHash("value"), GetHash("default"))}, | |
| 46 {"compare_bool(false)/", OP_COMPARE_NODE_BOOL(VALUE_FALSE)}, | |
| 47 {"compare_bool(true)/", OP_COMPARE_NODE_BOOL(VALUE_TRUE)}, | |
| 48 {"compare_hash(\"foo\")/", OP_COMPARE_NODE_HASH(GetHash("foo"))}, | |
| 49 {"compare_hash(\"bar\")/", OP_COMPARE_NODE_HASH(GetHash("bar"))}, | |
| 50 {"break/", OP_STOP_EXECUTING_SENTENCE}, | |
| 51 {"break;", OP_STOP_EXECUTING_SENTENCE + OP_END_OF_SENTENCE}}; | |
| 52 | |
| 53 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | |
| 54 SCOPED_TRACE(cases[i].source_code); | |
| 55 std::string bytecode; | |
| 56 EXPECT_TRUE( | |
| 57 JtlCompiler::Compile(cases[i].source_code, kHashSeed, &bytecode, NULL)); | |
| 58 EXPECT_EQ(cases[i].expected_bytecode, bytecode); | |
| 59 } | |
| 60 } | |
| 61 | |
| 62 TEST(JtlCompiler, CompileEntireProgram) { | |
| 63 const char kSourceCode[] = | |
| 64 "// Store \"x\"=true if path is found.\n" | |
| 65 "node(\"foo\")/node(\"bar\")/store_bool(\"x\", true);\n" | |
| 66 "// ...\n" | |
| 67 "// Store \"y\"=\"1\" if above value is set.\n" | |
| 68 "compare_stored_bool(\"x\", true, false)/store_hash(\"y\", \"1\");\n"; | |
| 69 | |
| 70 std::string expected_bytecode = | |
| 71 OP_NAVIGATE(GetHash("foo")) + OP_NAVIGATE(GetHash("bar")) + | |
| 72 OP_STORE_BOOL(GetHash("x"), VALUE_TRUE) + OP_END_OF_SENTENCE + | |
| 73 OP_COMPARE_STORED_BOOL(GetHash("x"), VALUE_TRUE, VALUE_FALSE) + | |
| 74 OP_STORE_HASH(GetHash("y"), GetHash("1")) + OP_END_OF_SENTENCE; | |
| 75 | |
| 76 std::string bytecode; | |
| 77 EXPECT_TRUE(JtlCompiler::Compile(kSourceCode, kHashSeed, &bytecode, NULL)); | |
| 78 EXPECT_EQ(expected_bytecode, bytecode); | |
| 79 } | |
| 80 | |
| 81 } // namespace | |
| OLD | NEW |