summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2023-09-11 13:44:48 +0200
committerJérémy Zurcher <jeremy@asynk.ch>2023-09-11 13:44:48 +0200
commit50686dd6037e7487479599c80890a683800f6a19 (patch)
tree3068eaa45cf08d3058aa8f371d8af5d8dc1f63b5
parent3616bd0a7dfe2235624b1ae97937b27899eaf75f (diff)
downloadcolonial-twilight-50686dd6037e7487479599c80890a683800f6a19.zip
colonial-twilight-50686dd6037e7487479599c80890a683800f6a19.tar.gz
Board : implement specs
-rw-r--r--lib/colonial_twilight/board.rb63
-rw-r--r--spec/board_spec.rb64
2 files changed, 64 insertions, 63 deletions
diff --git a/lib/colonial_twilight/board.rb b/lib/colonial_twilight/board.rb
index 4acf9b3..5fc46b7 100644
--- a/lib/colonial_twilight/board.rb
+++ b/lib/colonial_twilight/board.rb
@@ -690,66 +690,3 @@ end
# class ColonialTwilight::Sector
# undef :adjacents=
# end
-
-if $PROGRAM_NAME == __FILE__
- def check(board)
- # puts '--- Coastal'
- # board.spaces.select{ |s| s.coastal? }.each { |s| puts s.name }
- raise 'coastal sectors error' if board.spaces.select(&:coastal?).size != 14
- # puts '--- not Mountain'
- # board.spaces.select{ |s| not s.mountain? }.each { |s| puts s.name }
- raise 'not moauntain sectors error' if board.spaces.reject(&:mountain?).size != 9
- # puts '--- Border'
- # board.spaces.select{ |s| s.border? }.each { |s| puts s.name }
- raise 'border sectors error' if board.spaces.select(&:border?).size != 9
- # puts '--- City'
- # board.spaces.select{ |s| s.city? }.each { |s| puts s.name }
- raise 'city sectors error' if board.spaces.select(&:city?).size != 3
-
- [[0, 11], [1, 9], [2, 9], [3, 1]].each do |p, n|
- # puts "--- Population #{p}"
- # board.spaces.select{ |s| s.pop==p }.each { |s| puts s.name }
- raise "population #{p} error" if board.spaces.select { |s| s.pop == p }.size != n
- end
- raise 'sectors count wrong' if board.sectors.size != 28
- end
-
- def check_forces what, board, vals
- sup, opp, gov, fln = 0, 0, 0, 0
- ft, fp, at, ap, g = 0, 0, 0, 0, 0
- gb, fb = 0, 0
- board.spaces.each do |s|
- sup += 1 if s.alignment == :support
- opp += 1 if s.alignment == :oppose
- gov += 1 if s.control == :GOV
- fln += 1 if s.control == :FLN
- unless s.country?
- ft += s.french_troops
- fp += s.french_police
- at += s.algerian_troops
- ap += s.algerian_police
- gb += s.gov_bases
- end
- g += s.fln_underground
- fb += s.fln_bases
- end
- raise "wrong support #{sup} != #{vals[0]}" if sup != vals[0]
- raise "wrong oppose #{opp} != #{vals[1]}" if opp != vals[1]
- raise "wrong GOV control #{gov} != #{vals[2]}" if gov != vals[2]
- raise "wrong FLN control #{fln} != #{vals[3]}" if fln != vals[3]
- raise "wrong french troops #{ft} != #{vals[4]}" if ft != vals[4]
- raise "wrong french police #{fp} != #{vals[5]}" if fp != vals[5]
- raise "wrong algerian troops #{at} != #{vals[6]}" if at != vals[6]
- raise "wrong algerian police #{ap} != #{vals[7]}" if ap != vals[7]
- raise "wrong Guerrillas #{g} != #{vals[8]}" if g != vals[8]
- raise "wrong GOV bases #{gb} != #{vals[9]}" if gb != vals[9]
- raise "wrong FLN bases #{fb} != #{vals[10]}" if fb != vals[10]
- end
-
- b = ColonialTwilight::Board.new
- puts 'check'
- check b
- b.load :short
- check_forces 'short', b, [3, 7, 16, 3, 9, 17, 3, 7, 17, 4, 8]
- puts 'ok'
-end
diff --git a/spec/board_spec.rb b/spec/board_spec.rb
new file mode 100644
index 0000000..d1defd4
--- /dev/null
+++ b/spec/board_spec.rb
@@ -0,0 +1,64 @@
+# frozen_string_literal: true
+
+require './lib/colonial_twilight/board'
+
+def compute(board)
+ vals = [0] * 11
+ board.spaces.each do |s|
+ vals[0] += 1 if s.alignment == :support
+ vals[1] += 1 if s.alignment == :oppose
+ vals[2] += 1 if s.control == :GOV
+ vals[3] += 1 if s.control == :FLN
+ unless s.country?
+ vals[4] += s.french_troops
+ vals[5] += s.french_police
+ vals[6] += s.algerian_troops
+ vals[7] += s.algerian_police
+ vals[8] += s.gov_bases
+ end
+ vals[9] += s.fln_underground
+ vals[10] += s.fln_bases
+ end
+ vals
+end
+
+describe ColonialTwilight::Board do
+
+ describe 'board setup' do
+ board = ColonialTwilight::Board.new
+ it 'has spaces' do expect(board.spaces.size).to eq(30) end
+ it 'has sectors' do expect(board.sectors.size).to eq(28) end
+ it 'has country sectors' do expect(board.spaces.select(&:country?).size).to eq(2) end
+ it 'has coastal sectors' do expect(board.spaces.select(&:coastal?).size).to eq(14) end
+ it 'has border sectors' do expect(board.spaces.select(&:border?).size).to eq(9) end
+ it 'has mountain sectors' do expect(board.spaces.select(&:mountain?).size).to eq(21) end
+ it 'has no mountain sectors' do expect(board.spaces.reject(&:mountain?).size).to eq(9) end
+ it 'has city sectors' do expect(board.spaces.select(&:city?).size).to eq(3) end
+ it 'has 0 pop sectors' do expect(board.spaces.select { |s| s.pop == 0 }.size).to eq(11) end
+ it 'has 1 pop sectors' do expect(board.spaces.select { |s| s.pop == 1 }.size).to eq(9) end
+ it 'has 2 pop sectors' do expect(board.spaces.select { |s| s.pop == 2 }.size).to eq(9) end
+ it 'has 3 pop sectors' do expect(board.spaces.select { |s| s.pop == 3 }.size).to eq(1) end
+ end
+
+ describe 'short scenario setup' do
+ board = ColonialTwilight::Board.new
+ board.load :short
+ it 'has 0 pop sectors' do expect(board.spaces.select { |s| s.pop == 0 }.size).to eq(14) end
+ it 'has 1 pop sectors' do expect(board.spaces.select { |s| s.pop == 1 }.size).to eq(6) end
+ it 'countries are independent' do expect(board.spaces.select(&:country?).select(&:independent?).size).to eq(2) end
+ vals = compute board
+ it 'total support' do expect(vals[0]).to eq(3) end
+ it 'total opposition' do expect(vals[1]).to eq(7) end
+ it 'total GOV control' do expect(vals[2]).to eq(16) end
+ it 'total FLN control' do expect(vals[3]).to eq(3) end
+ it 'total french troops' do expect(vals[4]).to eq(9) end
+ it 'total french police' do expect(vals[5]).to eq(17) end
+ it 'total algerian troops' do expect(vals[6]).to eq(3) end
+ it 'total algerian police' do expect(vals[7]).to eq(7) end
+ it 'total GOV bases' do expect(vals[8]).to eq(4) end
+ it 'total FLN undergound' do expect(vals[9]).to eq(17) end
+ it 'total FLN bases' do expect(vals[10]).to eq(8) end
+ it 'oppositon + bases' do expect(board.opposition_bases).to eq(19) end
+ it 'support + commitment' do expect(board.support_commitment).to eq (22) end
+ end
+end