diff options
Diffstat (limited to 'spec/unit')
-rwxr-xr-x | spec/unit/application/puppet.rb | 30 | ||||
-rw-r--r-- | spec/unit/application/puppetmasterd.rb | 12 | ||||
-rwxr-xr-x | spec/unit/indirector/queue.rb | 30 | ||||
-rwxr-xr-x | spec/unit/network/formats.rb | 78 | ||||
-rwxr-xr-x | spec/unit/relationship.rb | 67 | ||||
-rwxr-xr-x | spec/unit/resource.rb | 63 | ||||
-rwxr-xr-x | spec/unit/resource/catalog.rb | 93 | ||||
-rwxr-xr-x | spec/unit/util/json.rb | 18 |
8 files changed, 181 insertions, 210 deletions
diff --git a/spec/unit/application/puppet.rb b/spec/unit/application/puppet.rb index 3c1143060..f4d606535 100755 --- a/spec/unit/application/puppet.rb +++ b/spec/unit/application/puppet.rb @@ -332,17 +332,17 @@ describe "Puppet" do end describe "the 'apply' command" do - confine "JSON library is missing; cannot test applying catalogs" => Puppet.features.json? + confine "PSON library is missing; cannot test applying catalogs" => Puppet.features.pson? before do - #Puppet::Resource::Catalog.stubs(:json_create).returns Puppet::Resource::Catalog.new - JSON.stubs(:parse).returns Puppet::Resource::Catalog.new + #Puppet::Resource::Catalog.stubs(:pson_create).returns Puppet::Resource::Catalog.new + PSON.stubs(:parse).returns Puppet::Resource::Catalog.new end it "should read the catalog in from disk if a file name is provided" do - @puppet.options[:catalog] = "/my/catalog.json" + @puppet.options[:catalog] = "/my/catalog.pson" - File.expects(:read).with("/my/catalog.json").returns "something" + File.expects(:read).with("/my/catalog.pson").returns "something" @puppet.apply end @@ -355,41 +355,41 @@ describe "Puppet" do @puppet.apply end - it "should deserialize the catalog from json" do - @puppet.options[:catalog] = "/my/catalog.json" + it "should deserialize the catalog from pson" do + @puppet.options[:catalog] = "/my/catalog.pson" File.expects(:read).returns "something" - JSON.expects(:parse).with("something").returns Puppet::Resource::Catalog.new + PSON.expects(:parse).with("something").returns Puppet::Resource::Catalog.new @puppet.apply end it "should fail helpfully if deserializing fails" do - @puppet.options[:catalog] = "/my/catalog.json" + @puppet.options[:catalog] = "/my/catalog.pson" File.expects(:read).returns "something" - JSON.expects(:parse).raises ArgumentError + PSON.expects(:parse).raises ArgumentError lambda { @puppet.apply }.should raise_error(Puppet::Error) end it "should convert plain data structures into a catalog if deserialization does not do so" do - @puppet.options[:catalog] = "/my/catalog.json" + @puppet.options[:catalog] = "/my/catalog.pson" File.expects(:read).returns "something" - JSON.expects(:parse).with("something").returns({:foo => "bar"}) - Puppet::Resource::Catalog.expects(:json_create).with({:foo => "bar"}).returns(Puppet::Resource::Catalog.new) + PSON.expects(:parse).with("something").returns({:foo => "bar"}) + Puppet::Resource::Catalog.expects(:pson_create).with({:foo => "bar"}).returns(Puppet::Resource::Catalog.new) @puppet.apply end it "should convert the catalog to a RAL catalog and use a Configurer instance to apply it" do - @puppet.options[:catalog] = "/my/catalog.json" + @puppet.options[:catalog] = "/my/catalog.pson" File.expects(:read).returns "something" catalog = Puppet::Resource::Catalog.new - JSON.expects(:parse).returns catalog + PSON.expects(:parse).returns catalog catalog.expects(:to_ral).returns "mycatalog" diff --git a/spec/unit/application/puppetmasterd.rb b/spec/unit/application/puppetmasterd.rb index a4875c713..980138782 100644 --- a/spec/unit/application/puppetmasterd.rb +++ b/spec/unit/application/puppetmasterd.rb @@ -294,11 +294,11 @@ describe "PuppetMaster" do Puppet.stubs(:err) @puppetmasterd.stubs(:exit) Puppet::Parser::Interpreter.stubs(:new).returns(@interpreter) - Puppet.features.stubs(:json?).returns true + Puppet.features.stubs(:pson?).returns true end - it "should fail if json isn't available" do - Puppet.features.expects(:json?).returns false + it "should fail if pson isn't available" do + Puppet.features.expects(:pson?).returns false lambda { @puppetmasterd.compile }.should raise_error end @@ -310,13 +310,13 @@ describe "PuppetMaster" do @puppetmasterd.compile end - it "should render the catalog to json and print the output" do + it "should render the catalog to pson and print the output" do @puppetmasterd.options[:node] = "foo" catalog = Puppet::Resource::Catalog.new - catalog.expects(:render).with(:json).returns "myjson" + catalog.expects(:render).with(:pson).returns "mypson" Puppet::Resource::Catalog.expects(:find).returns catalog - $stdout.expects(:puts).with("myjson") + $stdout.expects(:puts).with("mypson") @puppetmasterd.compile end diff --git a/spec/unit/indirector/queue.rb b/spec/unit/indirector/queue.rb index c81cefec9..0748b5817 100755 --- a/spec/unit/indirector/queue.rb +++ b/spec/unit/indirector/queue.rb @@ -9,25 +9,25 @@ end class FooExampleData attr_accessor :name - def self.json_create(json) - new(json['data'].to_sym) + def self.pson_create(pson) + new(pson['data'].to_sym) end def initialize(name = nil) @name = name if name end - def render(format = :json) - to_json + def render(format = :pson) + to_pson end - def to_json(*args) - {:json_class => self.class.to_s, :data => name}.to_json(*args) + def to_pson(*args) + {:type => self.class.to_s, :data => name}.to_pson(*args) end end describe Puppet::Indirector::Queue do - confine "JSON library is missing; cannot test queueing" => Puppet.features.json? + confine "PSON library is missing; cannot test queueing" => Puppet.features.pson? before :each do @model = mock 'model' @@ -51,8 +51,8 @@ describe Puppet::Indirector::Queue do @request = stub 'request', :key => :me, :instance => @subject end - it "should require JSON" do - Puppet.features.expects(:json?).returns false + it "should require PSON" do + Puppet.features.expects(:pson?).returns false lambda { @store_class.new }.should raise_error(ArgumentError) end @@ -63,16 +63,16 @@ describe Puppet::Indirector::Queue do end describe "when saving" do - it 'should render the instance using json' do - @subject.expects(:render).with(:json) + it 'should render the instance using pson' do + @subject.expects(:render).with(:pson) @store.client.stubs(:send_message) @store.save(@request) end it "should send the rendered message to the appropriate queue on the client" do - @subject.expects(:render).returns "myjson" + @subject.expects(:render).returns "mypson" - @store.client.expects(:send_message).with(:my_queue, "myjson") + @store.client.expects(:send_message).with(:my_queue, "mypson") @store.save(@request) end @@ -89,8 +89,8 @@ describe Puppet::Indirector::Queue do @store_class.stubs(:model).returns @model end - it "should use the model's Format support to intern the message from json" do - @model.expects(:convert_from).with(:json, "mymessage") + it "should use the model's Format support to intern the message from pson" do + @model.expects(:convert_from).with(:pson, "mymessage") @store_class.client.expects(:subscribe).yields("mymessage") @store_class.subscribe {|o| o } diff --git a/spec/unit/network/formats.rb b/spec/unit/network/formats.rb index c766f55f0..de2e0afe3 100755 --- a/spec/unit/network/formats.rb +++ b/spec/unit/network/formats.rb @@ -4,13 +4,13 @@ require File.dirname(__FILE__) + '/../../spec_helper' require 'puppet/network/formats' -class JsonTest +class PsonTest attr_accessor :string def ==(other) string == other.string end - def self.from_json(data) + def self.from_pson(data) new(data) end @@ -18,11 +18,11 @@ class JsonTest @string = string end - def to_json(*args) + def to_pson(*args) { - 'json_class' => self.class.name, + 'type' => self.class.name, 'data' => @string - }.to_json(*args) + }.to_pson(*args) end end @@ -173,76 +173,76 @@ describe "Puppet Network Format" do end end - it "should include a json format" do - Puppet::Network::FormatHandler.format(:json).should_not be_nil + it "should include a pson format" do + Puppet::Network::FormatHandler.format(:pson).should_not be_nil end - describe "json" do - confine "Missing 'json' library" => Puppet.features.json? + describe "pson" do + confine "Missing 'pson' library" => Puppet.features.pson? before do - @json = Puppet::Network::FormatHandler.format(:json) + @pson = Puppet::Network::FormatHandler.format(:pson) end - it "should have its mime type set to text/json" do - Puppet::Network::FormatHandler.format(:json).mime.should == "text/json" + it "should have its mime type set to text/pson" do + Puppet::Network::FormatHandler.format(:pson).mime.should == "text/pson" end it "should require the :render_method" do - Puppet::Network::FormatHandler.format(:json).required_methods.should be_include(:render_method) + Puppet::Network::FormatHandler.format(:pson).required_methods.should be_include(:render_method) end it "should require the :intern_method" do - Puppet::Network::FormatHandler.format(:json).required_methods.should be_include(:intern_method) + Puppet::Network::FormatHandler.format(:pson).required_methods.should be_include(:intern_method) end it "should have a weight of 10" do - @json.weight.should == 10 + @pson.weight.should == 10 end describe "when supported" do - it "should render by calling 'to_json' on the instance" do - instance = JsonTest.new("foo") - instance.expects(:to_json).returns "foo" - @json.render(instance).should == "foo" + it "should render by calling 'to_pson' on the instance" do + instance = PsonTest.new("foo") + instance.expects(:to_pson).returns "foo" + @pson.render(instance).should == "foo" end - it "should render multiple instances by calling 'to_json' on the array" do + it "should render multiple instances by calling 'to_pson' on the array" do instances = [mock('instance')] - instances.expects(:to_json).returns "foo" + instances.expects(:to_pson).returns "foo" - @json.render_multiple(instances).should == "foo" + @pson.render_multiple(instances).should == "foo" end - it "should intern by calling 'JSON.parse' on the text and then using from_json to convert the data into an instance" do + it "should intern by calling 'PSON.parse' on the text and then using from_pson to convert the data into an instance" do text = "foo" - JSON.expects(:parse).with("foo").returns("json_class" => "JsonTest", "data" => "foo") - JsonTest.expects(:from_json).with("foo").returns "parsed_json" - @json.intern(JsonTest, text).should == "parsed_json" + PSON.expects(:parse).with("foo").returns("type" => "PsonTest", "data" => "foo") + PsonTest.expects(:from_pson).with("foo").returns "parsed_pson" + @pson.intern(PsonTest, text).should == "parsed_pson" end - it "should not render twice if 'JSON.parse' creates the appropriate instance" do + it "should not render twice if 'PSON.parse' creates the appropriate instance" do text = "foo" - instance = JsonTest.new("foo") - JSON.expects(:parse).with("foo").returns(instance) - JsonTest.expects(:from_json).never - @json.intern(JsonTest, text).should equal(instance) + instance = PsonTest.new("foo") + PSON.expects(:parse).with("foo").returns(instance) + PsonTest.expects(:from_pson).never + @pson.intern(PsonTest, text).should equal(instance) end - it "should intern by calling 'JSON.parse' on the text and then using from_json to convert the actual into an instance if the json has no class/data separation" do + it "should intern by calling 'PSON.parse' on the text and then using from_pson to convert the actual into an instance if the pson has no class/data separation" do text = "foo" - JSON.expects(:parse).with("foo").returns("foo") - JsonTest.expects(:from_json).with("foo").returns "parsed_json" - @json.intern(JsonTest, text).should == "parsed_json" + PSON.expects(:parse).with("foo").returns("foo") + PsonTest.expects(:from_pson).with("foo").returns "parsed_pson" + @pson.intern(PsonTest, text).should == "parsed_pson" end it "should intern multiples by parsing the text and using 'class.intern' on each resulting data structure" do text = "foo" - JSON.expects(:parse).with("foo").returns ["bar", "baz"] - JsonTest.expects(:from_json).with("bar").returns "BAR" - JsonTest.expects(:from_json).with("baz").returns "BAZ" - @json.intern_multiple(JsonTest, text).should == %w{BAR BAZ} + PSON.expects(:parse).with("foo").returns ["bar", "baz"] + PsonTest.expects(:from_pson).with("bar").returns "BAR" + PsonTest.expects(:from_pson).with("baz").returns "BAZ" + @pson.intern_multiple(PsonTest, text).should == %w{BAR BAZ} end end end diff --git a/spec/unit/relationship.rb b/spec/unit/relationship.rb index fd7e0aaf7..5a52cb587 100755 --- a/spec/unit/relationship.rb +++ b/spec/unit/relationship.rb @@ -155,68 +155,43 @@ describe Puppet::Relationship, " when matching edges with a non-standard event" end end -describe Puppet::Relationship, "when converting to json" do - confine "Missing 'json' library" => Puppet.features.json? +describe Puppet::Relationship, "when converting to pson" do + confine "Missing 'pson' library" => Puppet.features.pson? before do @edge = Puppet::Relationship.new(:a, :b, :event => :random, :callback => :whatever) end - def json_output_should - @edge.class.expects(:json_create).with { |hash| yield hash } - end - - # LAK:NOTE For all of these tests, we convert back to the edge so we can - # trap the actual data structure then. - it "should set the 'json_class' to Puppet::Relationship" do - json_output_should { |hash| hash['json_class'] == "Puppet::Relationship" } - - JSON.parse @edge.to_json - end - it "should store the stringified source as the source in the data" do - json_output_should { |hash| hash['data']['source'] == "a" } - - JSON.parse @edge.to_json + PSON.parse(@edge.to_pson)["source"].should == "a" end it "should store the stringified target as the target in the data" do - json_output_should { |hash| hash['data']['target'] == "b" } - - JSON.parse @edge.to_json + PSON.parse(@edge.to_pson)['target'].should == "b" end - it "should store the jsonified event as the event in the data" do - @edge.event = :random - json_output_should { |hash| hash['data']['event'] == "random" } - - JSON.parse @edge.to_json + it "should store the psonified event as the event in the data" do + PSON.parse(@edge.to_pson)["event"].should == "random" end it "should not store an event when none is set" do @edge.event = nil - json_output_should { |hash| hash['data']['event'].nil? } - - JSON.parse @edge.to_json + PSON.parse(@edge.to_pson)["event"].should be_nil end - it "should store the jsonified callback as the callback in the data" do + it "should store the psonified callback as the callback in the data" do @edge.callback = "whatever" - json_output_should { |hash| hash['data']['callback'] == "whatever" } - - JSON.parse @edge.to_json + PSON.parse(@edge.to_pson)["callback"].should == "whatever" end it "should not store a callback when none is set in the edge" do @edge.callback = nil - json_output_should { |hash| hash['data']['callback'].nil? } - - JSON.parse @edge.to_json + PSON.parse(@edge.to_pson)["callback"].should be_nil end end -describe Puppet::Relationship, "when converting from json" do - confine "Missing 'json' library" => Puppet.features.json? +describe Puppet::Relationship, "when converting from pson" do + confine "Missing 'pson' library" => Puppet.features.pson? before do @event = "random" @@ -227,35 +202,35 @@ describe Puppet::Relationship, "when converting from json" do "event" => @event, "callback" => @callback } - @json = { - "json_class" => "Puppet::Relationship", + @pson = { + "type" => "Puppet::Relationship", "data" => @data } end - def json_result_should + def pson_result_should Puppet::Relationship.expects(:new).with { |*args| yield args } end - it "should be extended with the JSON utility module" do - Puppet::Relationship.metaclass.ancestors.should be_include(Puppet::Util::Json) + it "should be extended with the PSON utility module" do + Puppet::Relationship.metaclass.ancestors.should be_include(Puppet::Util::Pson) end # LAK:NOTE For all of these tests, we convert back to the edge so we can # trap the actual data structure then. it "should pass the source in as the first argument" do - Puppet::Relationship.from_json("source" => "mysource", "target" => "mytarget").source.should == "mysource" + Puppet::Relationship.from_pson("source" => "mysource", "target" => "mytarget").source.should == "mysource" end it "should pass the target in as the second argument" do - Puppet::Relationship.from_json("source" => "mysource", "target" => "mytarget").target.should == "mytarget" + Puppet::Relationship.from_pson("source" => "mysource", "target" => "mytarget").target.should == "mytarget" end it "should pass the event as an argument if it's provided" do - Puppet::Relationship.from_json("source" => "mysource", "target" => "mytarget", "event" => "myevent", "callback" => "eh").event.should == "myevent" + Puppet::Relationship.from_pson("source" => "mysource", "target" => "mytarget", "event" => "myevent", "callback" => "eh").event.should == "myevent" end it "should pass the callback as an argument if it's provided" do - Puppet::Relationship.from_json("source" => "mysource", "target" => "mytarget", "callback" => "mycallback").callback.should == "mycallback" + Puppet::Relationship.from_pson("source" => "mysource", "target" => "mytarget", "callback" => "mycallback").callback.should == "mycallback" end end diff --git a/spec/unit/resource.rb b/spec/unit/resource.rb index c233dd3f8..b26f4f923 100755 --- a/spec/unit/resource.rb +++ b/spec/unit/resource.rb @@ -354,63 +354,60 @@ describe Puppet::Resource do end end - describe "when converting to json" do - confine "Missing 'json' library" => Puppet.features.json? + describe "when converting to pson" do + confine "Missing 'pson' library" => Puppet.features.pson? - def json_output_should - @resource.class.expects(:json_create).with { |hash| yield hash } + def pson_output_should + @resource.class.expects(:pson_create).with { |hash| yield hash } end - it "should include the json util module" do - Puppet::Resource.metaclass.ancestors.should be_include(Puppet::Util::Json) + it "should include the pson util module" do + Puppet::Resource.metaclass.ancestors.should be_include(Puppet::Util::Pson) end # LAK:NOTE For all of these tests, we convert back to the resource so we can # trap the actual data structure then. - it "should set its json_class to 'Puppet::Resource'" do - JSON.parse(Puppet::Resource.new("file", "yay").to_json).should be_instance_of(Puppet::Resource) - end it "should set its type to the provided type" do - JSON.parse(Puppet::Resource.new("File", "/foo").to_json).type.should == "File" + Puppet::Resource.from_pson(PSON.parse(Puppet::Resource.new("File", "/foo").to_pson)).type.should == "File" end it "should set its title to the provided title" do - JSON.parse(Puppet::Resource.new("File", "/foo").to_json).title.should == "/foo" + Puppet::Resource.from_pson(PSON.parse(Puppet::Resource.new("File", "/foo").to_pson)).title.should == "/foo" end it "should include all tags from the resource" do resource = Puppet::Resource.new("File", "/foo") resource.tag("yay") - JSON.parse(resource.to_json).tags.should == resource.tags + Puppet::Resource.from_pson(PSON.parse(resource.to_pson)).tags.should == resource.tags end it "should include the file if one is set" do resource = Puppet::Resource.new("File", "/foo") resource.file = "/my/file" - JSON.parse(resource.to_json).file.should == "/my/file" + Puppet::Resource.from_pson(PSON.parse(resource.to_pson)).file.should == "/my/file" end it "should include the line if one is set" do resource = Puppet::Resource.new("File", "/foo") resource.line = 50 - JSON.parse(resource.to_json).line.should == 50 + Puppet::Resource.from_pson(PSON.parse(resource.to_pson)).line.should == 50 end it "should include the 'exported' value if one is set" do resource = Puppet::Resource.new("File", "/foo") resource.exported = true - JSON.parse(resource.to_json).exported.should be_true + Puppet::Resource.from_pson(PSON.parse(resource.to_pson)).exported.should be_true end it "should set 'exported' to false if no value is set" do resource = Puppet::Resource.new("File", "/foo") - JSON.parse(resource.to_json).exported.should be_false + Puppet::Resource.from_pson(PSON.parse(resource.to_pson)).exported.should be_false end it "should set all of its parameters as the 'parameters' entry" do @@ -418,16 +415,16 @@ describe Puppet::Resource do resource[:foo] = %w{bar eh} resource[:fee] = %w{baz} - result = JSON.parse(resource.to_json) + result = Puppet::Resource.from_pson(PSON.parse(resource.to_pson)) result["foo"].should == %w{bar eh} result["fee"].should == %w{baz} end end - describe "when converting from json" do - confine "Missing 'json' library" => Puppet.features.json? + describe "when converting from pson" do + confine "Missing 'pson' library" => Puppet.features.pson? - def json_result_should + def pson_result_should Puppet::Resource.expects(:new).with { |hash| yield hash } end @@ -439,59 +436,59 @@ describe Puppet::Resource do end it "should set its type to the provided type" do - Puppet::Resource.from_json(@data).type.should == "File" + Puppet::Resource.from_pson(@data).type.should == "File" end it "should set its title to the provided title" do - Puppet::Resource.from_json(@data).title.should == "yay" + Puppet::Resource.from_pson(@data).title.should == "yay" end it "should tag the resource with any provided tags" do @data['tags'] = %w{foo bar} - resource = Puppet::Resource.from_json(@data) + resource = Puppet::Resource.from_pson(@data) resource.tags.should be_include("foo") resource.tags.should be_include("bar") end it "should set its file to the provided file" do @data['file'] = "/foo/bar" - Puppet::Resource.from_json(@data).file.should == "/foo/bar" + Puppet::Resource.from_pson(@data).file.should == "/foo/bar" end it "should set its line to the provided line" do @data['line'] = 50 - Puppet::Resource.from_json(@data).line.should == 50 + Puppet::Resource.from_pson(@data).line.should == 50 end - it "should 'exported' to true if set in the json data" do + it "should 'exported' to true if set in the pson data" do @data['exported'] = true - Puppet::Resource.from_json(@data).exported.should be_true + Puppet::Resource.from_pson(@data).exported.should be_true end - it "should 'exported' to false if not set in the json data" do - Puppet::Resource.from_json(@data).exported.should be_false + it "should 'exported' to false if not set in the pson data" do + Puppet::Resource.from_pson(@data).exported.should be_false end it "should fail if no title is provided" do @data.delete('title') - lambda { Puppet::Resource.from_json(@data) }.should raise_error(ArgumentError) + lambda { Puppet::Resource.from_pson(@data) }.should raise_error(ArgumentError) end it "should fail if no type is provided" do @data.delete('type') - lambda { Puppet::Resource.from_json(@data) }.should raise_error(ArgumentError) + lambda { Puppet::Resource.from_pson(@data) }.should raise_error(ArgumentError) end it "should set each of the provided parameters" do @data['parameters'] = {'foo' => %w{one two}, 'fee' => %w{three four}} - resource = Puppet::Resource.from_json(@data) + resource = Puppet::Resource.from_pson(@data) resource['foo'].should == %w{one two} resource['fee'].should == %w{three four} end it "should convert single-value array parameters to normal values" do @data['parameters'] = {'foo' => %w{one}} - resource = Puppet::Resource.from_json(@data) + resource = Puppet::Resource.from_pson(@data) resource['foo'].should == %w{one} end end diff --git a/spec/unit/resource/catalog.rb b/spec/unit/resource/catalog.rb index 1fbe3a984..39f185a7c 100755 --- a/spec/unit/resource/catalog.rb +++ b/spec/unit/resource/catalog.rb @@ -887,71 +887,70 @@ describe Puppet::Resource::Catalog, "when compiling" do end end -describe Puppet::Resource::Catalog, "when converting to json" do - confine "Missing 'json' library" => Puppet.features.json? +describe Puppet::Resource::Catalog, "when converting to pson" do + confine "Missing 'pson' library" => Puppet.features.pson? before do @catalog = Puppet::Resource::Catalog.new("myhost") end - def json_output_should - @catalog.class.expects(:json_create).with { |hash| yield hash } + def pson_output_should + @catalog.class.expects(:pson_create).with { |hash| yield hash }.returns(:something) end # LAK:NOTE For all of these tests, we convert back to the resource so we can # trap the actual data structure then. - it "should set its json_class to 'Puppet::Resource::Catalog'" do - json_output_should { |hash| hash['json_class'] == "Puppet::Resource::Catalog" } + it "should set its document_type to 'Catalog'" do + pson_output_should { |hash| hash['document_type'] == "Catalog" } - JSON.parse @catalog.to_json + PSON.parse @catalog.to_pson end it "should set its data as a hash" do - json_output_should { |hash| hash['data'].is_a?(Hash) } - JSON.parse @catalog.to_json + pson_output_should { |hash| hash['data'].is_a?(Hash) } + PSON.parse @catalog.to_pson end [:name, :version, :tags].each do |param| it "should set its #{param} to the #{param} of the resource" do @catalog.send(param.to_s + "=", "testing") unless @catalog.send(param) - json_output_should { |hash| hash['data'][param.to_s] == @catalog.send(param) } - JSON.parse @catalog.to_json + pson_output_should { |hash| hash['data'][param.to_s] == @catalog.send(param) } + PSON.parse @catalog.to_pson end end - it "should convert its resources to a JSON-encoded array and store it as the 'resources' data" do - one = stub 'one', :to_json => '"one_resource"', :ref => "Foo[one]" - two = stub 'two', :to_json => '"two_resource"', :ref => "Foo[two]" + it "should convert its resources to a PSON-encoded array and store it as the 'resources' data" do + one = stub 'one', :to_pson_data_hash => "one_resource", :ref => "Foo[one]" + two = stub 'two', :to_pson_data_hash => "two_resource", :ref => "Foo[two]" @catalog.add_resource(one) @catalog.add_resource(two) # TODO this should really guarantee sort order - json_output_should { |hash| JSON.parse(hash['data']['resources']).sort == ["one_resource", "two_resource"].sort } - JSON.parse @catalog.to_json + PSON.parse(@catalog.to_pson,:create_additions => false)['data']['resources'].sort.should == ["one_resource", "two_resource"].sort + end - it "should convert its edges to a JSON-encoded array and store it as the 'edges' data" do - one = stub 'one', :to_json => '"one_resource"', :ref => 'Foo[one]' - two = stub 'two', :to_json => '"two_resource"', :ref => 'Foo[two]' - three = stub 'three', :to_json => '"three_resource"', :ref => 'Foo[three]' + it "should convert its edges to a PSON-encoded array and store it as the 'edges' data" do + one = stub 'one', :to_pson_data_hash => "one_resource", :ref => 'Foo[one]' + two = stub 'two', :to_pson_data_hash => "two_resource", :ref => 'Foo[two]' + three = stub 'three', :to_pson_data_hash => "three_resource", :ref => 'Foo[three]' @catalog.add_edge(one, two) @catalog.add_edge(two, three) - @catalog.edge(one, two).expects(:to_json).returns '"one_two_json"' - @catalog.edge(two, three).expects(:to_json).returns '"two_three_json"' + @catalog.edge(one, two ).expects(:to_pson_data_hash).returns "one_two_pson" + @catalog.edge(two, three).expects(:to_pson_data_hash).returns "two_three_pson" - json_output_should { |hash| JSON.parse(hash['data']['edges']).sort == %w{one_two_json two_three_json}.sort } - JSON.parse @catalog.to_json + PSON.parse(@catalog.to_pson,:create_additions => false)['data']['edges'].sort.should == %w{one_two_pson two_three_pson}.sort end end -describe Puppet::Resource::Catalog, "when converting from json" do - confine "Missing 'json' library" => Puppet.features.json? +describe Puppet::Resource::Catalog, "when converting from pson" do + confine "Missing 'pson' library" => Puppet.features.pson? - def json_result_should + def pson_result_should Puppet::Resource::Catalog.expects(:new).with { |hash| yield hash } end @@ -959,54 +958,54 @@ describe Puppet::Resource::Catalog, "when converting from json" do @data = { 'name' => "myhost" } - @json = { - 'json_class' => 'Puppet::Resource::Catalog', - 'data' => @data + @pson = { + 'document_type' => 'Puppet::Resource::Catalog', + 'data' => @data, + 'metadata' => {} } @catalog = Puppet::Resource::Catalog.new("myhost") Puppet::Resource::Catalog.stubs(:new).returns @catalog end - it "should be extended with the JSON utility module" do - Puppet::Resource::Catalog.metaclass.ancestors.should be_include(Puppet::Util::Json) + it "should be extended with the PSON utility module" do + Puppet::Resource::Catalog.metaclass.ancestors.should be_include(Puppet::Util::Pson) end it "should create it with the provided name" do Puppet::Resource::Catalog.expects(:new).with('myhost').returns @catalog - JSON.parse @json.to_json + PSON.parse @pson.to_pson end it "should set the provided version on the catalog if one is set" do @data['version'] = 50 @catalog.expects(:version=).with(@data['version']) - JSON.parse @json.to_json + PSON.parse @pson.to_pson end it "should set any provided tags on the catalog" do @data['tags'] = %w{one two} @catalog.expects(:tag).with("one", "two") - JSON.parse @json.to_json + PSON.parse @pson.to_pson end it 'should convert the resources list into resources and add each of them' do @data['resources'] = [Puppet::Resource.new(:file, "/foo"), Puppet::Resource.new(:file, "/bar")] @catalog.expects(:add_resource).times(2).with { |res| res.type == "File" } - - JSON.parse @json.to_json + PSON.parse @pson.to_pson end - it 'should convert resources even if they do not include "json_class" information' do + it 'should convert resources even if they do not include "type" information' do @data['resources'] = [Puppet::Resource.new(:file, "/foo")] - @data['resources'][0].expects(:to_json).returns "{\"title\":\"\\/foo\",\"tags\":[\"file\"],\"type\":\"File\"}" + @data['resources'][0].expects(:to_pson).returns '{"title":"/foo","tags":["file"],"type":"File"}' @catalog.expects(:add_resource).with { |res| res.type == "File" } - JSON.parse @json.to_json + PSON.parse @pson.to_pson end it 'should convert the edges list into edges and add each of them' do @@ -1020,12 +1019,12 @@ describe Puppet::Resource::Catalog, "when converting from json" do @catalog.expects(:add_edge).with { |edge| edge.event == "one" } @catalog.expects(:add_edge).with { |edge| edge.event == "two" } - JSON.parse @json.to_json + PSON.parse @pson.to_pson end - it "should be able to convert relationships that do not include 'json_class' information" do + it "should be able to convert relationships that do not include 'type' information" do one = Puppet::Relationship.new("osource", "otarget", :event => "one", :callback => "refresh") - one.expects(:to_json).returns "{\"event\":\"one\",\"callback\":\"refresh\",\"source\":\"osource\",\"target\":\"otarget\"}" + one.expects(:to_pson).returns "{\"event\":\"one\",\"callback\":\"refresh\",\"source\":\"osource\",\"target\":\"otarget\"}" @data['edges'] = [one] @@ -1033,7 +1032,7 @@ describe Puppet::Resource::Catalog, "when converting from json" do @catalog.expects(:add_edge).with { |edge| edge.event == "one" } - JSON.parse @json.to_json + PSON.parse @pson.to_pson end it "should set the source and target for each edge to the actual resource" do @@ -1046,7 +1045,7 @@ describe Puppet::Resource::Catalog, "when converting from json" do @catalog.expects(:add_edge).with { |edge| edge.source == "source_resource" and edge.target == "target_resource" } - JSON.parse @json.to_json + PSON.parse @pson.to_pson end it "should fail if the source resource cannot be found" do @@ -1057,7 +1056,7 @@ describe Puppet::Resource::Catalog, "when converting from json" do @catalog.expects(:resource).with("source").returns(nil) @catalog.stubs(:resource).with("target").returns("target_resource") - lambda { JSON.parse @json.to_json }.should raise_error(ArgumentError) + lambda { PSON.parse @pson.to_pson }.should raise_error(ArgumentError) end it "should fail if the target resource cannot be found" do @@ -1068,6 +1067,6 @@ describe Puppet::Resource::Catalog, "when converting from json" do @catalog.stubs(:resource).with("source").returns("source_resource") @catalog.expects(:resource).with("target").returns(nil) - lambda { JSON.parse @json.to_json }.should raise_error(ArgumentError) + lambda { PSON.parse @pson.to_pson }.should raise_error(ArgumentError) end end diff --git a/spec/unit/util/json.rb b/spec/unit/util/json.rb index 5ef5dc6ac..ce9648616 100755 --- a/spec/unit/util/json.rb +++ b/spec/unit/util/json.rb @@ -2,20 +2,20 @@ Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } -require 'puppet/util/json' +require 'puppet/util/pson' -class JsonUtil - include Puppet::Util::Json +class PsonUtil + include Puppet::Util::Pson end -describe Puppet::Util::Json do +describe Puppet::Util::Pson do it "should fail if no data is provided" do - lambda { JsonUtil.new.json_create("json_class" => "foo") }.should raise_error(ArgumentError) + lambda { PsonUtil.new.pson_create("type" => "foo") }.should raise_error(ArgumentError) end - it "should call 'from_json' with the provided data" do - json = JsonUtil.new - json.expects(:from_json).with("mydata") - json.json_create("json_class" => "foo", "data" => "mydata") + it "should call 'from_pson' with the provided data" do + pson = PsonUtil.new + pson.expects(:from_pson).with("mydata") + pson.pson_create("type" => "foo", "data" => "mydata") end end |