From 23781f394d16c84d761a1b9fa3ad1e65576a2a82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Wed, 8 Nov 2023 21:59:40 +0100 Subject: Spaces : add #resettled? and #shift_terror(n) and specs --- lib/colonial_twilight/spaces.rb | 12 ++- spec/spaces_spec.rb | 169 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 180 insertions(+), 1 deletion(-) create mode 100644 spec/spaces_spec.rb diff --git a/lib/colonial_twilight/spaces.rb b/lib/colonial_twilight/spaces.rb index 6bf9384..909e9db 100644 --- a/lib/colonial_twilight/spaces.rb +++ b/lib/colonial_twilight/spaces.rb @@ -146,9 +146,19 @@ module ColonialTwilight @forces.add(type, num) end + def shift_terror(num = 1) + @terror += num + + raise "terror cant be negative" if @terror.negative? + end + + def resettled? + @resettled + end + def resettle! raise "can't resettle a country " if country? - raise "can't resettle a sector with a population > 1" if @pop != 1 + raise "can't resettle a sector with a population =! 1" if @pop != 1 @pop = 0 @resettled = true diff --git a/spec/spaces_spec.rb b/spec/spaces_spec.rb new file mode 100644 index 0000000..0c7e6bc --- /dev/null +++ b/spec/spaces_spec.rb @@ -0,0 +1,169 @@ +# frozen_string_literal: true + +require './lib/colonial_twilight/spaces' +# require './spec/mock_board' + +describe ColonialTwilight::Track do + before do + @t = ColonialTwilight::Track.new(10) + end + + it 'initialize' do + expect(@t.v).to eq 0 + end + + it 'shift' do + expect(@t.shift(3)).to eq 3 + expect(@t.shift(-2)).to eq 1 + end + + it 'clamp' do + expect(@t.clamp(12)).to eq 10 + end + + it 'overflow' do + expect { @t.shift(11) }.to raise_error(Exception) + end + + it 'data' do + expect(@t.shift(3)).to eq 3 + expect(@t.data).to eq 3 + end +end + +describe ColonialTwilight::Sector do + before do + @s = ColonialTwilight::Sector.new('Name', 'Wilaya', 'I', 0) + end + + it 'to_s data inspect' do + expect(@s.to_s).to eq 'Name' + expect(@s.data.instance_of?(Hash)).to be true + expect(@s.inspect.instance_of?(String)).to be true + end + + it 'is a Sector' do + expect(@s.sector?).to be true + expect(@s.city?).to be false + expect(@s.country?).to be false + end + + it 'has border' do + s = ColonialTwilight::Sector.new('Name', 'Wilaya', 'I', 1, ColonialTwilight::Sector::BORDER) + expect(s.border?).to be true + expect(s.coastal?).to be false + expect(s.mountain?).to be false + end + + it 'is coastal' do + s = ColonialTwilight::Sector.new('Name', 'Wilaya', 'I', 1, ColonialTwilight::Sector::COASTAL) + expect(s.border?).to be false + expect(s.coastal?).to be true + expect(s.mountain?).to be false + end + + it 'has mountains' do + s = ColonialTwilight::Sector.new('Name', 'Wilaya', 'I', 1, ColonialTwilight::Sector::MOUNTAIN) + expect(s.border?).to be false + expect(s.coastal?).to be false + expect(s.mountain?).to be true + end + + it 'has all' do + s = ColonialTwilight::Sector.new('Name', 'Wilaya', 'I', 1, ColonialTwilight::Sector::BORDER || + ColonialTwilight::Sector::COASTAL || ColonialTwilight::Sector::MOUNTAIN) + expect(s.border?).to be true + expect(s.coastal?).to be false + expect(s.mountain?).to be false + end + + it 'terror' do + expect(@s.terror?).to be false + expect(@s.terror).to eq 0 + @s.shift_terror 2 + expect(@s.terror?).to be true + expect(@s.terror).to eq 2 + expect { @t.shift_terrort(-3) }.to raise_error(Exception) + end + + it 'alignment' do + expect(@s.oppose?).to be false + expect(@s.neutral?).to be true + expect(@s.support?).to be false + expect { @s.shift :wrong }.to raise_error(Exception) + end + + it 'shift alignment toward oppose' do + @s.shift :oppose + expect(@s.oppose?).to be true + expect(@s.neutral?).to be false + expect(@s.support?).to be false + end + + it 'shift alignment toward support' do + @s.shift :support + expect(@s.oppose?).to be false + expect(@s.neutral?).to be false + expect(@s.support?).to be true + end + + it 'control' do + expect(@s.fln_control?).to be false + expect(@s.uncontrolled?).to be true + expect(@s.gov_control?).to be false + end + + it 'control fln' do + @s.add :fln_active + expect(@s.fln_control?).to be true + expect(@s.uncontrolled?).to be false + expect(@s.gov_control?).to be false + end + + it 'control gov' do + @s.add :french_troops + expect(@s.fln_control?).to be false + expect(@s.uncontrolled?).to be false + expect(@s.gov_control?).to be true + end + + it 'resettle' do + expect { @s.resettle! }.to raise_error(Exception) + @s.pop = 2 + expect { @s.resettle! }.to raise_error(Exception) + @s.pop = 1 + @s.resettle! + expect(@s.resettled?).to be true + expect(@s.pop).to eq 0 + end +end + +describe ColonialTwilight::City do + before do + @c = ColonialTwilight::City.new('Name', 'Wilaya', 'I', 0) + end + + it 'is a City' do + expect(@c.sector?).to be false + expect(@c.city?).to be true + expect(@c.country?).to be false + end +end + +describe ColonialTwilight::Country do + before do + @c = ColonialTwilight::Country.new('Name') + end + + it 'is a Coutry' do + expect(@c.sector?).to be false + expect(@c.city?).to be false + expect(@c.country?).to be true + end + + it 'independent' do + expect(@c.independent?).to be false + @c.independent! + expect(@c.independent?).to be true + end +end -- cgit v1.1-2-g2b99