Skip to content

Commit 07f48a5

Browse files
committed
Add tests for cross-crate condition handling. Close #5446.
1 parent 6e30db4 commit 07f48a5

8 files changed

+215
-0
lines changed

src/test/auxiliary/xc_conditions.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#[crate_type="lib"];
12+
13+
condition! {
14+
pub oops: int -> int;
15+
}
16+
17+
pub fn trouble() -> int {
18+
oops::cond.raise(1)
19+
}

src/test/auxiliary/xc_conditions_2.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#[crate_type="lib"];
12+
13+
condition! {
14+
pub oops: int -> int;
15+
}

src/test/auxiliary/xc_conditions_3.rs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#[crate_type="lib"];
12+
13+
condition! {
14+
pub oops: int -> int;
15+
}
16+
17+
pub fn guard(k: extern fn() -> int, x: int) -> int {
18+
do oops::cond.trap(|i| i*x).inside {
19+
k()
20+
}
21+
}

src/test/auxiliary/xc_conditions_4.rs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#[crate_type="lib"];
12+
13+
#[deriving(Eq)]
14+
pub enum Color {
15+
Red, Green, Blue
16+
}
17+
18+
condition! {
19+
pub oops: (int,float,~str) -> ::Color;
20+
}
21+
22+
pub trait Thunk<T> {
23+
fn call(self) -> T;
24+
}
25+
26+
pub fn callback<T,TH:Thunk<T>>(t:TH) -> T {
27+
t.call()
28+
}
29+
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// xfail-fast
12+
// aux-build:xc_conditions.rs
13+
14+
extern mod xc_conditions;
15+
use xc_conditions::oops;
16+
use xc_conditions::trouble;
17+
18+
// Tests of cross-crate conditions; the condition is
19+
// defined in lib, and we test various combinations
20+
// of `trap` and `raise` in the client or the lib where
21+
// the condition was defined. Also in test #4 we use
22+
// more complex features (generics, traits) in
23+
// combination with the condition.
24+
//
25+
// trap raise
26+
// ------------
27+
// xc_conditions : client lib
28+
// xc_conditions_2: client client
29+
// xc_conditions_3: lib client
30+
// xc_conditions_4: client client (with traits)
31+
//
32+
// the trap=lib, raise=lib case isn't tested since
33+
// there's no cross-crate-ness to test in that case.
34+
35+
pub fn main() {
36+
do oops::cond.trap(|_i| 12345).inside {
37+
let x = trouble();
38+
assert_eq!(x,12345);
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// xfail-fast
12+
// aux-build:xc_conditions_2.rs
13+
14+
extern mod xc_conditions_2;
15+
use xcc = xc_conditions_2;
16+
17+
pub fn main() {
18+
do xcc::oops::cond.trap(|_| 1).inside {
19+
xcc::oops::cond.raise(1);
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// xfail-fast
12+
// aux-build:xc_conditions_3.rs
13+
14+
extern mod xc_conditions_3;
15+
use xcc = xc_conditions_3;
16+
17+
pub fn main() {
18+
assert_eq!(xcc::guard(a, 1), 40);
19+
}
20+
21+
pub fn a() -> int {
22+
assert_eq!(xcc::oops::cond.raise(7), 7);
23+
xcc::guard(b, 2)
24+
}
25+
26+
pub fn b() -> int {
27+
assert_eq!(xcc::oops::cond.raise(8), 16);
28+
xcc::guard(c, 3)
29+
}
30+
31+
pub fn c() -> int {
32+
assert_eq!(xcc::oops::cond.raise(9), 18);
33+
xcc::guard(d, 4)
34+
}
35+
36+
pub fn d() -> int {
37+
xcc::oops::cond.raise(10)
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// xfail-fast
12+
// aux-build:xc_conditions_4.rs
13+
14+
extern mod xc_conditions_4;
15+
use xcc = xc_conditions_4;
16+
17+
struct SThunk {
18+
x: int
19+
}
20+
21+
impl xcc::Thunk<xcc::Color> for SThunk {
22+
fn call(self) -> xcc::Color {
23+
xcc::oops::cond.raise((self.x, 1.23, ~"oh no"))
24+
}
25+
}
26+
27+
pub fn main() {
28+
do xcc::oops::cond.trap(|_| xcc::Red).inside {
29+
let t = SThunk { x : 10 };
30+
assert_eq!(xcc::callback(t), xcc::Red)
31+
}
32+
}

0 commit comments

Comments
 (0)