Added more tests
This commit is contained in:
@@ -106,6 +106,27 @@ pub void test_let_bool()
|
|||||||
assert_eq!(t, !f);
|
assert_eq!(t, !f);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub void test_let_decl_only()
|
||||||
|
{
|
||||||
|
let x;
|
||||||
|
x = 42;
|
||||||
|
assert_eq!(x, 42);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub void test_let_typed_decl_only()
|
||||||
|
{
|
||||||
|
i32 x;
|
||||||
|
x = 100;
|
||||||
|
assert_eq!(x, 100);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub void test_let_decl_mut_no_init()
|
||||||
|
{
|
||||||
|
let mut x;
|
||||||
|
x = 5;
|
||||||
|
assert_eq!(x, 5);
|
||||||
|
}
|
||||||
|
|
||||||
pub void run_tests()
|
pub void run_tests()
|
||||||
{
|
{
|
||||||
test_let_basic();
|
test_let_basic();
|
||||||
@@ -120,4 +141,7 @@ pub void run_tests()
|
|||||||
test_let_with_type_and_init();
|
test_let_with_type_and_init();
|
||||||
test_string_literal();
|
test_string_literal();
|
||||||
test_let_bool();
|
test_let_bool();
|
||||||
|
test_let_decl_only();
|
||||||
|
test_let_typed_decl_only();
|
||||||
|
test_let_decl_mut_no_init();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -155,6 +155,63 @@ pub void test_binary_assign()
|
|||||||
assert_eq!(x, 2);
|
assert_eq!(x, 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub void test_binary_assign_bit()
|
||||||
|
{
|
||||||
|
let mut x = 10;
|
||||||
|
x &= 12;
|
||||||
|
assert_eq!(x, 8);
|
||||||
|
x |= 1;
|
||||||
|
assert_eq!(x, 9);
|
||||||
|
x ^= 3;
|
||||||
|
assert_eq!(x, 10);
|
||||||
|
let mut y = 8;
|
||||||
|
y <<= 2;
|
||||||
|
assert_eq!(y, 32);
|
||||||
|
y >>= 1;
|
||||||
|
assert_eq!(y, 16);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub void test_empty_tuple()
|
||||||
|
{
|
||||||
|
let t = ();
|
||||||
|
assert_eq!(t, ());
|
||||||
|
}
|
||||||
|
|
||||||
|
pub void test_empty_array()
|
||||||
|
{
|
||||||
|
let arr = [0; 0];
|
||||||
|
assert_eq!(arr.len(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub void test_range_inclusive()
|
||||||
|
{
|
||||||
|
let mut sum = 0;
|
||||||
|
for i in 1 ..= 3 {
|
||||||
|
sum += i;
|
||||||
|
}
|
||||||
|
assert_eq!(sum, 6);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub void test_closure_no_args()
|
||||||
|
{
|
||||||
|
let f = () => 42;
|
||||||
|
assert_eq!(f(), 42);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub void test_closure_with_typed_params()
|
||||||
|
{
|
||||||
|
let add = (i32 a, i32 b) => a + b;
|
||||||
|
assert_eq!(add(3, 4), 7);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub void test_mut_ref()
|
||||||
|
{
|
||||||
|
let mut x = 42;
|
||||||
|
let r = &mut x;
|
||||||
|
*r = 100;
|
||||||
|
assert_eq!(x, 100);
|
||||||
|
}
|
||||||
|
|
||||||
pub void run_tests()
|
pub void run_tests()
|
||||||
{
|
{
|
||||||
test_literals();
|
test_literals();
|
||||||
@@ -174,4 +231,11 @@ pub void run_tests()
|
|||||||
test_if_expr();
|
test_if_expr();
|
||||||
test_range_expr();
|
test_range_expr();
|
||||||
test_binary_assign();
|
test_binary_assign();
|
||||||
|
test_binary_assign_bit();
|
||||||
|
test_empty_tuple();
|
||||||
|
test_empty_array();
|
||||||
|
test_range_inclusive();
|
||||||
|
test_closure_no_args();
|
||||||
|
test_closure_with_typed_params();
|
||||||
|
test_mut_ref();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -160,6 +160,68 @@ pub void test_for_with_pattern_destructure()
|
|||||||
assert_eq!(keys, 3);
|
assert_eq!(keys, 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub void test_c_for_loop()
|
||||||
|
{
|
||||||
|
let mut sum = 0;
|
||||||
|
for (let mut i = 0; i < 4; i++) {
|
||||||
|
sum += i;
|
||||||
|
}
|
||||||
|
assert_eq!(sum, 6);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub void test_unsafe_block()
|
||||||
|
{
|
||||||
|
let mut x = 42;
|
||||||
|
unsafe {
|
||||||
|
let r = &mut x;
|
||||||
|
*r = 100;
|
||||||
|
}
|
||||||
|
assert_eq!(x, 100);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub void test_match_parenthesized()
|
||||||
|
{
|
||||||
|
let result;
|
||||||
|
match (2) {
|
||||||
|
1 => result = 10,
|
||||||
|
2 => result = 20,
|
||||||
|
_ => panic!(),
|
||||||
|
}
|
||||||
|
assert_eq!(result, 20);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub void test_if_parenthesized()
|
||||||
|
{
|
||||||
|
let mut x = 0;
|
||||||
|
if (true) {
|
||||||
|
x = 1;
|
||||||
|
}
|
||||||
|
assert_eq!(x, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub void test_while_parenthesized()
|
||||||
|
{
|
||||||
|
let mut count = 0;
|
||||||
|
while (count < 3) {
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
assert_eq!(count, 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub void test_loop_with_break_value()
|
||||||
|
{
|
||||||
|
let mut x = 0;
|
||||||
|
let result;
|
||||||
|
loop {
|
||||||
|
if x >= 3 {
|
||||||
|
result = x;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
x++;
|
||||||
|
}
|
||||||
|
assert_eq!(result, 3);
|
||||||
|
}
|
||||||
|
|
||||||
pub void run_tests()
|
pub void run_tests()
|
||||||
{
|
{
|
||||||
test_var_decl();
|
test_var_decl();
|
||||||
@@ -176,4 +238,10 @@ pub void run_tests()
|
|||||||
test_match_or_pattern();
|
test_match_or_pattern();
|
||||||
test_match_default();
|
test_match_default();
|
||||||
test_for_with_pattern_destructure();
|
test_for_with_pattern_destructure();
|
||||||
|
test_c_for_loop();
|
||||||
|
test_unsafe_block();
|
||||||
|
test_match_parenthesized();
|
||||||
|
test_if_parenthesized();
|
||||||
|
test_while_parenthesized();
|
||||||
|
test_loop_with_break_value();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -52,6 +52,30 @@ pub void test_fn_return_type() {
|
|||||||
assert_eq!(add(3, 4), 7);
|
assert_eq!(add(3, 4), 7);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub void test_generic_type() {
|
||||||
|
Option<i32> x = Option::Some(42);
|
||||||
|
assert_eq!(x.is_some(), true);
|
||||||
|
}
|
||||||
|
|
||||||
|
type MyInt = i32;
|
||||||
|
|
||||||
|
pub void test_type_alias() {
|
||||||
|
MyInt x = 42;
|
||||||
|
assert_eq!(x, 42);
|
||||||
|
}
|
||||||
|
|
||||||
|
const i32 MY_CONST = 100;
|
||||||
|
|
||||||
|
pub void test_const_decl() {
|
||||||
|
assert_eq!(MY_CONST, 100);
|
||||||
|
}
|
||||||
|
|
||||||
|
static i32 my_static = 200;
|
||||||
|
|
||||||
|
pub void test_static_decl() {
|
||||||
|
assert_eq!(my_static, 200);
|
||||||
|
}
|
||||||
|
|
||||||
pub void run_tests() {
|
pub void run_tests() {
|
||||||
test_integer_type();
|
test_integer_type();
|
||||||
test_float_type();
|
test_float_type();
|
||||||
@@ -62,4 +86,8 @@ pub void run_tests() {
|
|||||||
test_mut_ref_type();
|
test_mut_ref_type();
|
||||||
test_void_type();
|
test_void_type();
|
||||||
test_fn_return_type();
|
test_fn_return_type();
|
||||||
|
test_generic_type();
|
||||||
|
test_type_alias();
|
||||||
|
test_const_decl();
|
||||||
|
test_static_decl();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,8 +22,21 @@ pub void test_allow_attribute()
|
|||||||
assert_eq!(x, 42);
|
assert_eq!(x, 42);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub struct Tagged
|
||||||
|
{
|
||||||
|
i32 id;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub void test_attribute_with_args()
|
||||||
|
{
|
||||||
|
Tagged t = Tagged { id: 1 };
|
||||||
|
assert_eq!(t.id, 1);
|
||||||
|
}
|
||||||
|
|
||||||
pub void run_tests()
|
pub void run_tests()
|
||||||
{
|
{
|
||||||
test_derive_attribute();
|
test_derive_attribute();
|
||||||
test_allow_attribute();
|
test_allow_attribute();
|
||||||
|
test_attribute_with_args();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -59,10 +59,73 @@ pub class Dog : Mammal
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub class Counter
|
||||||
|
{
|
||||||
|
pub i32 count;
|
||||||
|
|
||||||
|
constructor()
|
||||||
|
{
|
||||||
|
self.count = 42;
|
||||||
|
}
|
||||||
|
|
||||||
|
i32 get_count(&self)
|
||||||
|
{
|
||||||
|
self.count
|
||||||
|
}
|
||||||
|
|
||||||
|
pub i32 static_fn()
|
||||||
|
{
|
||||||
|
99
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub void test_class_no_inheritance()
|
||||||
|
{
|
||||||
|
Counter c = Counter::new();
|
||||||
|
assert_eq!(c.get_count(), 42);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub class WithDefault
|
||||||
|
{
|
||||||
|
pub String name = "default".to_string();
|
||||||
|
pub i32 value = 42;
|
||||||
|
|
||||||
|
constructor()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
pub String get_name(&self)
|
||||||
|
{
|
||||||
|
self.name.clone()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub void test_class_field_default()
|
||||||
|
{
|
||||||
|
WithDefault w = WithDefault::new();
|
||||||
|
assert_eq!(w.get_name(), "default");
|
||||||
|
assert_eq!(w.value, 42);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub class EmptyClass
|
||||||
|
{
|
||||||
|
constructor()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub void test_class_empty()
|
||||||
|
{
|
||||||
|
EmptyClass e = EmptyClass::new();
|
||||||
|
}
|
||||||
|
|
||||||
pub void run_tests()
|
pub void run_tests()
|
||||||
{
|
{
|
||||||
let dawg = Dog::new();
|
let dawg = Dog::new();
|
||||||
dawg.args_test(10, "hello");
|
dawg.args_test(10, "hello");
|
||||||
dawg.test_speak();
|
dawg.test_speak();
|
||||||
assert_eq!(format!("{dawg}"), "🐾 Rex");
|
assert_eq!(format!("{dawg}"), "🐾 Rex");
|
||||||
|
test_class_no_inheritance();
|
||||||
|
test_class_field_default();
|
||||||
|
test_class_empty();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -90,10 +90,40 @@ pub void test_enum_eq()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum Option<T>
|
||||||
|
{
|
||||||
|
None;
|
||||||
|
Some(T);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub void test_generic_enum()
|
||||||
|
{
|
||||||
|
Option<i32> x = Option::Some(42);
|
||||||
|
match x {
|
||||||
|
Option::Some(v) => {
|
||||||
|
assert_eq!(v, 42);
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
assert_eq!(0, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Option<bool> y = Option::None;
|
||||||
|
match y {
|
||||||
|
Option::None => {
|
||||||
|
assert_eq!(1, 1);
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
assert_eq!(0, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub void run_tests()
|
pub void run_tests()
|
||||||
{
|
{
|
||||||
test_enum_named();
|
test_enum_named();
|
||||||
test_enum_tuple();
|
test_enum_tuple();
|
||||||
test_enum_struct();
|
test_enum_struct();
|
||||||
test_enum_eq();
|
test_enum_eq();
|
||||||
|
test_generic_enum();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -69,6 +69,17 @@ pub void test_recursive_fn()
|
|||||||
assert_eq!(factorial(5), 120);
|
assert_eq!(factorial(5), 120);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
T identity<T>(T x)
|
||||||
|
{
|
||||||
|
x
|
||||||
|
}
|
||||||
|
|
||||||
|
pub void test_fn_generic()
|
||||||
|
{
|
||||||
|
assert_eq!(identity(42), 42);
|
||||||
|
assert_eq!(identity("hello"), "hello");
|
||||||
|
}
|
||||||
|
|
||||||
pub void run_tests()
|
pub void run_tests()
|
||||||
{
|
{
|
||||||
test_basic_function();
|
test_basic_function();
|
||||||
@@ -79,4 +90,5 @@ pub void run_tests()
|
|||||||
test_method_call();
|
test_method_call();
|
||||||
test_nested_fn_calls();
|
test_nested_fn_calls();
|
||||||
test_recursive_fn();
|
test_recursive_fn();
|
||||||
|
test_fn_generic();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -72,9 +72,29 @@ pub void test_multiple_impls()
|
|||||||
assert_eq!(c.y, 6);
|
assert_eq!(c.y, 6);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct Wrapper<T>
|
||||||
|
{
|
||||||
|
T value;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Copy> Wrapper<T>
|
||||||
|
{
|
||||||
|
pub T get(&self)
|
||||||
|
{
|
||||||
|
self.value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub void test_impl_generic()
|
||||||
|
{
|
||||||
|
Wrapper<i32> w = Wrapper { value: 42 };
|
||||||
|
assert_eq!(w.get(), 42);
|
||||||
|
}
|
||||||
|
|
||||||
pub void run_tests()
|
pub void run_tests()
|
||||||
{
|
{
|
||||||
test_inherent_impl();
|
test_inherent_impl();
|
||||||
test_trait_impl_block();
|
test_trait_impl_block();
|
||||||
test_multiple_impls();
|
test_multiple_impls();
|
||||||
|
test_impl_generic();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -59,10 +59,55 @@ pub void test_struct_nested()
|
|||||||
assert_eq!(l.end.y, 10);
|
assert_eq!(l.end.y, 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct Empty {}
|
||||||
|
|
||||||
|
pub void test_empty_struct()
|
||||||
|
{
|
||||||
|
let e = Empty {};
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Pair<T>
|
||||||
|
{
|
||||||
|
T first;
|
||||||
|
T second;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub void test_generic_struct()
|
||||||
|
{
|
||||||
|
Pair<i32> p = Pair<i32> { first: 1, second: 2 };
|
||||||
|
assert_eq!(p.first, 1);
|
||||||
|
assert_eq!(p.second, 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub void test_struct_partial_destructure()
|
||||||
|
{
|
||||||
|
let p = Point {
|
||||||
|
x: 10,
|
||||||
|
y: 20,
|
||||||
|
};
|
||||||
|
let Point {x, ..} = p;
|
||||||
|
assert_eq!(x, 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub void test_struct_destructure_rename()
|
||||||
|
{
|
||||||
|
let p = Point {
|
||||||
|
x: 5,
|
||||||
|
y: 15,
|
||||||
|
};
|
||||||
|
let Point {x: a, y: b} = p;
|
||||||
|
assert_eq!(a, 5);
|
||||||
|
assert_eq!(b, 15);
|
||||||
|
}
|
||||||
|
|
||||||
pub void run_tests()
|
pub void run_tests()
|
||||||
{
|
{
|
||||||
test_struct_create_access();
|
test_struct_create_access();
|
||||||
test_struct_mut_field();
|
test_struct_mut_field();
|
||||||
test_struct_destructure();
|
test_struct_destructure();
|
||||||
test_struct_nested();
|
test_struct_nested();
|
||||||
|
test_empty_struct();
|
||||||
|
test_generic_struct();
|
||||||
|
test_struct_partial_destructure();
|
||||||
|
test_struct_destructure_rename();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user