test: Simulate abstract methods with template modules

This commit is contained in:
Brian Anderson 2012-04-14 02:14:05 -07:00
parent 611061b6c3
commit 658b6a741b
5 changed files with 74 additions and 0 deletions

View file

@ -0,0 +1,14 @@
type T = cat;
enum cat {
howlycat,
meowlycat
}
fn animal() -> str { "cat" }
fn talk(c: cat) -> str {
alt c {
howlycat { "howl" }
meowlycat { "meow" }
}
}

View file

@ -0,0 +1,9 @@
type T = dog;
enum dog {
dog
}
fn animal() -> str { "dog" }
fn talk(_d: dog) -> str { "woof" }

View file

@ -0,0 +1,9 @@
impl talky for T {
// 'animal' and 'talk' functions are implemented by the module
// instantiating the talky trait. They are 'abstract'
fn says() -> str {
animal() + " says '" + talk(self) + "'"
}
}

View file

@ -0,0 +1,28 @@
#[no_core];
#[path = "module-polymorphism4-files"]
mod cat {
import inst::*;
#[path = "cat.rs"]
mod inst;
#[path = "trait.rs"]
mod trait;
}
#[path = "module-polymorphism4-files"]
mod dog {
import inst::*;
#[path = "dog.rs"]
mod inst;
#[path = "trait.rs"]
mod trait;
}

View file

@ -0,0 +1,14 @@
// This isn't really xfailed; it's used by the
// module-polymorphism.rc test
// xfail-test
fn main() {
import cat::trait::talky;
import dog::trait::talky;
let cat1 = cat::inst::meowlycat;
let cat2 = cat::inst::howlycat;
let dog = dog::inst::dog;
assert cat1.says() == "cat says 'meow'";
assert cat2.says() == "cat says 'howl'";
assert dog.says() == "dog says 'woof'";
}