File: unique_spec.rb

package info (click to toggle)
puppet 5.5.22-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 21,316 kB
  • sloc: ruby: 254,925; sh: 1,608; xml: 219; makefile: 153; sql: 103
file content (111 lines) | stat: -rw-r--r-- 4,566 bytes parent folder | download | duplicates (4)
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
require 'spec_helper'

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

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

  context 'produces the unique set of chars from a String such that' do
    it 'same case is considered unique' do
      expect(compile_to_catalog(<<-SOURCE)).to have_resource('Notify[test]').with_parameter(:message, "abc")
          notify{ 'test': message => 'abcbbcc'.unique }
        SOURCE
    end

    it 'different case is not considered unique' do
      expect(compile_to_catalog(<<-SOURCE)).to have_resource('Notify[test]').with_parameter(:message, "abcABC")
          notify{ 'test': message => 'abcAbbBccC'.unique }
        SOURCE
    end

    it 'case independent matching can be performed with a lambda' do
      expect(compile_to_catalog(<<-SOURCE)).to have_resource('Notify[test]').with_parameter(:message, "abc")
          notify{ 'test': message => 'abcAbbBccC'.unique |$x| { String($x, '%d') } }
        SOURCE
    end

    it 'the first found value in the unique set is used' do
      expect(compile_to_catalog(<<-SOURCE)).to have_resource('Notify[test]').with_parameter(:message, "AbC")
          notify{ 'test': message => 'AbCAbbBccC'.unique |$x| { String($x, '%d') } }
        SOURCE
    end
  end

  context 'produces the unique set of values from an Array such that' do
    it 'ruby equality is used to compute uniqueness by default' do
      expect(compile_to_catalog(<<-SOURCE)).to have_resource('Notify[test]').with_parameter(:message, ['a', 'b', 'c', 'B', 'C'])
          notify{ 'test': message => [a, b, c, a, 'B', 'C'].unique }
        SOURCE
    end

    it 'accepts a lambda to perform the value to use for uniqueness' do
      expect(compile_to_catalog(<<-SOURCE)).to have_resource('Notify[test]').with_parameter(:message, ['a', 'b', 'c'])
          notify{ 'test': message => [a, b, c, a, 'B', 'C'].unique |$x| { String($x, '%d') }}
        SOURCE
    end

    it 'the first found value in the unique set is used' do
      expect(compile_to_catalog(<<-SOURCE)).to have_resource('Notify[test]').with_parameter(:message, ['A', 'b', 'C'])
          notify{ 'test': message => ['A', b, 'C', a, 'B', 'c'].unique |$x| { String($x, '%d') }}
        SOURCE
    end
  end

  context 'produces the unique set of values from an Hash such that' do
    it 'resulting keys and values in hash are arrays' do
      expect(compile_to_catalog(<<-SOURCE)).to have_resource('Notify[test]').with_parameter(:message, {['a'] => [10], ['b']=>[20]})
          notify{ 'test': message => {a => 10, b => 20}.unique }
        SOURCE
    end

    it 'resulting keys contain all keys with same value' do
      expect(compile_to_catalog(<<-SOURCE)).to have_resource('Notify[test]').with_parameter(:message, {['a', 'b'] => [10], ['c']=>[20]})
          notify{ 'test': message => {a => 10, b => 10, c => 20}.unique }
        SOURCE
    end

    it 'resulting values contain Ruby == unique set of values' do
      expect(compile_to_catalog(<<-SOURCE)).to have_resource('Notify[test]').with_parameter(:message, {['a'] => [10], ['b', 'c']=>[11, 20]})
          notify{ 'test': message => {a => 10, b => 11, c => 20}.unique |$x| { if $x > 10 {bigly} else { $x }}}
        SOURCE
    end
  end

  context 'produces the unique set of values from an Iterable' do
    it 'such as reverse_each - in reverse order' do
      expect(compile_to_catalog(<<-SOURCE)).to have_resource('Notify[test]').with_parameter(:message, ['B','b','a'])
          notify{ 'test': message => ['a', 'b', 'B'].reverse_each.unique }
        SOURCE
    end

    it 'such as Integer[1,5]' do
      expect(compile_to_catalog(<<-SOURCE)).to have_resource('Notify[test]').with_parameter(:message, [1,2,3,4,5])
          notify{ 'test': message => Integer[1,5].unique }
        SOURCE
    end

    it 'such as the Integer 3' do
      expect(compile_to_catalog(<<-SOURCE)).to have_resource('Notify[test]').with_parameter(:message, [0,1,2])
          notify{ 'test': message => 3.unique }
        SOURCE
    end

    it 'allows lambda to be used with Iterable' do
      expect(compile_to_catalog(<<-SOURCE)).to have_resource('Notify[test]').with_parameter(:message, ['B','a'])
          notify{ 'test': message => ['a', 'b', 'B'].reverse_each.unique |$x| { String($x, '%d') }}
        SOURCE
    end
  end

  it 'errors when given unsupported data type as input' do
    expect do
      compile_to_catalog(<<-SOURCE)
        undef.unique
      SOURCE
    end.to raise_error(/expects an Iterable value, got Undef/)
  end


end