1
exercism fetch lua queen-attack

queen-attack_spec.lua

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
local Queen = require('queen-attack')

describe('collatz-conjecture', function()
  it('queen with a valid position', function()
    assert.has_no_error(function()
      Queen({ row = 2, column = 2 })
    end)
  end)

  it('queen must have a positive row', function()
    assert.has_error(function()
      Queen({ row = -2, column = 2 })
    end)
  end)

  it('queen must have row on board', function()
    assert.has_error(function()
      Queen({ row = 8, column = 4 })
    end)
  end)

  it('queen must have positive column', function()
    assert.has_error(function()
      Queen({ row = 2, column = -2 })
    end)
  end)

  it('queen must have column on board', function()
    assert.has_error(function()
      Queen({ row = 4, column = 8 })
    end)
  end)

  it('can not attack', function()
    local q1 = Queen({ row = 2, column = 4 })
    local q2 = Queen({ row = 6, column = 6 })
    assert.is_false(q1.can_attack(q2))
  end)

  it('can attack on same row', function()
    local q1 = Queen({ row = 2, column = 4 })
    local q2 = Queen({ row = 2, column = 6 })
    assert.is_true(q1.can_attack(q2))
  end)

  it('can attack on same column', function()
    local q1 = Queen({ row = 4, column = 5 })
    local q2 = Queen({ row = 2, column = 5 })
    assert.is_true(q1.can_attack(q2))
  end)

  it('can attack on first diagonal', function()
    local q1 = Queen({ row = 2, column = 2 })
    local q2 = Queen({ row = 0, column = 4 })
    assert.is_true(q1.can_attack(q2))
  end)

  it('can attack on second diagonal', function()
    local q1 = Queen({ row = 2, column = 2 })
    local q2 = Queen({ row = 3, column = 1 })
    assert.is_true(q1.can_attack(q2))
  end)

  it('can attack on third diagonal', function()
    local q1 = Queen({ row = 2, column = 2 })
    local q2 = Queen({ row = 1, column = 1 })
    assert.is_true(q1.can_attack(q2))
  end)

  it('can attack on fourth diagonal', function()
    local q1 = Queen({ row = 2, column = 2 })
    local q2 = Queen({ row = 5, column = 5 })
    assert.is_true(q1.can_attack(q2))
  end)
end)