Skip to content

Commit 637167f

Browse files
committed
Add more tests for array
1 parent c6cc2fb commit 637167f

File tree

4 files changed

+86
-2
lines changed

4 files changed

+86
-2
lines changed

src/error/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,10 @@ mod tests {
465465
EvalexprError::expected_type(&Value::Tuple(vec![]), Value::Empty),
466466
EvalexprError::expected_tuple(Value::Empty)
467467
);
468+
assert_eq!(
469+
EvalexprError::expected_type(&Value::Array(vec![]), Value::Empty),
470+
EvalexprError::expected_array(Value::Empty)
471+
);
468472
assert_eq!(
469473
EvalexprError::expected_type(&Value::Empty, Value::String("abc".to_string())),
470474
EvalexprError::expected_empty(Value::String("abc".to_string()))

src/token/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ mod tests {
517517
#[test]
518518
fn test_token_display() {
519519
let token_string =
520-
"+ - * / % ^ == != > < >= <= && || ! ( ) = += -= *= /= %= ^= &&= ||= , ; ";
520+
"+ - * / % ^ == != > < >= <= && || ! ( ) = += -= *= /= %= ^= &&= ||= , ; { } ";
521521
let tokens = tokenize(token_string).unwrap();
522522
let mut result_string = String::new();
523523

src/value/mod.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ impl TryFrom<Value> for () {
315315

316316
#[cfg(test)]
317317
mod tests {
318-
use crate::value::{TupleType, Value};
318+
use crate::value::{ArrayType, TupleType, Value};
319319

320320
#[test]
321321
fn test_value_conversions() {
@@ -330,6 +330,16 @@ mod tests {
330330
Value::from(TupleType::new()).as_tuple(),
331331
Ok(TupleType::new())
332332
);
333+
assert_eq!(Value::from(TupleType::new()).as_vec(), Ok(vec![]));
334+
assert_eq!(Value::Array(ArrayType::new()).as_vec(), Ok(vec![]));
335+
assert_eq!(
336+
Value::from(TupleType::new()).as_slice(),
337+
Ok(vec![].as_slice())
338+
);
339+
assert_eq!(
340+
Value::Array(ArrayType::new()).as_slice(),
341+
Ok(vec![].as_slice())
342+
);
333343
}
334344

335345
#[test]
@@ -339,5 +349,6 @@ mod tests {
339349
assert!(Value::from(3.3).is_float());
340350
assert!(Value::from(true).is_boolean());
341351
assert!(Value::from(TupleType::new()).is_tuple());
352+
assert!(Value::Array(ArrayType::new()).is_array());
342353
}
343354
}

tests/integration.rs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,10 @@ fn test_builtin_functions() {
572572
eval("str::from(1, 2, 3)"),
573573
Ok(Value::String(String::from("(1, 2, 3)")))
574574
);
575+
assert_eq!(
576+
eval("str::from({1, 2, 3})"),
577+
Ok(Value::String(String::from("{1, 2, 3}")))
578+
);
575579
assert_eq!(eval("str::from()"), Ok(Value::String(String::from("()"))));
576580
assert_eq!(
577581
eval("str::substring(\"foobar\", 3)"),
@@ -910,6 +914,46 @@ fn test_shortcut_functions() {
910914
Err(EvalexprError::VariableIdentifierNotFound("3a3".to_owned()))
911915
);
912916

917+
assert_eq!(eval_array("{3,3}"), Ok(vec![Value::Int(3), Value::Int(3)]));
918+
assert_eq!(
919+
eval_array("33"),
920+
Err(EvalexprError::ExpectedArray {
921+
actual: Value::Int(33)
922+
})
923+
);
924+
assert_eq!(
925+
eval_array("3a3"),
926+
Err(EvalexprError::VariableIdentifierNotFound("3a3".to_owned()))
927+
);
928+
assert_eq!(
929+
eval_array_with_context("{3,3}", &context),
930+
Ok(vec![Value::Int(3), Value::Int(3)])
931+
);
932+
assert_eq!(
933+
eval_array_with_context("33", &context),
934+
Err(EvalexprError::ExpectedArray {
935+
actual: Value::Int(33)
936+
})
937+
);
938+
assert_eq!(
939+
eval_array_with_context("3a3", &context),
940+
Err(EvalexprError::VariableIdentifierNotFound("3a3".to_owned()))
941+
);
942+
assert_eq!(
943+
eval_array_with_context_mut("{3,3}", &mut context),
944+
Ok(vec![Value::Int(3), Value::Int(3)])
945+
);
946+
assert_eq!(
947+
eval_array_with_context_mut("33", &mut context),
948+
Err(EvalexprError::ExpectedArray {
949+
actual: Value::Int(33)
950+
})
951+
);
952+
assert_eq!(
953+
eval_array_with_context_mut("3a3", &mut context),
954+
Err(EvalexprError::VariableIdentifierNotFound("3a3".to_owned()))
955+
);
956+
913957
assert_eq!(eval_empty(""), Ok(EMPTY_VALUE));
914958
assert_eq!(eval_empty("()"), Ok(EMPTY_VALUE));
915959
assert_eq!(
@@ -1789,6 +1833,12 @@ fn test_error_constructors() {
17891833
actual: Value::Int(4)
17901834
})
17911835
);
1836+
assert_eq!(
1837+
eval_array("4"),
1838+
Err(EvalexprError::ExpectedArray {
1839+
actual: Value::Int(4)
1840+
})
1841+
);
17921842
assert_eq!(
17931843
Value::Tuple(vec![Value::Int(4), Value::Int(5)]).as_fixed_len_tuple(3),
17941844
Err(EvalexprError::ExpectedFixedLengthTuple {
@@ -1898,6 +1948,7 @@ fn test_value_type() {
18981948
assert_eq!(ValueType::from(&Value::Int(0)), ValueType::Int);
18991949
assert_eq!(ValueType::from(&Value::Boolean(true)), ValueType::Boolean);
19001950
assert_eq!(ValueType::from(&Value::Tuple(Vec::new())), ValueType::Tuple);
1951+
assert_eq!(ValueType::from(&Value::Array(Vec::new())), ValueType::Array);
19011952
assert_eq!(ValueType::from(&Value::Empty), ValueType::Empty);
19021953

19031954
assert_eq!(
@@ -1914,20 +1965,26 @@ fn test_value_type() {
19141965
ValueType::from(&mut Value::Tuple(Vec::new())),
19151966
ValueType::Tuple
19161967
);
1968+
assert_eq!(
1969+
ValueType::from(&mut Value::Array(Vec::new())),
1970+
ValueType::Array
1971+
);
19171972
assert_eq!(ValueType::from(&mut Value::Empty), ValueType::Empty);
19181973

19191974
assert!(!Value::String(String::new()).is_number());
19201975
assert!(Value::Float(0.0).is_number());
19211976
assert!(Value::Int(0).is_number());
19221977
assert!(!Value::Boolean(true).is_number());
19231978
assert!(!Value::Tuple(Vec::new()).is_number());
1979+
assert!(!Value::Array(Vec::new()).is_number());
19241980
assert!(!Value::Empty.is_number());
19251981

19261982
assert!(!Value::String(String::new()).is_empty());
19271983
assert!(!Value::Float(0.0).is_empty());
19281984
assert!(!Value::Int(0).is_empty());
19291985
assert!(!Value::Boolean(true).is_empty());
19301986
assert!(!Value::Tuple(Vec::new()).is_empty());
1987+
assert!(!Value::Array(Vec::new()).is_empty());
19311988
assert!(Value::Empty.is_empty());
19321989

19331990
assert_eq!(
@@ -1955,6 +2012,12 @@ fn test_value_type() {
19552012
actual: Value::Tuple(Vec::new())
19562013
})
19572014
);
2015+
assert_eq!(
2016+
Value::Array(Vec::new()).as_float(),
2017+
Err(EvalexprError::ExpectedFloat {
2018+
actual: Value::Array(Vec::new())
2019+
})
2020+
);
19582021
assert_eq!(
19592022
Value::Empty.as_float(),
19602023
Err(EvalexprError::ExpectedFloat {
@@ -2059,6 +2122,12 @@ fn test_value_type() {
20592122
actual: Value::Tuple(Vec::new())
20602123
})
20612124
);
2125+
assert_eq!(
2126+
Value::Array(Vec::new()).as_empty(),
2127+
Err(EvalexprError::ExpectedEmpty {
2128+
actual: Value::Array(Vec::new())
2129+
})
2130+
);
20622131
assert_eq!(Value::Empty.as_empty(), Ok(()));
20632132

20642133
assert_eq!(

0 commit comments

Comments
 (0)