Tao Bao | d770d2e | 2016-10-03 15:26:06 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 17 | #include <memory> |
Tao Bao | d770d2e | 2016-10-03 15:26:06 -0700 | [diff] [blame] | 18 | #include <string> |
| 19 | |
| 20 | #include <gtest/gtest.h> |
| 21 | |
| 22 | #include "edify/expr.h" |
| 23 | |
Tao Bao | d8d514f | 2018-07-09 13:32:28 -0700 | [diff] [blame] | 24 | static void expect(const std::string& expr_str, const char* expected) { |
| 25 | std::unique_ptr<Expr> e; |
| 26 | int error_count = 0; |
| 27 | EXPECT_EQ(0, ParseString(expr_str, &e, &error_count)); |
| 28 | EXPECT_EQ(0, error_count); |
Tao Bao | d770d2e | 2016-10-03 15:26:06 -0700 | [diff] [blame] | 29 | |
Tao Bao | d8d514f | 2018-07-09 13:32:28 -0700 | [diff] [blame] | 30 | State state(expr_str, nullptr); |
Tao Bao | d770d2e | 2016-10-03 15:26:06 -0700 | [diff] [blame] | 31 | |
Tao Bao | d8d514f | 2018-07-09 13:32:28 -0700 | [diff] [blame] | 32 | std::string result; |
| 33 | bool status = Evaluate(&state, e, &result); |
Tao Bao | d770d2e | 2016-10-03 15:26:06 -0700 | [diff] [blame] | 34 | |
Tao Bao | d8d514f | 2018-07-09 13:32:28 -0700 | [diff] [blame] | 35 | if (expected == nullptr) { |
| 36 | EXPECT_FALSE(status); |
| 37 | } else { |
| 38 | EXPECT_STREQ(expected, result.c_str()); |
| 39 | } |
Tao Bao | d770d2e | 2016-10-03 15:26:06 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | class EdifyTest : public ::testing::Test { |
Tao Bao | d8d514f | 2018-07-09 13:32:28 -0700 | [diff] [blame] | 43 | protected: |
| 44 | void SetUp() { |
| 45 | RegisterBuiltins(); |
| 46 | } |
Tao Bao | d770d2e | 2016-10-03 15:26:06 -0700 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | TEST_F(EdifyTest, parsing) { |
| 50 | expect("a", "a"); |
| 51 | expect("\"a\"", "a"); |
| 52 | expect("\"\\x61\"", "a"); |
| 53 | expect("# this is a comment\n" |
| 54 | " a\n" |
| 55 | " \n", |
| 56 | "a"); |
| 57 | } |
| 58 | |
| 59 | TEST_F(EdifyTest, sequence) { |
| 60 | // sequence operator |
| 61 | expect("a; b; c", "c"); |
| 62 | } |
| 63 | |
| 64 | TEST_F(EdifyTest, concat) { |
| 65 | // string concat operator |
| 66 | expect("a + b", "ab"); |
| 67 | expect("a + \n \"b\"", "ab"); |
| 68 | expect("a + b +\nc\n", "abc"); |
| 69 | |
| 70 | // string concat function |
| 71 | expect("concat(a, b)", "ab"); |
| 72 | expect("concat(a,\n \"b\")", "ab"); |
| 73 | expect("concat(a + b,\nc,\"d\")", "abcd"); |
| 74 | expect("\"concat\"(a + b,\nc,\"d\")", "abcd"); |
| 75 | } |
| 76 | |
| 77 | TEST_F(EdifyTest, logical) { |
| 78 | // logical and |
| 79 | expect("a && b", "b"); |
| 80 | expect("a && \"\"", ""); |
| 81 | expect("\"\" && b", ""); |
| 82 | expect("\"\" && \"\"", ""); |
| 83 | expect("\"\" && abort()", ""); // test short-circuiting |
| 84 | expect("t && abort()", nullptr); |
| 85 | |
| 86 | // logical or |
| 87 | expect("a || b", "a"); |
| 88 | expect("a || \"\"", "a"); |
| 89 | expect("\"\" || b", "b"); |
| 90 | expect("\"\" || \"\"", ""); |
| 91 | expect("a || abort()", "a"); // test short-circuiting |
| 92 | expect("\"\" || abort()", NULL); |
| 93 | |
| 94 | // logical not |
| 95 | expect("!a", ""); |
| 96 | expect("! \"\"", "t"); |
| 97 | expect("!!a", "t"); |
| 98 | } |
| 99 | |
| 100 | TEST_F(EdifyTest, precedence) { |
| 101 | // precedence |
| 102 | expect("\"\" == \"\" && b", "b"); |
| 103 | expect("a + b == ab", "t"); |
| 104 | expect("ab == a + b", "t"); |
| 105 | expect("a + (b == ab)", "a"); |
| 106 | expect("(ab == a) + b", "b"); |
| 107 | } |
| 108 | |
| 109 | TEST_F(EdifyTest, substring) { |
| 110 | // substring function |
| 111 | expect("is_substring(cad, abracadabra)", "t"); |
| 112 | expect("is_substring(abrac, abracadabra)", "t"); |
| 113 | expect("is_substring(dabra, abracadabra)", "t"); |
| 114 | expect("is_substring(cad, abracxadabra)", ""); |
| 115 | expect("is_substring(abrac, axbracadabra)", ""); |
| 116 | expect("is_substring(dabra, abracadabrxa)", ""); |
| 117 | } |
| 118 | |
| 119 | TEST_F(EdifyTest, ifelse) { |
| 120 | // ifelse function |
| 121 | expect("ifelse(t, yes, no)", "yes"); |
| 122 | expect("ifelse(!t, yes, no)", "no"); |
| 123 | expect("ifelse(t, yes, abort())", "yes"); |
| 124 | expect("ifelse(!t, abort(), no)", "no"); |
| 125 | } |
| 126 | |
| 127 | TEST_F(EdifyTest, if_statement) { |
| 128 | // if "statements" |
| 129 | expect("if t then yes else no endif", "yes"); |
| 130 | expect("if \"\" then yes else no endif", "no"); |
| 131 | expect("if \"\" then yes endif", ""); |
| 132 | expect("if \"\"; t then yes endif", "yes"); |
| 133 | } |
| 134 | |
| 135 | TEST_F(EdifyTest, comparison) { |
| 136 | // numeric comparisons |
| 137 | expect("less_than_int(3, 14)", "t"); |
| 138 | expect("less_than_int(14, 3)", ""); |
| 139 | expect("less_than_int(x, 3)", ""); |
| 140 | expect("less_than_int(3, x)", ""); |
| 141 | expect("greater_than_int(3, 14)", ""); |
| 142 | expect("greater_than_int(14, 3)", "t"); |
| 143 | expect("greater_than_int(x, 3)", ""); |
| 144 | expect("greater_than_int(3, x)", ""); |
| 145 | } |
| 146 | |
| 147 | TEST_F(EdifyTest, big_string) { |
Tao Bao | d8d514f | 2018-07-09 13:32:28 -0700 | [diff] [blame] | 148 | expect(std::string(8192, 's'), std::string(8192, 's').c_str()); |
Tao Bao | d770d2e | 2016-10-03 15:26:06 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Tao Bao | 39119ad | 2016-10-10 22:52:18 -0700 | [diff] [blame] | 151 | TEST_F(EdifyTest, unknown_function) { |
Tao Bao | d8d514f | 2018-07-09 13:32:28 -0700 | [diff] [blame] | 152 | const char* script1 = "unknown_function()"; |
| 153 | std::unique_ptr<Expr> expr; |
| 154 | int error_count = 0; |
| 155 | EXPECT_EQ(1, ParseString(script1, &expr, &error_count)); |
| 156 | EXPECT_EQ(1, error_count); |
Tao Bao | 39119ad | 2016-10-10 22:52:18 -0700 | [diff] [blame] | 157 | |
Tao Bao | d8d514f | 2018-07-09 13:32:28 -0700 | [diff] [blame] | 158 | const char* script2 = "abc; unknown_function()"; |
| 159 | error_count = 0; |
| 160 | EXPECT_EQ(1, ParseString(script2, &expr, &error_count)); |
| 161 | EXPECT_EQ(1, error_count); |
Tao Bao | 39119ad | 2016-10-10 22:52:18 -0700 | [diff] [blame] | 162 | |
Tao Bao | d8d514f | 2018-07-09 13:32:28 -0700 | [diff] [blame] | 163 | const char* script3 = "unknown_function1() || yes"; |
| 164 | error_count = 0; |
| 165 | EXPECT_EQ(1, ParseString(script3, &expr, &error_count)); |
| 166 | EXPECT_EQ(1, error_count); |
Tao Bao | 39119ad | 2016-10-10 22:52:18 -0700 | [diff] [blame] | 167 | } |