Updated type grammar #65
This commit is contained in:
@@ -1,21 +0,0 @@
|
||||
#[derive(Debug)]
|
||||
pub struct Debuggable {
|
||||
i32 value,
|
||||
}
|
||||
|
||||
pub void test_derive_attribute() {
|
||||
let d = Debuggable { value: 42 };
|
||||
assert_eq!(d.value, 42);
|
||||
}
|
||||
|
||||
#[allow(unused_variables)]
|
||||
pub void test_allow_attribute() {
|
||||
let x = 42;
|
||||
let y = "unused";
|
||||
assert_eq!(x, 42);
|
||||
}
|
||||
|
||||
pub void run_tests() {
|
||||
test_derive_attribute();
|
||||
test_allow_attribute();
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
pub class Counter {
|
||||
pub i32 count;
|
||||
|
||||
pub constructor(i32 start) {
|
||||
self.count = start;
|
||||
}
|
||||
|
||||
pub void increment(self mut*) {
|
||||
self.count++;
|
||||
}
|
||||
|
||||
pub i32 get(self*) {
|
||||
self.count
|
||||
}
|
||||
}
|
||||
|
||||
pub void test_class_create() {
|
||||
let c = Counter::new(0);
|
||||
assert_eq!(c.get(), 0);
|
||||
}
|
||||
|
||||
pub void test_class_method() {
|
||||
let mut c = Counter::new(5);
|
||||
c.increment();
|
||||
assert_eq!(c.get(), 6);
|
||||
}
|
||||
|
||||
pub void test_class_field_access() {
|
||||
let c = Counter::new(42);
|
||||
assert_eq!(c.count, 42);
|
||||
}
|
||||
|
||||
class NamedCounter : Counter {
|
||||
pub String name;
|
||||
|
||||
pub constructor(i32 start, String name) {
|
||||
self._super = Counter::new(start);
|
||||
|
||||
self.name = name;
|
||||
}
|
||||
|
||||
pub String get_name(self*) {
|
||||
self.name.clone()
|
||||
}
|
||||
}
|
||||
|
||||
pub void test_class_inherit() {
|
||||
let nc = NamedCounter::new(10, "test".to_string());
|
||||
assert_eq!(nc.get_name(), "test");
|
||||
assert_eq!(nc.count, 10);
|
||||
}
|
||||
|
||||
pub void run_tests() {
|
||||
test_class_create();
|
||||
test_class_method();
|
||||
test_class_field_access();
|
||||
test_class_inherit();
|
||||
}
|
||||
@@ -1,60 +0,0 @@
|
||||
enum OptionInt {
|
||||
None,
|
||||
Some[i32],
|
||||
}
|
||||
|
||||
pub void test_enum_named() {
|
||||
let x = OptionInt::None;
|
||||
match (x) {
|
||||
OptionInt::None => { assert_eq!(1, 1); }
|
||||
OptionInt::Some[_] => { assert_eq!(0, 1); }
|
||||
}
|
||||
}
|
||||
|
||||
pub void test_enum_tuple() {
|
||||
let x = OptionInt::Some(42);
|
||||
match (x) {
|
||||
OptionInt::None => { assert_eq!(0, 1); }
|
||||
OptionInt::Some[v] => { assert_eq!(v, 42); }
|
||||
}
|
||||
}
|
||||
|
||||
enum Shape {
|
||||
Circle { i32 radius },
|
||||
Rect { i32 w, i32 h },
|
||||
}
|
||||
|
||||
pub void test_enum_struct() {
|
||||
let c = Shape::Circle { radius: 5 };
|
||||
match (c) {
|
||||
Shape::Circle { radius } => { assert_eq!(radius, 5); }
|
||||
Shape::Rect { .. } => { assert_eq!(0, 1); }
|
||||
}
|
||||
|
||||
let r = Shape::Rect { w: 3, h: 4 };
|
||||
match (r) {
|
||||
Shape::Circle { .. } => { assert_eq!(0, 1); }
|
||||
Shape::Rect { w, h } => { assert_eq!(w * h, 12); }
|
||||
}
|
||||
}
|
||||
|
||||
enum Color {
|
||||
Red,
|
||||
Green,
|
||||
Blue,
|
||||
}
|
||||
|
||||
pub void test_enum_eq() {
|
||||
let c = Color::Red;
|
||||
match (c) {
|
||||
Color::Red => { assert_eq!(1, 1); }
|
||||
_ => { assert_eq!(0, 1); }
|
||||
}
|
||||
}
|
||||
|
||||
pub void run_tests() {
|
||||
test_enum_named();
|
||||
test_enum_tuple();
|
||||
test_enum_struct();
|
||||
test_enum_eq();
|
||||
}
|
||||
@@ -1,64 +0,0 @@
|
||||
void greet() {
|
||||
let mut x = 1;
|
||||
x++;
|
||||
}
|
||||
|
||||
pub void test_basic_function() {
|
||||
greet();
|
||||
}
|
||||
|
||||
i32 add(i32 a, i32 b) {
|
||||
a + b
|
||||
}
|
||||
|
||||
pub void test_fn_params() {
|
||||
assert_eq!(add(2, 3), 5);
|
||||
}
|
||||
|
||||
pub i32 test_fn_return() {
|
||||
42
|
||||
}
|
||||
|
||||
pub i32 test_fn_early_return() {
|
||||
return 100;
|
||||
0
|
||||
}
|
||||
|
||||
pub i32 test_fn_mut_param(i32 mut x) {
|
||||
x = x + 10;
|
||||
x
|
||||
}
|
||||
|
||||
pub void test_method_call() {
|
||||
str* s = "hello";
|
||||
assert_eq!(s.len(), 5);
|
||||
assert_eq!(s.to_uppercase(), "HELLO");
|
||||
}
|
||||
|
||||
i32 triple(i32 x) {
|
||||
add(x, add(x, x))
|
||||
}
|
||||
|
||||
pub void test_nested_fn_calls() {
|
||||
assert_eq!(triple(5), 15);
|
||||
}
|
||||
|
||||
i32 factorial(i32 n) {
|
||||
if (n <= 1) 1
|
||||
else (n) * (factorial(n - 1))
|
||||
}
|
||||
|
||||
pub void test_recursive_fn() {
|
||||
assert_eq!(factorial(5), 120);
|
||||
}
|
||||
|
||||
pub void run_tests() {
|
||||
test_basic_function();
|
||||
test_fn_params();
|
||||
assert_eq!(test_fn_return(), 42);
|
||||
assert_eq!(test_fn_early_return(), 100);
|
||||
assert_eq!(test_fn_mut_param(5), 15);
|
||||
test_method_call();
|
||||
test_nested_fn_calls();
|
||||
test_recursive_fn();
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
struct Vector2 {
|
||||
i32 x,
|
||||
i32 y,
|
||||
}
|
||||
|
||||
impl Vector2 {
|
||||
pub i32 magnitude(self*) {
|
||||
self.x * self.x + self.y * self.y
|
||||
}
|
||||
}
|
||||
|
||||
pub void test_inherent_impl() {
|
||||
let v = Vector2 { x: 3, y: 4 };
|
||||
assert_eq!(v.magnitude(), 25);
|
||||
}
|
||||
|
||||
trait Display {
|
||||
String display(self*);
|
||||
}
|
||||
|
||||
impl Display for Vector2 {
|
||||
String display(self*) {
|
||||
format!("({}, {})", self.x, self.y)
|
||||
}
|
||||
}
|
||||
|
||||
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 Vector2 add(self*, Vector2 other) {
|
||||
Vector2 {
|
||||
x: self.x + other.x,
|
||||
y: self.y + other.y,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub void test_multiple_impls() {
|
||||
let a = Vector2 { x: 1, y: 2 };
|
||||
let b = Vector2 { x: 3, y: 4 };
|
||||
let c = a.add(b);
|
||||
assert_eq!(c.x, 4);
|
||||
assert_eq!(c.y, 6);
|
||||
}
|
||||
|
||||
pub void run_tests() {
|
||||
test_inherent_impl();
|
||||
test_trait_impl_block();
|
||||
test_multiple_impls();
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
struct Point {
|
||||
i32 x,
|
||||
i32 y,
|
||||
}
|
||||
|
||||
pub void test_struct_create_access() {
|
||||
let p = Point { x: 10, y: 20 };
|
||||
assert_eq!(p.x, 10);
|
||||
assert_eq!(p.y, 20);
|
||||
}
|
||||
|
||||
pub void test_struct_mut_field() {
|
||||
let mut p = Point { x: 1, y: 2 };
|
||||
p.x = 100;
|
||||
assert_eq!(p.x, 100);
|
||||
}
|
||||
|
||||
pub void test_struct_destructure() {
|
||||
let p = Point { x: 3, y: 4 };
|
||||
let Point { x, y } = p;
|
||||
assert_eq!(x, 3);
|
||||
assert_eq!(y, 4);
|
||||
}
|
||||
|
||||
struct Line {
|
||||
Point start,
|
||||
Point end,
|
||||
}
|
||||
|
||||
pub void test_struct_nested() {
|
||||
let l = Line {
|
||||
start: Point { x: 0, y: 0 },
|
||||
end: Point { x: 5, y: 10 },
|
||||
};
|
||||
assert_eq!(l.start.x, 0);
|
||||
assert_eq!(l.end.y, 10);
|
||||
}
|
||||
|
||||
pub void run_tests() {
|
||||
test_struct_create_access();
|
||||
test_struct_mut_field();
|
||||
test_struct_destructure();
|
||||
test_struct_nested();
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
trait Speak {
|
||||
String speak(self*);
|
||||
}
|
||||
|
||||
struct Dog {
|
||||
String name,
|
||||
}
|
||||
|
||||
impl Speak for Dog {
|
||||
String speak(self*) {
|
||||
"woof".to_string()
|
||||
}
|
||||
}
|
||||
|
||||
pub void test_trait_impl() {
|
||||
let d = Dog { name: "Rex".to_string() };
|
||||
assert_eq!(d.speak(), "woof");
|
||||
}
|
||||
|
||||
trait Greet : Speak {
|
||||
String greet(self*);
|
||||
}
|
||||
|
||||
impl Greet for Dog {
|
||||
String greet(self*) {
|
||||
"hello".to_string()
|
||||
}
|
||||
}
|
||||
|
||||
pub void test_trait_with_requirement() {
|
||||
let d = Dog { name: "Buddy".to_string() };
|
||||
assert_eq!(d.speak(), "woof");
|
||||
assert_eq!(d.greet(), "hello");
|
||||
}
|
||||
|
||||
pub void run_tests() {
|
||||
test_trait_impl();
|
||||
test_trait_with_requirement();
|
||||
}
|
||||
Reference in New Issue
Block a user