Add test namespaces

This commit is contained in:
Anton Vakhrushev 2019-10-13 13:30:18 +03:00
parent 19f6d479f2
commit f440b34f38
7 changed files with 156 additions and 154 deletions

View File

@ -1,21 +1,23 @@
require "./spec_helper" require "./spec_helper"
describe Game::Building do module Game::TestBuilding
describe Building do
it "should create storehouse" do it "should create storehouse" do
bg = Game::Building.new Game::Building::Type::Storehouse, storage: 100 bg = Building.new Building::Type::Storehouse, storage: 100
bg.storage.should eq 100 bg.storage.should eq 100
end end
it "should create crystal miner" do it "should create crystal miner" do
bg = Game::Building.new Game::Building::Type::CrystalMiner, **{ bg = Building.new Building::Type::CrystalMiner, **{
production: Game::Production.new( production: Production.new(
ts: 20, ts: 20,
input: Game::ResourceBag.new, input: ResourceBag.new,
output: Game::ResourceBag.new({ output: ResourceBag.new({
Game::Resource::Type::Crystals => 100, Resource::Type::Crystals => 100,
}) })
), ),
} }
bg.production.as(Game::Production).ts.should eq 20 bg.production.as(Production).ts.should eq 20
end
end end
end end

View File

@ -1,8 +1,9 @@
require "./spec_helper" require "./spec_helper"
describe CLI::CommandRouter do module CLI::TestCommandRouter
describe CommandRouter do
it "should handle simple command as block" do it "should handle simple command as block" do
router = CLI::CommandRouter.new router = CommandRouter.new
x = 10 x = 10
router.add "plus" do |p| router.add "plus" do |p|
x += 5 x += 5
@ -12,7 +13,7 @@ describe CLI::CommandRouter do
end end
it "should handle simple command as proc" do it "should handle simple command as proc" do
router = CLI::CommandRouter.new router = CommandRouter.new
x = 10 x = 10
cb = ->(params : Hash(String, String)) { x += 5 } cb = ->(params : Hash(String, String)) { x += 5 }
router.add "plus", &cb router.add "plus", &cb
@ -21,7 +22,7 @@ describe CLI::CommandRouter do
end end
it "should handle command with argument" do it "should handle command with argument" do
router = CLI::CommandRouter.new router = CommandRouter.new
x = 10 x = 10
router.add "plus {x}" do |params| router.add "plus {x}" do |params|
x += params["x"].to_i32 x += params["x"].to_i32
@ -31,7 +32,7 @@ describe CLI::CommandRouter do
end end
it "should handle command with three arguments" do it "should handle command with three arguments" do
router = CLI::CommandRouter.new router = CommandRouter.new
x = 0 x = 0
router.add "plus {x} {y} {z}" do |p| router.add "plus {x} {y} {z}" do |p|
x = p["x"].to_i32 + p["y"].to_i32 + p["z"].to_i32 x = p["x"].to_i32 + p["y"].to_i32 + p["z"].to_i32
@ -41,7 +42,7 @@ describe CLI::CommandRouter do
end end
it "should match commands from begin of string" do it "should match commands from begin of string" do
router = CLI::CommandRouter.new router = CommandRouter.new
x = 5 x = 5
router.add "plus" do |p| router.add "plus" do |p|
x += 10 x += 10
@ -53,3 +54,4 @@ describe CLI::CommandRouter do
x.should eq 20 x.should eq 20
end end
end end
end

View File

