Skip to content
Snippets Groups Projects
test_suite.rs 2.66 KiB
Newer Older
use crate::parse::parse_program;

#[test]
fn binary_ops() {
	parse_program("1+1").expect("Failed binary add");
	parse_program("1-1").expect("Failed binary sub");
	parse_program("1*1").expect("Failed binary mul");
	parse_program("1/1").expect("Failed binary div");
	parse_program("1%1").expect("Failed binary mod");
}

#[test]
fn unary_ops() {
	parse_program("-1").expect("Failed binary inv");
	parse_program("!1").expect("Failed binary not");
}

#[test]
fn indentifiers() {
	parse_program("let my_ident").expect("Failed declare");
	parse_program("let my_ident = 123").expect("Failed declare/assign");
}

#[test]
fn conditional() {
	parse_program("if false { }").expect("Failed conditional with literal");
	parse_program("if some_ident { }").expect("Failed conditional with identifier");
	parse_program("if 1 + 1 { }").expect("Failed conditional with expr");
	parse_program("if (let ident = false) { }").expect("Failed conditional with decl");
}

#[test]
fn import_export() {
	parse_program(r#"import { foo } from "core:runtime""#).expect("Failed import with one item");
	parse_program(r#"import { foo, bar } from "core:runtime""#)
		.expect("Failed import with multiple items");
	parse_program(r#"import { foo as baz, bar } from "core:runtime""#)
		.expect("Failed import with multiple items and alias");
	parse_program(r#"import { foo as qwert, bar as asdf } from "core:runtime""#)
		.expect("Failed import with multiple alias");
	parse_program(r#"export { foo, bar }"#).expect("Failed export with multiple items");
	parse_program(r#"export { foo as qrweas, bar }"#)
		.expect("Failed export with multiple items and alias");
}

#[test]
fn expression_list() {
	parse_program("foo; bar").expect("Failed simple expression list");
	parse_program("if flop { 123 + 2323 } else { let boo = false }; let glob = \"swarmp\"")
		.expect("Failed simple expression list");
}
Louis's avatar
Louis committed

#[test]
fn function_calls() {
	parse_program("some_func()").expect("Failed empty function params");
	parse_program("some_func(123)").expect("Failed single function param");
	parse_program("some_func(false, 2929)").expect("Failed multiple function params");
	parse_program("some_func(250, if true { \"some val\" } else { \"123\" })")
		.expect("Failed complex function params");
}
Louis's avatar
Louis committed

#[test]
fn declare_function() {
	parse_program("fn some_func() {}").expect("Failed basic function");
	parse_program("fn some_func(my_param) {}").expect("Failed basic function");
	parse_program("fn some_func(my_param, second) {}").expect("Failed basic function");
	parse_program("fn some_func(my_param, second = 1234) {}")
		.expect("Failed basic function with defaults");
	parse_program("fn add(first, second) { first + second }")
		.expect("Failed basic function with body");
}