Skip to content

Commit

Permalink
Merge pull request #42 from sinproject-iwasaki/41-rstest
Browse files Browse the repository at this point in the history
rstest #41
  • Loading branch information
sinproject-iwasaki authored Mar 6, 2024
2 parents aa37539 + 04e7f80 commit b265e43
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 52 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"msvc",
"Noto",
"rngs",
"rstest",
"rustflags",
"rustfmt",
"Tetris",
Expand Down
140 changes: 140 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ opt-level = 3
[dependencies]
bevy = { version = "0.13.0", features = ["dynamic_linking"] }
rand = "0.8.5"

[dev-dependencies]
rstest = "0.18.2"
73 changes: 21 additions & 52 deletions src/block_pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,57 +83,26 @@ impl BlockPatterns {
#[cfg(test)]
mod tests {
use super::*;

struct PatternTest {
pattern: fn() -> BlockPattern,
expected_color: Color,
expected_positions: Vec<(i32, i32)>,
}

#[test]
fn test_block_patterns() {
let tests = vec![
PatternTest {
pattern: BlockPattern::i,
expected_color: Color::rgb_u8(110, 237, 240),
expected_positions: vec![(0, 0), (0, -1), (0, 1), (0, 2)],
},
PatternTest {
pattern: BlockPattern::l,
expected_color: Color::rgb_u8(230, 163, 58),
expected_positions: vec![(0, 0), (0, -1), (0, 1), (-1, 1)],
},
PatternTest {
pattern: BlockPattern::j,
expected_color: Color::rgb_u8(0, 0, 232),
expected_positions: vec![(0, 0), (0, -1), (0, 1), (1, 1)],
},
PatternTest {
pattern: BlockPattern::z,
expected_color: Color::rgb_u8(220, 48, 33),
expected_positions: vec![(0, 0), (0, -1), (1, 0), (1, 1)],
},
PatternTest {
pattern: BlockPattern::s,
expected_color: Color::rgb_u8(110, 237, 72),
expected_positions: vec![(0, 0), (1, 0), (0, 1), (1, -1)],
},
PatternTest {
pattern: BlockPattern::o,
expected_color: Color::rgb_u8(243, 241, 80),
expected_positions: vec![(0, 0), (0, 1), (1, 0), (1, 1)],
},
PatternTest {
pattern: BlockPattern::t,
expected_color: Color::rgb_u8(148, 29, 230),
expected_positions: vec![(0, 0), (-1, 0), (1, 0), (0, 1)],
},
];

for test in tests {
let pattern = (test.pattern)();
assert_eq!(pattern.color, test.expected_color);
assert_eq!(pattern.positions, test.expected_positions);
}
use rstest::rstest;

#[rstest]
#[case::i(BlockPattern::i(), Color::rgb_u8(110, 237, 240), vec![(0, 0), (0, -1), (0, 1), (0, 2)])]
#[case::l(BlockPattern::l(), Color::rgb_u8(230, 163, 58), vec![(0, 0), (0, -1), (0, 1), (-1, 1)])]
#[case::j(BlockPattern::j(), Color::rgb_u8(0, 0, 232), vec![(0, 0), (0, -1), (0, 1), (1, 1)])]
#[case::z(BlockPattern::z(), Color::rgb_u8(220, 48, 33), vec![(0, 0), (0, -1), (1, 0), (1, 1)])]
#[case::s(BlockPattern::s(), Color::rgb_u8(110, 237, 72), vec![(0, 0), (1, 0), (0, 1), (1, -1)])]
#[case::o(BlockPattern::o(), Color::rgb_u8(243, 241, 80), vec![(0, 0), (0, 1), (1, 0), (1, 1)])]
#[case::t(BlockPattern::t(), Color::rgb_u8(148, 29, 230), vec![(0, 0), (-1, 0), (1, 0), (0, 1)])]
#[should_panic]
#[case::color_mismatch(BlockPattern::l(), Color::rgb_u8(110, 237, 240), vec![(0, 0), (0, -1), (0, 1), (-1, 1)])]
#[should_panic]
#[case::positions_mismatch(BlockPattern::l(), Color::rgb_u8(230, 163, 58), vec![(0, 0), (0, -1), (1, 1), (-1, 1)])]
fn test_block_patterns(
#[case] pattern: BlockPattern,
#[case] expected_color: Color,
#[case] expected_positions: Vec<(i32, i32)>,
) {
assert_eq!(pattern.color, expected_color);
assert_eq!(pattern.positions, expected_positions)
}
}

0 comments on commit b265e43

Please sign in to comment.