summaryrefslogtreecommitdiffstats
path: root/spec/lib/matchers/json.rb
diff options
context:
space:
mode:
authorLuke Kanies <luke@puppetlabs.com>2011-04-12 23:54:08 -0700
committerLuke Kanies <luke@puppetlabs.com>2011-04-14 17:07:39 -0700
commite424740d78b8b72dc6bd7ebbbe27b237347d67f5 (patch)
tree9f2afddc64f32990f298689e327ac05fcb2b4719 /spec/lib/matchers/json.rb
parentf37b2e106ed171042c94a1a0b8fd31a254a69588 (diff)
downloadpuppet-e424740d78b8b72dc6bd7ebbbe27b237347d67f5.tar.gz
puppet-e424740d78b8b72dc6bd7ebbbe27b237347d67f5.tar.xz
puppet-e424740d78b8b72dc6bd7ebbbe27b237347d67f5.zip
Adding json-specific matchers
These make the JSON tests much easier to read and write. They're the first custom matchers that I can find, so they're breaking a bit of new ground, but the JSON tests were pretty hard to read and there was a lot of duplication, so it seemed worth it. Note that for some reason they're not working on Facts - it seems to get immediately turned into a full instance by the JSON parsing subsystem, and I've no idea why. Reviewed-by: Daniel Pittman <daniel@puppetlabs.com> Signed-off-by: Luke Kanies <luke@puppetlabs.com>
Diffstat (limited to 'spec/lib/matchers/json.rb')
-rw-r--r--spec/lib/matchers/json.rb111
1 files changed, 111 insertions, 0 deletions
diff --git a/spec/lib/matchers/json.rb b/spec/lib/matchers/json.rb
new file mode 100644
index 000000000..798e4cd21
--- /dev/null
+++ b/spec/lib/matchers/json.rb
@@ -0,0 +1,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