1
0
dust/dust-lang/tests/values.rs

162 lines
3.8 KiB
Rust
Raw Normal View History

2024-03-24 19:35:19 +00:00
use std::collections::BTreeMap;
2024-03-09 00:05:17 +00:00
use dust_lang::{
2024-03-25 04:16:55 +00:00
abstract_tree::{Type, WithPos},
2024-03-17 04:49:01 +00:00
error::{Error, 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-03-24 19:35:19 +00:00
interpret("test", "[1, 2, 'foobar']"),
2024-03-09 00:05:17 +00:00
Ok(Some(Value::list(vec![
2024-03-24 16:21:08 +00:00
Value::integer(1).with_position((1, 2)),
Value::integer(2).with_position((4, 5)),
Value::string("foobar".to_string()).with_position((7, 15)),
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(),
&vec![Error::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-03-20 15:43:47 +00:00
expected_position: (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
}