test: Simulate abstract methods with template modules
This commit is contained in:
parent
611061b6c3
commit
658b6a741b
5 changed files with 74 additions and 0 deletions
14
src/test/run-pass/module-polymorphism4-files/cat.rs
Normal file
14
src/test/run-pass/module-polymorphism4-files/cat.rs
Normal 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" }
|
||||
}
|
||||
}
|
9
src/test/run-pass/module-polymorphism4-files/dog.rs
Normal file
9
src/test/run-pass/module-polymorphism4-files/dog.rs
Normal file
|
@ -0,0 +1,9 @@
|
|||
type T = dog;
|
||||
|
||||
enum dog {
|
||||
dog
|
||||
}
|
||||
|
||||
fn animal() -> str { "dog" }
|
||||
fn talk(_d: dog) -> str { "woof" }
|
||||
|
9
src/test/run-pass/module-polymorphism4-files/trait.rs
Normal file
9
src/test/run-pass/module-polymorphism4-files/trait.rs
Normal 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) + "'"
|
||||
}
|
||||
|
||||
}
|
28
src/test/run-pass/module-polymorphism4.rc
Normal file
28
src/test/run-pass/module-polymorphism4.rc
Normal 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;
|
||||
|
||||
}
|
14
src/test/run-pass/module-polymorphism4.rs
Normal file
14
src/test/run-pass/module-polymorphism4.rs
Normal 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'";
|
||||
}
|
Loading…
Reference in a new issue