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

156 lines
3.5 KiB
Rust
Raw Normal View History

2024-03-09 00:05:17 +00:00
use std::collections::BTreeMap;
use dust_lang::{
abstract_tree::{Identifier, Type},
2024-03-17 04:49:01 +00:00
error::{Error, TypeConflict, ValidationError},
2024-03-09 00:05:17 +00:00
*,
};
#[test]
fn none() {
assert_eq!(interpret("x = 9"), Ok(None));
assert_eq!(interpret("x = 1 + 1"), Ok(None));
}
#[test]
fn integer() {
assert_eq!(interpret("1"), Ok(Some(Value::integer(1))));
assert_eq!(interpret("123"), Ok(Some(Value::integer(123))));
assert_eq!(interpret("-666"), Ok(Some(Value::integer(-666))));
}
#[test]
fn integer_saturation() {
assert_eq!(
interpret("9223372036854775807 + 1"),
Ok(Some(Value::integer(i64::MAX)))
);
assert_eq!(
interpret("-9223372036854775808 - 1"),
Ok(Some(Value::integer(i64::MIN)))
);
}
#[test]
fn float() {
assert_eq!(
interpret("1.7976931348623157e308"),
Ok(Some(Value::float(f64::MAX)))
);
assert_eq!(
interpret("-1.7976931348623157e308"),
Ok(Some(Value::float(f64::MIN)))
);
}
#[test]
fn float_saturation() {
assert_eq!(
interpret("1.7976931348623157e308 + 1"),
Ok(Some(Value::float(f64::MAX)))
);
assert_eq!(
interpret("-1.7976931348623157e308 - 1"),
Ok(Some(Value::float(f64::MIN)))
);
}
#[test]
fn string() {
assert_eq!(
interpret("\"one\""),
Ok(Some(Value::string("one".to_string())))
);
assert_eq!(
interpret("'one'"),
Ok(Some(Value::string("one".to_string())))
);
assert_eq!(
interpret("`one`"),
Ok(Some(Value::string("one".to_string())))
);
assert_eq!(
interpret("`'one'`"),
Ok(Some(Value::string("'one'".to_string())))
);
assert_eq!(
interpret("'`one`'"),
Ok(Some(Value::string("`one`".to_string())))
);
assert_eq!(
interpret("\"'one'\""),
Ok(Some(Value::string("'one'".to_string())))
);
}
#[test]
fn list() {
assert_eq!(
interpret("[1, 2, 'foobar']"),
Ok(Some(Value::list(vec![
Value::integer(1),
Value::integer(2),
Value::string("foobar".to_string()),
])))
);
}
#[test]
fn empty_list() {
assert_eq!(interpret("[]"), Ok(Some(Value::list(Vec::new()))));
}
#[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!(
interpret("{ x = 1, foo = 'bar' }"),
Ok(Some(Value::map(map)))
);
}
#[test]
fn empty_map() {
assert_eq!(interpret("{}"), Ok(Some(Value::map(BTreeMap::new()))));
}
#[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-09 02:05:56 +00:00
interpret("{ 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-09 02:05:56 +00:00
interpret("{ foo : bool = 'bar' }"),
2024-03-09 00:05:17 +00:00
Err(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(),
expected_position: (8, 13).into(),
2024-03-17 11:31:45 +00:00
},
2024-03-17 12:30:46 +00:00
position: (0, 22).into()
2024-03-09 00:05:17 +00:00
}])
);
}
#[test]
fn range() {
assert_eq!(interpret("0..100"), Ok(Some(Value::range(0..100))));
}