1
0

Add tests

This commit is contained in:
Jeff 2024-12-03 16:03:23 -05:00
parent 372a438a7f
commit 9b9d27820b
3 changed files with 157 additions and 18 deletions

View File

@ -1693,10 +1693,7 @@ impl<'src> Compiler<'src> {
} }
fn expect_dividable_type(argument_type: &Type, position: &Span) -> Result<(), CompileError> { fn expect_dividable_type(argument_type: &Type, position: &Span) -> Result<(), CompileError> {
if matches!( if matches!(argument_type, Type::Byte | Type::Float | Type::Integer) {
argument_type,
Type::Byte | Type::Character | Type::Float | Type::Integer
) {
Ok(()) Ok(())
} else { } else {
Err(CompileError::CannotDivideType { Err(CompileError::CannotDivideType {

View File

@ -19,36 +19,171 @@ fn divide_boolean_left() {
#[test] #[test]
fn divide_boolean_right() { fn divide_boolean_right() {
let source = "1 / true"; let source = "1 / true";
assert_eq!(
compile(source),
Err(DustError::Compile {
error: CompileError::CannotDivideType {
argument_type: Type::Boolean,
position: Span(4, 8)
},
source,
})
);
} }
#[test] #[test]
fn divide_character_left() { fn divide_character_left() {
let source = "'a' / 1"; let source = "'a' / 1";
assert_eq!(
compile(source),
Err(DustError::Compile {
error: CompileError::CannotDivideType {
argument_type: Type::Character,
position: Span(0, 3)
},
source,
})
);
} }
#[test] #[test]
fn divide_character_right() { fn divide_character_right() {
let source = "1 / 'a'"; let source = "1 / 'a'";
assert_eq!(
compile(source),
Err(DustError::Compile {
error: CompileError::CannotDivideType {
argument_type: Type::Character,
position: Span(4, 7)
},
source,
})
);
}
#[test]
fn divide_float_and_character() {
let source = "1.0 / 'a'";
assert_eq!(
compile(source),
Err(DustError::Compile {
error: CompileError::CannotDivideType {
argument_type: Type::Character,
position: Span(6, 9)
},
source,
})
);
}
#[test]
fn divide_float_and_integer() {
let source = "1.0 / 1";
assert_eq!(
compile(source),
Err(DustError::Compile {
error: CompileError::CannotDivideArguments {
left_type: Type::Float,
right_type: Type::Integer,
position: Span(0, 7)
},
source,
})
);
} }
#[test] #[test]
fn divide_function_left() { fn divide_function_left() {
let source = "fn(){} / 1"; let source = "fn(){} / 1";
assert_eq!(
compile(source),
Err(DustError::Compile {
error: CompileError::CannotDivideType {
argument_type: Type::Function(FunctionType {
type_parameters: None,
value_parameters: None,
return_type: Box::new(Type::None)
}),
position: Span(0, 6)
},
source,
})
);
} }
#[test] #[test]
fn divide_function_right() { fn divide_function_right() {
let source = "1 / fn(){}"; let source = "1 / fn(){}";
assert_eq!(
compile(source),
Err(DustError::Compile {
error: CompileError::CannotDivideType {
argument_type: Type::Function(FunctionType {
type_parameters: None,
value_parameters: None,
return_type: Box::new(Type::None)
}),
position: Span(4, 10)
},
source,
})
);
}
#[test]
fn divide_integer_and_float() {
let source = "1 / 1.0";
assert_eq!(
compile(source),
Err(DustError::Compile {
error: CompileError::CannotDivideArguments {
left_type: Type::Integer,
right_type: Type::Float,
position: Span(0, 7)
},
source,
})
);
} }
#[test] #[test]
fn divide_list_left() { fn divide_list_left() {
let source = "[1, 2] / 1"; let source = "[1, 2] / 1";
assert_eq!(
compile(source),
Err(DustError::Compile {
error: CompileError::CannotDivideType {
argument_type: Type::List(Box::new(Type::Integer)),
position: Span(0, 6)
},
source,
})
);
} }
#[test] #[test]
fn divide_list_right() { fn divide_list_right() {
let source = "1 / [1, 2]"; let source = "1 / [1, 2]";
assert_eq!(
compile(source),
Err(DustError::Compile {
error: CompileError::CannotDivideType {
argument_type: Type::List(Box::new(Type::Integer)),
position: Span(4, 10)
},
source,
})
);
} }
// #[test] // #[test]
@ -64,24 +199,31 @@ fn divide_list_right() {
#[test] #[test]
fn divide_string_left() { fn divide_string_left() {
let source = "\"hello\" / 1"; let source = "\"hello\" / 1";
assert_eq!(
compile(source),
Err(DustError::Compile {
error: CompileError::CannotDivideType {
argument_type: Type::String,
position: Span(0, 7)
},
source,
})
);
} }
#[test] #[test]
fn divide_string_right() { fn divide_string_right() {
let source = "1 / \"hello\""; let source = "1 / \"hello\"";
}
#[test] assert_eq!(
fn divide_float_and_character() { compile(source),
let source = "1.0 / 'a'"; Err(DustError::Compile {
} error: CompileError::CannotDivideType {
argument_type: Type::String,
#[test] position: Span(4, 11)
fn divide_float_and_integer() { },
let source = "1.0 / 1"; source,
} })
);
#[test]
fn divide_integer_and_float() {
let source = "1 / 1.0";
} }