deltacloud-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lut...@apache.org
Subject [5/9] CIMI unit tests: compact the file hierarchy of tests some
Date Fri, 09 Nov 2012 19:49:39 GMT
http://git-wip-us.apache.org/repos/asf/deltacloud/blob/01305cf1/server/tests/cimi/spec/cimi/model/schema_spec.rb
----------------------------------------------------------------------
diff --git a/server/tests/cimi/spec/cimi/model/schema_spec.rb b/server/tests/cimi/spec/cimi/model/schema_spec.rb
deleted file mode 100644
index 2b82e36..0000000
--- a/server/tests/cimi/spec/cimi/model/schema_spec.rb
+++ /dev/null
@@ -1,245 +0,0 @@
-# Licensed to the Apache Software Foundation (ASF) under one or more
-# contributor license agreements.  See the NOTICE file distributed with
-# this work for additional information regarding copyright ownership.  The
-# ASF licenses this file to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance with the
-# License.  You may obtain a copy of the License at
-#
-#       http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
-# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
-# License for the specific language governing permissions and limitations
-# under the License.
-#
-require 'rubygems'
-require 'require_relative' if RUBY_VERSION < '1.9'
-
-require_relative '../../spec_helper.rb' if require 'minitest/autorun'
-
-describe "Schema" do
-  before do
-    @schema = CIMI::Model::Schema.new
-  end
-
-  it "does not allow adding attributes after being used for conversion" do
-    @schema.scalar(:before)
-    @schema.from_json({})
-    lambda { @schema.scalar(:after) }.must_raise RuntimeError, 'The schema has already been
used to convert objects'
-  end
-
-  describe "scalars" do
-    before(:each) do
-      @schema.scalar(:attr)
-      @schema.text(:camel_hump)
-
-      @schema.attribute_names.must_equal [:attr, :camel_hump]
-    end
-
-    let :sample_xml do
-      parse_xml("<camelHump>bumpy</camelHump>", :keep_root => true)
-    end
-
-    it "should camel case attribute names for JSON" do
-      obj = @schema.from_json("camelHump" => "bumpy")
-      obj.wont_be_nil
-      obj[:camel_hump].must_equal "bumpy"
-
-      json = @schema.to_json(obj)
-      json['camelHump'].must_equal "bumpy"
-    end
-
-    it "should camel case attribute names for XML" do
-      obj = @schema.from_xml(sample_xml)
-
-      obj.wont_be_nil
-      obj[:camel_hump].must_equal "bumpy"
-
-      xml = @schema.to_xml(obj)
-
-      xml['camelHump'].must_equal [{ "content" => "bumpy" }]
-    end
-
-    it "should allow aliasing the XML and JSON name" do
-      @schema.scalar :aliased, :xml_name => "xml", :json_name => "json"
-      obj = @schema.from_xml({"aliased" => "no", "xml" => "yes"}, {})
-      obj[:aliased].must_equal "yes"
-
-      obj = @schema.from_json({"aliased" => "no", "json" => "yes"}, {})
-      obj[:aliased].must_equal "yes"
-    end
-  end
-
-  describe "hrefs" do
-    before do
-      @schema.href(:meter)
-    end
-
-    it "should extract the href attribute from XML" do
-      xml = parse_xml("<meter href='http://example.org/'/>")
-
-      obj = @schema.from_xml(xml)
-      check obj
-      @schema.to_xml(obj).must_equal xml
-    end
-
-    it "should extract the href attribute from JSON" do
-      json = { "meter" =>  { "href" => "http://example.org/" } }
-
-      obj = @schema.from_json(json)
-      check obj
-      @schema.to_json(obj).must_equal json
-    end
-
-    def check(obj)
-      obj.wont_be_nil
-      obj[:meter].href.must_equal 'http://example.org/'
-    end
-  end
-
-  describe "structs" do
-    before do
-      @schema.struct(:struct, :content => :scalar) do
-        scalar   :href
-      end
-      @schema.attribute_names.must_equal [:struct]
-    end
-
-    let(:sample_json) do
-      { "struct" => { "scalar" => "v1", "href" => "http://example.org/" } }
-    end
-
-    let (:sample_xml) do
-      parse_xml("<struct href='http://example.org/'>v1</struct>")
-    end
-
-    let (:sample_xml_no_href) do
-      parse_xml("<struct>v1</struct>")
-    end
-
-    describe "JSON conversion" do
-      it "should convert empty hash" do
-        model = @schema.from_json({ })
-        check_empty_struct model
-        @schema.to_json(model).keys.must_be_empty
-      end
-
-      it "should convert empty body" do
-        model = @schema.from_json({ "struct" => { } })
-        check_empty_struct model
-        @schema.to_json(model).keys.must_be_empty
-      end
-
-      it "should convert values" do
-        model = @schema.from_json(sample_json)
-        check_struct model
-        @schema.to_json(model).must_equal sample_json
-      end
-    end
-
-    describe "XML conversion" do
-      it "should convert empty hash" do
-        model = @schema.from_xml({ })
-        check_empty_struct model
-        @schema.to_xml(model).keys.must_be_empty
-      end
-
-      it "should convert empty body" do
-        model = @schema.from_json({ "struct" => { } })
-        check_empty_struct model
-        @schema.to_xml(model).keys.must_be_empty
-      end
-
-      it "should convert values" do
-        model = @schema.from_xml(sample_xml)
-        check_struct model
-        @schema.to_xml(model).must_equal sample_xml
-      end
-
-      it "should handle missing attributes" do
-        model = @schema.from_xml(sample_xml_no_href)
-        check_struct model, :nil_href => true
-        @schema.to_xml(model).must_equal sample_xml_no_href
-      end
-    end
-
-    def check_struct(obj, opts = {})
-      obj.wont_be_nil
-      obj[:struct].wont_be_nil
-      obj[:struct].scalar.must_equal "v1"
-      if opts[:nil_href]
-        obj[:struct].href.must_be_nil
-      else
-        obj[:struct].href.must_equal "http://example.org/"
-      end
-    end
-
-    def check_empty_struct(obj)
-      obj.wont_be_nil
-      obj[:struct].wont_be_nil
-      obj[:struct].scalar.must_be_nil
-      obj[:struct].href.must_be_nil
-    end
-  end
-
-  describe "arrays" do
-    before do
-      @schema.array(:structs, :content => :scalar) do
-        scalar :href
-      end
-    end
-
-    let(:sample_json) do
-      { "structs" => [{ "scalar" => "v1", "href" => "http://example.org/1" },
-                      { "scalar" => "v2", "href" => "http://example.org/2" }] }
-    end
-
-    let (:sample_xml) do
-      parse_xml("<wrapper>
-  <struct href='http://example.org/1'>v1</struct>
-  <struct href='http://example.org/2'>v2</struct>
-</wrapper>", :keep_root => false)
-    end
-
-    it "should convert missing array from JSON" do
-      obj = @schema.from_json({})
-
-      obj.wont_be_nil
-      obj[:structs].must_be_empty
-      @schema.to_json(obj).keys.must_be_empty
-    end
-
-    it "should convert empty array from JSON" do
-      obj = @schema.from_json("structs" => [])
-
-      obj.wont_be_nil
-      obj[:structs].must_be_empty
-      @schema.to_json(obj).keys.must_be_empty
-    end
-
-    it "should convert arrays from JSON" do
-      obj = @schema.from_json(sample_json)
-
-      check_structs(obj)
-      @schema.to_json(obj).must_equal sample_json
-    end
-
-    it "should convert arrays from XML" do
-      obj = @schema.from_xml(sample_xml)
-
-      check_structs(obj)
-      @schema.to_xml(obj).must_equal sample_xml
-    end
-
-    def check_structs(obj)
-      obj.wont_be_nil
-      obj[:structs].size.must_equal 2
-      obj[:structs][0].scalar.must_equal "v1"
-      obj[:structs][0].href.must_equal "http://example.org/1"
-      obj[:structs][1].scalar.must_equal "v2"
-      obj[:structs][1].href.must_equal "http://example.org/2"
-    end
-  end
-
-end

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/01305cf1/server/tests/cimi/spec/cimi/model/volume_configuration_spec.rb
----------------------------------------------------------------------
diff --git a/server/tests/cimi/spec/cimi/model/volume_configuration_spec.rb b/server/tests/cimi/spec/cimi/model/volume_configuration_spec.rb
deleted file mode 100644
index 1bbdfe9..0000000
--- a/server/tests/cimi/spec/cimi/model/volume_configuration_spec.rb
+++ /dev/null
@@ -1,34 +0,0 @@
-
-# Licensed to the Apache Software Foundation (ASF) under one or more
-# contributor license agreements.  See the NOTICE file distributed with
-# this work for additional information regarding copyright ownership.  The
-# ASF licenses this file to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance with the
-# License.  You may obtain a copy of the License at
-#
-#       http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
-# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
-# License for the specific language governing permissions and limitations
-# under the License.
-#
-require 'rubygems'
-require 'require_relative' if RUBY_VERSION < '1.9'
-
-require_relative '../../spec_helper.rb' if require 'minitest/autorun'
-
-describe "Volume Configuration model" do
-
-  before do
-    @xml = IO::read(File::join(DATA_DIR, "volume_configuration.xml"))
-    @json = IO::read(File::join(DATA_DIR, "volume_configuration.json"))
-  end
-
-  it "can be constructed from XML and JSON" do
-    should_properly_serialize_model CIMI::Model::VolumeConfiguration, @xml, @json
-  end
-
-
-end

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/01305cf1/server/tests/cimi/spec/cimi/model/volume_image_spec.rb
----------------------------------------------------------------------
diff --git a/server/tests/cimi/spec/cimi/model/volume_image_spec.rb b/server/tests/cimi/spec/cimi/model/volume_image_spec.rb
deleted file mode 100644
index 197b2db..0000000
--- a/server/tests/cimi/spec/cimi/model/volume_image_spec.rb
+++ /dev/null
@@ -1,33 +0,0 @@
-# Licensed to the Apache Software Foundation (ASF) under one or more
-# contributor license agreements.  See the NOTICE file distributed with
-# this work for additional information regarding copyright ownership.  The
-# ASF licenses this file to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance with the
-# License.  You may obtain a copy of the License at
-#
-#       http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
-# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
-# License for the specific language governing permissions and limitations
-# under the License.
-#
-require 'rubygems'
-require 'require_relative' if RUBY_VERSION < '1.9'
-
-require_relative '../../spec_helper.rb' if require 'minitest/autorun'
-
-describe "Volume Image model" do
-
-  before do
-    @xml = IO::read(File::join(DATA_DIR, "volume_image.xml"))
-    @json = IO::read(File::join(DATA_DIR, "volume_image.json"))
-  end
-
-  it "can be constructed from XML and JSON" do
-    should_properly_serialize_model CIMI::Model::VolumeImage, @xml, @json
-  end
-
-
-end

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/01305cf1/server/tests/cimi/spec/cimi/model/volume_spec.rb
----------------------------------------------------------------------
diff --git a/server/tests/cimi/spec/cimi/model/volume_spec.rb b/server/tests/cimi/spec/cimi/model/volume_spec.rb
deleted file mode 100644
index 7e6449a..0000000
--- a/server/tests/cimi/spec/cimi/model/volume_spec.rb
+++ /dev/null
@@ -1,32 +0,0 @@
-# Licensed to the Apache Software Foundation (ASF) under one or more
-# contributor license agreements.  See the NOTICE file distributed with
-# this work for additional information regarding copyright ownership.  The
-# ASF licenses this file to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance with the
-# License.  You may obtain a copy of the License at
-#
-#       http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
-# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
-# License for the specific language governing permissions and limitations
-# under the License.
-#
-require 'rubygems'
-require 'require_relative' if RUBY_VERSION < '1.9'
-
-require_relative '../../spec_helper.rb' if require 'minitest/autorun'
-
-describe "Volume model" do
-
-  before do
-    @xml = IO::read(File::join(DATA_DIR, "volume.xml"))
-    @json = IO::read(File::join(DATA_DIR, "volume.json"))
-  end
-
-  it "can be constructed from XML and JSON" do
-    should_properly_serialize_model CIMI::Model::Volume, @xml, @json
-  end
-
-end

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/01305cf1/server/tests/cimi/spec/cimi/model/volume_template_spec.rb
----------------------------------------------------------------------
diff --git a/server/tests/cimi/spec/cimi/model/volume_template_spec.rb b/server/tests/cimi/spec/cimi/model/volume_template_spec.rb
deleted file mode 100644
index bfddcd4..0000000
--- a/server/tests/cimi/spec/cimi/model/volume_template_spec.rb
+++ /dev/null
@@ -1,32 +0,0 @@
-# Licensed to the Apache Software Foundation (ASF) under one or more
-# contributor license agreements.  See the NOTICE file distributed with
-# this work for additional information regarding copyright ownership.  The
-# ASF licenses this file to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance with the
-# License.  You may obtain a copy of the License at
-#
-#       http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
-# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
-# License for the specific language governing permissions and limitations
-# under the License.
-#
-require 'rubygems'
-require 'require_relative' if RUBY_VERSION < '1.9'
-
-require_relative '../../spec_helper.rb' if require 'minitest/autorun'
-
-describe "Volume Template model" do
-
-  before do
-    @xml = IO::read(File::join(DATA_DIR, "volume_template.xml"))
-    @json = IO::read(File::join(DATA_DIR, "volume_template.json"))
-  end
-
-  it "can be constructed from XML and JSON" do
-    should_properly_serialize_model CIMI::Model::VolumeTemplate, @xml, @json
-  end
-
-end

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/01305cf1/server/tests/cimi/spec/spec_helper.rb
----------------------------------------------------------------------
diff --git a/server/tests/cimi/spec/spec_helper.rb b/server/tests/cimi/spec/spec_helper.rb
deleted file mode 100644
index aeffc28..0000000
--- a/server/tests/cimi/spec/spec_helper.rb
+++ /dev/null
@@ -1,136 +0,0 @@
-# Licensed to the Apache Software Foundation (ASF) under one or more
-# contributor license agreements.  See the NOTICE file distributed with
-# this work for additional information regarding copyright ownership.  The
-# ASF licenses this file to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance with the
-# License.  You may obtain a copy of the License at
-#
-#       http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
-# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
-# License for the specific language governing permissions and limitations
-# under the License.
-#
-
-require 'rubygems'
-require 'minitest/autorun'
-require 'minitest/spec'
-require 'xmlsimple'
-require 'require_relative' if RUBY_VERSION < '1.9'
-
-require_relative '../../../lib/deltacloud/core_ext.rb'
-require_relative '../../../lib/cimi/models.rb'
-
-DATA_DIR = File::join(File::expand_path(File::dirname(__FILE__)), 'cimi', 'data')
-
-def parse_xml(xml, opts = {})
-  opts[:force_content] = true
-  opts[:keep_root] = true unless opts.has_key?(:keep_root)
-  XmlSimple.xml_in(xml, opts)
-end
-
-class HashCmp
-  def initialize(exp, act)
-    @exp = exp
-    @act = act
-    @io = StringIO.new
-  end
-
-  def match?
-    @equal = true
-    compare_values(@exp, @act, [])
-    @equal
-  end
-
-  def errors
-    @io.string
-  end
-
-  private
-  def compare_values(exp, act, path)
-    if exp.is_a?(String)
-      mismatch("entries differ", exp, act, path) unless exp == act
-    elsif exp.is_a?(Array)
-      mismatch("expected array", exp, act, path) unless act.is_a?(Array)
-      unless act.size == exp.size
-        mismatch("different array lengths", exp, act, path)
-      end
-      name = path.pop
-      if name == "property"
-        # Special hack for properties, since they truly are a hash
-        # and therefore not ordered, even though XmlSimple
-        # represents them as an array; since that array can be generated
-        # from a hash, in Ruby 1.8 the order of the array is random
-        exp = exp.sort! { |item1, item2| item1["name"] <=> item2["name"] }
-        act = act.sort! { |item1, item2| item1["name"] <=> item2["name"] }
-      end
-      0.upto(exp.size-1) do |i|
-        compare_values(exp[i], act[i], path + [ "#{name}[#{i}]" ])
-      end
-    elsif exp.is_a?(Hash)
-      unless act.is_a?(Hash)
-        mismatch("expected Hash", exp, act, path)
-        return
-      end
-      unless (missing = exp.keys - act.keys).empty?
-        error "Missing key(s) at /#{path.join("/")}: #{missing.inspect}"
-      end
-      unless (excess = act.keys - exp.keys).empty?
-        error "Excess key(s) at /#{path.join("/")}: #{excess.inspect}"
-      end
-      (exp.keys - missing - excess).each do |k|
-        compare_values(exp[k], act[k], path + [ k ])
-      end
-    end
-  end
-
-  def mismatch(msg, exp, act, path)
-    error "#{msg}[#{fmt(path)}]: #{exp.inspect} != #{act.inspect}"
-  end
-
-  def error(msg)
-    @equal = false
-    @io.puts msg
-  end
-
-  def fmt(path)
-    "/#{path.join("/")}"
-  end
-end
-
-def should_properly_serialize_model(model_class, xml, json)
-  # Roundtrip in same format
-  model_class.from_xml(xml).must_serialize_to xml, :fmt => :xml
-  model_class.from_json(json).must_serialize_to json, :fmt => :json
-  # Roundtrip crossing format
-  model_class.from_xml(xml).must_serialize_to json, :fmt => :json
-  model_class.from_json(json).must_serialize_to xml, :fmt => :xml
-end
-
-module MiniTest::Assertions
-
-  def assert_serialize_to(exp, act, opts)
-    raise "missing format; use :fmt => [:xml || :json]" if opts[:fmt].nil?
-    exp, act = [exp, act].map { |x| convert(x, opts[:fmt]) }
-    m = HashCmp.new(exp, act)
-    assert m.match?,  "#{opts[:fmt].to_s.upcase} documents do not match\n" + m.errors
-  end
-
-  def convert(x, fmt)
-    if fmt == :json
-      x = x.to_json if x.is_a?(CIMI::Model::Base)
-      x = JSON.parse(x) if x.is_a?(String)
-    elsif fmt == :xml
-      x = x.to_xml if x.is_a?(CIMI::Model::Base)
-      x = parse_xml(x)  if x.is_a?(String)
-    else
-      raise "Invalid format #{fmt}"
-    end
-    x
-  end
-
-end
-
-CIMI::Model::Base.infect_an_assertion :assert_serialize_to, :must_serialize_to

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/01305cf1/server/tests/cimi/spec_helper.rb
----------------------------------------------------------------------
diff --git a/server/tests/cimi/spec_helper.rb b/server/tests/cimi/spec_helper.rb
new file mode 100644
index 0000000..e1f2ee4
--- /dev/null
+++ b/server/tests/cimi/spec_helper.rb
@@ -0,0 +1,136 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.  The
+# ASF licenses this file to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance with the
+# License.  You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+# License for the specific language governing permissions and limitations
+# under the License.
+#
+
+require 'rubygems'
+require 'minitest/autorun'
+require 'minitest/spec'
+require 'xmlsimple'
+require 'require_relative' if RUBY_VERSION < '1.9'
+
+require_relative '../../lib/deltacloud/core_ext.rb'
+require_relative '../../lib/cimi/models.rb'
+
+DATA_DIR = File::join(File::expand_path(File::dirname(__FILE__)), 'data')
+
+def parse_xml(xml, opts = {})
+  opts[:force_content] = true
+  opts[:keep_root] = true unless opts.has_key?(:keep_root)
+  XmlSimple.xml_in(xml, opts)
+end
+
+class HashCmp
+  def initialize(exp, act)
+    @exp = exp
+    @act = act
+    @io = StringIO.new
+  end
+
+  def match?
+    @equal = true
+    compare_values(@exp, @act, [])
+    @equal
+  end
+
+  def errors
+    @io.string
+  end
+
+  private
+  def compare_values(exp, act, path)
+    if exp.is_a?(String)
+      mismatch("entries differ", exp, act, path) unless exp == act
+    elsif exp.is_a?(Array)
+      mismatch("expected array", exp, act, path) unless act.is_a?(Array)
+      unless act.size == exp.size
+        mismatch("different array lengths", exp, act, path)
+      end
+      name = path.pop
+      if name == "property"
+        # Special hack for properties, since they truly are a hash
+        # and therefore not ordered, even though XmlSimple
+        # represents them as an array; since that array can be generated
+        # from a hash, in Ruby 1.8 the order of the array is random
+        exp = exp.sort! { |item1, item2| item1["name"] <=> item2["name"] }
+        act = act.sort! { |item1, item2| item1["name"] <=> item2["name"] }
+      end
+      0.upto(exp.size-1) do |i|
+        compare_values(exp[i], act[i], path + [ "#{name}[#{i}]" ])
+      end
+    elsif exp.is_a?(Hash)
+      unless act.is_a?(Hash)
+        mismatch("expected Hash", exp, act, path)
+        return
+      end
+      unless (missing = exp.keys - act.keys).empty?
+        error "Missing key(s) at /#{path.join("/")}: #{missing.inspect}"
+      end
+      unless (excess = act.keys - exp.keys).empty?
+        error "Excess key(s) at /#{path.join("/")}: #{excess.inspect}"
+      end
+      (exp.keys - missing - excess).each do |k|
+        compare_values(exp[k], act[k], path + [ k ])
+      end
+    end
+  end
+
+  def mismatch(msg, exp, act, path)
+    error "#{msg}[#{fmt(path)}]: #{exp.inspect} != #{act.inspect}"
+  end
+
+  def error(msg)
+    @equal = false
+    @io.puts msg
+  end
+
+  def fmt(path)
+    "/#{path.join("/")}"
+  end
+end
+
+def should_properly_serialize_model(model_class, xml, json)
+  # Roundtrip in same format
+  model_class.from_xml(xml).must_serialize_to xml, :fmt => :xml
+  model_class.from_json(json).must_serialize_to json, :fmt => :json
+  # Roundtrip crossing format
+  model_class.from_xml(xml).must_serialize_to json, :fmt => :json
+  model_class.from_json(json).must_serialize_to xml, :fmt => :xml
+end
+
+module MiniTest::Assertions
+
+  def assert_serialize_to(exp, act, opts)
+    raise "missing format; use :fmt => [:xml || :json]" if opts[:fmt].nil?
+    exp, act = [exp, act].map { |x| convert(x, opts[:fmt]) }
+    m = HashCmp.new(exp, act)
+    assert m.match?,  "#{opts[:fmt].to_s.upcase} documents do not match\n" + m.errors
+  end
+
+  def convert(x, fmt)
+    if fmt == :json
+      x = x.to_json if x.is_a?(CIMI::Model::Base)
+      x = JSON.parse(x) if x.is_a?(String)
+    elsif fmt == :xml
+      x = x.to_xml if x.is_a?(CIMI::Model::Base)
+      x = parse_xml(x)  if x.is_a?(String)
+    else
+      raise "Invalid format #{fmt}"
+    end
+    x
+  end
+
+end
+
+CIMI::Model::Base.infect_an_assertion :assert_serialize_to, :must_serialize_to


Mime
View raw message