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
it "should create storehouse" do describe Building do
bg = Game::Building.new Game::Building::Type::Storehouse, storage: 100 it "should create storehouse" do
bg.storage.should eq 100 bg = Building.new Building::Type::Storehouse, storage: 100
end bg.storage.should eq 100
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,55 +1,57 @@
require "./spec_helper" require "./spec_helper"
describe CLI::CommandRouter do module CLI::TestCommandRouter
it "should handle simple command as block" do describe CommandRouter do
router = CLI::CommandRouter.new it "should handle simple command as block" do
x = 10 router = CommandRouter.new
router.add "plus" do |p| x = 10
x += 5 router.add "plus" do |p|
x += 5
end
router.handle "plus"
x.should eq 15
end end
router.handle "plus"
x.should eq 15
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
router.handle "plus" router.handle "plus"
x.should eq 15 x.should eq 15
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
end
router.handle "plus 5"
x.should eq 15
end end
router.handle "plus 5"
x.should eq 15
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
end
router.handle "plus 1 3 6"
x.should eq 10
end end
router.handle "plus 1 3 6"
x.should eq 10
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
end
router.add "mult and plus" do |p|
x = x * 2 + 10
end
router.handle "mult and plus"
x.should eq 20
end end
router.add "mult and plus" do |p|
x = x * 2 + 10
end
router.handle "mult and plus"
x.should eq 20
end end
end end

View File

@ -1,35 +1,46 @@
require "./spec_helper" require "./spec_helper"
describe Game::Command do module Game::TestCommand
it "should complete build command" do extend self
world = Game::World.new create_map_2x2
point = Game::Point.new 1, 0 def create_map_2x2 : Map
building = Game::Building.new Game::Building::Type::StartPoint, **{ map = Map.new 2, 2
construction: Game::Construction.free 10, map.set(MainBaseTile.new(Point.new(0, 0)))
} map.set(CrystalTile.new(Point.new(1, 1), 100))
command = Game::BuildCommand.new point, building map
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)
end end
it "should restrict building if not enought resources" do describe Command do
world = Game::World.new create_map_2x2 it "should complete build command" do
point = Game::Point.new 1, 0 world = World.new create_map_2x2
building = Game::Building.new Game::Building::Type::StartPoint, **{ point = Point.new 1, 0
construction: Game::Construction.new( building = Building.new Building::Type::StartPoint, **{
ts: 10, construction: Construction.free 10,
cost: Game::ResourceBag.new({ }
Game::Resource::Type::Crystals => 100, command = BuildCommand.new point, building
}) world.push command
), tile = world.map.get point
} tile.should be_a(ConstructionSiteTile)
command = Game::BuildCommand.new point, building world.run 10
expect_raises(Game::NotEnoughtResources) do tile = world.map.get point
world.push(command) 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 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)
it "can calc distance" do p2 = Point.new(5, 5)
p1.distance(p2).should eq 10
end it "can calc distance" do
it "can calc reverse destance" do p1.distance(p2).should eq 10
p2.distance(p1).should eq 10 end
it "can calc reverse destance" do
p2.distance(p1).should eq 10
end
end end
end end

View File

@ -1,49 +1,51 @@
require "./spec_helper" require "./spec_helper"
macro define_dummy_classes(count) module Game::TestQueue
{% for i in (1...count) %} macro define_dummy_classes(count)
class Test::DummyCommand{{ i }} < Game::Command {% for i in (1...count) %}
def start(world) : Int32 class DummyCommand{{ i }} < Command
end def start(world) : TimeSpan
end
def finish(world) def finish(world)
end end
def desc : String def desc : String
"" ""
end
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 %}
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 commands in proper order" do
it "should pop nil on empty queue" do queue = Queue.new
queue = Game::Queue.new queue.push(10, DummyCommand1.new)
item = queue.pop(50) queue.push(50, DummyCommand2.new)
item.should be_nil queue.push(30, DummyCommand3.new)
end item1 = queue.pop(100)
item1.as(Queue::Item).command.should be_a(DummyCommand1)
it "should pop command on one element queue" do item2 = queue.pop(100)
queue = Game::Queue.new item2.as(Queue::Item).command.should be_a(DummyCommand3)
queue.push(10, Test::DummyCommand1.new) item3 = queue.pop(100)
item = queue.pop(50) item3.as(Queue::Item).command.should be_a(DummyCommand2)
item.nil?.should be_false end
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)
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