Skip to content

Commit 9617bc5

Browse files
committed
More cleanups.
1 parent e1be08d commit 9617bc5

File tree

1 file changed

+22
-24
lines changed

1 file changed

+22
-24
lines changed

src/tester.rs

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,10 @@ impl<G: Gen> QuickCheck<G> {
7272
if ntests >= self.tests {
7373
break
7474
}
75-
let r = f.result(&mut self.gen);
76-
match r.status {
77-
Pass => ntests += 1,
78-
Discard => continue,
79-
Fail => return Err(r),
75+
match f.result(&mut self.gen) {
76+
TestResult { status: Pass, .. } => ntests += 1,
77+
TestResult { status: Discard, .. } => continue,
78+
r @ TestResult { status: Fail, .. } => return Err(r),
8079
}
8180
}
8281
Ok(ntests)
@@ -128,7 +127,7 @@ pub fn quickcheck<A: Testable>(f: A) { QuickCheck::new().quickcheck(f) }
128127
pub struct TestResult {
129128
status: Status,
130129
arguments: Vec<String>,
131-
err: String,
130+
err: Option<String>,
132131
}
133132

134133
/// Whether a test has passed, failed or been discarded.
@@ -142,11 +141,10 @@ impl TestResult {
142141
/// Produces a test result that indicates the current test has failed.
143142
pub fn failed() -> TestResult { TestResult::from_bool(false) }
144143

145-
/// Produces a test result that indicates failure from a runtime
146-
/// error.
147-
pub fn error(msg: &str) -> TestResult {
144+
/// Produces a test result that indicates failure from a runtime error.
145+
pub fn error<S: Into<String>>(msg: S) -> TestResult {
148146
let mut r = TestResult::from_bool(false);
149-
r.err = msg.to_string();
147+
r.err = Some(msg.into());
150148
r
151149
}
152150

@@ -158,7 +156,7 @@ impl TestResult {
158156
TestResult {
159157
status: Discard,
160158
arguments: vec![],
161-
err: "".to_string(),
159+
err: None,
162160
}
163161
}
164162

@@ -169,7 +167,7 @@ impl TestResult {
169167
TestResult {
170168
status: if b { Pass } else { Fail },
171169
arguments: vec![],
172-
err: "".to_string(),
170+
err: None,
173171
}
174172
}
175173

@@ -197,19 +195,20 @@ impl TestResult {
197195
/// Returns `true` if and only if this test result describes a failing
198196
/// test as a result of a run time error.
199197
pub fn is_error(&self) -> bool {
200-
self.is_failure() && self.err.len() > 0
198+
self.is_failure() && self.err.is_some()
201199
}
202200

203201
fn failed_msg(&self) -> String {
204-
if self.err.len() == 0 {
205-
format!(
206-
"[quickcheck] TEST FAILED. Arguments: ({})",
207-
self.arguments.connect(", "))
208-
} else {
209-
format!(
210-
"[quickcheck] TEST FAILED (runtime error). \
211-
Arguments: ({})\nError: {}",
212-
self.arguments.connect(", "), self.err)
202+
match self.err {
203+
None => {
204+
format!("[quickcheck] TEST FAILED. Arguments: ({})",
205+
self.arguments.connect(", "))
206+
}
207+
Some(ref err) => {
208+
format!("[quickcheck] TEST FAILED (runtime error). \
209+
Arguments: ({})\nError: {}",
210+
self.arguments.connect(", "), err)
211+
}
213212
}
214213
}
215214
}
@@ -365,7 +364,7 @@ impl<A, B, C, D, T> Fun<A, B, C, D, T> for fn(A, B, C, D) -> T
365364
fn shrink<G, T, A, B, C, D, F>(g: &mut G, fun: &F) -> TestResult
366365
where G: Gen, T: Testable, A: AShow, B: AShow, C: AShow, D: AShow,
367366
F: Fun<A, B, C, D, T> {
368-
let (a, b, c, d): (A, B, C, D) = arby(g);
367+
let (a, b, c, d): (A, B, C, D) = Arbitrary::arbitrary(g);
369368
let r = fun.call(g, Some(&a), Some(&b), Some(&c), Some(&d));
370369
match r.status {
371370
Pass|Discard => r,
@@ -416,4 +415,3 @@ fn safe<T, F>(fun: F) -> Result<T, String>
416415
/// Convenient aliases.
417416
trait AShow : Arbitrary + Debug {}
418417
impl<A: Arbitrary + Debug> AShow for A {}
419-
fn arby<A: Arbitrary, G: Gen>(g: &mut G) -> A { Arbitrary::arbitrary(g) }

0 commit comments

Comments
 (0)