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.
5 #include "chrome/tools/profile_reset/jtl_compiler.h"
9 #include "base/values.h"
10 #include "chrome/browser/profile_resetter/jtl_foundation.h"
11 #include "chrome/browser/profile_resetter/jtl_instructions.h"
12 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h"
17 const char kTestHashSeed[] = "test-hash-seed";
19 // Helpers -------------------------------------------------------------------
21 std::string GetHash(const std::string& input) {
22 return jtl_foundation::Hasher(kTestHashSeed).GetHash(input);
25 // Tests ---------------------------------------------------------------------
27 // Note: Parsing and parsing-related errors are unit-tested separately in more
28 // detail in "jtl_parser_unittest.cc". Here, most of the time, we assume that
29 // creating the parse tree works.
31 TEST(JtlCompiler, CompileSingleInstructions) {
33 std::string source_code;
34 std::string expected_bytecode;
36 {"go(\"foo\").", OP_NAVIGATE(GetHash("foo"))},
37 {"go(\"has whitespace\t\").",
38 OP_NAVIGATE(GetHash("has whitespace\t"))},
39 {"any.", OP_NAVIGATE_ANY},
40 {"back.", OP_NAVIGATE_BACK},
41 {"store_bool(\"name\", true).",
42 OP_STORE_BOOL(GetHash("name"), VALUE_TRUE)},
43 {"compare_stored_bool(\"name\", true, false).",
44 OP_COMPARE_STORED_BOOL(GetHash("name"), VALUE_TRUE, VALUE_FALSE)},
45 {"store_hash(\"name\", \"" + GetHash("value") + "\").",
46 OP_STORE_HASH(GetHash("name"), GetHash("value"))},
47 {"store_hashed(\"name\", \"value\").",
48 OP_STORE_HASH(GetHash("name"), GetHash("value"))},
49 {"store_node_bool(\"name\").",
50 OP_STORE_NODE_BOOL(GetHash("name"))},
51 {"store_node_hash(\"name\").",
52 OP_STORE_NODE_HASH(GetHash("name"))},
53 {"compare_stored_hashed(\"name\", \"value\", \"default\").",
54 OP_COMPARE_STORED_HASH(
55 GetHash("name"), GetHash("value"), GetHash("default"))},
56 {"compare_bool(false).", OP_COMPARE_NODE_BOOL(VALUE_FALSE)},
57 {"compare_bool(true).", OP_COMPARE_NODE_BOOL(VALUE_TRUE)},
58 {"compare_hashed(\"foo\").", OP_COMPARE_NODE_HASH(GetHash("foo"))},
59 {"compare_hashed_not(\"foo\").",
60 OP_COMPARE_NODE_HASH_NOT(GetHash("foo"))},
61 {"compare_to_stored_bool(\"name\").",
62 OP_COMPARE_NODE_TO_STORED_BOOL(GetHash("name"))},
63 {"compare_to_stored_hash(\"name\").",
64 OP_COMPARE_NODE_TO_STORED_HASH(GetHash("name"))},
65 {"break.", OP_STOP_EXECUTING_SENTENCE},
66 {"break;", OP_STOP_EXECUTING_SENTENCE + OP_END_OF_SENTENCE}};
68 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
69 SCOPED_TRACE(cases[i].source_code);
71 EXPECT_TRUE(JtlCompiler::Compile(
72 cases[i].source_code, kTestHashSeed, &bytecode, NULL));
73 EXPECT_EQ(cases[i].expected_bytecode, bytecode);
77 TEST(JtlCompiler, CompileEntireProgram) {
78 const char kSourceCode[] =
79 "// Store \"x\"=true if path is found.\n"
80 "go(\"foo\").go(\"bar\").store_bool(\"x\", true);\n"
82 "// Store \"y\"=\"1\" if above value is set.\n"
83 "compare_stored_bool(\"x\", true, false).store_hashed(\"y\", \"1\");\n";
85 std::string expected_bytecode =
86 OP_NAVIGATE(GetHash("foo")) +
87 OP_NAVIGATE(GetHash("bar")) +
88 OP_STORE_BOOL(GetHash("x"), VALUE_TRUE) + OP_END_OF_SENTENCE +
89 OP_COMPARE_STORED_BOOL(GetHash("x"), VALUE_TRUE, VALUE_FALSE) +
90 OP_STORE_HASH(GetHash("y"), GetHash("1")) + OP_END_OF_SENTENCE;
94 JtlCompiler::Compile(kSourceCode, kTestHashSeed, &bytecode, NULL));
95 EXPECT_EQ(expected_bytecode, bytecode);
98 TEST(JtlCompiler, InvalidOperationName) {
99 const char kSourceCode[] = "any()\n.\nnon_existent_instruction\n(\n)\n;\n";
101 std::string bytecode;
102 JtlCompiler::CompileError error;
104 JtlCompiler::Compile(kSourceCode, kTestHashSeed, &bytecode, &error));
105 EXPECT_THAT(error.context, testing::StartsWith("non_existent_instruction"));
106 EXPECT_EQ(2u, error.line_number);
107 EXPECT_EQ(JtlCompiler::CompileError::INVALID_OPERATION_NAME,
111 TEST(JtlCompiler, InvalidArgumentsCount) {
112 const char* kSourceCodes[] = {
113 "any().\nstore_bool(\"name\", true, \"superfluous argument\");\n",
114 "any().\nstore_bool(\"name\");"}; // missing argument
116 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kSourceCodes); ++i) {
117 SCOPED_TRACE(kSourceCodes[i]);
118 std::string bytecode;
119 JtlCompiler::CompileError error;
120 EXPECT_FALSE(JtlCompiler::Compile(
121 kSourceCodes[i], kTestHashSeed, &bytecode, &error));
122 EXPECT_THAT(error.context, testing::StartsWith("store_bool"));
123 EXPECT_EQ(1u, error.line_number);
124 EXPECT_EQ(JtlCompiler::CompileError::INVALID_ARGUMENT_COUNT,
129 TEST(JtlCompiler, InvalidArgumentType) {
131 std::string expected_context_prefix;
132 std::string source_code;
134 {"compare_bool", "any()\n.\ncompare_bool(\"foo\");"},
136 "any()\n.\ncompare_bool(\"01234567890123456789012345678901\");"},
137 {"compare_hashed", "any()\n.\ncompare_hashed(false);"},
138 {"store_hash", "any()\n.\nstore_hash(\"name\", false);"},
139 {"store_hash", "any()\n.\nstore_hash(\"name\", \"foo\");"},
140 {"compare_stored_bool",
141 "any()\n.\ncompare_stored_bool(\"name\", \"need a bool\", false);"},
142 {"compare_stored_bool",
143 "any()\n.\ncompare_stored_bool(\"name\", false, \"need a bool\");"}};
145 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
146 SCOPED_TRACE(cases[i].source_code);
147 std::string bytecode;
148 JtlCompiler::CompileError error;
149 EXPECT_FALSE(JtlCompiler::Compile(
150 cases[i].source_code, kTestHashSeed, &bytecode, &error));
151 EXPECT_THAT(error.context,
152 testing::StartsWith(cases[i].expected_context_prefix));
153 EXPECT_EQ(2u, error.line_number);
154 EXPECT_EQ(JtlCompiler::CompileError::INVALID_ARGUMENT_TYPE,
159 TEST(JtlCompiler, MistmatchedDoubleQuotes) {
160 const char kSourceCode[] = "any().\ngo(\"ok\", \"stray quote).break();";
162 std::string bytecode;
163 JtlCompiler::CompileError error;
165 JtlCompiler::Compile(kSourceCode, kTestHashSeed, &bytecode, &error));
166 EXPECT_EQ(1u, error.line_number);
167 EXPECT_EQ(JtlCompiler::CompileError::MISMATCHED_DOUBLE_QUOTES,
171 TEST(JtlCompiler, ParsingError) {
172 const char kSourceCode[] = "any().\ngo()missing_separator();";
174 std::string bytecode;
175 JtlCompiler::CompileError error;
177 JtlCompiler::Compile(kSourceCode, kTestHashSeed, &bytecode, &error));
178 EXPECT_THAT(error.context, testing::StartsWith("go"));
179 EXPECT_EQ(1u, error.line_number);
180 EXPECT_EQ(JtlCompiler::CompileError::PARSING_ERROR, error.error_code);