summaryrefslogtreecommitdiff
path: root/spec/integration/network/formats_spec.rb
blob: 686961338a2b1a3bf15a9ab4991c76ab22849bf9 (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
#! /usr/bin/env ruby
require 'spec_helper'

require 'puppet/network/formats'

class PsonIntTest
  attr_accessor :string
  def ==(other)
    other.class == self.class and string == other.string
  end

  def self.from_data_hash(data)
    new(data[0])
  end

  def initialize(string)
    @string = string
  end

  def to_pson(*args)
    {
      'type' => self.class.name,
      'data' => [@string]
    }.to_pson(*args)
  end

  def self.canonical_order(s)
    s.gsub(/\{"data":\[(.*?)\],"type":"PsonIntTest"\}/,'{"type":"PsonIntTest","data":[\1]}')
  end

end

describe Puppet::Network::FormatHandler.format(:s) do
  before do
    @format = Puppet::Network::FormatHandler.format(:s)
  end

  it "should support certificates" do
    @format.should be_supported(Puppet::SSL::Certificate)
  end

  it "should not support catalogs" do
    @format.should_not be_supported(Puppet::Resource::Catalog)
  end
end

describe Puppet::Network::FormatHandler.format(:pson) do
  before do
    @pson = Puppet::Network::FormatHandler.format(:pson)
  end

  it "should be able to render an instance to pson" do
    instance = PsonIntTest.new("foo")
    PsonIntTest.canonical_order(@pson.render(instance)).should == PsonIntTest.canonical_order('{"type":"PsonIntTest","data":["foo"]}' )
  end

  it "should be able to render arrays to pson" do
    @pson.render([1,2]).should == '[1,2]'
  end

  it "should be able to render arrays containing hashes to pson" do
    @pson.render([{"one"=>1},{"two"=>2}]).should == '[{"one":1},{"two":2}]'
  end

  it "should be able to render multiple instances to pson" do
    one = PsonIntTest.new("one")
    two = PsonIntTest.new("two")

    PsonIntTest.canonical_order(@pson.render([one,two])).should == PsonIntTest.canonical_order('[{"type":"PsonIntTest","data":["one"]},{"type":"PsonIntTest","data":["two"]}]')
  end

  it "should be able to intern pson into an instance" do
    @pson.intern(PsonIntTest, '{"type":"PsonIntTest","data":["foo"]}').should == PsonIntTest.new("foo")
  end

  it "should be able to intern pson with no class information into an instance" do
    @pson.intern(PsonIntTest, '["foo"]').should == PsonIntTest.new("foo")
  end

  it "should be able to intern multiple instances from pson" do
    @pson.intern_multiple(PsonIntTest, '[{"type": "PsonIntTest", "data": ["one"]},{"type": "PsonIntTest", "data": ["two"]}]').should == [
      PsonIntTest.new("one"), PsonIntTest.new("two")
    ]
  end

  it "should be able to intern multiple instances from pson with no class information" do
    @pson.intern_multiple(PsonIntTest, '[["one"],["two"]]').should == [
      PsonIntTest.new("one"), PsonIntTest.new("two")
    ]
  end
end