Sanity-check the code examples that appear in the object system docs.
This commit is contained in:
parent
6402b63b4f
commit
f2e22b2e8e
1 changed files with 66 additions and 0 deletions
66
src/test/run-pass/obj-docs.rs
Normal file
66
src/test/run-pass/obj-docs.rs
Normal file
|
@ -0,0 +1,66 @@
|
|||
// Sanity-check the code examples that appear in the object system
|
||||
// documentation.
|
||||
|
||||
fn main() {
|
||||
|
||||
// Ref.Item.Obj
|
||||
obj counter(state: @mutable int) {
|
||||
fn incr() {
|
||||
*state += 1;
|
||||
}
|
||||
fn get() -> int {
|
||||
ret *state;
|
||||
}
|
||||
}
|
||||
|
||||
let c: counter = counter(@mutable 1);
|
||||
|
||||
c.incr();
|
||||
c.incr();
|
||||
assert c.get() == 3;
|
||||
|
||||
obj my_obj() {
|
||||
fn get() -> int {
|
||||
ret 3;
|
||||
}
|
||||
fn foo() -> int{
|
||||
let c = self.get();
|
||||
ret c + 2; // returns 5
|
||||
}
|
||||
}
|
||||
|
||||
let o = my_obj();
|
||||
assert o.foo() == 5;
|
||||
|
||||
// Ref.Type.Obj
|
||||
type taker = obj {
|
||||
fn take(int);
|
||||
};
|
||||
|
||||
obj adder(x: @mutable int) {
|
||||
fn take(y: int) {
|
||||
*x += y;
|
||||
}
|
||||
}
|
||||
|
||||
obj sender(c: chan[int]) {
|
||||
fn take(z: int) {
|
||||
c <| z;
|
||||
}
|
||||
}
|
||||
|
||||
fn give_ints(t: taker) {
|
||||
t.take(1);
|
||||
t.take(2);
|
||||
t.take(3);
|
||||
}
|
||||
|
||||
let p: port[int] = port();
|
||||
|
||||
let t1: taker = adder(@mutable 0);
|
||||
let t2: taker = sender(chan(p));
|
||||
|
||||
give_ints(t1);
|
||||
give_ints(t2);
|
||||
}
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue