From 681a1c7971d78c53dc9a0747ae4d983ff6b0d670 Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Fri, 5 Aug 2011 08:25:03 +0100 Subject: Prep for stdlib merge * Renamed load_yaml & load_json to parseyaml & parsejson * Renamed is_valid_* functions and remove the 'valid_' --- lib/puppet/parser/functions/is_domain_name.rb | 27 +++++++++++ lib/puppet/parser/functions/is_ip_address.rb | 32 +++++++++++++ lib/puppet/parser/functions/is_mac_address.rb | 27 +++++++++++ .../parser/functions/is_valid_domain_name.rb | 27 ----------- lib/puppet/parser/functions/is_valid_ip_address.rb | 32 ------------- .../parser/functions/is_valid_mac_address.rb | 27 ----------- lib/puppet/parser/functions/load_json.rb | 26 ---------- lib/puppet/parser/functions/load_yaml.rb | 24 ---------- lib/puppet/parser/functions/parsejson.rb | 26 ++++++++++ lib/puppet/parser/functions/parseyaml.rb | 24 ++++++++++ spec/unit/parser/functions/is_domain_name_spec.rb | 56 ++++++++++++++++++++++ spec/unit/parser/functions/is_ip_address_spec.rb | 45 +++++++++++++++++ spec/unit/parser/functions/is_mac_address_spec.rb | 36 ++++++++++++++ .../parser/functions/is_valid_domain_name_spec.rb | 56 ---------------------- .../parser/functions/is_valid_ip_address_spec.rb | 45 ----------------- .../parser/functions/is_valid_mac_address_spec.rb | 36 -------------- spec/unit/parser/functions/load_json_spec.rb | 29 ----------- spec/unit/parser/functions/load_yaml_spec.rb | 31 ------------ spec/unit/parser/functions/parsejson_spec.rb | 29 +++++++++++ spec/unit/parser/functions/parseyaml_spec.rb | 31 ++++++++++++ 20 files changed, 333 insertions(+), 333 deletions(-) create mode 100644 lib/puppet/parser/functions/is_domain_name.rb create mode 100644 lib/puppet/parser/functions/is_ip_address.rb create mode 100644 lib/puppet/parser/functions/is_mac_address.rb delete mode 100644 lib/puppet/parser/functions/is_valid_domain_name.rb delete mode 100644 lib/puppet/parser/functions/is_valid_ip_address.rb delete mode 100644 lib/puppet/parser/functions/is_valid_mac_address.rb delete mode 100644 lib/puppet/parser/functions/load_json.rb delete mode 100644 lib/puppet/parser/functions/load_yaml.rb create mode 100644 lib/puppet/parser/functions/parsejson.rb create mode 100644 lib/puppet/parser/functions/parseyaml.rb create mode 100644 spec/unit/parser/functions/is_domain_name_spec.rb create mode 100644 spec/unit/parser/functions/is_ip_address_spec.rb create mode 100644 spec/unit/parser/functions/is_mac_address_spec.rb delete mode 100644 spec/unit/parser/functions/is_valid_domain_name_spec.rb delete mode 100644 spec/unit/parser/functions/is_valid_ip_address_spec.rb delete mode 100644 spec/unit/parser/functions/is_valid_mac_address_spec.rb delete mode 100644 spec/unit/parser/functions/load_json_spec.rb delete mode 100644 spec/unit/parser/functions/load_yaml_spec.rb create mode 100644 spec/unit/parser/functions/parsejson_spec.rb create mode 100644 spec/unit/parser/functions/parseyaml_spec.rb diff --git a/lib/puppet/parser/functions/is_domain_name.rb b/lib/puppet/parser/functions/is_domain_name.rb new file mode 100644 index 0000000..4e92939 --- /dev/null +++ b/lib/puppet/parser/functions/is_domain_name.rb @@ -0,0 +1,27 @@ +# +# is_domain_name.rb +# + +module Puppet::Parser::Functions + newfunction(:is_domain_name, :type => :rvalue, :doc => <<-EOS +Returns true if the string passed to this function is a valid IP address. Support for IPv4 and IPv6 address types is included. + EOS + ) do |arguments| + + if (arguments.size != 1) then + raise(Puppet::ParseError, "is_domain_name(): Wrong number of arguments "+ + "given #{arguments.size} for 1") + end + + domain = arguments[0] + + if domain =~ /^(([a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])\.?$/ then + return true + else + return false + end + + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/is_ip_address.rb b/lib/puppet/parser/functions/is_ip_address.rb new file mode 100644 index 0000000..b4a9a15 --- /dev/null +++ b/lib/puppet/parser/functions/is_ip_address.rb @@ -0,0 +1,32 @@ +# +# is_ip_address.rb +# + +module Puppet::Parser::Functions + newfunction(:is_ip_address, :type => :rvalue, :doc => <<-EOS +Returns true if the string passed to this function is a valid IP address. + EOS + ) do |arguments| + + require 'ipaddr' + + if (arguments.size != 1) then + raise(Puppet::ParseError, "is_ip_address(): Wrong number of arguments "+ + "given #{arguments.size} for 1") + end + + begin + ip = IPAddr.new(arguments[0]) + rescue ArgumentError + return false + end + + if ip.ipv4? or ip.ipv6? then + return true + else + return false + end + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/is_mac_address.rb b/lib/puppet/parser/functions/is_mac_address.rb new file mode 100644 index 0000000..1b3088a --- /dev/null +++ b/lib/puppet/parser/functions/is_mac_address.rb @@ -0,0 +1,27 @@ +# +# is_mac_address.rb +# + +module Puppet::Parser::Functions + newfunction(:is_mac_address, :type => :rvalue, :doc => <<-EOS +Returns true if the string passed to this function is a valid mac address. + EOS + ) do |arguments| + + if (arguments.size != 1) then + raise(Puppet::ParseError, "is_mac_address(): Wrong number of arguments "+ + "given #{arguments.size} for 1") + end + + mac = arguments[0] + + if /^[a-fA-F0-9]{1,2}:[a-fA-F0-9]{1,2}:[a-fA-F0-9]{1,2}:[a-fA-F0-9]{1,2}:[a-fA-F0-9]{1,2}:[a-fA-F0-9]{1,2}$/.match(mac) then + return true + else + return false + end + + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/is_valid_domain_name.rb b/lib/puppet/parser/functions/is_valid_domain_name.rb deleted file mode 100644 index 7dd9c0b..0000000 --- a/lib/puppet/parser/functions/is_valid_domain_name.rb +++ /dev/null @@ -1,27 +0,0 @@ -# -# is_valid_domain_name.rb -# - -module Puppet::Parser::Functions - newfunction(:is_valid_domain_name, :type => :rvalue, :doc => <<-EOS -Returns true if the string passed to this function is a valid IP address. Support for IPv4 and IPv6 address types is included. - EOS - ) do |arguments| - - if (arguments.size != 1) then - raise(Puppet::ParseError, "is_valid_domain_name(): Wrong number of arguments "+ - "given #{arguments.size} for 1") - end - - domain = arguments[0] - - if domain =~ /^(([a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])\.?$/ then - return true - else - return false - end - - end -end - -# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/is_valid_ip_address.rb b/lib/puppet/parser/functions/is_valid_ip_address.rb deleted file mode 100644 index 604d938..0000000 --- a/lib/puppet/parser/functions/is_valid_ip_address.rb +++ /dev/null @@ -1,32 +0,0 @@ -# -# is_valid_ip_address.rb -# - -module Puppet::Parser::Functions - newfunction(:is_valid_ip_address, :type => :rvalue, :doc => <<-EOS -Returns true if the string passed to this function is a valid IP address. - EOS - ) do |arguments| - - require 'ipaddr' - - if (arguments.size != 1) then - raise(Puppet::ParseError, "is_valid_ip_address(): Wrong number of arguments "+ - "given #{arguments.size} for 1") - end - - begin - ip = IPAddr.new(arguments[0]) - rescue ArgumentError - return false - end - - if ip.ipv4? or ip.ipv6? then - return true - else - return false - end - end -end - -# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/is_valid_mac_address.rb b/lib/puppet/parser/functions/is_valid_mac_address.rb deleted file mode 100644 index 53199b6..0000000 --- a/lib/puppet/parser/functions/is_valid_mac_address.rb +++ /dev/null @@ -1,27 +0,0 @@ -# -# is_valid_mac_address.rb -# - -module Puppet::Parser::Functions - newfunction(:is_valid_mac_address, :type => :rvalue, :doc => <<-EOS -Returns true if the string passed to this function is a valid mac address. - EOS - ) do |arguments| - - if (arguments.size != 1) then - raise(Puppet::ParseError, "is_valid_mac_address(): Wrong number of arguments "+ - "given #{arguments.size} for 1") - end - - mac = arguments[0] - - if /^[a-fA-F0-9]{1,2}:[a-fA-F0-9]{1,2}:[a-fA-F0-9]{1,2}:[a-fA-F0-9]{1,2}:[a-fA-F0-9]{1,2}:[a-fA-F0-9]{1,2}$/.match(mac) then - return true - else - return false - end - - end -end - -# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/load_json.rb b/lib/puppet/parser/functions/load_json.rb deleted file mode 100644 index 333cf11..0000000 --- a/lib/puppet/parser/functions/load_json.rb +++ /dev/null @@ -1,26 +0,0 @@ -# -# load_json.rb -# - -module Puppet::Parser::Functions - newfunction(:load_json, :type => :rvalue, :doc => <<-EOS -This function accepts JSON as a string and converts into the correct Puppet -structure. - EOS - ) do |arguments| - - if (arguments.size != 1) then - raise(Puppet::ParseError, "load_json(): Wrong number of arguments "+ - "given #{arguments.size} for 1") - end - - json = arguments[0] - - require 'json' - - JSON.load(json) - - end -end - -# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/load_yaml.rb b/lib/puppet/parser/functions/load_yaml.rb deleted file mode 100644 index 2683277..0000000 --- a/lib/puppet/parser/functions/load_yaml.rb +++ /dev/null @@ -1,24 +0,0 @@ -# -# load_yaml.rb -# - -module Puppet::Parser::Functions - newfunction(:load_yaml, :type => :rvalue, :doc => <<-EOS -This function accepts YAML as a string and converts it into the correct -Puppet structure. - EOS - ) do |arguments| - - if (arguments.size != 1) then - raise(Puppet::ParseError, "load_yaml(): Wrong number of arguments "+ - "given #{arguments.size} for 1") - end - - require 'yaml' - - YAML::load(arguments[0]) - - end -end - -# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/parsejson.rb b/lib/puppet/parser/functions/parsejson.rb new file mode 100644 index 0000000..cf3b12c --- /dev/null +++ b/lib/puppet/parser/functions/parsejson.rb @@ -0,0 +1,26 @@ +# +# parsejson.rb +# + +module Puppet::Parser::Functions + newfunction(:parsejson, :type => :rvalue, :doc => <<-EOS +This function accepts JSON as a string and converts into the correct Puppet +structure. + EOS + ) do |arguments| + + if (arguments.size != 1) then + raise(Puppet::ParseError, "parsejson(): Wrong number of arguments "+ + "given #{arguments.size} for 1") + end + + json = arguments[0] + + require 'json' + + JSON.load(json) + + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/parseyaml.rb b/lib/puppet/parser/functions/parseyaml.rb new file mode 100644 index 0000000..e8ac8a4 --- /dev/null +++ b/lib/puppet/parser/functions/parseyaml.rb @@ -0,0 +1,24 @@ +# +# parseyaml.rb +# + +module Puppet::Parser::Functions + newfunction(:parseyaml, :type => :rvalue, :doc => <<-EOS +This function accepts YAML as a string and converts it into the correct +Puppet structure. + EOS + ) do |arguments| + + if (arguments.size != 1) then + raise(Puppet::ParseError, "parseyaml(): Wrong number of arguments "+ + "given #{arguments.size} for 1") + end + + require 'yaml' + + YAML::load(arguments[0]) + + end +end + +# vim: set ts=2 sw=2 et : diff --git a/spec/unit/parser/functions/is_domain_name_spec.rb b/spec/unit/parser/functions/is_domain_name_spec.rb new file mode 100644 index 0000000..ec7c7f5 --- /dev/null +++ b/spec/unit/parser/functions/is_domain_name_spec.rb @@ -0,0 +1,56 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the is_domain_name function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("is_domain_name").should == "function_is_domain_name" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_is_domain_name([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should return true if a valid domain name" do + result = @scope.function_is_domain_name(["foo.bar.com"]) + result.should(be_true) + end + + it "should allow domain parts to start with numbers" do + result = @scope.function_is_domain_name(["3foo.2bar.com"]) + result.should(be_true) + end + + it "should allow domain to end with a dot" do + result = @scope.function_is_domain_name(["3foo.2bar.com."]) + result.should(be_true) + end + + it "should allow a single part domain" do + result = @scope.function_is_domain_name(["orange"]) + result.should(be_true) + end + + it "should return false if domain parts start with hyphens" do + result = @scope.function_is_domain_name(["-3foo.2bar.com"]) + result.should(be_false) + end + + it "should return true if domain contains hyphens" do + result = @scope.function_is_domain_name(["3foo-bar.2bar-fuzz.com"]) + result.should(be_true) + end + + it "should return false if domain name contains spaces" do + result = @scope.function_is_domain_name(["not valid"]) + result.should(be_false) + end + +end diff --git a/spec/unit/parser/functions/is_ip_address_spec.rb b/spec/unit/parser/functions/is_ip_address_spec.rb new file mode 100644 index 0000000..98ce828 --- /dev/null +++ b/spec/unit/parser/functions/is_ip_address_spec.rb @@ -0,0 +1,45 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the is_ip_address function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("is_ip_address").should == "function_is_ip_address" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_is_ip_address([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should return true if an IPv4 address" do + result = @scope.function_is_ip_address(["1.2.3.4"]) + result.should(eq(true)) + end + + it "should return true if a full IPv6 address" do + result = @scope.function_is_ip_address(["fe80:0000:cd12:d123:e2f8:47ff:fe09:dd74"]) + result.should(eq(true)) + end + + it "should return true if a compressed IPv6 address" do + result = @scope.function_is_ip_address(["fe00::1"]) + result.should(eq(true)) + end + + it "should return false if not valid" do + result = @scope.function_is_ip_address(["asdf"]) + result.should(eq(false)) + end + + it "should return false if IP octets out of range" do + result = @scope.function_is_ip_address(["1.1.1.300"]) + result.should(eq(false)) + end +end diff --git a/spec/unit/parser/functions/is_mac_address_spec.rb b/spec/unit/parser/functions/is_mac_address_spec.rb new file mode 100644 index 0000000..c9b9637 --- /dev/null +++ b/spec/unit/parser/functions/is_mac_address_spec.rb @@ -0,0 +1,36 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the is_mac_address function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("is_mac_address").should == "function_is_mac_address" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_is_mac_address([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should return true if a valid mac address" do + result = @scope.function_is_mac_address(["00:a0:1f:12:7f:a0"]) + result.should(eq(true)) + end + + it "should return false if octets are out of range" do + result = @scope.function_is_mac_address(["00:a0:1f:12:7f:g0"]) + result.should(eq(false)) + end + + it "should return false if not valid" do + result = @scope.function_is_mac_address(["not valid"]) + result.should(eq(false)) + end + +end diff --git a/spec/unit/parser/functions/is_valid_domain_name_spec.rb b/spec/unit/parser/functions/is_valid_domain_name_spec.rb deleted file mode 100644 index 3339447..0000000 --- a/spec/unit/parser/functions/is_valid_domain_name_spec.rb +++ /dev/null @@ -1,56 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the is_valid_domain_name function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("is_valid_domain_name").should == "function_is_valid_domain_name" - end - - it "should raise a ParseError if there is less than 1 arguments" do - lambda { @scope.function_is_valid_domain_name([]) }.should( raise_error(Puppet::ParseError)) - end - - it "should return true if a valid domain name" do - result = @scope.function_is_valid_domain_name(["foo.bar.com"]) - result.should(be_true) - end - - it "should allow domain parts to start with numbers" do - result = @scope.function_is_valid_domain_name(["3foo.2bar.com"]) - result.should(be_true) - end - - it "should allow domain to end with a dot" do - result = @scope.function_is_valid_domain_name(["3foo.2bar.com."]) - result.should(be_true) - end - - it "should allow a single part domain" do - result = @scope.function_is_valid_domain_name(["orange"]) - result.should(be_true) - end - - it "should return false if domain parts start with hyphens" do - result = @scope.function_is_valid_domain_name(["-3foo.2bar.com"]) - result.should(be_false) - end - - it "should return true if domain contains hyphens" do - result = @scope.function_is_valid_domain_name(["3foo-bar.2bar-fuzz.com"]) - result.should(be_true) - end - - it "should return false if domain name contains spaces" do - result = @scope.function_is_valid_domain_name(["not valid"]) - result.should(be_false) - end - -end diff --git a/spec/unit/parser/functions/is_valid_ip_address_spec.rb b/spec/unit/parser/functions/is_valid_ip_address_spec.rb deleted file mode 100644 index 2883aaa..0000000 --- a/spec/unit/parser/functions/is_valid_ip_address_spec.rb +++ /dev/null @@ -1,45 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the is_valid_ip_address function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("is_valid_ip_address").should == "function_is_valid_ip_address" - end - - it "should raise a ParseError if there is less than 1 arguments" do - lambda { @scope.function_is_valid_ip_address([]) }.should( raise_error(Puppet::ParseError)) - end - - it "should return true if an IPv4 address" do - result = @scope.function_is_valid_ip_address(["1.2.3.4"]) - result.should(eq(true)) - end - - it "should return true if a full IPv6 address" do - result = @scope.function_is_valid_ip_address(["fe80:0000:cd12:d123:e2f8:47ff:fe09:dd74"]) - result.should(eq(true)) - end - - it "should return true if a compressed IPv6 address" do - result = @scope.function_is_valid_ip_address(["fe00::1"]) - result.should(eq(true)) - end - - it "should return false if not valid" do - result = @scope.function_is_valid_ip_address(["asdf"]) - result.should(eq(false)) - end - - it "should return false if IP octets out of range" do - result = @scope.function_is_valid_ip_address(["1.1.1.300"]) - result.should(eq(false)) - end -end diff --git a/spec/unit/parser/functions/is_valid_mac_address_spec.rb b/spec/unit/parser/functions/is_valid_mac_address_spec.rb deleted file mode 100644 index 62e6fee..0000000 --- a/spec/unit/parser/functions/is_valid_mac_address_spec.rb +++ /dev/null @@ -1,36 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the is_valid_mac_address function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("is_valid_mac_address").should == "function_is_valid_mac_address" - end - - it "should raise a ParseError if there is less than 1 arguments" do - lambda { @scope.function_is_valid_mac_address([]) }.should( raise_error(Puppet::ParseError)) - end - - it "should return true if a valid mac address" do - result = @scope.function_is_valid_mac_address(["00:a0:1f:12:7f:a0"]) - result.should(eq(true)) - end - - it "should return false if octets are out of range" do - result = @scope.function_is_valid_mac_address(["00:a0:1f:12:7f:g0"]) - result.should(eq(false)) - end - - it "should return false if not valid" do - result = @scope.function_is_valid_mac_address(["not valid"]) - result.should(eq(false)) - end - -end diff --git a/spec/unit/parser/functions/load_json_spec.rb b/spec/unit/parser/functions/load_json_spec.rb deleted file mode 100644 index 73a4566..0000000 --- a/spec/unit/parser/functions/load_json_spec.rb +++ /dev/null @@ -1,29 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the load_json function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("load_json").should == "function_load_json" - end - - it "should raise a ParseError if there is less than 1 arguments" do - lambda { @scope.function_load_json([]) }.should( raise_error(Puppet::ParseError)) - end - - it "should convert JSON to a data structure" do - json = <<-EOS -["aaa","bbb","ccc"] -EOS - result = @scope.function_load_json([json]) - result.should(eq(['aaa','bbb','ccc'])) - end - -end diff --git a/spec/unit/parser/functions/load_yaml_spec.rb b/spec/unit/parser/functions/load_yaml_spec.rb deleted file mode 100644 index 2498d12..0000000 --- a/spec/unit/parser/functions/load_yaml_spec.rb +++ /dev/null @@ -1,31 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the load_yaml function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("load_yaml").should == "function_load_yaml" - end - - it "should raise a ParseError if there is less than 1 arguments" do - lambda { @scope.function_load_yaml([]) }.should( raise_error(Puppet::ParseError)) - end - - it "should convert YAML to a data structure" do - yaml = <<-EOS -- aaa -- bbb -- ccc -EOS - result = @scope.function_load_yaml([yaml]) - result.should(eq(['aaa','bbb','ccc'])) - end - -end diff --git a/spec/unit/parser/functions/parsejson_spec.rb b/spec/unit/parser/functions/parsejson_spec.rb new file mode 100644 index 0000000..26eea36 --- /dev/null +++ b/spec/unit/parser/functions/parsejson_spec.rb @@ -0,0 +1,29 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the parsejson function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("parsejson").should == "function_parsejson" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_parsejson([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should convert JSON to a data structure" do + json = <<-EOS +["aaa","bbb","ccc"] +EOS + result = @scope.function_parsejson([json]) + result.should(eq(['aaa','bbb','ccc'])) + end + +end diff --git a/spec/unit/parser/functions/parseyaml_spec.rb b/spec/unit/parser/functions/parseyaml_spec.rb new file mode 100644 index 0000000..f9cb049 --- /dev/null +++ b/spec/unit/parser/functions/parseyaml_spec.rb @@ -0,0 +1,31 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the parseyaml function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("parseyaml").should == "function_parseyaml" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_parseyaml([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should convert YAML to a data structure" do + yaml = <<-EOS +- aaa +- bbb +- ccc +EOS + result = @scope.function_parseyaml([yaml]) + result.should(eq(['aaa','bbb','ccc'])) + end + +end -- cgit v1.2.3