Updated tests and fixed grammar
This commit is contained in:
@@ -200,7 +200,8 @@ attributes = {
|
||||
// DECLARATION HELPERS
|
||||
// ======================================================
|
||||
var_decl = {
|
||||
("let" | type_expr) ~ pattern
|
||||
("let" ~ pattern) |
|
||||
type_expr ~ (!tuple_pattern ~ pattern)
|
||||
}
|
||||
|
||||
field = {
|
||||
|
||||
@@ -1,44 +1,44 @@
|
||||
pub module decl_test;
|
||||
|
||||
pub fn void test_let_basic() {
|
||||
pub void test_let_basic() {
|
||||
let x = 42;
|
||||
assert_eq!(x, 42);
|
||||
}
|
||||
|
||||
pub fn void test_let_typed() {
|
||||
let x i32 = 42;
|
||||
pub void test_let_typed() {
|
||||
i32 x = 42;
|
||||
assert_eq!(x, 42);
|
||||
|
||||
let s *str = "hello";
|
||||
str& s = "hello";
|
||||
assert_eq!(s, "hello");
|
||||
|
||||
let b bool = true;
|
||||
bool b = true;
|
||||
assert_eq!(b, true);
|
||||
|
||||
let f f64 = 3.14;
|
||||
f64 f = 3.14;
|
||||
assert_eq!(f, 3.14);
|
||||
}
|
||||
|
||||
pub fn void test_let_mut() {
|
||||
pub void test_let_mut() {
|
||||
let mut _x = 42;
|
||||
_x = 100;
|
||||
assert_eq!(_x, 100);
|
||||
}
|
||||
|
||||
pub fn void test_let_tuple_destructure() {
|
||||
pub void test_let_tuple_destructure() {
|
||||
let (a, b) = (10, "hello");
|
||||
assert_eq!(a, 10);
|
||||
assert_eq!(b, "hello");
|
||||
}
|
||||
|
||||
pub fn void test_let_nested_tuple() {
|
||||
pub void test_let_nested_tuple() {
|
||||
let (a, (b, c)) = (1, (2, 3));
|
||||
assert_eq!(a, 1);
|
||||
assert_eq!(b, 2);
|
||||
assert_eq!(c, 3);
|
||||
}
|
||||
|
||||
pub fn void test_let_pattern_irrefutable() {
|
||||
pub void test_let_pattern_irrefutable() {
|
||||
let x = 5;
|
||||
let (y, z) = (1, 2);
|
||||
assert_eq!(x, 5);
|
||||
@@ -46,30 +46,30 @@ pub fn void test_let_pattern_irrefutable() {
|
||||
assert_eq!(z, 2);
|
||||
}
|
||||
|
||||
pub fn void test_multiple_decls() {
|
||||
pub void test_multiple_decls() {
|
||||
let a = 1;
|
||||
let b = 2;
|
||||
let c = 3;
|
||||
assert_eq!(a + b + c, 6);
|
||||
}
|
||||
|
||||
pub fn void test_let_with_type_and_init() {
|
||||
let x f64 = 10.0;
|
||||
pub void test_let_with_type_and_init() {
|
||||
f64 x = 10.0;
|
||||
assert_eq!(x, 10.0);
|
||||
}
|
||||
|
||||
pub fn void test_string_literal() {
|
||||
pub void test_string_literal() {
|
||||
let s = "hello, world!";
|
||||
assert_eq!(s.len(), 13);
|
||||
}
|
||||
|
||||
pub fn void test_let_bool() {
|
||||
pub void test_let_bool() {
|
||||
let t = true;
|
||||
let f = false;
|
||||
assert_eq!(t, !f);
|
||||
}
|
||||
|
||||
pub fn void run_tests() {
|
||||
pub void run_tests() {
|
||||
test_let_basic();
|
||||
test_let_typed();
|
||||
test_let_mut();
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
pub module expr_test;
|
||||
|
||||
pub fn void test_literals() {
|
||||
pub void test_literals() {
|
||||
assert_eq!(42, 42);
|
||||
assert_eq!(3.14, 3.14);
|
||||
assert_eq!(true, true);
|
||||
@@ -8,28 +8,28 @@ pub fn void test_literals() {
|
||||
assert_eq!("hello", "hello");
|
||||
}
|
||||
|
||||
pub fn void test_tuples() {
|
||||
pub void test_tuples() {
|
||||
let t = (1, true, "three");
|
||||
assert_eq!(t.0, 1);
|
||||
assert_eq!(t.1, true);
|
||||
assert_eq!(t.2, "three");
|
||||
}
|
||||
|
||||
pub fn void test_arrays() {
|
||||
pub void test_arrays() {
|
||||
let arr = [1, 2, 3];
|
||||
assert_eq!(arr[0], 1);
|
||||
assert_eq!(arr[1], 2);
|
||||
assert_eq!(arr[2], 3);
|
||||
}
|
||||
|
||||
pub fn void test_array_repeat() {
|
||||
pub void test_array_repeat() {
|
||||
let arr = [0; 5];
|
||||
assert_eq!(arr[0], 0);
|
||||
assert_eq!(arr[4], 0);
|
||||
assert_eq!(arr.len(), 5);
|
||||
}
|
||||
|
||||
pub fn void test_binary_arith() {
|
||||
pub void test_binary_arith() {
|
||||
assert_eq!(10 + 5, 15);
|
||||
assert_eq!(10 - 5, 5);
|
||||
assert_eq!(10 * 5, 50);
|
||||
@@ -37,7 +37,7 @@ pub fn void test_binary_arith() {
|
||||
assert_eq!(10 % 3, 1);
|
||||
}
|
||||
|
||||
pub fn void test_binary_cmp() {
|
||||
pub void test_binary_cmp() {
|
||||
assert_eq!(5 < 10, true);
|
||||
assert_eq!(10 <= 10, true);
|
||||
assert_eq!(10 > 5, true);
|
||||
@@ -46,14 +46,14 @@ pub fn void test_binary_cmp() {
|
||||
assert_eq!(5 != 10, true);
|
||||
}
|
||||
|
||||
pub fn void test_binary_logic() {
|
||||
pub void test_binary_logic() {
|
||||
assert_eq!(true && true, true);
|
||||
assert_eq!(true && false, false);
|
||||
assert_eq!(true || false, true);
|
||||
assert_eq!(false || false, false);
|
||||
}
|
||||
|
||||
pub fn void test_binary_bit() {
|
||||
pub void test_binary_bit() {
|
||||
assert_eq!(5 & 3, 1);
|
||||
assert_eq!(5 | 3, 7);
|
||||
assert_eq!(5 ^ 3, 6);
|
||||
@@ -61,20 +61,20 @@ pub fn void test_binary_bit() {
|
||||
assert_eq!(4 >> 1, 2);
|
||||
}
|
||||
|
||||
pub fn void test_prefix() {
|
||||
pub void test_prefix() {
|
||||
let x = 42;
|
||||
assert_eq!(-x, -42);
|
||||
assert_eq!(!true, false);
|
||||
assert_eq!(!false, true);
|
||||
}
|
||||
|
||||
pub fn void test_ref_deref() {
|
||||
pub void test_ref_deref() {
|
||||
let x = 42;
|
||||
let r = &x;
|
||||
assert_eq!(*r, 42);
|
||||
}
|
||||
|
||||
pub fn void test_postfix_inc_dec() {
|
||||
pub void test_postfix_inc_dec() {
|
||||
let mut x = 5;
|
||||
x++;
|
||||
assert_eq!(x, 6);
|
||||
@@ -82,17 +82,17 @@ pub fn void test_postfix_inc_dec() {
|
||||
assert_eq!(x, 5);
|
||||
}
|
||||
|
||||
pub fn void test_as_cast() {
|
||||
pub void test_as_cast() {
|
||||
let x = 42;
|
||||
assert_eq!(x as f64, 42.0);
|
||||
}
|
||||
|
||||
pub fn void test_closure() {
|
||||
pub void test_closure() {
|
||||
let add = fn(a, b) -> a + b;
|
||||
assert_eq!(add(2, 3), 5);
|
||||
}
|
||||
|
||||
pub fn void test_block_expr() {
|
||||
pub void test_block_expr() {
|
||||
let x = {
|
||||
let y = 10;
|
||||
y + 5
|
||||
@@ -100,7 +100,7 @@ pub fn void test_block_expr() {
|
||||
assert_eq!(x, 15);
|
||||
}
|
||||
|
||||
pub fn void test_if_expr() {
|
||||
pub void test_if_expr() {
|
||||
let x = if true { 1 } else { 2 };
|
||||
assert_eq!(x, 1);
|
||||
|
||||
@@ -108,7 +108,7 @@ pub fn void test_if_expr() {
|
||||
assert_eq!(y, 2);
|
||||
}
|
||||
|
||||
pub fn void test_range_expr() {
|
||||
pub void test_range_expr() {
|
||||
let r = 0..5;
|
||||
let mut count = 0;
|
||||
for i in r {
|
||||
@@ -117,7 +117,7 @@ pub fn void test_range_expr() {
|
||||
assert_eq!(count, 5);
|
||||
}
|
||||
|
||||
pub fn void test_binary_assign() {
|
||||
pub void test_binary_assign() {
|
||||
let mut x = 10;
|
||||
x += 5;
|
||||
assert_eq!(x, 15);
|
||||
@@ -131,7 +131,7 @@ pub fn void test_binary_assign() {
|
||||
assert_eq!(x, 2);
|
||||
}
|
||||
|
||||
pub fn void run_tests() {
|
||||
pub void run_tests() {
|
||||
test_literals();
|
||||
test_tuples();
|
||||
test_arrays();
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
pub module common;
|
||||
|
||||
pub fn void test_package() {}
|
||||
pub void test_package() {}
|
||||
|
||||
@@ -1,23 +1,23 @@
|
||||
pub module statement_test;
|
||||
|
||||
pub fn void test_var_decl() {
|
||||
pub void test_var_decl() {
|
||||
let x = 42;
|
||||
assert_eq!(x, 42);
|
||||
|
||||
let y i32 = 10;
|
||||
i32 y = 10;
|
||||
assert_eq!(y, 10);
|
||||
|
||||
let name *str = "mist";
|
||||
str& name = "mist";
|
||||
assert_eq!(name, "mist");
|
||||
}
|
||||
|
||||
pub fn void test_var_decl_with_init() {
|
||||
pub void test_var_decl_with_init() {
|
||||
let mut x = 0;
|
||||
x = 5;
|
||||
assert_eq!(x, 5);
|
||||
}
|
||||
|
||||
pub fn void test_if_statement() {
|
||||
pub void test_if_statement() {
|
||||
let mut x = 0;
|
||||
|
||||
if true {
|
||||
@@ -33,7 +33,7 @@ pub fn void test_if_statement() {
|
||||
assert_eq!(x, 3);
|
||||
}
|
||||
|
||||
pub fn void test_if_else_if() {
|
||||
pub void test_if_else_if() {
|
||||
let mut x = 0;
|
||||
|
||||
if x == 1 {
|
||||
@@ -46,7 +46,7 @@ pub fn void test_if_else_if() {
|
||||
assert_eq!(x, 20);
|
||||
}
|
||||
|
||||
pub fn void test_while_loop() {
|
||||
pub void test_while_loop() {
|
||||
let mut count = 0;
|
||||
|
||||
while count < 5 {
|
||||
@@ -55,7 +55,7 @@ pub fn void test_while_loop() {
|
||||
assert_eq!(count, 5);
|
||||
}
|
||||
|
||||
pub fn void test_for_loop() {
|
||||
pub void test_for_loop() {
|
||||
let mut sum = 0;
|
||||
|
||||
for i in 0..4 {
|
||||
@@ -64,7 +64,7 @@ pub fn void test_for_loop() {
|
||||
assert_eq!(sum, 6);
|
||||
}
|
||||
|
||||
pub fn void test_loop_stmt() {
|
||||
pub void test_loop_stmt() {
|
||||
let mut count = 0;
|
||||
|
||||
loop {
|
||||
@@ -76,7 +76,7 @@ pub fn void test_loop_stmt() {
|
||||
assert_eq!(count, 3);
|
||||
}
|
||||
|
||||
pub fn void test_break_continue() {
|
||||
pub void test_break_continue() {
|
||||
let mut sum = 0;
|
||||
|
||||
for i in 0..10 {
|
||||
@@ -91,11 +91,11 @@ pub fn void test_break_continue() {
|
||||
assert_eq!(sum, 1 + 3 + 5);
|
||||
}
|
||||
|
||||
pub fn i32 test_return() {
|
||||
pub i32 test_return() {
|
||||
return 42;
|
||||
}
|
||||
|
||||
pub fn void test_block_scope() {
|
||||
pub void test_block_scope() {
|
||||
let x = {
|
||||
let inner = 10;
|
||||
inner + 5
|
||||
@@ -103,7 +103,7 @@ pub fn void test_block_scope() {
|
||||
assert_eq!(x, 15);
|
||||
}
|
||||
|
||||
pub fn void test_match_literal() {
|
||||
pub void test_match_literal() {
|
||||
let x = 2;
|
||||
let result;
|
||||
|
||||
@@ -116,9 +116,9 @@ pub fn void test_match_literal() {
|
||||
assert_eq!(result, 20);
|
||||
}
|
||||
|
||||
pub fn void test_match_or_pattern() {
|
||||
pub void test_match_or_pattern() {
|
||||
let x = 1;
|
||||
let result i32;
|
||||
i32 result;
|
||||
|
||||
match x {
|
||||
1 | 2 => { result = 100; }
|
||||
@@ -128,7 +128,7 @@ pub fn void test_match_or_pattern() {
|
||||
assert_eq!(result, 100);
|
||||
}
|
||||
|
||||
pub fn void test_match_default() {
|
||||
pub void test_match_default() {
|
||||
let x = 99;
|
||||
let result;
|
||||
|
||||
@@ -139,7 +139,7 @@ pub fn void test_match_default() {
|
||||
assert_eq!(result, 99);
|
||||
}
|
||||
|
||||
pub fn void test_for_with_pattern_destructure() {
|
||||
pub void test_for_with_pattern_destructure() {
|
||||
let pairs = [(1, "a"), (2, "b")];
|
||||
let mut keys = 0;
|
||||
|
||||
@@ -149,7 +149,7 @@ pub fn void test_for_with_pattern_destructure() {
|
||||
assert_eq!(keys, 3);
|
||||
}
|
||||
|
||||
pub fn void run_tests() {
|
||||
pub void run_tests() {
|
||||
test_var_decl();
|
||||
test_var_decl_with_init();
|
||||
test_if_statement();
|
||||
|
||||
@@ -1,58 +1,58 @@
|
||||
pub module types_test;
|
||||
|
||||
pub fn void test_integer_type() {
|
||||
let x i32 = 42;
|
||||
pub void test_integer_type() {
|
||||
i32 x = 42;
|
||||
assert_eq!(x, 42);
|
||||
}
|
||||
|
||||
pub fn void test_float_type() {
|
||||
let x f64 = 3.14;
|
||||
pub void test_float_type() {
|
||||
f64 x = 3.14;
|
||||
assert_eq!(x, 3.14);
|
||||
}
|
||||
|
||||
pub fn void test_bool_type() {
|
||||
let x bool = true;
|
||||
pub void test_bool_type() {
|
||||
bool x = true;
|
||||
assert_eq!(x, true);
|
||||
|
||||
let y bool = false;
|
||||
bool y = false;
|
||||
assert_eq!(y, false);
|
||||
}
|
||||
|
||||
pub fn void test_string_type() {
|
||||
let s *str = "hello";
|
||||
pub void test_string_type() {
|
||||
str& s = "hello";
|
||||
assert_eq!(s, "hello");
|
||||
}
|
||||
|
||||
pub fn void test_tuple_type() {
|
||||
let t (i32, *str) = (42, "answer");
|
||||
pub void test_tuple_type() {
|
||||
(i32, str&) t = (42, "answer");
|
||||
assert_eq!(t.0, 42);
|
||||
assert_eq!(t.1, "answer");
|
||||
}
|
||||
|
||||
pub fn void test_ref_type() {
|
||||
let x i32 = 42;
|
||||
let r *i32 = &x;
|
||||
pub void test_ref_type() {
|
||||
i32 x = 42;
|
||||
i32& r = &x;
|
||||
assert_eq!(*r, 42);
|
||||
}
|
||||
|
||||
pub fn void test_mut_ref_type() {
|
||||
pub void test_mut_ref_type() {
|
||||
let mut x = 42;
|
||||
let r *mut i32 = &mut x;
|
||||
i32 mut& r = &mut x;
|
||||
*r = 100;
|
||||
assert_eq!(x, 100);
|
||||
}
|
||||
|
||||
pub fn void test_void_type() {}
|
||||
pub void test_void_type() {}
|
||||
|
||||
fn i32 add(a i32, b i32) {
|
||||
i32 add(i32 a, i32 b) {
|
||||
a + b
|
||||
}
|
||||
|
||||
pub fn void test_fn_return_type() {
|
||||
pub void test_fn_return_type() {
|
||||
assert_eq!(add(3, 4), 7);
|
||||
}
|
||||
|
||||
pub fn void run_tests() {
|
||||
pub void run_tests() {
|
||||
test_integer_type();
|
||||
test_float_type();
|
||||
test_bool_type();
|
||||
|
||||
@@ -2,22 +2,22 @@ pub module attribute_test;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Debuggable {
|
||||
value i32,
|
||||
i32 value,
|
||||
}
|
||||
|
||||
pub fn void test_derive_attribute() {
|
||||
pub void test_derive_attribute() {
|
||||
let d = Debuggable { value: 42 };
|
||||
assert_eq!(d.value, 42);
|
||||
}
|
||||
|
||||
#[allow(unused_variables)]
|
||||
pub fn void test_allow_attribute() {
|
||||
pub void test_allow_attribute() {
|
||||
let x = 42;
|
||||
let y = "unused";
|
||||
assert_eq!(x, 42);
|
||||
}
|
||||
|
||||
pub fn void run_tests() {
|
||||
pub void run_tests() {
|
||||
test_derive_attribute();
|
||||
test_allow_attribute();
|
||||
}
|
||||
|
||||
@@ -1,21 +1,21 @@
|
||||
pub module class_test;
|
||||
|
||||
pub class Animal {
|
||||
pub name String;
|
||||
pub String name;
|
||||
|
||||
constructor() {
|
||||
self.name = "Rex".to_string();
|
||||
}
|
||||
|
||||
pub fn String speak(*self) {
|
||||
pub String speak(&self) {
|
||||
"Unknown".to_string()
|
||||
}
|
||||
|
||||
pub fn void test_speak(*self) {
|
||||
pub void test_speak(&self) {
|
||||
assert_eq!(self.speak(), "Woof!");
|
||||
}
|
||||
|
||||
pub fn void static_method() {}
|
||||
pub void static_method() {}
|
||||
}
|
||||
|
||||
pub class Mammal : Animal {
|
||||
@@ -23,7 +23,7 @@ pub class Mammal : Animal {
|
||||
super = Super::new();
|
||||
}
|
||||
|
||||
pub fn void args_test(*self, x i32, y *str) {
|
||||
pub void args_test(&self, i32 x, str& y) {
|
||||
assert_eq!(x, 10);
|
||||
assert_eq!(y, "hello");
|
||||
}
|
||||
@@ -34,18 +34,18 @@ pub class Dog : Mammal {
|
||||
super = Super::new();
|
||||
}
|
||||
|
||||
pub fn String speak(*self) override(Animal) {
|
||||
pub String speak(&self) override(Animal) {
|
||||
"Woof!".to_string()
|
||||
}
|
||||
|
||||
impl std::fmt::Display {
|
||||
fn Result<(), std::fmt::Error> fmt(*self, f *mut std::fmt::Formatter<'_>) {
|
||||
Result<(), std::fmt::Error> fmt(&self, std::fmt::Formatter<'_> mut& f) {
|
||||
write!(f, "🐾 {}", self.name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn void run_tests() {
|
||||
pub void run_tests() {
|
||||
let dawg = Dog::new();
|
||||
|
||||
dawg.args_test(10, "hello");
|
||||
|
||||
@@ -5,7 +5,7 @@ enum OptionInt {
|
||||
Some(i32),
|
||||
}
|
||||
|
||||
pub fn void test_enum_named() {
|
||||
pub void test_enum_named() {
|
||||
let x = OptionInt::None;
|
||||
match x {
|
||||
OptionInt::None => { assert_eq!(1, 1); }
|
||||
@@ -13,7 +13,7 @@ pub fn void test_enum_named() {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn void test_enum_tuple() {
|
||||
pub void test_enum_tuple() {
|
||||
let x = OptionInt::Some(42);
|
||||
match x {
|
||||
OptionInt::None => { assert_eq!(0, 1); }
|
||||
@@ -22,11 +22,11 @@ pub fn void test_enum_tuple() {
|
||||
}
|
||||
|
||||
enum Shape {
|
||||
Circle { radius i32 },
|
||||
Rect { w i32, h i32 },
|
||||
Circle { i32 radius },
|
||||
Rect { i32 w, i32 h },
|
||||
}
|
||||
|
||||
pub fn void test_enum_struct() {
|
||||
pub void test_enum_struct() {
|
||||
let c = Shape::Circle { radius: 5 };
|
||||
match c {
|
||||
Shape::Circle { radius } => { assert_eq!(radius, 5); }
|
||||
@@ -46,7 +46,7 @@ enum Color {
|
||||
Blue,
|
||||
}
|
||||
|
||||
pub fn void test_enum_eq() {
|
||||
pub void test_enum_eq() {
|
||||
let c = Color::Red;
|
||||
match c {
|
||||
Color::Red => { assert_eq!(1, 1); }
|
||||
@@ -54,7 +54,7 @@ pub fn void test_enum_eq() {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn void run_tests() {
|
||||
pub void run_tests() {
|
||||
test_enum_named();
|
||||
test_enum_tuple();
|
||||
test_enum_struct();
|
||||
|
||||
@@ -1,60 +1,60 @@
|
||||
pub module function_test;
|
||||
|
||||
fn void greet() {
|
||||
void greet() {
|
||||
let mut x = 1;
|
||||
x++;
|
||||
}
|
||||
|
||||
pub fn void test_basic_function() {
|
||||
pub void test_basic_function() {
|
||||
greet();
|
||||
}
|
||||
|
||||
fn i32 add(a i32, b i32) {
|
||||
i32 add(i32 a, i32 b) {
|
||||
a + b
|
||||
}
|
||||
|
||||
pub fn void test_fn_params() {
|
||||
pub void test_fn_params() {
|
||||
assert_eq!(add(2, 3), 5);
|
||||
}
|
||||
|
||||
pub fn i32 test_fn_return() {
|
||||
pub i32 test_fn_return() {
|
||||
42
|
||||
}
|
||||
|
||||
pub fn i32 test_fn_early_return() {
|
||||
pub i32 test_fn_early_return() {
|
||||
return 100;
|
||||
0
|
||||
}
|
||||
|
||||
pub fn i32 test_fn_mut_param(mut x i32) {
|
||||
pub i32 test_fn_mut_param(i32 mut x) {
|
||||
x = x + 10;
|
||||
x
|
||||
}
|
||||
|
||||
pub fn void test_method_call() {
|
||||
pub void test_method_call() {
|
||||
let s = "hello";
|
||||
assert_eq!(s.len(), 5);
|
||||
assert_eq!(s.to_uppercase(), "HELLO");
|
||||
}
|
||||
|
||||
fn i32 triple(x i32) {
|
||||
i32 triple(i32 x) {
|
||||
add(x, add(x, x))
|
||||
}
|
||||
|
||||
pub fn void test_nested_fn_calls() {
|
||||
pub void test_nested_fn_calls() {
|
||||
assert_eq!(triple(5), 15);
|
||||
}
|
||||
|
||||
fn i32 factorial(n i32) {
|
||||
i32 factorial(i32 n) {
|
||||
if n <= 1 { 1 }
|
||||
else { n * factorial(n - 1) }
|
||||
}
|
||||
|
||||
pub fn void test_recursive_fn() {
|
||||
pub void test_recursive_fn() {
|
||||
assert_eq!(factorial(5), 120);
|
||||
}
|
||||
|
||||
pub fn void run_tests() {
|
||||
pub void run_tests() {
|
||||
test_basic_function();
|
||||
test_fn_params();
|
||||
assert_eq!(test_fn_return(), 42);
|
||||
|
||||
@@ -1,39 +1,39 @@
|
||||
pub module impl_test;
|
||||
|
||||
struct Vector2 {
|
||||
x i32,
|
||||
y i32,
|
||||
i32 x,
|
||||
i32 y,
|
||||
}
|
||||
|
||||
impl Vector2 {
|
||||
pub fn i32 magnitude(*self) {
|
||||
pub i32 magnitude(&self) {
|
||||
self.x * self.x + self.y * self.y
|
||||
}
|
||||
}
|
||||
|
||||
pub fn void test_inherent_impl() {
|
||||
pub void test_inherent_impl() {
|
||||
let v = Vector2 { x: 3, y: 4 };
|
||||
assert_eq!(v.magnitude(), 25);
|
||||
}
|
||||
|
||||
trait Display {
|
||||
fn String display(*self);
|
||||
String display(&self);
|
||||
}
|
||||
|
||||
impl Display for Vector2 {
|
||||
fn String display(*self) {
|
||||
String display(&self) {
|
||||
format!("({}, {})", self.x, self.y)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn void test_trait_impl_block() {
|
||||
pub void test_trait_impl_block() {
|
||||
let v = Vector2 { x: 1, y: 2 };
|
||||
let s = v.display();
|
||||
assert_eq!(s.len() > 0, true);
|
||||
}
|
||||
|
||||
impl Vector2 {
|
||||
pub fn Vector2 add(*self, other Vector2) {
|
||||
pub Vector2 add(&self, Vector2 other) {
|
||||
Vector2 {
|
||||
x: self.x + other.x,
|
||||
y: self.y + other.y,
|
||||
@@ -41,7 +41,7 @@ impl Vector2 {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn void test_multiple_impls() {
|
||||
pub void test_multiple_impls() {
|
||||
let a = Vector2 { x: 1, y: 2 };
|
||||
let b = Vector2 { x: 3, y: 4 };
|
||||
let c = a.add(b);
|
||||
@@ -49,7 +49,7 @@ pub fn void test_multiple_impls() {
|
||||
assert_eq!(c.y, 6);
|
||||
}
|
||||
|
||||
pub fn void run_tests() {
|
||||
pub void run_tests() {
|
||||
test_inherent_impl();
|
||||
test_trait_impl_block();
|
||||
test_multiple_impls();
|
||||
|
||||
@@ -1,23 +1,23 @@
|
||||
pub module struct_test;
|
||||
|
||||
struct Point {
|
||||
x i32,
|
||||
y i32,
|
||||
i32 x,
|
||||
i32 y,
|
||||
}
|
||||
|
||||
pub fn void test_struct_create_access() {
|
||||
pub void test_struct_create_access() {
|
||||
let p = Point { x: 10, y: 20 };
|
||||
assert_eq!(p.x, 10);
|
||||
assert_eq!(p.y, 20);
|
||||
}
|
||||
|
||||
pub fn void test_struct_mut_field() {
|
||||
pub void test_struct_mut_field() {
|
||||
let mut p = Point { x: 1, y: 2 };
|
||||
p.x = 100;
|
||||
assert_eq!(p.x, 100);
|
||||
}
|
||||
|
||||
pub fn void test_struct_destructure() {
|
||||
pub void test_struct_destructure() {
|
||||
let p = Point { x: 3, y: 4 };
|
||||
let Point { x, y } = p;
|
||||
assert_eq!(x, 3);
|
||||
@@ -25,11 +25,11 @@ pub fn void test_struct_destructure() {
|
||||
}
|
||||
|
||||
struct Line {
|
||||
start Point,
|
||||
end Point,
|
||||
Point start,
|
||||
Point end,
|
||||
}
|
||||
|
||||
pub fn void test_struct_nested() {
|
||||
pub void test_struct_nested() {
|
||||
let l = Line {
|
||||
start: Point { x: 0, y: 0 },
|
||||
end: Point { x: 5, y: 10 },
|
||||
@@ -38,7 +38,7 @@ pub fn void test_struct_nested() {
|
||||
assert_eq!(l.end.y, 10);
|
||||
}
|
||||
|
||||
pub fn void run_tests() {
|
||||
pub void run_tests() {
|
||||
test_struct_create_access();
|
||||
test_struct_mut_field();
|
||||
test_struct_destructure();
|
||||
|
||||
@@ -1,41 +1,41 @@
|
||||
pub module trait_test;
|
||||
|
||||
trait Speak {
|
||||
fn String speak(*self);
|
||||
String speak(&self);
|
||||
}
|
||||
|
||||
trait Greet : Speak {
|
||||
fn String greet(*self);
|
||||
String greet(&self);
|
||||
}
|
||||
|
||||
struct Dog {
|
||||
name String,
|
||||
String name,
|
||||
}
|
||||
|
||||
impl Speak for Dog {
|
||||
fn String speak(*self) {
|
||||
String speak(&self) {
|
||||
"woof".to_string()
|
||||
}
|
||||
}
|
||||
|
||||
impl Greet for Dog {
|
||||
fn String greet(*self) {
|
||||
String greet(&self) {
|
||||
"hello".to_string()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn void test_trait_impl() {
|
||||
pub void test_trait_impl() {
|
||||
let d = Dog { name: "Rex".to_string() };
|
||||
assert_eq!(d.speak(), "woof");
|
||||
}
|
||||
|
||||
pub fn void test_trait_with_requirement() {
|
||||
pub void test_trait_with_requirement() {
|
||||
let d = Dog { name: "Buddy".to_string() };
|
||||
assert_eq!(d.speak(), "woof");
|
||||
assert_eq!(d.greet(), "hello");
|
||||
}
|
||||
|
||||
pub fn void run_tests() {
|
||||
pub void run_tests() {
|
||||
test_trait_impl();
|
||||
test_trait_with_requirement();
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
fn void main() {
|
||||
void main() {
|
||||
println!("running common tests...");
|
||||
common::test_package();
|
||||
common::expr_test::run_tests();
|
||||
|
||||
Reference in New Issue
Block a user