Don't like ads? PRO users don't see any ads ;-)

CSD coding dojo, tests

By: cesargm on Mar 26th, 2012  |  syntax: Ruby  |  size: 7.50 KB  |  hits: 39  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. require_relative '../lib/parseator.rb'
  2.  
  3. describe Model do
  4.   before :each do
  5.     @example_in = {"l" => ["bool", false],
  6.                            "p" => ["number", 8080],
  7.                            "d" => ["string", "/usr/local"] }
  8.     @model = Model.new(@example_in)
  9.   end
  10.   describe "sunny day tests" do
  11.     it "instantiates correctly" do
  12.       @model.should be_an_instance_of Model
  13.     end
  14.     it "returns a correct flag type" do
  15.       @model.flag_type("p").should == "number"
  16.     end
  17.     it "returns an incorrect flag type" do
  18.       @model.flag_type("x").should be_nil
  19.     end
  20.     it "returns a correct flag default" do
  21.       @model.flag_default("p").should == 8080
  22.     end
  23.     it "returns an incorrect flag default" do
  24.       @model.flag_default("x").should be_nil
  25.     end
  26.     it "raises exception on incorrect boolean flag" do
  27.       lambda { @model.parse_flag("l","poop") }.should raise_error IncorrectParam, "Incorrect parameter -l poop"
  28.     end
  29.     it "parses correctly a boolean flag" do
  30.       lambda { @model.parse_flag("l", nil) }.should_not raise_error IncorrectParam, "Incorrect parameter -l poop"
  31.       @model.parse_flag("l", nil).should == true
  32.     end
  33.     it "raises exception on incorrect string flag" do
  34.       lambda { @model.parse_flag("d",nil) }.should raise_error IncorrectParam, "Incorrect parameter -d "
  35.       lambda { @model.parse_flag("d","8080") }.should raise_error IncorrectParam, "Incorrect parameter -d 8080"
  36.     end
  37.     it "parses correctly a string flag" do
  38.       lambda { @model.parse_flag("d", "/usr/logs") }.should_not raise_error IncorrectParam, "Incorrect parameter -l poop"
  39.       @model.parse_flag("d", "/usr/logs").should == "/usr/logs"
  40.     end
  41.     it "raises exception on incorrect number flag" do
  42.       lambda { @model.parse_flag("p",nil) }.should raise_error IncorrectParam, "Incorrect parameter -p "
  43.       lambda { @model.parse_flag("p","poop") }.should raise_error IncorrectParam, "Incorrect parameter -p poop"
  44.     end
  45.     it "parses correctly a number flag" do
  46.       lambda { @model.parse_flag("p", "8080") }.should_not raise_error IncorrectParam
  47.       @model.parse_flag("p", "8080").should == 8080
  48.     end
  49.     it "returns an array of flags" do
  50.       @model.flags.length.should == 3
  51.       @model.flags.include?("l").should == true
  52.       @model.flags.include?("p").should == true
  53.       @model.flags.include?("d").should == true
  54.       @model.flags.include?("m").should == false
  55.     end
  56.   end
  57. end
  58.  
  59. describe Parseator do
  60.   before :each do
  61.     @example_in = {"l" => ["bool", false],
  62.                            "p" => ["number", 8080],
  63.                            "d" => ["string", "/usr/local"] }
  64.     @parser = Parseator.new(@example_in)
  65.   end
  66.   describe ", Sintactic Tests" do
  67.     it "instantiates a Parseator object" do
  68.      @parser.should be_an_instance_of Parseator
  69.     end
  70.     it "parses the empty string as valid" do
  71.       @parser.valid?("").should ==  true
  72.     end
  73.     it "fails if first flag does not start with -" do
  74.       @parser.valid?("poop").should ==  false
  75.     end
  76.     it "parses a single boolean flag" do
  77.       @parser.valid?("-t").should ==  true
  78.     end
  79.     it "parses N boolean flags" do
  80.       @parser.valid?("-p -m").should == true
  81.     end
  82.     it "parses a single flag with args" do
  83.       @parser.valid?("-p 8080").should == true
  84.     end
  85.     it "parses a mix of boolean and arg flags without negative numbers" do
  86.       @parser.valid?("-p 8080 -m -l /hola/yo").should == true
  87.     end
  88.     it "parses a mix of boolean and arg flags without negative numbers with traling and leading spaces" do
  89.       @parser.valid?("-p 8080 -m -l /hola/yo").should == true
  90.     end
  91.     it "fails with an incorrect mix of boolean and arg flags with negative numbers" do
  92.       @parser.valid?("-p 8080 -m -500 p -l /hola/yo").should == false
  93.     end
  94.     it "parses a mix of boolean and arg flags with negative numbers" do
  95.       @parser.valid?("-p 8080 -m -500 -l /hola/yo").should == true
  96.     end
  97.     it "parses a mix of boolean and arg flags with negative numbers without spaces before argument" do
  98.       @parser.valid?("-p8080 -m -500 -l /hola/yo").should == true
  99.     end
  100.   end
  101.  
  102.   describe ", Semantic Tests, " do
  103.     it "raises exception when passing a flag that is not defined" do
  104.       lambda { @parser.parse("-t") }.should raise_error UndefinedParam
  105.     end
  106.     it "returns a hash with default value when passing a single boolean flag" do
  107.       test_string = "-l"
  108.       @parser.parse(test_string).should have_key("l")
  109.       @parser.parse(test_string)["l"].should == true
  110.     end
  111.     it "returns a hash with false when passing an empty string, for the boolean flag" do
  112.       @parser.parse("").should have_key("l")
  113.       @parser.parse("")["l"].should == false
  114.     end
  115.     it "returns a hash with value when passing a single string" do
  116.       test_string = "-d /usr/bin"
  117.       @parser.parse(test_string).should have_key("d")
  118.       @parser.parse(test_string)["d"].should == "/usr/bin"
  119.     end
  120.     it "returns a hash with value when passing a single number" do
  121.       test_string = "-p 8080"
  122.       @parser.parse(test_string).should have_key("p")
  123.       @parser.parse(test_string)["p"].should == 8080
  124.     end
  125.     it "returns a hash with value when passing a single negative number" do
  126.       test_string = "-p -8080"
  127.       @parser.parse(test_string).should have_key("p")
  128.       @parser.parse(test_string)["p"].should == -8080
  129.     end
  130.     it "returns correctly when passing mix of strings, booleans and numbers" do
  131.       test_string = "-p -8080 -l -d /usr/test"
  132.       @parser.parse(test_string).should have_key("p")
  133.       @parser.parse(test_string)["p"].should == -8080
  134.       @parser.parse(test_string).should have_key("d")
  135.       @parser.parse(test_string)["d"].should == "/usr/test"
  136.       @parser.parse(test_string).should have_key("l")
  137.       @parser.parse(test_string)["l"].should == true
  138.     end
  139.     it "defaults correctly with some params defined" do
  140.       test_string = "-p -8080 -d /usr/test"
  141.       @parser.parse(test_string).should have_key("l")
  142.       @parser.parse("")["l"].should == false
  143.     end
  144.     it "defaults correctly with no params defined, aka empty string test" do
  145.       test_string = ""
  146.       @parser.parse(test_string).should have_key("l")
  147.       @parser.parse("")["l"].should == false
  148.       @parser.parse(test_string).should have_key("p")
  149.       @parser.parse(test_string)["p"].should == 8080
  150.       @parser.parse(test_string).should have_key("d")
  151.       @parser.parse(test_string)["d"].should == "/usr/local"
  152.     end
  153.     it "fails when passing valid and invalid flags" do
  154.       test_string = "-p -8080 -m /usr/test"
  155.       lambda { @parser.parse(test_string) }.should raise_error UndefinedParam
  156.     end
  157.     it "raises exception, with explanation test when passing a flag that is not defined" do
  158.       lambda { @parser.parse("-t") }.should raise_error UndefinedParam, "Incorrect parameter -t "
  159.     end
  160.     it "raises exception, with explanation, passing a defined flag with wrong param" do
  161.       lambda { @parser.parse("-l /usr/test") }.should raise_error IncorrectParam, "Incorrect parameter -l /usr/test"
  162.     end
  163.     it "raises exception, with explanation, passing a number flag to a string flag" do
  164.       lambda { @parser.parse("-d 8080") }.should raise_error IncorrectParam, "Incorrect parameter -d 8080"
  165.     end
  166.     it "raises exception, with explanation, passing a number flag with wrong param" do
  167.       lambda { @parser.parse("-p /usr/test") }.should raise_error IncorrectParam, "Incorrect parameter -p /usr/test"
  168.     end
  169.   end
  170. end