Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync queen with problem-specifications #1818

Merged
merged 1 commit into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions exercises/practice/queen-attack/.meta/test_template.tera
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use queen_attack::{ChessPiece, ChessPosition, Queen};

{% for test in cases %}
#[test]
{% if loop.index != 1 -%}
#[ignore]
{% endif -%}
fn {{ test.description | slugify | replace(from="-", to="_") }}() {
{% if test.property == "create" %}
let chess_position = ChessPosition::new({{ test.input.queen.position.row }}, {{ test.input.queen.position.column }});
{%- if test.expected is object %}
assert!(chess_position.is_none());
{% else %}
assert!(chess_position.is_some());
{% endif -%}
{% else %}
let white_queen = Queen::new(ChessPosition::new({{ test.input.white_queen.position.row }}, {{ test.input.white_queen.position.column }}).unwrap());
let black_queen = Queen::new(ChessPosition::new({{ test.input.black_queen.position.row }}, {{ test.input.black_queen.position.column }}).unwrap());
{%- if test.expected %}
assert!(white_queen.can_attack(&black_queen));
{% else %}
assert!(!white_queen.can_attack(&black_queen));
{% endif -%}
{% endif %}
}
{% endfor -%}
52 changes: 49 additions & 3 deletions exercises/practice/queen-attack/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,49 @@
# This is an auto-generated file. Regular comments will be removed when this
# file is regenerated. Regenerating will not touch any manually added keys,
# so comments can be added in a "comment" key.
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[3ac4f735-d36c-44c4-a3e2-316f79704203]
description = "Test creation of Queens with valid and invalid positions -> queen with a valid position"

[4e812d5d-b974-4e38-9a6b-8e0492bfa7be]
description = "Test creation of Queens with valid and invalid positions -> queen must have positive row"

[f07b7536-b66b-4f08-beb9-4d70d891d5c8]
description = "Test creation of Queens with valid and invalid positions -> queen must have row on board"

[15a10794-36d9-4907-ae6b-e5a0d4c54ebe]
description = "Test creation of Queens with valid and invalid positions -> queen must have positive column"

[6907762d-0e8a-4c38-87fb-12f2f65f0ce4]
description = "Test creation of Queens with valid and invalid positions -> queen must have column on board"

[33ae4113-d237-42ee-bac1-e1e699c0c007]
description = "Test the ability of one queen to attack another -> cannot attack"

[eaa65540-ea7c-4152-8c21-003c7a68c914]
description = "Test the ability of one queen to attack another -> can attack on same row"

[bae6f609-2c0e-4154-af71-af82b7c31cea]
description = "Test the ability of one queen to attack another -> can attack on same column"

[0e1b4139-b90d-4562-bd58-dfa04f1746c7]
description = "Test the ability of one queen to attack another -> can attack on first diagonal"

[ff9b7ed4-e4b6-401b-8d16-bc894d6d3dcd]
description = "Test the ability of one queen to attack another -> can attack on second diagonal"

[0a71e605-6e28-4cc2-aa47-d20a2e71037a]
description = "Test the ability of one queen to attack another -> can attack on third diagonal"

[0790b588-ae73-4f1f-a968-dd0b34f45f86]
description = "Test the ability of one queen to attack another -> can attack on fourth diagonal"

[543f8fd4-2597-4aad-8d77-cbdab63619f8]
description = "Test the ability of one queen to attack another -> cannot attack if falling diagonals are only the same when reflected across the longest falling diagonal"
62 changes: 33 additions & 29 deletions exercises/practice/queen-attack/tests/queen-attack.rs
Original file line number Diff line number Diff line change
@@ -1,96 +1,100 @@
use queen_attack::*;