@ -1,35 +1,46 @@
require "./spec_helper" require "./spec_helper"
describe Game::Command do 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
describe Command do
it "should complete build command" do it "should complete build command" do
world = Game::World.new create_map_2x2 world = World.new create_map_2x2
point = Game::Point.new 1, 0 point = Point.new 1, 0
building = Game::Building.new Game::Building::Type::StartPoint, **{ building = Building.new Building::Type::StartPoint, **{
construction: Game::Construction.free 10, construction: Construction.free 10,
} }
command = Game::BuildCommand.new point, building command = BuildCommand.new point, building
world.push command world.push command
tile = world.map.get point tile = world.map.get point
tile.should be_a(Game::ConstructionSiteTile) tile.should be_a(ConstructionSiteTile)
world.run 10 world.run 10
tile = world.map.get point tile = world.map.get point
tile.should be_a(Game::BuildingTile) tile.should be_a(BuildingTile)
end end
it "should restrict building if not enought resources" do it "should restrict building if not enought resources" do
world = Game::World.new create_map_2x2 world = World.new create_map_2x2
point = Game::Point.new 1, 0 point = Point.new 1, 0
building = Game::Building.new Game::Building::Type::StartPoint, **{ building = Building.new Building::Type::StartPoint, **{
construction: Game::Construction.new( construction: Construction.new(
ts: 10, ts: 10,
cost: Game::ResourceBag.new({ cost: ResourceBag.new({
Game::Resource::Type::Crystals => 100, Resource::Type::Crystals => 100,
}) })
), ),
} }
command = Game::BuildCommand.new point, building command = BuildCommand.new point, building
expect_raises(Game::NotEnoughtResources) do expect_raises(NotEnoughtResources) do
world.push(command) world.push(command)
end end
end end
end end
end

View File

@ -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

View File

@ -1,12 +1,16 @@
require "./spec_helper" require "./spec_helper"
describe Game::Point do module Game::TestPoint
p1 = Game::Point.new(0, 0) describe Point do
p2 = Game::Point.new(5, 5) p1 = Point.new(0, 0)
p2 = Point.new(5, 5)
it "can calc distance" do it "can calc distance" do
p1.distance(p2).should eq 10 p1.distance(p2).should eq 10
end end
it "can calc reverse destance" do it "can calc reverse destance" do
p2.distance(p1).should eq 10 p2.distance(p1).should eq 10
end end
end end
end

View File

@ -1,9 +1,10 @@
require "./spec_helper" require "./spec_helper"
module Game::TestQueue
macro define_dummy_classes(count) macro define_dummy_classes(count)
{% for i in (1...count) %} {% for i in (1...count) %}
class Test::DummyCommand{{ i }} < Game::Command class DummyCommand{{ i }} < Command
def start(world) : Int32 def start(world) : TimeSpan
end end
def finish(world) def finish(world)
@ -18,32 +19,33 @@ end
define_dummy_classes(5) define_dummy_classes(5)
describe Game::Queue do describe Queue do
it "should pop nil on empty queue" do it "should pop nil on empty queue" do
queue = Game::Queue.new queue = Queue.new
item = queue.pop(50) item = queue.pop(50)
item.should be_nil item.should be_nil
end end
it "should pop command on one element queue" do it "should pop command on one element queue" do
queue = Game::Queue.new queue = Queue.new
queue.push(10, Test::DummyCommand1.new) queue.push(10, DummyCommand1.new)
item = queue.pop(50) item = queue.pop(50)
item.nil?.should be_false item.nil?.should be_false
item.as(Game::Queue::Item).ts.should eq 10 item.as(Queue::Item).ts.should eq 10
item.as(Game::Queue::Item).command.should be_a(Test::DummyCommand1) item.as(Queue::Item).command.should be_a(DummyCommand1)
end end
it "should pop commands in proper order" do it "should pop commands in proper order" do
queue = Game::Queue.new queue = Queue.new
queue.push(10, Test::DummyCommand1.new) queue.push(10, DummyCommand1.new)
queue.push(50, Test::DummyCommand2.new) queue.push(50, DummyCommand2.new)
queue.push(30, Test::DummyCommand3.new) queue.push(30, DummyCommand3.new)
item1 = queue.pop(100) item1 = queue.pop(100)
item1.as(Game::Queue::Item).command.should be_a(Test::DummyCommand1) item1.as(Queue::Item).command.should be_a(DummyCommand1)
item2 = queue.pop(100) item2 = queue.pop(100)
item2.as(Game::Queue::Item).command.should be_a(Test::DummyCommand3) item2.as(Queue::Item).command.should be_a(DummyCommand3)
item3 = queue.pop(100) item3 = queue.pop(100)
item3.as(Game::Queue::Item).command.should be_a(Test::DummyCommand2) item3.as(Queue::Item).command.should be_a(DummyCommand2)
end
end end
end end

View File

@ -3,9 +3,4 @@ require "../src/game/**"
require "../src/cli/**" require "../src/cli/**"
require "../src/app" require "../src/app"
def create_map_2x2 : Game::Map # @todo Add helpers here
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