- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / tools / profile_reset / jtl_compiler_unittest.cc
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/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"
14
15 namespace {
16
17 const char kTestHashSeed[] = "test-hash-seed";
18
19 // Helpers -------------------------------------------------------------------
20
21 std::string GetHash(const std::string& input) {
22   return jtl_foundation::Hasher(kTestHashSeed).GetHash(input);
23 }
24
25 // Tests ---------------------------------------------------------------------
26
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.
30
31 TEST(JtlCompiler, CompileSingleInstructions) {
32   struct TestCase {
33     std::string source_code;
34     std::string expected_bytecode;
35   } cases[] = {
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}};
67
68   for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
69     SCOPED_TRACE(cases[i].source_code);
70     std::string bytecode;
71     EXPECT_TRUE(JtlCompiler::Compile(
72         cases[i].source_code, kTestHashSeed, &bytecode, NULL));
73     EXPECT_EQ(cases[i].expected_bytecode, bytecode);
74   }
75 }
76
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"
81       "// ...\n"
82       "// Store \"y\"=\"1\" if above value is set.\n"
83       "compare_stored_bool(\"x\", true, false).store_hashed(\"y\", \"1\");\n";
84
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;
91
92   std::string bytecode;
93   EXPECT_TRUE(
94       JtlCompiler::Compile(kSourceCode, kTestHashSeed, &bytecode, NULL));
95   EXPECT_EQ(expected_bytecode, bytecode);
96 }
97
98 TEST(JtlCompiler, InvalidOperationName) {
99   const char kSourceCode[] = "any()\n.\nnon_existent_instruction\n(\n)\n;\n";
100
101   std::string bytecode;
102   JtlCompiler::CompileError error;
103   EXPECT_FALSE(
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,
108             error.error_code);
109 }
110
111 TEST(JtlCompiler, InvalidArgumentsCount) {
112   const char* kSourceCodes[] = {
113       "any().\nstore_bool(\"name\", true, \"superfluous argument\");\n",
114       "any().\nstore_bool(\"name\");"};  // missing argument
115
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,
125               error.error_code);
126   }
127 }
128
129 TEST(JtlCompiler, InvalidArgumentType) {
130   struct TestCase {
131     std::string expected_context_prefix;
132     std::string source_code;
133   } cases[] = {
134         {"compare_bool", "any()\n.\ncompare_bool(\"foo\");"},
135         {"compare_bool",
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\");"}};
144
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,
155               error.error_code);
156   }
157 }
158
159 TEST(JtlCompiler, MistmatchedDoubleQuotes) {
160   const char kSourceCode[] = "any().\ngo(\"ok\", \"stray quote).break();";
161
162   std::string bytecode;
163   JtlCompiler::CompileError error;
164   EXPECT_FALSE(
165       JtlCompiler::Compile(kSourceCode, kTestHashSeed, &bytecode, &error));
166   EXPECT_EQ(1u, error.line_number);
167   EXPECT_EQ(JtlCompiler::CompileError::MISMATCHED_DOUBLE_QUOTES,
168             error.error_code);
169 }
170
171 TEST(JtlCompiler, ParsingError) {
172   const char kSourceCode[] = "any().\ngo()missing_separator();";
173
174   std::string bytecode;
175   JtlCompiler::CompileError error;
176   EXPECT_FALSE(
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);
181 }
182
183 }  // namespace