require_relative '../lib/parseator.rb'
describe Model do
before :each do
@example_in = {"l" => ["bool", false],
"p" => ["number", 8080],
"d" => ["string", "/usr/local"] }
@model = Model.new(@example_in)
end
describe "sunny day tests" do
it "instantiates correctly" do
@model.should be_an_instance_of Model
end
it "returns a correct flag type" do
@model.flag_type("p").should == "number"
end
it "returns an incorrect flag type" do
@model.flag_type("x").should be_nil
end
it "returns a correct flag default" do
@model.flag_default("p").should == 8080
end
it "returns an incorrect flag default" do
@model.flag_default("x").should be_nil
end
it "raises exception on incorrect boolean flag" do
lambda { @model.parse_flag("l","poop") }.should raise_error IncorrectParam, "Incorrect parameter -l poop"
end
it "parses correctly a boolean flag" do
lambda { @model.parse_flag("l", nil) }.should_not raise_error IncorrectParam, "Incorrect parameter -l poop"
@model.parse_flag("l", nil).should == true
end
it "raises exception on incorrect string flag" do
lambda { @model.parse_flag("d",nil) }.should raise_error IncorrectParam, "Incorrect parameter -d "
lambda { @model.parse_flag("d","8080") }.should raise_error IncorrectParam, "Incorrect parameter -d 8080"
end
it "parses correctly a string flag" do
lambda { @model.parse_flag("d", "/usr/logs") }.should_not raise_error IncorrectParam, "Incorrect parameter -l poop"
@model.parse_flag("d", "/usr/logs").should == "/usr/logs"
end
it "raises exception on incorrect number flag" do
lambda { @model.parse_flag("p",nil) }.should raise_error IncorrectParam, "Incorrect parameter -p "
lambda { @model.parse_flag("p","poop") }.should raise_error IncorrectParam, "Incorrect parameter -p poop"
end
it "parses correctly a number flag" do
lambda { @model.parse_flag("p", "8080") }.should_not raise_error IncorrectParam
@model.parse_flag("p", "8080").should == 8080
end
it "returns an array of flags" do
@model.flags.length.should == 3
@model.flags.include?("l").should == true
@model.flags.include?("p").should == true
@model.flags.include?("d").should == true
@model.flags.include?("m").should == false
end
end
end
describe Parseator do
before :each do
@example_in = {"l" => ["bool", false],
"p" => ["number", 8080],
"d" => ["string", "/usr/local"] }
@parser = Parseator.new(@example_in)
end
describe ", Sintactic Tests" do
it "instantiates a Parseator object" do
@parser.should be_an_instance_of Parseator
end
it "parses the empty string as valid" do
@parser.valid?("").should == true
end
it "fails if first flag does not start with -" do
@parser.valid?("poop").should == false
end
it "parses a single boolean flag" do
@parser.valid?("-t").should == true
end
it "parses N boolean flags" do
@parser.valid?("-p -m").should == true
end
it "parses a single flag with args" do
@parser.valid?("-p 8080").should == true
end
it "parses a mix of boolean and arg flags without negative numbers" do
@parser.valid?("-p 8080 -m -l /hola/yo").should == true
end
it "parses a mix of boolean and arg flags without negative numbers with traling and leading spaces" do
@parser.valid?("-p 8080 -m -l /hola/yo").should == true
end
it "fails with an incorrect mix of boolean and arg flags with negative numbers" do
@parser.valid?("-p 8080 -m -500 p -l /hola/yo").should == false
end
it "parses a mix of boolean and arg flags with negative numbers" do
@parser.valid?("-p 8080 -m -500 -l /hola/yo").should == true
end
it "parses a mix of boolean and arg flags with negative numbers without spaces before argument" do
@parser.valid?("-p8080 -m -500 -l /hola/yo").should == true
end
end
describe ", Semantic Tests, " do
it "raises exception when passing a flag that is not defined" do
lambda { @parser.parse("-t") }.should raise_error UndefinedParam
end
it "returns a hash with default value when passing a single boolean flag" do
test_string = "-l"
@parser.parse(test_string).should have_key("l")
@parser.parse(test_string)["l"].should == true
end
it "returns a hash with false when passing an empty string, for the boolean flag" do
@parser.parse("").should have_key("l")
@parser.parse("")["l"].should == false
end
it "returns a hash with value when passing a single string" do
test_string = "-d /usr/bin"
@parser.parse(test_string).should have_key("d")
@parser.parse(test_string)["d"].should == "/usr/bin"
end
it "returns a hash with value when passing a single number" do
test_string = "-p 8080"
@parser.parse(test_string).should have_key("p")
@parser.parse(test_string)["p"].should == 8080
end
it "returns a hash with value when passing a single negative number" do
test_string = "-p -8080"
@parser.parse(test_string).should have_key("p")
@parser.parse(test_string)["p"].should == -8080
end
it "returns correctly when passing mix of strings, booleans and numbers" do
test_string = "-p -8080 -l -d /usr/test"
@parser.parse(test_string).should have_key("p")
@parser.parse(test_string)["p"].should == -8080
@parser.parse(test_string).should have_key("d")
@parser.parse(test_string)["d"].should == "/usr/test"
@parser.parse(test_string).should have_key("l")
@parser.parse(test_string)["l"].should == true
end
it "defaults correctly with some params defined" do
test_string = "-p -8080 -d /usr/test"
@parser.parse(test_string).should have_key("l")
@parser.parse("")["l"].should == false
end
it "defaults correctly with no params defined, aka empty string test" do
test_string = ""
@parser.parse(test_string).should have_key("l")
@parser.parse("")["l"].should == false
@parser.parse(test_string).should have_key("p")
@parser.parse(test_string)["p"].should == 8080
@parser.parse(test_string).should have_key("d")
@parser.parse(test_string)["d"].should == "/usr/local"
end
it "fails when passing valid and invalid flags" do
test_string = "-p -8080 -m /usr/test"
lambda { @parser.parse(test_string) }.should raise_error UndefinedParam
end
it "raises exception, with explanation test when passing a flag that is not defined" do
lambda { @parser.parse("-t") }.should raise_error UndefinedParam, "Incorrect parameter -t "
end
it "raises exception, with explanation, passing a defined flag with wrong param" do
lambda { @parser.parse("-l /usr/test") }.should raise_error IncorrectParam, "Incorrect parameter -l /usr/test"
end
it "raises exception, with explanation, passing a number flag to a string flag" do
lambda { @parser.parse("-d 8080") }.should raise_error IncorrectParam, "Incorrect parameter -d 8080"
end
it "raises exception, with explanation, passing a number flag with wrong param" do
lambda { @parser.parse("-p /usr/test") }.should raise_error IncorrectParam, "Incorrect parameter -p /usr/test"
end
end
end