Skip to content

Commit 04af43f

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 04af43f

File tree

1 file changed

+43
-22
lines changed

1 file changed

+43
-22
lines changed

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

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,53 @@ 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+
$property_test_func($( $param ),* )
14+
}
15+
)+
16+
}
17+
}
18+
19+
fn test_classification(num: u64, result: Classification) {
20+
assert_eq!(classify(num), Ok(result));
21+
}
22+
523
#[test]
624
fn basic() {
725
assert_eq!(classify(0), Err("Number must be positive"));
826
}
927

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));
28+
29+
tests! {
30+
test_classification {
31+
#[ignore]
32+
test_1(1, Classification::Deficient);
33+
#[ignore]
34+
test_2(2, Classification::Deficient);
35+
#[ignore]
36+
test_4(4, Classification::Deficient);
37+
#[ignore]
38+
test_6(6, Classification::Perfect);
39+
#[ignore]
40+
test_12(12, Classification::Abundant);
41+
#[ignore]
42+
test_28(28, Classification::Perfect);
43+
#[ignore]
44+
test_30(30, Classification::Abundant);
45+
#[ignore]
46+
test_32(32, Classification::Deficient);
47+
#[ignore]
48+
test_33550335(33550335, Classification::Abundant);
49+
#[ignore]
50+
test_33550336(33550336, Classification::Perfect);
51+
#[ignore]
52+
test_33550337(33550337, Classification::Deficient);
3253
}
3354
}

0 commit comments

Comments
 (0)