146 lines
2.2 KiB
Plaintext
146 lines
2.2 KiB
Plaintext
pub module class_test;
|
|
|
|
pub enum AnimalType {
|
|
Mammal;
|
|
}
|
|
|
|
pub class Animal
|
|
{
|
|
pub String name;
|
|
|
|
constructor()
|
|
{
|
|
self.name = "Rex".to_string();
|
|
}
|
|
|
|
pub virtual AnimalType get_animal_type(&self) {
|
|
panic!()
|
|
}
|
|
|
|
pub virtual String speak(&self)
|
|
{
|
|
"Unknown".to_string()
|
|
}
|
|
|
|
pub void test_speak<'a>(&'a self)
|
|
{
|
|
assert_eq!(self.speak(), "Woof!");
|
|
}
|
|
|
|
pub void static_method()
|
|
{
|
|
}
|
|
}
|
|
|
|
pub class Mammal : Animal
|
|
{
|
|
constructor()
|
|
{
|
|
super = Super::new();
|
|
}
|
|
|
|
// override without a target assumes current super
|
|
pub AnimalType get_animal_type(&self) override {
|
|
AnimalType::Mammal
|
|
}
|
|
|
|
pub void args_test(&self, i32 x, str& y)
|
|
{
|
|
assert_eq!(x, 10);
|
|
assert_eq!(y, "hello");
|
|
}
|
|
}
|
|
|
|
pub class Dog : Mammal
|
|
{
|
|
constructor()
|
|
{
|
|
super = Super::new();
|
|
}
|
|
|
|
pub String speak(&self) override(Animal)
|
|
{
|
|
"Woof!".to_string()
|
|
}
|
|
|
|
impl std::fmt::Display
|
|
{
|
|
Result<(), std::fmt::Error> fmt(&self, std::fmt::Formatter<'_> mut& f)
|
|
{
|
|
write!(f, "🐾 {}", self.name)
|
|
}
|
|
}
|
|
}
|
|
|
|
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()
|
|
{
|
|
let dawg = Dog::new();
|
|
dawg.args_test(10, "hello");
|
|
dawg.test_speak();
|
|
dawg.get_animal_type();
|
|
assert_eq!(format!("{dawg}"), "🐾 Rex");
|
|
test_class_no_inheritance();
|
|
test_class_field_default();
|
|
test_class_empty();
|
|
}
|