File: stringify-hash_spec.rb

package info (click to toggle)
ruby-stringify-hash 0.0.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 116 kB
  • sloc: ruby: 181; makefile: 3
file content (103 lines) | stat: -rw-r--r-- 3,350 bytes parent folder | download | duplicates (3)
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
require "spec_helper"

describe StringifyHash do
  let(:options)    { StringifyHash.new }

  #test options hash methods
  it "converts all string keys to symbols when doing direct assignment" do
    options['key'] = 'value'
    expect(options.has_key?(:key)) === true  and expect(options.has_key?('key')) === false
  end

  it "can look up by string or symbol key" do
    options.merge({'key' => 'value'})
    expect(options['key']) === 'value' and expect(options[:key]) === 'value'
  end

  it "can delete by string of symbol key" do
    options['key'] = 'value'
    expect(options.delete('key')) === 'value' and expect(options.delete(:key)) === 'value'
  end

  it "when merged with a Hash remains an StringifyHash" do
    options.merge({'key' => 'value'})
    expect(options.is_a?(StringifyHash)) === true
  end

  it "when merged with a hash that contains a hash, the sub-hash becomes an StringifyHash" do
    newhash = options.merge({'key' => {'subkey' => 'subvalue'}})
    expect(newhash[:key].is_a?(StringifyHash)) === true and expect(newhash[:key][:subkey]) === 'subvalue'
  end

  it "does not alter the original hash when doing a merge" do
    options.merge({'key' => {'subkey' => 'subvalue'}})
    expect(options[:key]).to be === nil
  end

  context 'pretty prints itself' do

    it 'in valid JSON' do
      require 'json'

      options['array'] = ['one', 'two', 'three']
      options['hash'] = {'subkey' => { 'subsub' => 1 }}
      options['nil'] = nil
      options['number'] = 4
      options['float'] = 1.0
      options['string'] = 'string'
      options['true'] = true

      expect{ JSON.parse( options.dump ) }.to_not raise_error
    end

    context 'for non collection values shows' do
      it 'non-string like values as bare words' do
        expect( options.fmt_value( 4 ) ).to be == "4"
        expect( options.fmt_value( 1.0 ) ).to be == "1.0"
        expect( options.fmt_value( true ) ).to be == "true"
        expect( options.fmt_value( false ) ).to be == "false"
      end

      it 'nil values as null' do
        expect( options.fmt_value( nil ) ).to be == 'null'
      end

      it 'strings within double quotes' do
        expect( options.fmt_value( 'thing' ) ).to be == '"thing"'
      end
    end

    context 'for list like collections shows' do
      it 'each element on a new line' do
        fmt_list = options.fmt_value( %w{ one two three } )

        expect( fmt_list ).to match(/^\s*"one",?$/)
        expect( fmt_list ).to match(/^\s*"two",?$/)
        expect( fmt_list ).to match(/^\s*"three",?$/)
      end

      it 'square brackets on either end of the list' do
        fmt_list = options.fmt_value( %w{ one two three } )

        expect( fmt_list ).to match( /\A\[\s*$/ )
        expect( fmt_list ).to match( /^\s*\]\Z/ )
      end
    end

    context 'for dict like collections shows' do
      it 'each element on a new line' do
        fmt_assoc = options.fmt_value( {:one => 'two', :two => 'three'} )

        expect( fmt_assoc ).to match(/^\s*"one": "two",?$/)
        expect( fmt_assoc ).to match(/^\s*"two": "three",?$/)
      end

      it 'curly braces on either end of the list' do
        fmt_assoc = options.fmt_value( {:one => 'two', :two => 'three'} )

        expect( fmt_assoc ).to match( /\A\{\s*$/ )
        expect( fmt_assoc ).to match( /^\s*\}\Z/ )
      end
    end
  end
end