File: a_serialized_hash.rb

package info (click to toggle)
ruby-snaky-hash 2.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,156 kB
  • sloc: ruby: 1,298; javascript: 529; makefile: 4; sh: 4
file content (154 lines) | stat: -rw-r--r-- 5,917 bytes parent folder | download
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
RSpec.shared_examples_for "a serialized hash" do
  describe "::dump" do
    after do
      subject.dump_value_extensions.reset
      subject.dump_hash_extensions.reset
    end

    it "returns a JSON string" do
      value = subject.dump({hello: "World"})
      expect(value).to eq '{"hello":"World"}'
    end

    it "does not remove nil values" do
      value = subject.dump({hello: "World", nilValue: nil})
      expect(value).to eq "{\"hello\":\"World\",\"nil_value\":null}"
    end

    it "does not remove empty strings" do
      value = subject.dump({hello: "World", nilValue: ""})
      expect(value).to eq "{\"hello\":\"World\",\"nil_value\":\"\"}"
    end

    it "does not remove false" do
      value = subject.dump({hello: "World", falseValue: false})
      expect(value).to eq '{"hello":"World","false_value":false}'
    end

    it "removes any empty items from top-level arrays" do
      value = subject.dump({hello: "World", array: [nil, 1, 2, "", false]})
      expect(value).to eq '{"hello":"World","array":[1,2,"",false]}'
    end

    it "passes through any extensions that have been added" do
      subject.dump_extensions.add(:test) { |value| value.upcase }
      value = subject.dump({hello: "WORLD"})
      expect(value).to eq '{"hello":"WORLD"}'
    end

    it "works with nested hashes" do
      subject.dump_extensions.add(:test) { |value| value.is_a?(String) ? value.upcase : value }
      value = subject.dump({hello: "world", nested: {vegetable: "potato", more_nesting: {fruit: "banana"}}})
      expect(value).to eq '{"hello":"WORLD","nested":{"vegetable":"POTATO","more_nesting":{"fruit":"BANANA"}}}'
    end

    it "works with nested hashes in arrays" do
      subject.dump_extensions.add(:test) { |value| value.is_a?(String) ? value.upcase : value }
      value = subject.dump({hello: "world", nested: {vegetables: [{name: "potato"}, {name: "cucumber"}], more_nesting: {fruits: [{name: "banana"}, {name: "apple"}]}}})
      expect(value).to eq '{"hello":"WORLD","nested":{"vegetables":[{"name":"POTATO"},{"name":"CUCUMBER"}],"more_nesting":{"fruits":[{"name":"BANANA"},{"name":"APPLE"}]}}}'
    end
  end

  describe "::load" do
    after do
      subject.load_value_extensions.reset
      subject.load_hash_extensions.reset
    end

    it "creates a Hashie::Mash from the given JSON" do
      hash = subject.load('{"hello":"world"}')
      expect(hash).to be_a Hashie::Mash
      expect(hash["hello"]).to eq "world"
    end

    it "creates an empty Mash if the given value is nil" do
      hash = subject.load(nil)
      expect(hash).to be_a Hashie::Mash
      expect(hash).to be_empty
    end

    it "creates a hash with nil value" do
      hash = subject.load('{"myCount":null}')
      expect(hash).to be_a Hashie::Mash
      expect(hash["myCount"]).to be_nil
    end

    it "creates a hash with empty value" do
      hash = subject.load('{"myCount":""}')
      expect(hash).to be_a Hashie::Mash
      expect(hash["myCount"]).to eq("")
    end

    it "creates a hash with 0 value" do
      hash = subject.load('{"myCount":0}')
      expect(hash).to be_a Hashie::Mash
      expect(hash["myCount"]).to eq(0)
    end

    it "creates an empty Mash if the JSON is an empty string" do
      hash = subject.load("")
      expect(hash).to be_a Hashie::Mash
      expect(hash).to be_empty
    end

    it "passes through any extensions that have been added" do
      subject.load_extensions.add(:test) { |value| value.upcase }
      hash = subject.load('{"hello":"world"}')
      expect(hash).to be_a Hashie::Mash
      expect(hash["hello"]).to eq "WORLD"
    end

    it "works with nested hashes" do
      subject.load_extensions.add(:test) { |value| value.is_a?(String) ? value.downcase : nil }
      hash = subject.load('{"hello":"WORLD","nested":{"vegetable":"POTATO","more_nesting":{"fruit":"BANANA"}}}')
      expect(hash).to be_a Hashie::Mash
      expect(hash).to eq({"hello" => "world", "nested" => {"vegetable" => "potato", "more_nesting" => {"fruit" => "banana"}}})
    end

    it "works with nested hashes in arrays" do
      subject.load_extensions.add(:test) { |value| value.is_a?(String) ? value.downcase : nil }
      hash = subject.load('{"num":3,"hello":"WORLD","nested":{"vegetables":[{"name":"POTATO"},{"name":"CUCUMBER"}],"more_nesting":{"fruits":[{"name":"BANANA"},{"name":"APPLE"}]}}}')
      expect(hash).to be_a Hashie::Mash
      expect(hash).to eq({"num" => nil, "hello" => "world", "nested" => {"vegetables" => [{"name" => "potato"}, {"name" => "cucumber"}], "more_nesting" => {"fruits" => [{"name" => "banana"}, {"name" => "apple"}]}}})
    end

    it "is unable to upcase keys, because instantiation of a SnakyHash downcases keys" do
      subject.load_hash_extensions.add(:test) { |value|
        if value.is_a?(Hash)
          value.transform_keys(&:upcase)
        else
          value
        end
      }
      hash = subject.load('{"some_hash":{"name":"Michael"}}')
      expect(hash).to be_a Hashie::Mash
      expect(hash).to eq({"some_hash" => {"name" => "Michael"}})
    end

    it "passes through hashes through their own extensions" do
      subject.load_hash_extensions.add(:test) { |value|
        if value.is_a?(Hash)
          value.transform_keys(&:next)
        else
          value
        end
      }
      hash = subject.load('{"some_hash":{"name":"Michael"}}')
      expect(hash).to be_a Hashie::Mash
      expect(hash).to eq({"some_hasi" => {"namf" => "Michael"}})
    end

    it "passes hashes through their own extension and return non-hash values properly" do
      subject.load_hash_extensions.add(:test) { |value|
        if value.is_a?(Hash)
          value.key?("name") ? value["name"] : value
        else
          value
        end
      }
      hash = subject.load('{"some_hash":{"name":"Michael"}}')
      expect(hash).to be_a Hashie::Mash
      expect(hash).to eq({"some_hash" => "Michael"})
    end
  end
end