1 /*
2 * Copyright (C) 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "src/trace_processor/util/sql_argument.h"
18
19 #include "test/gtest_and_gmock.h"
20
21 namespace perfetto {
22 namespace trace_processor {
23 namespace sql_argument {
24 namespace {
25
ParseArgsSuccessfully(const std::string & args,std::vector<ArgumentDefinition> expected)26 void ParseArgsSuccessfully(const std::string& args,
27 std::vector<ArgumentDefinition> expected) {
28 std::vector<ArgumentDefinition> actual;
29 base::Status status = ParseArgumentDefinitions(args, actual);
30 ASSERT_TRUE(status.ok()) << status.c_message();
31 ASSERT_EQ(expected, actual);
32 }
33
ParseArgsWithFailure(const std::string & args)34 void ParseArgsWithFailure(const std::string& args) {
35 std::vector<ArgumentDefinition> actual;
36 ASSERT_FALSE(ParseArgumentDefinitions(args, actual).ok());
37 }
38
TEST(SqlArgumentTest,IsValidName)39 TEST(SqlArgumentTest, IsValidName) {
40 ASSERT_TRUE(IsValidName("foo"));
41 ASSERT_TRUE(IsValidName("bar"));
42 ASSERT_TRUE(IsValidName("foo_bar"));
43 ASSERT_TRUE(IsValidName("foo1234"));
44 ASSERT_TRUE(IsValidName("1234Foo"));
45 ASSERT_FALSE(IsValidName("foo-bar"));
46 ASSERT_FALSE(IsValidName("foo#123"));
47 }
48
TEST(SqlArgumentTest,ParseType)49 TEST(SqlArgumentTest, ParseType) {
50 ASSERT_EQ(ParseType("PROTO"), Type::kProto);
51 ASSERT_EQ(ParseType("BOOL"), Type::kBool);
52 ASSERT_EQ(ParseType("UNKNOWN"), std::nullopt);
53 ASSERT_EQ(ParseType("UINT"), Type::kUint);
54 }
55
TEST(SqlArgumentTest,TypeToFriendlyString)56 TEST(SqlArgumentTest, TypeToFriendlyString) {
57 ASSERT_STREQ(TypeToHumanFriendlyString(Type::kProto), "PROTO");
58 ASSERT_STREQ(TypeToHumanFriendlyString(Type::kBool), "BOOL");
59 ASSERT_STREQ(TypeToHumanFriendlyString(Type::kUint), "UINT");
60 }
61
TEST(SqlArgumentTest,TypeToSqlValueType)62 TEST(SqlArgumentTest, TypeToSqlValueType) {
63 ASSERT_EQ(TypeToSqlValueType(Type::kProto), SqlValue::Type::kBytes);
64 ASSERT_EQ(TypeToSqlValueType(Type::kBool), SqlValue::Type::kLong);
65 ASSERT_EQ(TypeToSqlValueType(Type::kUint), SqlValue::Type::kLong);
66 }
67
TEST(SqlArgumentTest,ParseArguments)68 TEST(SqlArgumentTest, ParseArguments) {
69 ParseArgsSuccessfully("", {});
70 ParseArgsSuccessfully("foo UINT", {ArgumentDefinition("$foo", Type::kUint)});
71 ParseArgsSuccessfully("foo UINT, bar LONG, baz PROTO",
72 {ArgumentDefinition("$foo", Type::kUint),
73 ArgumentDefinition("$bar", Type::kLong),
74 ArgumentDefinition("$baz", Type::kProto)});
75 ParseArgsSuccessfully("\nfoo UINT,\n bar LONG, baz PROTO\n",
76 {ArgumentDefinition("$foo", Type::kUint),
77 ArgumentDefinition("$bar", Type::kLong),
78 ArgumentDefinition("$baz", Type::kProto)});
79 ParseArgsSuccessfully("foo123 UINT",
80 {ArgumentDefinition("$foo123", Type::kUint)});
81
82 ParseArgsWithFailure("foo");
83 ParseArgsWithFailure("foo bar UINT, baz UINT");
84 ParseArgsWithFailure("foo UINT32");
85 ParseArgsWithFailure("foo#bar UINT");
86 ParseArgsWithFailure("foo-bar UINT");
87 }
88
89 } // namespace
90 } // namespace sql_argument
91 } // namespace trace_processor
92 } // namespace perfetto
93