From 6827ad804f2eb44839f9a3e99f40f1bb2284f491 Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Fri, 29 Jul 2011 20:57:10 +0100 Subject: (#1) provide some more detailed tests for a number of functions. --- spec/unit/parser/functions/abs_spec.rb | 7 ++++++- spec/unit/parser/functions/downcase_spec.rb | 5 +++++ spec/unit/parser/functions/empty_spec.rb | 5 +++++ spec/unit/parser/functions/flatten_spec.rb | 5 +++++ spec/unit/parser/functions/is_array_spec.rb | 5 +++++ spec/unit/parser/functions/is_float_spec.rb | 2 +- spec/unit/parser/functions/num2bool_spec.rb | 7 ++++++- spec/unit/parser/functions/shuffle_spec.rb | 10 ++++++++++ spec/unit/parser/functions/strftime_spec.rb | 15 +++++++++++++++ spec/unit/parser/functions/time_spec.rb | 15 +++++++++++++++ spec/unit/parser/functions/type_spec.rb | 12 +++++++++++- 11 files changed, 84 insertions(+), 4 deletions(-) (limited to 'spec') diff --git a/spec/unit/parser/functions/abs_spec.rb b/spec/unit/parser/functions/abs_spec.rb index 6bf0b41..65ba2e8 100755 --- a/spec/unit/parser/functions/abs_spec.rb +++ b/spec/unit/parser/functions/abs_spec.rb @@ -19,8 +19,13 @@ describe "the abs function" do end it "should convert a negative number into a positive" do - result = @scope.function_abs([-34]) + result = @scope.function_abs(["-34"]) result.should(eq(34)) end + it "should do nothing with a positive number" do + result = @scope.function_abs(["5678"]) + result.should(eq(5678)) + end + end diff --git a/spec/unit/parser/functions/downcase_spec.rb b/spec/unit/parser/functions/downcase_spec.rb index 0d53220..0bccd5f 100755 --- a/spec/unit/parser/functions/downcase_spec.rb +++ b/spec/unit/parser/functions/downcase_spec.rb @@ -23,4 +23,9 @@ describe "the downcase function" do result.should(eq("asfd")) end + it "should do nothing to a string that is already downcase" do + result = @scope.function_downcase(["asdf asdf"]) + result.should(eq("asdf asdf")) + end + end diff --git a/spec/unit/parser/functions/empty_spec.rb b/spec/unit/parser/functions/empty_spec.rb index 6c0fe69..cb0021f 100755 --- a/spec/unit/parser/functions/empty_spec.rb +++ b/spec/unit/parser/functions/empty_spec.rb @@ -23,4 +23,9 @@ describe "the empty function" do result.should(eq(true)) end + it "should return a false for a non-empty string" do + result = @scope.function_empty(['asdf']) + result.should(eq(false)) + end + end diff --git a/spec/unit/parser/functions/flatten_spec.rb b/spec/unit/parser/functions/flatten_spec.rb index 91cf4ef..7bedeb2 100755 --- a/spec/unit/parser/functions/flatten_spec.rb +++ b/spec/unit/parser/functions/flatten_spec.rb @@ -23,4 +23,9 @@ describe "the flatten function" do result.should(eq(["a","b","c","d","e","f","g"])) end + it "should do nothing to a structure that is already flat" do + result = @scope.function_flatten([["a","b","c","d"]]) + result.should(eq(["a","b","c","d"])) + end + end diff --git a/spec/unit/parser/functions/is_array_spec.rb b/spec/unit/parser/functions/is_array_spec.rb index 15b563c..537595c 100644 --- a/spec/unit/parser/functions/is_array_spec.rb +++ b/spec/unit/parser/functions/is_array_spec.rb @@ -28,4 +28,9 @@ describe "the is_array function" do result.should(eq(false)) end + it "should return false if passed a string" do + result = @scope.function_is_array(["asdf"]) + result.should(eq(false)) + end + end diff --git a/spec/unit/parser/functions/is_float_spec.rb b/spec/unit/parser/functions/is_float_spec.rb index 35c0dd6..55ba8cf 100644 --- a/spec/unit/parser/functions/is_float_spec.rb +++ b/spec/unit/parser/functions/is_float_spec.rb @@ -28,7 +28,7 @@ describe "the is_float function" do result.should(eq(false)) end - it "should return false if not an integer" do + it "should return false if an integer" do result = @scope.function_is_float(["3"]) result.should(eq(false)) end diff --git a/spec/unit/parser/functions/num2bool_spec.rb b/spec/unit/parser/functions/num2bool_spec.rb index 2f6435d..6585273 100644 --- a/spec/unit/parser/functions/num2bool_spec.rb +++ b/spec/unit/parser/functions/num2bool_spec.rb @@ -20,7 +20,12 @@ describe "the num2bool function" do it "should return true if 1" do result = @scope.function_num2bool(["1"]) - result.should(eq(true)) + result.should(be_true) + end + + it "should return false if 0" do + result = @scope.function_num2bool(["0"]) + result.should(be_false) end end diff --git a/spec/unit/parser/functions/shuffle_spec.rb b/spec/unit/parser/functions/shuffle_spec.rb index cf063c6..936c2fd 100644 --- a/spec/unit/parser/functions/shuffle_spec.rb +++ b/spec/unit/parser/functions/shuffle_spec.rb @@ -18,4 +18,14 @@ describe "the shuffle function" do lambda { @scope.function_shuffle([]) }.should( raise_error(Puppet::ParseError)) end + it "should shuffle a string and the result should be the same size" do + result = @scope.function_shuffle(["asdf"]) + result.size.should(eq(4)) + end + + it "should shuffle a string but the sorted contents should still be the same" do + result = @scope.function_shuffle(["adfs"]) + result.split("").sort.to_s.should(eq("adfs")) + end + end diff --git a/spec/unit/parser/functions/strftime_spec.rb b/spec/unit/parser/functions/strftime_spec.rb index e377954..f7a2cd9 100644 --- a/spec/unit/parser/functions/strftime_spec.rb +++ b/spec/unit/parser/functions/strftime_spec.rb @@ -18,4 +18,19 @@ describe "the strftime function" do lambda { @scope.function_strftime([]) }.should( raise_error(Puppet::ParseError)) end + it "using %s should be higher then when I wrote this test" do + result = @scope.function_strftime(["%s"]) + result.to_i.should(be > 1311953157) + end + + it "using %s should be lower then 1.5 trillion" do + result = @scope.function_strftime(["%s"]) + result.to_i.should(be < 1500000000) + end + + it "should return a date when given %Y-%m-%d" do + result = @scope.function_strftime(["%Y-%m-%d"]) + result.should =~ /^\d{4}-\d{2}-\d{2}$/ + end + end diff --git a/spec/unit/parser/functions/time_spec.rb b/spec/unit/parser/functions/time_spec.rb index 8bf5982..666e8e0 100644 --- a/spec/unit/parser/functions/time_spec.rb +++ b/spec/unit/parser/functions/time_spec.rb @@ -18,4 +18,19 @@ describe "the time function" do lambda { @scope.function_time(['','']) }.should( raise_error(Puppet::ParseError)) end + it "should return a number" do + result = @scope.function_time([]) + result.class.should(eq(Fixnum)) + end + + it "should be higher then when I wrote this test" do + result = @scope.function_time([]) + result.should(be > 1311953157) + end + + it "should be lower then 1.5 trillion" do + result = @scope.function_time([]) + result.should(be < 1500000000) + end + end diff --git a/spec/unit/parser/functions/type_spec.rb b/spec/unit/parser/functions/type_spec.rb index fddb87d..36c3823 100644 --- a/spec/unit/parser/functions/type_spec.rb +++ b/spec/unit/parser/functions/type_spec.rb @@ -18,9 +18,19 @@ describe "the type function" do lambda { @scope.function_type([]) }.should( raise_error(Puppet::ParseError)) end - it "should return a type when given a string" do + it "should return String when given a string" do result = @scope.function_type(["aaabbbbcccc"]) result.should(eq('String')) end + it "should return Array when given an array" do + result = @scope.function_type([["aaabbbbcccc","asdf"]]) + result.should(eq('Array')) + end + + it "should return Hash when given a hash" do + result = @scope.function_type([{"a"=>1,"b"=>2}]) + result.should(eq('Hash')) + end + end -- cgit v1.2.3