2024-03-24 19:35:19 +00:00
|
|
|
use std::collections::BTreeMap;
|
2024-03-09 00:05:17 +00:00
|
|
|
|
|
|
|
use dust_lang::{
|
2024-06-19 08:56:56 +00:00
|
|
|
abstract_tree::Type,
|
2024-06-18 23:42:04 +00:00
|
|
|
error::{DustError, TypeConflict, ValidationError},
|
2024-03-25 04:16:55 +00:00
|
|
|
identifier::Identifier,
|
2024-03-09 00:05:17 +00:00
|
|
|
*,
|
|
|
|
};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn none() {
|
2024-03-24 19:35:19 +00:00
|
|
|
assert_eq!(interpret("test", "x = 9"), Ok(None));
|
|
|
|
assert_eq!(interpret("test", "x = 1 + 1"), Ok(None));
|
2024-03-09 00:05:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn integer() {
|
2024-03-24 19:35:19 +00:00
|
|
|
assert_eq!(interpret("test", "1"), Ok(Some(Value::integer(1))));
|
|
|
|
assert_eq!(interpret("test", "123"), Ok(Some(Value::integer(123))));
|
|
|
|
assert_eq!(interpret("test", "-666"), Ok(Some(Value::integer(-666))));
|
2024-03-09 00:05:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn integer_saturation() {
|
|
|
|
assert_eq!(
|
2024-03-24 19:35:19 +00:00
|
|
|
interpret("test", "9223372036854775807 + 1"),
|
2024-03-09 00:05:17 +00:00
|
|
|
Ok(Some(Value::integer(i64::MAX)))
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2024-03-24 19:35:19 +00:00
|
|
|
interpret("test", "-9223372036854775808 - 1"),
|
2024-03-09 00:05:17 +00:00
|
|
|
Ok(Some(Value::integer(i64::MIN)))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn float() {
|
|
|
|
assert_eq!(
|
2024-03-24 19:35:19 +00:00
|
|
|
interpret("test", "1.7976931348623157e308"),
|
2024-03-09 00:05:17 +00:00
|
|
|
Ok(Some(Value::float(f64::MAX)))
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2024-03-24 19:35:19 +00:00
|
|
|
interpret("test", "-1.7976931348623157e308"),
|
2024-03-09 00:05:17 +00:00
|
|
|
Ok(Some(Value::float(f64::MIN)))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn float_saturation() {
|
|
|
|
assert_eq!(
|
2024-03-24 19:35:19 +00:00
|
|
|
interpret("test", "1.7976931348623157e308 + 1"),
|
2024-03-09 00:05:17 +00:00
|
|
|
Ok(Some(Value::float(f64::MAX)))
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2024-03-24 19:35:19 +00:00
|
|
|
interpret("test", "-1.7976931348623157e308 - 1"),
|
2024-03-09 00:05:17 +00:00
|
|
|
Ok(Some(Value::float(f64::MIN)))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn string() {
|
|
|
|
assert_eq!(
|
2024-03-24 19:35:19 +00:00
|
|
|
interpret("test", "\"one\""),
|
2024-03-09 00:05:17 +00:00
|
|
|
Ok(Some(Value::string("one".to_string())))
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2024-03-24 19:35:19 +00:00
|
|
|
interpret("test", "'one'"),
|
2024-03-09 00:05:17 +00:00
|
|
|
Ok(Some(Value::string("one".to_string())))
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2024-03-24 19:35:19 +00:00
|
|
|
interpret("test", "`one`"),
|
2024-03-09 00:05:17 +00:00
|
|
|
Ok(Some(Value::string("one".to_string())))
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2024-03-24 19:35:19 +00:00
|
|
|
interpret("test", "`'one'`"),
|
2024-03-09 00:05:17 +00:00
|
|
|
Ok(Some(Value::string("'one'".to_string())))
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2024-03-24 19:35:19 +00:00
|
|
|
interpret("test", "'`one`'"),
|
2024-03-09 00:05:17 +00:00
|
|
|
Ok(Some(Value::string("`one`".to_string())))
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2024-03-24 19:35:19 +00:00
|
|
|
interpret("test", "\"'one'\""),
|
2024-03-09 00:05:17 +00:00
|
|
|
Ok(Some(Value::string("'one'".to_string())))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn list() {
|
|
|
|
assert_eq!(
|
2024-07-11 21:54:59 +00:00
|
|
|
interpret("test", "[1, 2, 3]"),
|
2024-03-09 00:05:17 +00:00
|
|
|
Ok(Some(Value::list(vec![
|
2024-06-19 08:56:56 +00:00
|
|
|
Value::integer(1),
|
|
|
|
Value::integer(2),
|
2024-07-11 21:54:59 +00:00
|
|
|
Value::integer(3),
|
2024-03-09 00:05:17 +00:00
|
|
|
])))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn empty_list() {
|
2024-03-24 19:35:19 +00:00
|
|
|
assert_eq!(interpret("test", "[]"), Ok(Some(Value::list(Vec::new()))));
|
2024-03-09 00:05:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn map() {
|
|
|
|
let mut map = BTreeMap::new();
|
|
|
|
|
|
|
|
map.insert(Identifier::new("x"), Value::integer(1));
|
|
|
|
map.insert(Identifier::new("foo"), Value::string("bar".to_string()));
|
|
|
|
|
|
|
|
assert_eq!(
|
2024-03-24 19:35:19 +00:00
|
|
|
interpret("test", "{ x = 1, foo = 'bar' }"),
|
2024-03-09 00:05:17 +00:00
|
|
|
Ok(Some(Value::map(map)))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn empty_map() {
|
2024-03-24 13:10:49 +00:00
|
|
|
assert_eq!(
|
2024-03-24 19:35:19 +00:00
|
|
|
interpret("test", "{}"),
|
2024-03-24 13:10:49 +00:00
|
|
|
Ok(Some(Value::map(BTreeMap::new())))
|
|
|
|
);
|
2024-03-09 00:05:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn map_types() {
|
|
|
|
let mut map = BTreeMap::new();
|
|
|
|
|
|
|
|
map.insert(Identifier::new("x"), Value::integer(1));
|
|
|
|
map.insert(Identifier::new("foo"), Value::string("bar".to_string()));
|
|
|
|
|
|
|
|
assert_eq!(
|
2024-03-24 19:35:19 +00:00
|
|
|
interpret("test", "{ x : int = 1, foo : str = 'bar' }"),
|
2024-03-09 00:05:17 +00:00
|
|
|
Ok(Some(Value::map(map)))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn map_type_errors() {
|
|
|
|
assert_eq!(
|
2024-03-24 19:35:19 +00:00
|
|
|
interpret("test", "{ foo : bool = 'bar' }")
|
2024-03-24 13:10:49 +00:00
|
|
|
.unwrap_err()
|
|
|
|
.errors(),
|
2024-06-18 23:42:04 +00:00
|
|
|
&vec![DustError::Validation {
|
2024-03-17 11:31:45 +00:00
|
|
|
error: ValidationError::TypeCheck {
|
|
|
|
conflict: TypeConflict {
|
|
|
|
actual: Type::String,
|
|
|
|
expected: Type::Boolean
|
|
|
|
},
|
2024-03-17 17:36:31 +00:00
|
|
|
actual_position: (15, 20).into(),
|
2024-06-17 14:10:06 +00:00
|
|
|
expected_position: Some((8, 12).into()),
|
2024-03-17 11:31:45 +00:00
|
|
|
},
|
2024-03-17 12:30:46 +00:00
|
|
|
position: (0, 22).into()
|
2024-03-24 13:10:49 +00:00
|
|
|
}]
|
2024-03-09 00:05:17 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn range() {
|
2024-03-24 19:35:19 +00:00
|
|
|
assert_eq!(interpret("test", "0..100"), Ok(Some(Value::range(0..100))));
|
2024-03-09 00:05:17 +00:00
|
|
|
}
|