diff --git a/lib/puppet/pops/utils.rb b/lib/puppet/pops/utils.rb index 7fd98c58f..69085cef1 100644 --- a/lib/puppet/pops/utils.rb +++ b/lib/puppet/pops/utils.rb @@ -1,122 +1,142 @@ # Provides utility methods module Puppet::Pops::Utils # Can the given o be converted to numeric? (or is numeric already) # Accepts a leading '::' # Returns a boolean if the value is numeric # If testing if value can be converted it is more efficient to call {#to_n} or {#to_n_with_radix} directly # and check if value is nil. def self.is_numeric?(o) case o when Numeric true else !!Puppet::Pops::Patterns::NUMERIC.match(relativize_name(o.to_s)) end end + # Convert a match from Puppet::Pops::Patterns::NUMERIC to floating point value if + # possible + def self.match_to_fp(match) + if match[5].to_s.length > 0 + # Use default radix (default is decimal == 10) for floats + # Do not convert a value that is 0 raised to 10^somevalue to float - the value is always 0 + # i.e. 0000.0e1, 0e1, 0.0000e1 + if Integer(match[4]) == 0 && match[5] =~ /\A\.?0*[eE].*\z/ + nil + else + fp_value = Float(match[2]) + if fp_value != Puppet::Pops::Types::TypeCalculator::TheInfinity + match[1] == '-' ? -fp_value : fp_value + else + nil + end + end + end + end + # To Numeric with radix, or nil if not a number. # If the value is already Numeric it is returned verbatim with a radix of 10. # @param o [String, Number] a string containing a number in octal, hex, integer (decimal) or floating point form # with optional sign +/- # @return [Array, nil] array with converted number and radix, or nil if not possible to convert # @api public # def self.to_n_with_radix o begin case o when String match = Puppet::Pops::Patterns::NUMERIC.match(relativize_name(o)) if !match nil elsif match[5].to_s.length > 0 - # Use default radix (default is decimal == 10) for floats - match[1] == '-' ? [-Float(match[2]), 10] : [Float(match[2]), 10] + fp_value = match_to_fp(match) + fp_value.nil? ? nil : [fp_value, 10] else # Set radix (default is decimal == 10) radix = 10 if match[3].to_s.length > 0 radix = 16 elsif match[4].to_s.length > 1 && match[4][0,1] == '0' radix = 8 end # Ruby 1.8.7 does not have a second argument to Kernel method that creates an # integer from a string, it relies on the prefix 0x, 0X, 0 (and unsupported in puppet binary 'b') # We have the correct string here, match[2] is safe to parse without passing on radix match[1] == '-' ? [-Integer(match[2]), radix] : [Integer(match[2]), radix] end when Numeric # Impossible to calculate radix, assume decimal [o, 10] else nil end rescue ArgumentError nil end end # To Numeric (or already numeric) # Returns nil if value is not numeric, else an Integer or Float. A String may have an optional sign. # # A leading '::' is accepted (and ignored) # def self.to_n o begin case o when String match = Puppet::Pops::Patterns::NUMERIC.match(relativize_name(o)) if !match nil elsif match[5].to_s.length > 0 - match[1] == '-' ? -Float(match[2]) : Float(match[2]) + match_to_fp(match) else match[1] == '-' ? -Integer(match[2]) : Integer(match[2]) end when Numeric o else nil end rescue ArgumentError nil end end # is the name absolute (i.e. starts with ::) def self.is_absolute? name name.start_with? "::" end def self.name_to_segments name name.split("::") end def self.relativize_name name is_absolute?(name) ? name[2..-1] : name end # Finds an existing adapter for o or for one of its containers, or nil, if none of the containers # was adapted with the given adapter. # This method can only be used with objects that respond to `:eContainer`. # with true. # # @see #find_closest_positioned # def self.find_adapter(o, adapter) return nil if o.nil? || (o.is_a?(Array) && o.empty?) a = adapter.get(o) return a if a return find_adapter(o.eContainer, adapter) end # Finds the closest positioned Puppet::Pops::Model::Positioned object, or object decorated with # a SourcePosAdapter, and returns # a SourcePosAdapter for the first found, or nil if not found. # def self.find_closest_positioned(o) return nil if o.nil? || o.is_a?(Puppet::Pops::Model::Program) || (o.is_a?(Array) && o.empty?) return find_adapter(o, Puppet::Pops::Adapters::SourcePosAdapter) unless o.is_a?(Puppet::Pops::Model::Positioned) o.offset.nil? ? find_closest_positioned(o.eContainer) : Puppet::Pops::Adapters::SourcePosAdapter.adapt(o) end end diff --git a/spec/unit/pops/utils_spec.rb b/spec/unit/pops/utils_spec.rb new file mode 100644 index 000000000..b6f414046 --- /dev/null +++ b/spec/unit/pops/utils_spec.rb @@ -0,0 +1,70 @@ +require 'spec_helper' +require 'puppet/pops' + +describe 'pops utils' do + context 'when converting strings to numbers' do + it 'should convert "0" to 0' do + expect(Puppet::Pops::Utils.to_n("0")).to eq(0) + end + + it 'should convert "0" to 0 with radix' do + expect(Puppet::Pops::Utils.to_n_with_radix("0")).to eq([0, 10]) + end + + it 'should convert "0.0" to 0.0' do + expect(Puppet::Pops::Utils.to_n("0.0")).to eq(0.0) + end + + it 'should convert "0.0" to 0.0 with radix' do + expect(Puppet::Pops::Utils.to_n_with_radix("0.0")).to eq([0.0, 10]) + end + + it 'should convert "0.01e1" to 0.01e1' do + expect(Puppet::Pops::Utils.to_n("0.01e1")).to eq(0.01e1) + expect(Puppet::Pops::Utils.to_n("0.01E1")).to eq(0.01e1) + end + + it 'should convert "0.01e1" to 0.01e1 with radix' do + expect(Puppet::Pops::Utils.to_n_with_radix("0.01e1")).to eq([0.01e1, 10]) + expect(Puppet::Pops::Utils.to_n_with_radix("0.01E1")).to eq([0.01e1, 10]) + end + + it 'should not convert "0e1" to floating point' do + expect(Puppet::Pops::Utils.to_n("0e1")).to be_nil + expect(Puppet::Pops::Utils.to_n("0E1")).to be_nil + end + + it 'should not convert "0e1" to floating point with radix' do + expect(Puppet::Pops::Utils.to_n_with_radix("0e1")).to be_nil + expect(Puppet::Pops::Utils.to_n_with_radix("0E1")).to be_nil + end + + it 'should not convert "0.0e1" to floating point' do + expect(Puppet::Pops::Utils.to_n("0.0e1")).to be_nil + expect(Puppet::Pops::Utils.to_n("0.0E1")).to be_nil + end + + it 'should not convert "0.0e1" to floating point with radix' do + expect(Puppet::Pops::Utils.to_n_with_radix("0.0e1")).to be_nil + expect(Puppet::Pops::Utils.to_n_with_radix("0.0E1")).to be_nil + end + + it 'should not convert "000000.0000e1" to floating point' do + expect(Puppet::Pops::Utils.to_n("000000.0000e1")).to be_nil + expect(Puppet::Pops::Utils.to_n("000000.0000E1")).to be_nil + end + + it 'should not convert "000000.0000e1" to floating point with radix' do + expect(Puppet::Pops::Utils.to_n_with_radix("000000.0000e1")).to be_nil + expect(Puppet::Pops::Utils.to_n_with_radix("000000.0000E1")).to be_nil + end + + it 'should not convert infinite values to floating point' do + expect(Puppet::Pops::Utils.to_n("4e999")).to be_nil + end + + it 'should not convert infinite values to floating point with_radix' do + expect(Puppet::Pops::Utils.to_n_with_radix("4e999")).to be_nil + end + end +end \ No newline at end of file