]> gitweb.fluxo.info Git - puppet-stdlib.git/commitdiff
Adds a convert_base function, which can convert numbers between bases
authorfhats <fhats@yelp.com>
Thu, 27 Aug 2015 09:40:20 +0000 (10:40 +0100)
committerDavid Schmitt <david.schmitt@puppetlabs.com>
Thu, 27 Aug 2015 09:42:13 +0000 (10:42 +0100)
Squashed, improved docs, updated error handling and unit tests by David S.

README.markdown
lib/puppet/parser/functions/convert_base.rb [new file with mode: 0644]
spec/functions/convert_base_spec.rb [new file with mode: 0644]

index 591c27bf65aaae70193b787e8c65f3e4f15a4af0..ccbceab5be20434b69085676b7246911ea579884 100644 (file)
@@ -155,6 +155,12 @@ Appends the contents of multiple arrays onto the first array given. For example:
   * `concat(['1','2','3'],'4',['5','6','7'])` returns ['1','2','3','4','5','6','7'].
   *Type*: rvalue.
 
+#### `convert_base`
+
+Converts a given integer or base 10 string representing an integer to a specified base, as a string. For example:
+  * `convert_base(5, 2)` results in: '101'
+  * `convert_base('254', '16')` results in: 'fe'
+
 #### `count`
 
 If called with only an array, it counts the number of elements that are **not** nil/undef. If called with a second argument, counts the number of elements in an array that matches the second argument. *Type*: rvalue.
diff --git a/lib/puppet/parser/functions/convert_base.rb b/lib/puppet/parser/functions/convert_base.rb
new file mode 100644 (file)
index 0000000..0fcbafe
--- /dev/null
@@ -0,0 +1,35 @@
+module Puppet::Parser::Functions
+
+  newfunction(:convert_base, :type => :rvalue, :arity => 2, :doc => <<-'ENDHEREDOC') do |args|
+
+    Converts a given integer or base 10 string representing an integer to a specified base, as a string.
+
+    Usage:
+
+      $binary_repr = convert_base(5, 2)  # $binary_repr is now set to "101"
+      $hex_repr = convert_base("254", "16")  # $hex_repr is now set to "fe"
+
+    ENDHEREDOC
+
+    raise Puppet::ParseError, ("convert_base(): First argument must be either a string or an integer") unless (args[0].is_a?(Integer) or args[0].is_a?(String))
+    raise Puppet::ParseError, ("convert_base(): Second argument must be either a string or an integer") unless (args[1].is_a?(Integer) or args[1].is_a?(String))
+
+    if args[0].is_a?(String)
+      raise Puppet::ParseError, ("convert_base(): First argument must be an integer or a string corresponding to an integer in base 10") unless args[0] =~ /^[0-9]+$/
+    end
+
+    if args[1].is_a?(String)
+      raise Puppet::ParseError, ("convert_base(): First argument must be an integer or a string corresponding to an integer in base 10") unless args[1] =~ /^[0-9]+$/
+    end
+
+    number_to_convert = args[0]
+    new_base = args[1]
+
+    number_to_convert = number_to_convert.to_i()
+    new_base = new_base.to_i()
+
+    raise Puppet::ParseError, ("convert_base(): base must be at least 2 and must not be greater than 36") unless new_base >= 2 and new_base <= 36
+
+    return number_to_convert.to_s(new_base)
+  end
+end
diff --git a/spec/functions/convert_base_spec.rb b/spec/functions/convert_base_spec.rb
new file mode 100644 (file)
index 0000000..8ab2284
--- /dev/null
@@ -0,0 +1,24 @@
+require 'spec_helper'
+
+describe 'convert_base' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(ArgumentError) }
+  it { is_expected.to run.with_params("asdf").and_raise_error(ArgumentError) }
+  it { is_expected.to run.with_params("asdf","moo","cow").and_raise_error(ArgumentError) }
+  it { is_expected.to run.with_params(["1"],"2").and_raise_error(Puppet::ParseError, /argument must be either a string or an integer/) }
+  it { is_expected.to run.with_params("1",["2"]).and_raise_error(Puppet::ParseError, /argument must be either a string or an integer/) }
+  it { is_expected.to run.with_params("1",1).and_raise_error(Puppet::ParseError, /base must be at least 2 and must not be greater than 36/) }
+  it { is_expected.to run.with_params("1",37).and_raise_error(Puppet::ParseError, /base must be at least 2 and must not be greater than 36/) }
+
+  it "should raise a ParseError if argument 1 is a string that does not correspond to an integer in base 10" do
+    is_expected.to run.with_params("ten",6).and_raise_error(Puppet::ParseError, /argument must be an integer or a string corresponding to an integer in base 10/)
+  end
+
+  it "should raise a ParseError if argument 2 is a string and does not correspond to an integer in base 10" do
+    is_expected.to run.with_params(100,"hex").and_raise_error(Puppet::ParseError, /argument must be an integer or a string corresponding to an integer in base 10/)
+  end
+
+  it { is_expected.to run.with_params("11",'16').and_return('b') }
+  it { is_expected.to run.with_params("35",'36').and_return('z') }
+  it { is_expected.to run.with_params(5, 2).and_return('101') }
+end