Welcome to mirror list, hosted at ThFree Co, Russian Federation.

json_spec.rb « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5186ab041da4143b5538b712eb05d522a6ae4f04 (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
# frozen_string_literal: true

require "spec_helper"

RSpec.describe Gitlab::Json do
  describe ".parse" do
    it "parses an object" do
      expect(subject.parse('{ "foo": "bar" }')).to eq({ "foo" => "bar" })
    end

    it "parses an array" do
      expect(subject.parse('[{ "foo": "bar" }]')).to eq([{ "foo" => "bar" }])
    end

    it "raises an error on a string" do
      expect { subject.parse('"foo"') }.to raise_error(JSON::ParserError)
    end

    it "raises an error on a true bool" do
      expect { subject.parse("true") }.to raise_error(JSON::ParserError)
    end

    it "raises an error on a false bool" do
      expect { subject.parse("false") }.to raise_error(JSON::ParserError)
    end
  end

  describe ".parse!" do
    it "parses an object" do
      expect(subject.parse!('{ "foo": "bar" }')).to eq({ "foo" => "bar" })
    end

    it "parses an array" do
      expect(subject.parse!('[{ "foo": "bar" }]')).to eq([{ "foo" => "bar" }])
    end

    it "raises an error on a string" do
      expect { subject.parse!('"foo"') }.to raise_error(JSON::ParserError)
    end

    it "raises an error on a true bool" do
      expect { subject.parse!("true") }.to raise_error(JSON::ParserError)
    end

    it "raises an error on a false bool" do
      expect { subject.parse!("false") }.to raise_error(JSON::ParserError)
    end
  end

  describe ".dump" do
    it "dumps an object" do
      expect(subject.dump({ "foo" => "bar" })).to eq('{"foo":"bar"}')
    end

    it "dumps an array" do
      expect(subject.dump([{ "foo" => "bar" }])).to eq('[{"foo":"bar"}]')
    end

    it "dumps a string" do
      expect(subject.dump("foo")).to eq('"foo"')
    end

    it "dumps a true bool" do
      expect(subject.dump(true)).to eq("true")
    end

    it "dumps a false bool" do
      expect(subject.dump(false)).to eq("false")
    end
  end

  describe ".generate" do
    it "delegates to the adapter" do
      args = [{ foo: "bar" }]

      expect(JSON).to receive(:generate).with(*args)

      subject.generate(*args)
    end
  end

  describe ".pretty_generate" do
    it "delegates to the adapter" do
      args = [{ foo: "bar" }]

      expect(JSON).to receive(:pretty_generate).with(*args)

      subject.pretty_generate(*args)
    end
  end
end