Add param handling to command router

This commit is contained in:
Anton Vakhrushev
2019-10-04 16:07:03 +03:00
parent 1f494c5a63
commit 1eea71177f
3 changed files with 64 additions and 10 deletions

View File

@ -2,13 +2,42 @@ require "./spec_helper"
require "../src/cli/*"
describe CLI::CommandRouter do
it "should handle simple command" do
it "should handle simple command as block" do
router = CLI::CommandRouter.new
x = 10
router.add "plus" do
router.add "plus" do |p|
x += 5
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 command with argument" do
router = CLI::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
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
end
router.handle "plus 1 3 6"
x.should eq 10
end
end