aboutsummaryrefslogtreecommitdiffstats
path: root/spec/bundler/bundler/yaml_serializer_spec.rb
blob: c28db5922385a23a3bef83d9514f2cd31658c511 (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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# frozen_string_literal: true
require "spec_helper"
require "bundler/yaml_serializer"

RSpec.describe Bundler::YAMLSerializer do
  subject(:serializer) { Bundler::YAMLSerializer }

  describe "#dump" do
    it "works for simple hash" do
      hash = { "Q" => "Where does Thursday come before Wednesday? In the dictionary. :P" }

      expected = strip_whitespace <<-YAML
          ---
          Q: "Where does Thursday come before Wednesday? In the dictionary. :P"
      YAML

      expect(serializer.dump(hash)).to eq(expected)
    end

    it "handles nested hash" do
      hash = {
        "nice-one" => {
          "read_ahead" => "All generalizations are false, including this one",
        },
      }

      expected = strip_whitespace <<-YAML
          ---
          nice-one:
            read_ahead: "All generalizations are false, including this one"
      YAML

      expect(serializer.dump(hash)).to eq(expected)
    end

    it "array inside an hash" do
      hash = {
        "nested_hash" => {
          "contains_array" => [
            "Jack and Jill went up the hill",
            "To fetch a pail of water.",
            "Jack fell down and broke his crown,",
            "And Jill came tumbling after.",
          ],
        },
      }

      expected = strip_whitespace <<-YAML
        ---
        nested_hash:
          contains_array:
          - "Jack and Jill went up the hill"
          - "To fetch a pail of water."
          - "Jack fell down and broke his crown,"
          - "And Jill came tumbling after."
      YAML

      expect(serializer.dump(hash)).to eq(expected)
    end
  end

  describe "#load" do
    it "works for simple hash" do
      yaml = strip_whitespace <<-YAML
        ---
        Jon: "Air is free dude!"
        Jack: "Yes.. until you buy a bag of chips!"
      YAML

      hash = {
        "Jon" => "Air is free dude!",
        "Jack" => "Yes.. until you buy a bag of chips!",
      }

      expect(serializer.load(yaml)).to eq(hash)
    end

    it "works for nested hash" do
      yaml = strip_whitespace <<-YAML
        ---
        baa:
          baa: "black sheep"
          have: "you any wool?"
          yes: "merry have I"
        three: "bags full"
      YAML

      hash = {
        "baa" => {
          "baa" => "black sheep",
          "have" => "you any wool?",
          "yes" => "merry have I",
        },
        "three" => "bags full",
      }

      expect(serializer.load(yaml)).to eq(hash)
    end

    it "handles colon in key/value" do
      yaml = strip_whitespace <<-YAML
        BUNDLE_MIRROR__HTTPS://RUBYGEMS__ORG/: http://rubygems-mirror.org
      YAML

      expect(serializer.load(yaml)).to eq("BUNDLE_MIRROR__HTTPS://RUBYGEMS__ORG/" => "http://rubygems-mirror.org")
    end

    it "handles arrays inside hashes" do
      yaml = strip_whitespace <<-YAML
        ---
        nested_hash:
          contains_array:
          - "Why shouldn't you write with a broken pencil?"
          - "Because it's pointless!"
      YAML

      hash = {
        "nested_hash" => {
          "contains_array" => [
            "Why shouldn't you write with a broken pencil?",
            "Because it's pointless!",
          ],
        },
      }

      expect(serializer.load(yaml)).to eq(hash)
    end

    it "handles windows-style CRLF line endings" do
      yaml = strip_whitespace(<<-YAML).gsub("\n", "\r\n")
        ---
        nested_hash:
          contains_array:
          - "Why shouldn't you write with a broken pencil?"
          - "Because it's pointless!"
          - oh so silly
      YAML

      hash = {
        "nested_hash" => {
          "contains_array" => [
            "Why shouldn't you write with a broken pencil?",
            "Because it's pointless!",
            "oh so silly",
          ],
        },
      }

      expect(serializer.load(yaml)).to eq(hash)
    end
  end

  describe "against yaml lib" do
    let(:hash) do
      {
        "a_joke" => {
          "my-stand" => "I can totally keep secrets",
          "but" => "The people I tell them to can't :P",
        },
        "more" => {
          "first" => [
            "Can a kangaroo jump higher than a house?",
            "Of course, a house doesn't jump at all.",
          ],
          "second" => [
            "What did the sea say to the sand?",
            "Nothing, it simply waved.",
          ],
          "array with empty string" => [""],
        },
        "sales" => {
          "item" => "A Parachute",
          "description" => "Only used once, never opened.",
        },
        "one-more" => "I'd tell you a chemistry joke but I know I wouldn't get a reaction.",
      }
    end

    context "#load" do
      it "retrieves the original hash" do
        require "yaml"
        expect(serializer.load(YAML.dump(hash))).to eq(hash)
      end
    end

    context "#dump" do
      it "retrieves the original hash" do
        require "yaml"
        expect(YAML.load(serializer.dump(hash))).to eq(hash)
      end
    end
  end
end