From f440b34f389628e60e77160872a760468fd18af9 Mon Sep 17 00:00:00 2001 From: Anton Vakhrushev Date: Sun, 13 Oct 2019 13:30:18 +0300 Subject: [PATCH] Add test namespaces --- spec/building_spec.cr | 34 +++++++------- spec/command_router_spec.cr | 88 +++++++++++++++++++------------------ spec/command_spec.cr | 67 ++++++++++++++++------------ spec/expansion_spec.cr | 14 ------ spec/point_spec.cr | 20 +++++---- spec/queue_spec.cr | 80 +++++++++++++++++---------------- spec/spec_helper.cr | 7 +-- 7 files changed, 156 insertions(+), 154 deletions(-) delete mode 100644 spec/expansion_spec.cr diff --git a/spec/building_spec.cr b/spec/building_spec.cr index 0c13e82..c1f5f8f 100644 --- a/spec/building_spec.cr +++ b/spec/building_spec.cr @@ -1,21 +1,23 @@ require "./spec_helper" -describe Game::Building do - it "should create storehouse" do - bg = Game::Building.new Game::Building::Type::Storehouse, storage: 100 - bg.storage.should eq 100 - end +module Game::TestBuilding + describe Building do + it "should create storehouse" do + bg = Building.new Building::Type::Storehouse, storage: 100 + bg.storage.should eq 100 + end - it "should create crystal miner" do - bg = Game::Building.new Game::Building::Type::CrystalMiner, **{ - production: Game::Production.new( - ts: 20, - input: Game::ResourceBag.new, - output: Game::ResourceBag.new({ - Game::Resource::Type::Crystals => 100, - }) - ), - } - bg.production.as(Game::Production).ts.should eq 20 + it "should create crystal miner" do + bg = Building.new Building::Type::CrystalMiner, **{ + production: Production.new( + ts: 20, + input: ResourceBag.new, + output: ResourceBag.new({ + Resource::Type::Crystals => 100, + }) + ), + } + bg.production.as(Production).ts.should eq 20 + end end end diff --git a/spec/command_router_spec.cr b/spec/command_router_spec.cr index 3925e05..a2037a3 100644 --- a/spec/command_router_spec.cr +++ b/spec/command_router_spec.cr @@ -1,55 +1,57 @@ require "./spec_helper" -describe CLI::CommandRouter do - it "should handle simple command as block" do - router = CLI::CommandRouter.new - x = 10 - router.add "plus" do |p| - x += 5 +module CLI::TestCommandRouter + describe CommandRouter do + it "should handle simple command as block" do + router = CommandRouter.new + x = 10 + router.add "plus" do |p| + x += 5 + end + router.handle "plus" + x.should eq 15 end - router.handle "plus" - x.should eq 15 - end - it "should handle simple command as proc" do - router = CLI::CommandRouter.new - x = 10 - cb = ->(params : Hash(String, String)) { x += 5 } - router.add "plus", &cb - router.handle "plus" - x.should eq 15 - end + it "should handle simple command as proc" do + router = CommandRouter.new + x = 10 + cb = ->(params : Hash(String, String)) { x += 5 } + router.add "plus", &cb + router.handle "plus" + x.should eq 15 + end - it "should handle command with argument" do - router = CLI::CommandRouter.new - x = 10 - router.add "plus {x}" do |params| - x += params["x"].to_i32 + it "should handle command with argument" do + router = CommandRouter.new + x = 10 + router.add "plus {x}" do |params| + x += params["x"].to_i32 + end + router.handle "plus 5" + x.should eq 15 end - router.handle "plus 5" - x.should eq 15 - end - it "should handle command with three arguments" do - router = CLI::CommandRouter.new - x = 0 - router.add "plus {x} {y} {z}" do |p| - x = p["x"].to_i32 + p["y"].to_i32 + p["z"].to_i32 + it "should handle command with three arguments" do + router = CommandRouter.new + x = 0 + router.add "plus {x} {y} {z}" do |p| + x = p["x"].to_i32 + p["y"].to_i32 + p["z"].to_i32 + end + router.handle "plus 1 3 6" + x.should eq 10 end - router.handle "plus 1 3 6" - x.should eq 10 - end - it "should match commands from begin of string" do - router = CLI::CommandRouter.new - x = 5 - router.add "plus" do |p| - x += 10 + it "should match commands from begin of string" do + router = CommandRouter.new + x = 5 + router.add "plus" do |p| + x += 10 + end + router.add "mult and plus" do |p| + x = x * 2 + 10 + end + router.handle "mult and plus" + x.should eq 20 end - router.add "mult and plus" do |p| - x = x * 2 + 10 - end - router.handle "mult and plus" - x.should eq 20 end end diff --git a/spec/command_spec.cr b/spec/command_spec.cr index 1ddbc44..fc26b02 100644 --- a/spec/command_spec.cr +++ b/spec/command_spec.cr @@ -1,35 +1,46 @@ require "./spec_helper" -describe Game::Command do - it "should complete build command" do - world = Game::World.new create_map_2x2 - point = Game::Point.new 1, 0 - building = Game::Building.new Game::Building::Type::StartPoint, **{ - construction: Game::Construction.free 10, - } - command = Game::BuildCommand.new point, building - world.push command - tile = world.map.get point - tile.should be_a(Game::ConstructionSiteTile) - world.run 10 - tile = world.map.get point - tile.should be_a(Game::BuildingTile) +module Game::TestCommand + extend self + + def create_map_2x2 : Map + map = Map.new 2, 2 + map.set(MainBaseTile.new(Point.new(0, 0))) + map.set(CrystalTile.new(Point.new(1, 1), 100)) + map end - it "should restrict building if not enought resources" do - world = Game::World.new create_map_2x2 - point = Game::Point.new 1, 0 - building = Game::Building.new Game::Building::Type::StartPoint, **{ - construction: Game::Construction.new( - ts: 10, - cost: Game::ResourceBag.new({ - Game::Resource::Type::Crystals => 100, - }) - ), - } - command = Game::BuildCommand.new point, building - expect_raises(Game::NotEnoughtResources) do - world.push(command) + describe Command do + it "should complete build command" do + world = World.new create_map_2x2 + point = Point.new 1, 0 + building = Building.new Building::Type::StartPoint, **{ + construction: Construction.free 10, + } + command = BuildCommand.new point, building + world.push command + tile = world.map.get point + tile.should be_a(ConstructionSiteTile) + world.run 10 + tile = world.map.get point + tile.should be_a(BuildingTile) + end + + it "should restrict building if not enought resources" do + world = World.new create_map_2x2 + point = Point.new 1, 0 + building = Building.new Building::Type::StartPoint, **{ + construction: Construction.new( + ts: 10, + cost: ResourceBag.new({ + Resource::Type::Crystals => 100, + }) + ), + } + command = BuildCommand.new point, building + expect_raises(NotEnoughtResources) do + world.push(command) + end end end end diff --git a/spec/expansion_spec.cr b/spec/expansion_spec.cr deleted file mode 100644 index c2827e0..0000000 --- a/spec/expansion_spec.cr +++ /dev/null @@ -1,14 +0,0 @@ -require "./spec_helper" - -describe Game::Point do - p1 = Game::Point.new(0, 0) - p2 = Game::Point.new(5, 5) - - it "can calc distance" do - p1.distance(p2).should eq 10 - end - - it "can calc reverse destance" do - p2.distance(p1).should eq 10 - end -end diff --git a/spec/point_spec.cr b/spec/point_spec.cr index 090320b..87af540 100644 --- a/spec/point_spec.cr +++ b/spec/point_spec.cr @@ -1,12 +1,16 @@ require "./spec_helper" -describe Game::Point do - p1 = Game::Point.new(0, 0) - p2 = Game::Point.new(5, 5) - it "can calc distance" do - p1.distance(p2).should eq 10 - end - it "can calc reverse destance" do - p2.distance(p1).should eq 10 +module Game::TestPoint + describe Point do + p1 = Point.new(0, 0) + p2 = Point.new(5, 5) + + it "can calc distance" do + p1.distance(p2).should eq 10 + end + + it "can calc reverse destance" do + p2.distance(p1).should eq 10 + end end end diff --git a/spec/queue_spec.cr b/spec/queue_spec.cr index fd90ca3..1cc559d 100644 --- a/spec/queue_spec.cr +++ b/spec/queue_spec.cr @@ -1,49 +1,51 @@ require "./spec_helper" -macro define_dummy_classes(count) - {% for i in (1...count) %} - class Test::DummyCommand{{ i }} < Game::Command - def start(world) : Int32 - end +module Game::TestQueue + macro define_dummy_classes(count) + {% for i in (1...count) %} + class DummyCommand{{ i }} < Command + def start(world) : TimeSpan + end - def finish(world) - end + def finish(world) + end - def desc : String - "" + def desc : String + "" + end end + {% end %} + end + + define_dummy_classes(5) + + describe Queue do + it "should pop nil on empty queue" do + queue = Queue.new + item = queue.pop(50) + item.should be_nil end - {% end %} -end -define_dummy_classes(5) + it "should pop command on one element queue" do + queue = Queue.new + queue.push(10, DummyCommand1.new) + item = queue.pop(50) + item.nil?.should be_false + item.as(Queue::Item).ts.should eq 10 + item.as(Queue::Item).command.should be_a(DummyCommand1) + end -describe Game::Queue do - it "should pop nil on empty queue" do - queue = Game::Queue.new - item = queue.pop(50) - item.should be_nil - end - - it "should pop command on one element queue" do - queue = Game::Queue.new - queue.push(10, Test::DummyCommand1.new) - item = queue.pop(50) - item.nil?.should be_false - item.as(Game::Queue::Item).ts.should eq 10 - item.as(Game::Queue::Item).command.should be_a(Test::DummyCommand1) - end - - it "should pop commands in proper order" do - queue = Game::Queue.new - queue.push(10, Test::DummyCommand1.new) - queue.push(50, Test::DummyCommand2.new) - queue.push(30, Test::DummyCommand3.new) - item1 = queue.pop(100) - item1.as(Game::Queue::Item).command.should be_a(Test::DummyCommand1) - item2 = queue.pop(100) - item2.as(Game::Queue::Item).command.should be_a(Test::DummyCommand3) - item3 = queue.pop(100) - item3.as(Game::Queue::Item).command.should be_a(Test::DummyCommand2) + it "should pop commands in proper order" do + queue = Queue.new + queue.push(10, DummyCommand1.new) + queue.push(50, DummyCommand2.new) + queue.push(30, DummyCommand3.new) + item1 = queue.pop(100) + item1.as(Queue::Item).command.should be_a(DummyCommand1) + item2 = queue.pop(100) + item2.as(Queue::Item).command.should be_a(DummyCommand3) + item3 = queue.pop(100) + item3.as(Queue::Item).command.should be_a(DummyCommand2) + end end end diff --git a/spec/spec_helper.cr b/spec/spec_helper.cr index 9a592c8..f2d27ff 100644 --- a/spec/spec_helper.cr +++ b/spec/spec_helper.cr @@ -3,9 +3,4 @@ require "../src/game/**" require "../src/cli/**" require "../src/app" -def create_map_2x2 : Game::Map - map = Game::Map.new 2, 2 - map.set(Game::MainBaseTile.new(Game::Point.new(0, 0))) - map.set(Game::CrystalTile.new(Game::Point.new(1, 1), 100)) - map -end +# @todo Add helpers here