summaryrefslogtreecommitdiffstats
path: root/spec/lib/matchers/json.rb
blob: 798e4cd21826beb1c5d8f6d6c2760bb61f525223 (plain)
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
RSpec::Matchers.define :set_json_attribute do |*attributes|
  def format
    @format ||= Puppet::Network::FormatHandler.format('pson')
  end

  chain :to do |value|
    @value = value
  end

  def json(instance)
    PSON.parse(instance.to_pson)
  end

  def attr_value(attrs, instance)
    attrs = attrs.dup
    hash = json(instance)['data']
    while attrs.length > 0
      name = attrs.shift
      hash = hash[name]
    end
    hash
  end

  match do |instance|
    result = attr_value(attributes, instance)
    if @value
      result == @value
    else
      ! result.nil?
    end
  end

  failure_message_for_should do |instance|
    if @value
      "expected #{instance.inspect} to set #{attributes.inspect} to #{@value.inspect}; got #{attr_value(attributes, instance).inspect}"
    else
      "expected #{instance.inspect} to set #{attributes.inspect} but was nil"
    end
  end

  failure_message_for_should_not do |instance|
    if @value
      "expected #{instance.inspect} not to set #{attributes.inspect} to #{@value.inspect}"
    else
      "expected #{instance.inspect} not to set #{attributes.inspect} to nil"
    end
  end
end

RSpec::Matchers.define :set_json_document_type_to do |type|
  def format
    @format ||= Puppet::Network::FormatHandler.format('pson')
  end

  match do |instance|
    json(instance)['document_type'] == type
  end

  def json(instance)
    PSON.parse(instance.to_pson)
  end

  failure_message_for_should do |instance|
    "expected #{instance.inspect} to set document_type to #{type.inspect}; got #{json(instance)['document_type'].inspect}"
  end

  failure_message_for_should_not do |instance|
    "expected #{instance.inspect} not to set document_type to #{type.inspect}"
  end
end

RSpec::Matchers.define :read_json_attribute do |attribute|
  def format
    @format ||= Puppet::Network::FormatHandler.format('pson')
  end

  chain :from do |value|
    @json = value
  end

  chain :as do |as|
    @value = as
  end

  match do |klass|
    raise "Must specify json with 'from'" unless @json

    @instance = format.intern(klass, @json)
    if @value
      @instance.send(attribute) == @value
    else
      ! @instance.send(attribute).nil?
    end
  end

  failure_message_for_should do |klass|
    if @value
      "expected #{klass} to read #{attribute} from #{@json} as #{@value.inspect}; got #{@instance.send(attribute).inspect}"
    else
      "expected #{klass} to read #{attribute} from #{@json} but was nil"
    end
  end

  failure_message_for_should_not do |klass|
    if @value
      "expected #{klass} not to set #{attribute} to #{@value}"
    else
      "expected #{klass} not to set #{attribute} to nil"
    end
  end
end