Skip to content

Commit 658b6a7

Browse files
committed
test: Simulate abstract methods with template modules
1 parent 611061b commit 658b6a7

File tree

5 files changed

+74
-0
lines changed

5 files changed

+74
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
type T = cat;
2+
3+
enum cat {
4+
howlycat,
5+
meowlycat
6+
}
7+
8+
fn animal() -> str { "cat" }
9+
fn talk(c: cat) -> str {
10+
alt c {
11+
howlycat { "howl" }
12+
meowlycat { "meow" }
13+
}
14+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
type T = dog;
2+
3+
enum dog {
4+
dog
5+
}
6+
7+
fn animal() -> str { "dog" }
8+
fn talk(_d: dog) -> str { "woof" }
9+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
impl talky for T {
2+
3+
// 'animal' and 'talk' functions are implemented by the module
4+
// instantiating the talky trait. They are 'abstract'
5+
fn says() -> str {
6+
animal() + " says '" + talk(self) + "'"
7+
}
8+
9+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#[no_core];
2+
3+
4+
#[path = "module-polymorphism4-files"]
5+
mod cat {
6+
7+
import inst::*;
8+
9+
#[path = "cat.rs"]
10+
mod inst;
11+
12+
#[path = "trait.rs"]
13+
mod trait;
14+
15+
}
16+
17+
#[path = "module-polymorphism4-files"]
18+
mod dog {
19+
20+
import inst::*;
21+
22+
#[path = "dog.rs"]
23+
mod inst;
24+
25+
#[path = "trait.rs"]
26+
mod trait;
27+
28+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// This isn't really xfailed; it's used by the
2+
// module-polymorphism.rc test
3+
// xfail-test
4+
5+
fn main() {
6+
import cat::trait::talky;
7+
import dog::trait::talky;
8+
let cat1 = cat::inst::meowlycat;
9+
let cat2 = cat::inst::howlycat;
10+
let dog = dog::inst::dog;
11+
assert cat1.says() == "cat says 'meow'";
12+
assert cat2.says() == "cat says 'howl'";
13+
assert dog.says() == "dog says 'woof'";
14+
}

0 commit comments

Comments
 (0)