2018-07-06 18:53:20 -05:00
|
|
|
// Copyright 2017 Jeremy Wall <jeremy@marzhillstudios.com>
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2018-06-04 21:45:44 -05:00
|
|
|
use super::Builder;
|
2018-05-22 18:02:44 -05:00
|
|
|
|
2018-06-04 21:45:44 -05:00
|
|
|
fn assert_build(input: &str) {
|
2018-06-04 22:24:38 -05:00
|
|
|
let mut b = Builder::new("<Eval>");
|
2018-06-04 21:45:44 -05:00
|
|
|
b.enable_validate_mode();
|
|
|
|
b.eval_string(input).unwrap();
|
|
|
|
if !b.assert_collector.success {
|
|
|
|
assert!(false, b.assert_collector.failures);
|
2018-05-22 18:02:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_comparisons() {
|
2018-06-04 21:45:44 -05:00
|
|
|
assert_build(
|
2018-06-06 21:02:02 -05:00
|
|
|
"let one = 1;
|
2018-05-22 18:02:44 -05:00
|
|
|
let two = 2;
|
|
|
|
let foo = \"foo\";
|
|
|
|
let bar = \"bar\";
|
|
|
|
let tpl1 = {
|
|
|
|
foo = \"bar\",
|
|
|
|
one = 1
|
|
|
|
};
|
|
|
|
let tpl2 = tpl1{};
|
|
|
|
let tpl3 = {
|
|
|
|
bar = \"foo\",
|
|
|
|
two = 1
|
|
|
|
};
|
|
|
|
let list = [1, 2, 3];
|
|
|
|
let list2 = list;
|
|
|
|
let list3 = [1, 2];
|
2018-06-04 21:45:44 -05:00
|
|
|
assert \"one == one\";
|
|
|
|
assert \"one == one\";
|
|
|
|
assert \"one >= one\";
|
|
|
|
assert \"two > one\";
|
|
|
|
assert \"two >= two\";
|
|
|
|
assert \"tpl1 == tpl2\";
|
|
|
|
assert \"tpl1 != tpl3\";
|
|
|
|
assert \"list == list2\";
|
|
|
|
assert \"list != list3\";
|
|
|
|
",
|
|
|
|
);
|
2018-05-22 18:02:44 -05:00
|
|
|
}
|
|
|
|
|
2018-06-10 13:51:19 -05:00
|
|
|
#[test]
|
|
|
|
fn test_empty_value() {
|
|
|
|
assert_build(
|
|
|
|
"let empty = NULL;
|
|
|
|
let tpl = {
|
|
|
|
foo = NULL,
|
|
|
|
};
|
|
|
|
assert \"tpl.foo == empty\";
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-05-22 18:02:44 -05:00
|
|
|
#[test]
|
|
|
|
fn test_deep_comparison() {
|
2018-06-06 21:02:02 -05:00
|
|
|
assert_build(
|
|
|
|
"let tpl1 = {
|
2018-05-22 18:02:44 -05:00
|
|
|
foo = \"bar\",
|
|
|
|
lst = [1, 2, 3],
|
|
|
|
inner = {
|
2018-06-10 13:51:19 -05:00
|
|
|
fld = \"value\",
|
2018-05-22 18:02:44 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
let copy = tpl1;
|
|
|
|
let extra = tpl1{one = 1};
|
|
|
|
let less = {
|
|
|
|
foo = \"bar\"
|
|
|
|
};
|
2018-06-04 21:45:44 -05:00
|
|
|
assert \"tpl1.inner == copy.inner\";
|
|
|
|
assert \"tpl1.inner.fld == copy.inner.fld\";
|
|
|
|
assert \"tpl1.lst == copy.lst\";
|
|
|
|
assert \"tpl1.foo == copy.foo\";
|
|
|
|
assert \"tpl1 == copy\";
|
|
|
|
assert \"tpl1 != extra\";
|
|
|
|
assert \"tpl1 != less\";
|
2018-06-06 21:02:02 -05:00
|
|
|
",
|
|
|
|
);
|
2018-05-22 18:02:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_expression_comparisons() {
|
2018-06-04 21:45:44 -05:00
|
|
|
assert_build("assert \"2 == 1+1\";");
|
|
|
|
assert_build("assert \"(1+1) == 2\";");
|
|
|
|
assert_build("assert \"(1+1) == (1+1)\";");
|
2018-06-06 21:02:02 -05:00
|
|
|
assert_build(
|
|
|
|
"let want = \"foo\";
|
|
|
|
assert \"select want, 1, { foo=2, } == 2\";",
|
|
|
|
);
|
2018-05-22 18:02:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_binary_operator_precedence() {
|
2018-06-04 21:45:44 -05:00
|
|
|
assert_build(
|
|
|
|
"let result = 2 * 2 + 1;
|
|
|
|
assert \"result == 5\";",
|
|
|
|
);
|
|
|
|
assert_build(
|
|
|
|
"let result = 2 + 2 * 3;
|
|
|
|
assert \"result == 8\";",
|
|
|
|
);
|
|
|
|
assert_build(
|
|
|
|
"let result = 2 * (2 + 1);
|
|
|
|
assert \"result == 6\";",
|
|
|
|
);
|
2018-05-22 18:02:44 -05:00
|
|
|
}
|