#[test]
fn chess_position_on_the_board_is_some() {
assert!(ChessPosition::new(2, 4).is_some());
fn queen_with_a_valid_position() {
let chess_position = ChessPosition::new(2, 2);
assert!(chess_position.is_some());
}

#[test]
#[ignore]
fn chess_position_off_the_board_is_none() {
assert!(ChessPosition::new(-1, 2).is_none());

assert!(ChessPosition::new(8, 2).is_none());
fn queen_must_have_positive_row() {
let chess_position = ChessPosition::new(-2, 2);
assert!(chess_position.is_none());
}

assert!(ChessPosition::new(5, -1).is_none());
#[test]
#[ignore]
fn queen_must_have_row_on_board() {
let chess_position = ChessPosition::new(8, 4);
assert!(chess_position.is_none());
}

assert!(ChessPosition::new(5, 8).is_none());
#[test]
#[ignore]
fn queen_must_have_positive_column() {
let chess_position = ChessPosition::new(2, -2);
assert!(chess_position.is_none());
}

#[test]
#[ignore]
fn queen_is_created_with_a_valid_position() {
Queen::new(ChessPosition::new(2, 4).unwrap());
fn queen_must_have_column_on_board() {
let chess_position = ChessPosition::new(4, 8);
assert!(chess_position.is_none());
}

#[test]
#[ignore]
fn queens_that_can_not_attack() {
fn cannot_attack() {
let white_queen = Queen::new(ChessPosition::new(2, 4).unwrap());
let black_queen = Queen::new(ChessPosition::new(6, 6).unwrap());

assert!(!white_queen.can_attack(&black_queen));
}

#[test]
#[ignore]
fn queens_on_the_same_rank_can_attack() {
fn can_attack_on_same_row() {
let white_queen = Queen::new(ChessPosition::new(2, 4).unwrap());
let black_queen = Queen::new(ChessPosition::new(2, 6).unwrap());

assert!(white_queen.can_attack(&black_queen));
}

#[test]
#[ignore]
fn queens_on_the_same_file_can_attack() {
fn can_attack_on_same_column() {
let white_queen = Queen::new(ChessPosition::new(4, 5).unwrap());
let black_queen = Queen::new(ChessPosition::new(3, 5).unwrap());

let black_queen = Queen::new(ChessPosition::new(2, 5).unwrap());
assert!(white_queen.can_attack(&black_queen));
}

#[test]
#[ignore]
fn queens_on_the_same_diagonal_can_attack_one() {
fn can_attack_on_first_diagonal() {
let white_queen = Queen::new(ChessPosition::new(2, 2).unwrap());
let black_queen = Queen::new(ChessPosition::new(0, 4).unwrap());

assert!(white_queen.can_attack(&black_queen));
}

#[test]
#[ignore]
fn queens_on_the_same_diagonal_can_attack_two() {
fn can_attack_on_second_diagonal() {
let white_queen = Queen::new(ChessPosition::new(2, 2).unwrap());
let black_queen = Queen::new(ChessPosition::new(3, 1).unwrap());

assert!(white_queen.can_attack(&black_queen));
}

#[test]
#[ignore]
fn queens_on_the_same_diagonal_can_attack_three() {
fn can_attack_on_third_diagonal() {
let white_queen = Queen::new(ChessPosition::new(2, 2).unwrap());
let black_queen = Queen::new(ChessPosition::new(1, 1).unwrap());

assert!(white_queen.can_attack(&black_queen));
}

#[test]
#[ignore]
fn queens_on_the_same_diagonal_can_attack_four() {
let white_queen = Queen::new(ChessPosition::new(2, 2).unwrap());
let black_queen = Queen::new(ChessPosition::new(5, 5).unwrap());

fn can_attack_on_fourth_diagonal() {
let white_queen = Queen::new(ChessPosition::new(1, 7).unwrap());
let black_queen = Queen::new(ChessPosition::new(0, 6).unwrap());
assert!(white_queen.can_attack(&black_queen));
}

#[test]
#[ignore]
fn queens_that_cannot_attack_with_equal_difference() {
fn cannot_attack_if_falling_diagonals_are_only_the_same_when_reflected_across_the_longest_falling_diagonal(
) {
let white_queen = Queen::new(ChessPosition::new(4, 1).unwrap());
let black_queen = Queen::new(ChessPosition::new(2, 5).unwrap());

assert!(!white_queen.can_attack(&black_queen));
}