Skip to content

Commit 97c5d27

Browse files
authored
Merge pull request #394 from coriolinus/perfect-numbers-non-table-tests
perfect-numbers: Update tests from table-based to macro-based
2 parents 5d9cc23 + 42e4d94 commit 97c5d27

File tree

4 files changed

+55
-30
lines changed

4 files changed

+55
-30
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ Note that:
9696

9797
- If the test suite is appreciably sped up by running in release mode, and there is reason to be confident that the example implementation does not contain any overflow errors, consider adding a file `.meta/test-in-release-mode`. This should contain brief comments explaining the situation.
9898

99+
- If your exercise implements macro-based testing (see [#392](https://github.com/exercism/rust/issues/392#issuecomment-343865993) and [`perfect-numbers.rs`](https://github.com/exercism/rust/blob/master/exercises/perfect-numbers/tests/perfect-numbers.rs)), you will likely run afoul of a CI check which counts the `#[ignore]` lines and compares the result to the number of `#[test]` lines. To fix this, add a file `.meta/ignore-count-ignores` to disable that check for your exercise.
100+
99101
- `README.md` may be [regenerated](https://github.com/exercism/docs/blob/master/maintaining-a-track/regenerating-exercise-readmes.md) from Exercism data. The generator will use the `description.md` from the exercise directory in the [problem-specifications repository](https://github.com/exercism/problem-specifications/tree/master/exercises), then any hints in `.meta/hints.md`, then the [Rust-specific instructions](https://github.com/exercism/rust/blob/master/config/exercise-readme-insert.md). The `## Source` section comes from the `metadata.yml` in the same directory. Convention is that the description of the source remains text and the link is both name and hyperlink of the markdown link.
100102

101103
- Be sure to add the exercise to an appropriate place in the `config.json` file. The position in the file determines the order exercises are sent. Generate a unique UUID for the exercise. Current difficuly levels in use are 1, 4, 7 and 10.

_test/count-ignores.sh

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,21 @@
33
repo=$(cd "$(dirname "$0")/.." && pwd)
44
exitcode=0
55

6-
for t in $repo/exercises/*/tests/*.rs; do
7-
tests=$(grep "^\s*\#\[test\]" $t | wc -l | tr -d '[:space:]')
8-
ignores=$(grep "^\s*\#\[ignore\]" $t | wc -l | tr -d '[:space:]')
9-
want_ignores=$(expr $tests - 1)
10-
if [ "$ignores" != "$want_ignores" ]; then
11-
echo "\033[1;31m$t: Has $tests tests and $ignores ignores (should be $want_ignores)\033[0m"
12-
exitcode=1
13-
fi
6+
for e in $repo/exercises/*; do
7+
if [ -f "$e/.meta/ignore-count-ignores" ]; then
8+
continue
9+
fi
10+
if [ -d "$e/tests" ]; then
11+
for t in $e/tests/*.rs; do
12+
tests=$(grep "^\s*\#\[test\]" $t | wc -l | tr -d '[:space:]')
13+
ignores=$(grep "^\s*\#\[ignore\]" $t | wc -l | tr -d '[:space:]')
14+
want_ignores=$(expr $tests - 1)
15+
if [ "$ignores" != "$want_ignores" ]; then
16+
echo "\033[1;31m$t: Has $tests tests and $ignores ignores (should be $want_ignores)\033[0m"
17+
exitcode=1
18+
fi
19+
done
20+
fi
1421
done
1522

1623
exit $exitcode
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Perfect numbers has tests generated by macro.
2+
This breaks the count-ignores.sh script:
3+
4+
```text
5+
exercises/perfect-numbers/tests/perfect-numbers.rs: Has 2 tests and 11 ignores (should be 1)
6+
```

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

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,42 @@ 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] test_1(1, Classification::Deficient);
32+
#[ignore] test_2(2, Classification::Deficient);
33+
#[ignore] test_4(4, Classification::Deficient);
34+
#[ignore] test_6(6, Classification::Perfect);
35+
#[ignore] test_12(12, Classification::Abundant);
36+
#[ignore] test_28(28, Classification::Perfect);
37+
#[ignore] test_30(30, Classification::Abundant);
38+
#[ignore] test_32(32, Classification::Deficient);
39+
#[ignore] test_33550335(33550335, Classification::Abundant);
40+
#[ignore] test_33550336(33550336, Classification::Perfect);
41+
#[ignore] test_33550337(33550337, Classification::Deficient);
3242
}
3343
}

0 commit comments

Comments
 (0)