Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(23)

Side by Side Diff: chrome/tools/profile_reset/jtl_compiler_unittest.cc

Issue 24998003: Compiler for the JSON Traversal Language. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed linker errors on Win. Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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/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 {"node(\"foo\")/", OP_NAVIGATE(GetHash("foo"))},
37 {"node(\"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\", \"value\")/",
46 OP_STORE_HASH(GetHash("name"), GetHash("value"))},
47 {"compare_stored_hash(\"name\", \"value\", \"default\")/",
48 OP_COMPARE_STORED_HASH(
49 GetHash("name"), GetHash("value"), GetHash("default"))},
50 {"compare_bool(false)/", OP_COMPARE_NODE_BOOL(VALUE_FALSE)},
51 {"compare_bool(true)/", OP_COMPARE_NODE_BOOL(VALUE_TRUE)},
52 {"compare_hash(\"foo\")/", OP_COMPARE_NODE_HASH(GetHash("foo"))},
53 {"compare_hash(\"bar\")/", OP_COMPARE_NODE_HASH(GetHash("bar"))},
54 {"break/", OP_STOP_EXECUTING_SENTENCE},
55 {"break;", OP_STOP_EXECUTING_SENTENCE + OP_END_OF_SENTENCE}};
56
57 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
58 SCOPED_TRACE(cases[i].source_code);
59 std::string bytecode;
60 EXPECT_TRUE(JtlCompiler::Compile(
61 cases[i].source_code, kTestHashSeed, &bytecode, NULL));
62 EXPECT_EQ(cases[i].expected_bytecode, bytecode);
63 }
64 }
65
66 TEST(JtlCompiler, CompileEntireProgram) {
67 const char kSourceCode[] =
68 "// Store \"x\"=true if path is found.\n"
69 "node(\"foo\")/node(\"bar\")/store_bool(\"x\", true);\n"
70 "// ...\n"
71 "// Store \"y\"=\"1\" if above value is set.\n"
72 "compare_stored_bool(\"x\", true, false)/store_hash(\"y\", \"1\");\n";
73
74 std::string expected_bytecode =
75 OP_NAVIGATE(GetHash("foo")) +
76 OP_NAVIGATE(GetHash("bar")) +
77 OP_STORE_BOOL(GetHash("x"), VALUE_TRUE) + OP_END_OF_SENTENCE +
78 OP_COMPARE_STORED_BOOL(GetHash("x"), VALUE_TRUE, VALUE_FALSE) +
79 OP_STORE_HASH(GetHash("y"), GetHash("1")) + OP_END_OF_SENTENCE;
80
81 std::string bytecode;
82 EXPECT_TRUE(
83 JtlCompiler::Compile(kSourceCode, kTestHashSeed, &bytecode, NULL));
84 EXPECT_EQ(expected_bytecode, bytecode);
85 }
86
87 TEST(JtlCompiler, InvalidOperationName) {
88 const char kSourceCode[] = "any()\n/\nnon_existent_instruction\n(\n)\n;\n";
89
90 std::string bytecode;
91 JtlCompiler::CompileError error;
92 EXPECT_FALSE(
93 JtlCompiler::Compile(kSourceCode, kTestHashSeed, &bytecode, &error));
94 EXPECT_THAT(error.context, testing::StartsWith("non_existent_instruction"));
95 EXPECT_EQ(2u, error.line_number);
96 EXPECT_EQ(JtlCompiler::CompileError::INVALID_OPERATION_NAME,
97 error.error_code);
98 }
99
100 TEST(JtlCompiler, InvalidArgumentsCount) {
101 const char* kSourceCodes[] = {
102 "any()/\nstore_bool(\"name\", true, \"superfluous argument\");\n",
103 "any()/\nstore_bool(\"name\");"}; // missing argument
104
105 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kSourceCodes); ++i) {
106 SCOPED_TRACE(kSourceCodes[i]);
107 std::string bytecode;
108 JtlCompiler::CompileError error;
109 EXPECT_FALSE(JtlCompiler::Compile(
110 kSourceCodes[i], kTestHashSeed, &bytecode, &error));
111 EXPECT_THAT(error.context, testing::StartsWith("store_bool"));
112 EXPECT_EQ(1u, error.line_number);
113 EXPECT_EQ(JtlCompiler::CompileError::INVALID_ARGUMENT_COUNT,
114 error.error_code);
115 }
116 }
117
118 TEST(JtlCompiler, InvalidArgumentType) {
119 const char* kSourceCodes[] = {
120 "any()\n/\ncompare_stored_bool(true, false, false);", // Arg#1 should be
121 // a hash.
122 "any()\n/\ncompare_stored_bool(\"name\", \"should be a bool\", false);",
123 "any()\n/\ncompare_stored_bool(\"name\", false, \"should be a bool\");"};
124
125 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kSourceCodes); ++i) {
126 SCOPED_TRACE(kSourceCodes[i]);
127 std::string bytecode;
128 JtlCompiler::CompileError error;
129 EXPECT_FALSE(JtlCompiler::Compile(
130 kSourceCodes[i], kTestHashSeed, &bytecode, &error));
131 EXPECT_THAT(error.context, testing::StartsWith("compare_stored_bool"));
132 EXPECT_EQ(2u, error.line_number);
133 EXPECT_EQ(JtlCompiler::CompileError::INVALID_ARGUMENT_TYPE,
134 error.error_code);
135 }
136 }
137
138 TEST(JtlCompiler, MistmatchedDoubleQuotes) {
139 const char kSourceCode[] = "any()/\nnode(\"ok\", \"stray quote)/break();";
140
141 std::string bytecode;
142 JtlCompiler::CompileError error;
143 EXPECT_FALSE(
144 JtlCompiler::Compile(kSourceCode, kTestHashSeed, &bytecode, &error));
145 EXPECT_EQ(1u, error.line_number);
146 EXPECT_EQ(JtlCompiler::CompileError::MISMATCHED_DOUBLE_QUOTES,
147 error.error_code);
148 }
149
150 TEST(JtlCompiler, ParsingError) {
151 const char kSourceCode[] = "any()/\nnode()missing_separator();";
152
153 std::string bytecode;
154 JtlCompiler::CompileError error;
155 EXPECT_FALSE(
156 JtlCompiler::Compile(kSourceCode, kTestHashSeed, &bytecode, &error));
157 EXPECT_THAT(error.context, testing::StartsWith("node"));
158 EXPECT_EQ(1u, error.line_number);
159 EXPECT_EQ(JtlCompiler::CompileError::PARSING_ERROR, error.error_code);
160 }
161
162 } // namespace
OLDNEW
« no previous file with comments | « chrome/tools/profile_reset/jtl_compiler_frontend.cc ('k') | chrome/tools/profile_reset/jtl_parser.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698