File: compare_spec.rb

package info (click to toggle)
puppet-agent 7.23.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 19,092 kB
  • sloc: ruby: 245,074; sh: 456; makefile: 38; xml: 33
file content (147 lines) | stat: -rw-r--r-- 5,220 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
require 'spec_helper'

require 'puppet_spec/compiler'
require 'matchers/resource'

describe 'the compare function' do
  include PuppetSpec::Compiler
  include Matchers::Resource

  {
    [0, 1]     => -1,
    [1, 0]     => 1,
    [1, 1]     => 0,
    [0.0, 1.0] => -1,
    [1.0, 0.0] => 1,
    [1.0, 1.0] => 0,
    [0.0, 1]   => -1,
    [1.0, 0]   => 1,
    [1.0, 1]   => 0,
    [0, 1.0]   => -1,
    [1, 0.0]   => 1,
    [1, 1.0]   => 0,
  }.each_pair do |values, expected|
    it "compares numeric/numeric such that compare(#{values[0]}, #{values[1]}) returns #{expected}" do
      expect(compile_to_catalog("notify { String( compare(#{values[0]},#{values[1]}) == #{expected}): }")).to have_resource("Notify[true]")
    end
  end

  {
    ['"a"', '"b"'] => -1,
    ['"b"', '"a"'] => 1,
    ['"b"', '"b"'] => 0,
    ['"A"', '"b"'] => -1,
    ['"B"', '"a"'] => 1,
    ['"B"', '"b"'] => 0,
  }.each_pair do |values, expected|
    it "compares String values by default such that compare(#{values[0]}, #{values[1]}) returns #{expected}" do
      expect(compile_to_catalog("notify { String( compare(#{values[0]},#{values[1]}) == #{expected}): }")).to have_resource("Notify[true]")
    end

    it "compares String values with third true arg such that compare(#{values[0]}, #{values[1]}, true) returns #{expected}" do
      expect(compile_to_catalog("notify { String( compare(#{values[0]},#{values[1]}, true) == #{expected}): }")).to have_resource("Notify[true]")
    end
  end

  {
    ['"a"', '"b"'] => -1,
    ['"b"', '"a"'] => 1,
    ['"b"', '"b"'] => 0,
    ['"A"', '"b"'] => -1,
    ['"B"', '"a"'] => -1,
    ['"B"', '"b"'] => -1,
  }.each_pair do |values, expected|
    it "compares String values with third arg false such that compare(#{values[0]}, #{values[1]}, false) returns #{expected}" do
      expect(compile_to_catalog("notify { String( compare(#{values[0]},#{values[1]}, false) == #{expected}): }")).to have_resource("Notify[true]")
    end
  end

  {
    ["Semver('1.0.0')", "Semver('2.0.0')"] => -1,
    ["Semver('2.0.0')", "Semver('1.0.0')"] => 1,
    ["Semver('2.0.0')", "Semver('2.0.0')"] => 0,
  }.each_pair do |values, expected|
    it "compares Semver values such that compare(#{values[0]}, #{values[1]}) returns #{expected}" do
      expect(compile_to_catalog("notify { String( compare(#{values[0]},#{values[1]}) == #{expected}): }")).to have_resource("Notify[true]")
    end
  end

  {
    ["Timespan(1)", "Timespan(2)"] => -1,
    ["Timespan(2)", "Timespan(1)"] => 1,
    ["Timespan(1)", "Timespan(1)"] => 0,
    ["Timespan(1)", 2] => -1,
    ["Timespan(2)", 1] => 1,
    ["Timespan(1)", 1] => 0,
    [1, "Timespan(2)"] => -1,
    [2, "Timespan(1)"] => 1,
    [1, "Timespan(1)"] => 0,

    ["Timestamp(1)", "Timestamp(2)"] => -1,
    ["Timestamp(2)", "Timestamp(1)"] => 1,
    ["Timestamp(1)", "Timestamp(1)"] => 0,
    ["Timestamp(1)", 2] => -1,
    ["Timestamp(2)", 1] => 1,
    ["Timestamp(1)", 1] => 0,
    [1, "Timestamp(2)"] => -1,
    [2, "Timestamp(1)"] => 1,
    [1, "Timestamp(1)"] => 0,

  }.each_pair do |values, expected|
    it "compares time values such that compare(#{values[0]}, #{values[1]}) returns #{expected}" do
      expect(compile_to_catalog("notify { String( compare(#{values[0]},#{values[1]}) == #{expected}): }")).to have_resource("Notify[true]")
    end
  end

  context "errors when" do
    [ [true, false],
      ['/x/', '/x/'],
      ['undef', 'undef'],
      ['undef', 1],
      [1, 'undef'],
      [[1], [1]],
      [{'a' => 1}, {'b' => 1}],
    ].each do |a, b|
      it "given values of non comparable types #{a.class}, #{b.class}" do
        expect { compile_to_catalog("compare(#{a},#{b})")}.to raise_error(/Non comparable type/)
      end
    end

    [
      [10, '"hello"'],
      ['"hello"', 10],
      [10.0, '"hello"'],
      ['"hello"', 10.0],
      ['Timespan(1)', '"hello"'],
      ['Timestamp(1)', '"hello"'],
      ['Timespan(1)', 'Semver("1.2.3")'],
      ['Timestamp(1)', 'Semver("1.2.3")'],
    ].each do |a, b|
      it "given values of comparable, but incompatible types #{a.class}, #{b.class}" do
        expect { compile_to_catalog("compare(#{a},#{b})")}.to raise_error(/Can only compare values of the same type/)
      end
    end

      [
        [10, 10],
        [10.0, 10],
        ['Timespan(1)', 'Timespan(1)'],
        ['Timestamp(1)', 'Timestamp(1)'],
        ['Semver("1.2.3")', 'Semver("1.2.3")'],
      ].each do |a, b|
        it "given ignore case true when values are comparable, but not both being strings" do
          expect { compile_to_catalog("compare(#{a},#{b}, true)")}.to raise_error(/can only be used when comparing strings/)
        end
        it "given ignore case false when values are comparable, but not both being strings" do
          expect { compile_to_catalog("compare(#{a},#{b}, false)")}.to raise_error(/can only be used when comparing strings/)
        end
      end

      it "given more than three arguments" do
          expect { compile_to_catalog("compare('a','b', false, false)")}.to raise_error(/Accepts at most 3 arguments, got 4/)
      end
  end

  # Error if not the same except Timestamp and Timespan that accepts Numeric on either side
  # Error for non supported - Boolean, Regexp, etc
end