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