File: options.rb

package info (click to toggle)
ruby-multi-json 1.15.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 404 kB
  • sloc: ruby: 1,530; makefile: 3
file content (115 lines) | stat: -rw-r--r-- 3,173 bytes parent folder | download | duplicates (4)
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
shared_examples_for 'has options' do |object|
  if object.respond_to?(:call)
    subject { object.call }
  else
    subject { object }
  end

  describe 'dump options' do
    before do
      subject.dump_options = nil
    end

    after do
      subject.dump_options = nil
    end

    it 'returns default options if not set' do
      expect(subject.dump_options).to eq(subject.default_dump_options)
    end

    it 'allows hashes' do
      subject.dump_options = {:foo => 'bar'}
      expect(subject.dump_options).to eq(:foo => 'bar')
    end

    it 'allows objects that implement #to_hash' do
      value = Class.new do
        def to_hash
          {:foo => 'bar'}
        end
      end.new

      subject.dump_options = value
      expect(subject.dump_options).to eq(:foo => 'bar')
    end

    it 'evaluates lambda returning options (with args)' do
      subject.dump_options = lambda { |a1, a2| {a1 => a2} }
      expect(subject.dump_options('1', '2')).to eq('1' => '2')
    end

    it 'evaluates lambda returning options (with no args)' do
      subject.dump_options = lambda { {:foo => 'bar'} }
      expect(subject.dump_options).to eq(:foo => 'bar')
    end

    it 'returns empty hash in all other cases' do
      subject.dump_options = true
      expect(subject.dump_options).to eq(subject.default_dump_options)

      subject.dump_options = false
      expect(subject.dump_options).to eq(subject.default_dump_options)

      subject.dump_options = 10
      expect(subject.dump_options).to eq(subject.default_dump_options)

      subject.dump_options = nil
      expect(subject.dump_options).to eq(subject.default_dump_options)
    end
  end

  describe 'load options' do
    before do
      subject.load_options = nil
    end

    after do
      subject.load_options = nil
    end

    it 'returns default options if not set' do
      expect(subject.load_options).to eq(subject.default_load_options)
    end

    it 'allows hashes' do
      subject.load_options = {:foo => 'bar'}
      expect(subject.load_options).to eq(:foo => 'bar')
    end

    it 'allows objects that implement #to_hash' do
      value = Class.new do
        def to_hash
          {:foo => 'bar'}
        end
      end.new

      subject.load_options = value
      expect(subject.load_options).to eq(:foo => 'bar')
    end

    it 'evaluates lambda returning options (with args)' do
      subject.load_options = lambda { |a1, a2| {a1 => a2} }
      expect(subject.load_options('1', '2')).to eq('1' => '2')
    end

    it 'evaluates lambda returning options (with no args)' do
      subject.load_options = lambda { {:foo => 'bar'} }
      expect(subject.load_options).to eq(:foo => 'bar')
    end

    it 'returns empty hash in all other cases' do
      subject.load_options = true
      expect(subject.load_options).to eq(subject.default_load_options)

      subject.load_options = false
      expect(subject.load_options).to eq(subject.default_load_options)

      subject.load_options = 10
      expect(subject.load_options).to eq(subject.default_load_options)

      subject.load_options = nil
      expect(subject.load_options).to eq(subject.default_load_options)
    end
  end
end