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