1
0
dust/tests/value.rs

148 lines
3.7 KiB
Rust
Raw Permalink Normal View History

2024-01-06 07:26:51 +00:00
use dust_lang::*;
#[test]
fn empty() {
2024-02-15 15:33:25 +00:00
assert_eq!(interpret("x = 9"), Ok(Value::none()));
assert_eq!(interpret("x = 1 + 1"), Ok(Value::none()));
2024-01-06 07:26:51 +00:00
}
#[test]
fn integer() {
assert_eq!(interpret("1"), Ok(Value::Integer(1)));
assert_eq!(interpret("123"), Ok(Value::Integer(123)));
assert_eq!(interpret("-666"), Ok(Value::Integer(-666)));
}
2024-01-30 04:57:13 +00:00
#[test]
fn integer_overflow() {
assert_eq!(
interpret("9223372036854775807 + 1"),
Ok(Value::Integer(i64::MIN))
);
assert_eq!(
interpret("-9223372036854775808 - 1"),
Ok(Value::Integer(i64::MAX))
);
}
2024-01-06 07:26:51 +00:00
#[test]
fn float() {
2024-01-30 04:57:13 +00:00
assert_eq!(
interpret("1.7976931348623157e308"),
2024-01-30 04:57:13 +00:00
Ok(Value::Float(f64::MAX))
);
assert_eq!(
interpret("-1.7976931348623157e308"),
2024-01-30 04:57:13 +00:00
Ok(Value::Float(f64::MIN))
);
2024-01-06 07:26:51 +00:00
}
#[test]
fn string() {
assert_eq!(interpret("\"one\""), Ok(Value::string("one".to_string())));
assert_eq!(interpret("'one'"), Ok(Value::string("one".to_string())));
assert_eq!(interpret("`one`"), Ok(Value::string("one".to_string())));
assert_eq!(interpret("`'one'`"), Ok(Value::string("'one'".to_string())));
assert_eq!(interpret("'`one`'"), Ok(Value::string("`one`".to_string())));
assert_eq!(
interpret("\"'one'\""),
Ok(Value::string("'one'".to_string()))
);
}
#[test]
fn list() {
assert_eq!(
interpret("[1, 2, 'foobar']"),
Ok(Value::List(List::with_items(vec![
Value::Integer(1),
Value::Integer(2),
Value::string("foobar".to_string()),
])))
);
}
2024-01-29 22:36:21 +00:00
#[test]
fn empty_list() {
assert_eq!(interpret("[]"), Ok(Value::List(List::new())));
}
2024-01-06 07:26:51 +00:00
#[test]
fn map() {
2024-02-15 05:53:43 +00:00
let mut map = Map::new();
2024-01-06 07:26:51 +00:00
2024-02-15 21:02:27 +00:00
map.set(Identifier::new("x"), Value::Integer(1));
map.set(Identifier::new("foo"), Value::string("bar".to_string()));
2024-01-06 07:26:51 +00:00
assert_eq!(interpret("{ x = 1, foo = 'bar' }"), Ok(Value::Map(map)));
}
2024-01-29 22:36:21 +00:00
#[test]
fn empty_map() {
assert_eq!(interpret("{}"), Ok(Value::Map(Map::new())));
}
2024-01-06 07:26:51 +00:00
#[test]
fn map_types() {
2024-02-15 05:53:43 +00:00
let mut map = Map::new();
2024-01-06 07:26:51 +00:00
2024-02-15 21:02:27 +00:00
map.set(Identifier::new("x"), Value::Integer(1));
map.set(Identifier::new("foo"), Value::string("bar".to_string()));
2024-01-06 07:26:51 +00:00
assert_eq!(
interpret("{ x <int> = 1, foo <str> = 'bar' }"),
Ok(Value::Map(map))
);
}
#[test]
fn map_type_errors() {
2024-02-01 00:35:27 +00:00
assert_eq!(
interpret("{ foo <bool> = 'bar' }"),
Err(Error::Validation(error::ValidationError::TypeCheck {
2024-01-06 07:26:51 +00:00
expected: Type::Boolean,
2024-02-01 00:35:27 +00:00
actual: Type::String,
position: SourcePosition {
start_byte: 0,
2024-02-01 02:21:42 +00:00
end_byte: 22,
start_row: 1,
2024-02-01 00:35:27 +00:00
start_column: 0,
2024-02-01 02:21:42 +00:00
end_row: 1,
end_column: 22
2024-02-01 00:35:27 +00:00
}
2024-01-06 07:26:51 +00:00
}))
2024-02-01 00:35:27 +00:00
);
2024-01-06 07:26:51 +00:00
}
#[test]
fn function() {
let result = interpret("() <int> { 1 }");
let value = result.unwrap();
let function = value.as_function().unwrap();
let function = if let Function::ContextDefined(function) = function {
function
} else {
panic!("Something is wrong with this test...");
};
assert_eq!(&Vec::<Identifier>::with_capacity(0), function.parameters());
assert_eq!(&Type::Integer, function.return_type());
let result = interpret("(x <bool>) <bool> { true }");
let value = result.unwrap();
let function = value.as_function().unwrap();
let function = if let Function::ContextDefined(function) = function {
function
} else {
panic!("Something is wrong with this test...");
};
2024-02-15 21:02:27 +00:00
assert_eq!(&vec![Identifier::new("x")], function.parameters());
2024-01-06 07:26:51 +00:00
assert_eq!(&Type::Boolean, function.return_type());
}
2024-01-25 01:11:34 +00:00
#[test]
fn range() {
2024-01-29 22:36:21 +00:00
assert_eq!(interpret("0..100"), Ok(Value::range(0, 100)));
2024-01-25 01:11:34 +00:00
}