Skip to content

Commit 7abce35

Browse files
committed
perfect-numbers: Update tests from table-based to macro-based
This PR is in response to #392. Perfect-numbers currently uses table-based testing. For reasons outlined in #392 (comment) I believe that table-based testing is not the appropriate way to do things. This is what macro-based tests might look like. Are they abstruse and opaque? Yes. However, they at least become individual tests, and I believe that this is _better than the status quo_.
1 parent 8a0ac5b commit 7abce35

File tree

1 file changed

+44
-22
lines changed

1 file changed

+44
-22
lines changed

exercises/perfect-numbers/tests/perfect-numbers.rs

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,54 @@ extern crate perfect_numbers;
22

33
use perfect_numbers::{Classification, classify};
44

5+
macro_rules! tests {
6+
($property_test_func:ident {
7+
$( $(#[$attr:meta])* $test_name:ident( $( $param:expr ),* ); )+
8+
}) => {
9+
$(
10+
$(#[$attr])*
11+
#[test]
12+
fn $test_name() {
13+
println!("Entering test");
14+
$property_test_func($( $param ),* )
15+
}
16+
)+
17+
}
18+
}
19+
20+
fn test_classification(num: u64, result: Classification) {
21+
assert_eq!(classify(num), Ok(result));
22+
}
23+
524
#[test]
625
fn basic() {
726
assert_eq!(classify(0), Err("Number must be positive"));
827
}
928

10-
#[test]
11-
#[ignore]
12-
fn test_all() {
13-
struct TestClassification {
14-
num: u64,
15-
result: perfect_numbers::Classification
16-
}
17-
let test_table: Vec<TestClassification> = vec![
18-
TestClassification { num: 6, result: Classification::Perfect },
19-
TestClassification { num: 28, result: Classification::Perfect },
20-
TestClassification { num: 33550336, result: Classification::Perfect },
21-
TestClassification { num: 12, result: Classification::Abundant },
22-
TestClassification { num: 30, result: Classification::Abundant },
23-
TestClassification { num: 33550335, result: Classification::Abundant },
24-
TestClassification { num: 2, result: Classification::Deficient },
25-
TestClassification { num: 4, result: Classification::Deficient },
26-
TestClassification { num: 32, result: Classification::Deficient },
27-
TestClassification { num: 33550337, result: Classification::Deficient },
28-
TestClassification { num: 1, result: Classification::Deficient },
29-
];
30-
for t in test_table {
31-
assert_eq!(classify(t.num), Ok(t.result));
29+
30+
tests! {
31+
test_classification {
32+
#[ignore]
33+
test_1(1, Classification::Deficient);
34+
#[ignore]
35+
test_2(2, Classification::Deficient);
36+
#[ignore]
37+
test_4(4, Classification::Deficient);
38+
#[ignore]
39+
test_6(6, Classification::Perfect);
40+
#[ignore]
41+
test_12(12, Classification::Abundant);
42+
#[ignore]
43+
test_28(28, Classification::Perfect);
44+
#[ignore]
45+
test_30(30, Classification::Abundant);
46+
#[ignore]
47+
test_32(32, Classification::Deficient);
48+
#[ignore]
49+
test_33550335(33550335, Classification::Abundant);
50+
#[ignore]
51+
test_33550336(33550336, Classification::Perfect);
52+
#[ignore]
53+
test_33550337(33550337, Classification::Deficient);
3254
}
3355
}

0 commit comments

Comments
 (0)