| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/tools/profile_reset/jtl_compiler.h" | 5 #include "chrome/tools/profile_reset/jtl_compiler.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #include "chrome/browser/profile_resetter/jtl_foundation.h" | 10 #include "chrome/browser/profile_resetter/jtl_foundation.h" |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 EXPECT_EQ(expected_bytecode, bytecode); | 106 EXPECT_EQ(expected_bytecode, bytecode); |
| 107 } | 107 } |
| 108 | 108 |
| 109 TEST(JtlCompiler, InvalidOperationName) { | 109 TEST(JtlCompiler, InvalidOperationName) { |
| 110 const char kSourceCode[] = "any()\n.\nnon_existent_instruction\n(\n)\n;\n"; | 110 const char kSourceCode[] = "any()\n.\nnon_existent_instruction\n(\n)\n;\n"; |
| 111 | 111 |
| 112 std::string bytecode; | 112 std::string bytecode; |
| 113 JtlCompiler::CompileError error; | 113 JtlCompiler::CompileError error; |
| 114 EXPECT_FALSE( | 114 EXPECT_FALSE( |
| 115 JtlCompiler::Compile(kSourceCode, kTestHashSeed, &bytecode, &error)); | 115 JtlCompiler::Compile(kSourceCode, kTestHashSeed, &bytecode, &error)); |
| 116 EXPECT_THAT(error.context, testing::StartsWith("non_existent_instruction")); | 116 EXPECT_THAT(error.context, |
| 117 testing::base::StartsWith("non_existent_instruction")); |
| 117 EXPECT_EQ(2u, error.line_number); | 118 EXPECT_EQ(2u, error.line_number); |
| 118 EXPECT_EQ(JtlCompiler::CompileError::INVALID_OPERATION_NAME, | 119 EXPECT_EQ(JtlCompiler::CompileError::INVALID_OPERATION_NAME, |
| 119 error.error_code); | 120 error.error_code); |
| 120 } | 121 } |
| 121 | 122 |
| 122 TEST(JtlCompiler, InvalidArgumentsCount) { | 123 TEST(JtlCompiler, InvalidArgumentsCount) { |
| 123 const char* const kSourceCodes[] = { | 124 const char* const kSourceCodes[] = { |
| 124 "any().\nstore_bool(\"name\", true, \"superfluous argument\");\n", | 125 "any().\nstore_bool(\"name\", true, \"superfluous argument\");\n", |
| 125 "any().\nstore_bool(\"name\");"}; // missing argument | 126 "any().\nstore_bool(\"name\");"}; // missing argument |
| 126 | 127 |
| 127 for (size_t i = 0; i < arraysize(kSourceCodes); ++i) { | 128 for (size_t i = 0; i < arraysize(kSourceCodes); ++i) { |
| 128 SCOPED_TRACE(kSourceCodes[i]); | 129 SCOPED_TRACE(kSourceCodes[i]); |
| 129 std::string bytecode; | 130 std::string bytecode; |
| 130 JtlCompiler::CompileError error; | 131 JtlCompiler::CompileError error; |
| 131 EXPECT_FALSE(JtlCompiler::Compile( | 132 EXPECT_FALSE(JtlCompiler::Compile( |
| 132 kSourceCodes[i], kTestHashSeed, &bytecode, &error)); | 133 kSourceCodes[i], kTestHashSeed, &bytecode, &error)); |
| 133 EXPECT_THAT(error.context, testing::StartsWith("store_bool")); | 134 EXPECT_THAT(error.context, testing::base::StartsWith("store_bool")); |
| 134 EXPECT_EQ(1u, error.line_number); | 135 EXPECT_EQ(1u, error.line_number); |
| 135 EXPECT_EQ(JtlCompiler::CompileError::INVALID_ARGUMENT_COUNT, | 136 EXPECT_EQ(JtlCompiler::CompileError::INVALID_ARGUMENT_COUNT, |
| 136 error.error_code); | 137 error.error_code); |
| 137 } | 138 } |
| 138 } | 139 } |
| 139 | 140 |
| 140 TEST(JtlCompiler, InvalidArgumentType) { | 141 TEST(JtlCompiler, InvalidArgumentType) { |
| 141 struct TestCase { | 142 struct TestCase { |
| 142 std::string expected_context_prefix; | 143 std::string expected_context_prefix; |
| 143 std::string source_code; | 144 std::string source_code; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 155 {"compare_substring_hashed", | 156 {"compare_substring_hashed", |
| 156 "any()\n.\ncompare_substring_hashed(true);"}}; | 157 "any()\n.\ncompare_substring_hashed(true);"}}; |
| 157 | 158 |
| 158 for (size_t i = 0; i < arraysize(cases); ++i) { | 159 for (size_t i = 0; i < arraysize(cases); ++i) { |
| 159 SCOPED_TRACE(cases[i].source_code); | 160 SCOPED_TRACE(cases[i].source_code); |
| 160 std::string bytecode; | 161 std::string bytecode; |
| 161 JtlCompiler::CompileError error; | 162 JtlCompiler::CompileError error; |
| 162 EXPECT_FALSE(JtlCompiler::Compile( | 163 EXPECT_FALSE(JtlCompiler::Compile( |
| 163 cases[i].source_code, kTestHashSeed, &bytecode, &error)); | 164 cases[i].source_code, kTestHashSeed, &bytecode, &error)); |
| 164 EXPECT_THAT(error.context, | 165 EXPECT_THAT(error.context, |
| 165 testing::StartsWith(cases[i].expected_context_prefix)); | 166 testing::base::StartsWith(cases[i].expected_context_prefix)); |
| 166 EXPECT_EQ(2u, error.line_number); | 167 EXPECT_EQ(2u, error.line_number); |
| 167 EXPECT_EQ(JtlCompiler::CompileError::INVALID_ARGUMENT_TYPE, | 168 EXPECT_EQ(JtlCompiler::CompileError::INVALID_ARGUMENT_TYPE, |
| 168 error.error_code); | 169 error.error_code); |
| 169 } | 170 } |
| 170 } | 171 } |
| 171 | 172 |
| 172 TEST(JtlCompiler, InvalidArgumentValue) { | 173 TEST(JtlCompiler, InvalidArgumentValue) { |
| 173 struct TestCase { | 174 struct TestCase { |
| 174 std::string expected_context_prefix; | 175 std::string expected_context_prefix; |
| 175 std::string source_code; | 176 std::string source_code; |
| 176 } cases[] = { | 177 } cases[] = { |
| 177 {"compare_substring_hashed", "compare_substring_hashed(\"\");"}}; | 178 {"compare_substring_hashed", "compare_substring_hashed(\"\");"}}; |
| 178 | 179 |
| 179 for (size_t i = 0; i < arraysize(cases); ++i) { | 180 for (size_t i = 0; i < arraysize(cases); ++i) { |
| 180 SCOPED_TRACE(cases[i].source_code); | 181 SCOPED_TRACE(cases[i].source_code); |
| 181 std::string bytecode; | 182 std::string bytecode; |
| 182 JtlCompiler::CompileError error; | 183 JtlCompiler::CompileError error; |
| 183 EXPECT_FALSE(JtlCompiler::Compile( | 184 EXPECT_FALSE(JtlCompiler::Compile( |
| 184 cases[i].source_code, kTestHashSeed, &bytecode, &error)); | 185 cases[i].source_code, kTestHashSeed, &bytecode, &error)); |
| 185 EXPECT_THAT(error.context, | 186 EXPECT_THAT(error.context, |
| 186 testing::StartsWith(cases[i].expected_context_prefix)); | 187 testing::base::StartsWith(cases[i].expected_context_prefix)); |
| 187 EXPECT_EQ(0u, error.line_number); | 188 EXPECT_EQ(0u, error.line_number); |
| 188 EXPECT_EQ(JtlCompiler::CompileError::INVALID_ARGUMENT_VALUE, | 189 EXPECT_EQ(JtlCompiler::CompileError::INVALID_ARGUMENT_VALUE, |
| 189 error.error_code); | 190 error.error_code); |
| 190 } | 191 } |
| 191 } | 192 } |
| 192 | 193 |
| 193 TEST(JtlCompiler, MistmatchedDoubleQuotes) { | 194 TEST(JtlCompiler, MistmatchedDoubleQuotes) { |
| 194 const char kSourceCode[] = "any().\ngo(\"ok\", \"stray quote).break();"; | 195 const char kSourceCode[] = "any().\ngo(\"ok\", \"stray quote).break();"; |
| 195 | 196 |
| 196 std::string bytecode; | 197 std::string bytecode; |
| 197 JtlCompiler::CompileError error; | 198 JtlCompiler::CompileError error; |
| 198 EXPECT_FALSE( | 199 EXPECT_FALSE( |
| 199 JtlCompiler::Compile(kSourceCode, kTestHashSeed, &bytecode, &error)); | 200 JtlCompiler::Compile(kSourceCode, kTestHashSeed, &bytecode, &error)); |
| 200 EXPECT_EQ(1u, error.line_number); | 201 EXPECT_EQ(1u, error.line_number); |
| 201 EXPECT_EQ(JtlCompiler::CompileError::MISMATCHED_DOUBLE_QUOTES, | 202 EXPECT_EQ(JtlCompiler::CompileError::MISMATCHED_DOUBLE_QUOTES, |
| 202 error.error_code); | 203 error.error_code); |
| 203 } | 204 } |
| 204 | 205 |
| 205 TEST(JtlCompiler, ParsingError) { | 206 TEST(JtlCompiler, ParsingError) { |
| 206 const char kSourceCode[] = "any().\ngo()missing_separator();"; | 207 const char kSourceCode[] = "any().\ngo()missing_separator();"; |
| 207 | 208 |
| 208 std::string bytecode; | 209 std::string bytecode; |
| 209 JtlCompiler::CompileError error; | 210 JtlCompiler::CompileError error; |
| 210 EXPECT_FALSE( | 211 EXPECT_FALSE( |
| 211 JtlCompiler::Compile(kSourceCode, kTestHashSeed, &bytecode, &error)); | 212 JtlCompiler::Compile(kSourceCode, kTestHashSeed, &bytecode, &error)); |
| 212 EXPECT_THAT(error.context, testing::StartsWith("go")); | 213 EXPECT_THAT(error.context, testing::base::StartsWith("go")); |
| 213 EXPECT_EQ(1u, error.line_number); | 214 EXPECT_EQ(1u, error.line_number); |
| 214 EXPECT_EQ(JtlCompiler::CompileError::PARSING_ERROR, error.error_code); | 215 EXPECT_EQ(JtlCompiler::CompileError::PARSING_ERROR, error.error_code); |
| 215 } | 216 } |
| 216 | 217 |
| 217 } // namespace | 218 } // namespace |
| OLD | NEW |