File: reverse_stackable_values_spec.rb

package info (click to toggle)
ruby-grape 1.6.2-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,156 kB
  • sloc: ruby: 25,265; makefile: 7
file content (135 lines) | stat: -rw-r--r-- 4,040 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
# frozen_string_literal: true

require 'spec_helper'
module Grape
  module Util
    describe ReverseStackableValues do
      subject { described_class.new(parent) }

      let(:parent) { described_class.new }

      describe '#keys' do
        it 'returns all keys' do
          subject[:some_thing] = :foo_bar
          subject[:some_thing_else] = :foo_bar
          expect(subject.keys).to eq %i[some_thing some_thing_else].sort
        end

        it 'returns merged keys with parent' do
          parent[:some_thing] = :foo
          parent[:some_thing_else] = :foo

          subject[:some_thing] = :foo_bar
          subject[:some_thing_more] = :foo_bar

          expect(subject.keys).to eq %i[some_thing some_thing_else some_thing_more].sort
        end
      end

      describe '#delete' do
        it 'deletes a key' do
          subject[:some_thing] = :new_foo_bar
          subject.delete :some_thing
          expect(subject[:some_thing]).to eq []
        end

        it 'does not delete parent values' do
          parent[:some_thing] = :foo
          subject[:some_thing] = :new_foo_bar
          subject.delete :some_thing
          expect(subject[:some_thing]).to eq [:foo]
        end
      end

      describe '#[]' do
        it 'returns an array of values' do
          subject[:some_thing] = :foo
          expect(subject[:some_thing]).to eq [:foo]
        end

        it 'returns parent value when no value is set' do
          parent[:some_thing] = :foo
          expect(subject[:some_thing]).to eq [:foo]
        end

        it 'combines parent and actual values (actual first)' do
          parent[:some_thing] = :foo
          subject[:some_thing] = :foo_bar
          expect(subject[:some_thing]).to eq %i[foo_bar foo]
        end

        it 'parent values are not changed' do
          parent[:some_thing] = :foo
          subject[:some_thing] = :foo_bar
          expect(parent[:some_thing]).to eq [:foo]
        end
      end

      describe '#[]=' do
        it 'sets a value' do
          subject[:some_thing] = :foo
          expect(subject[:some_thing]).to eq [:foo]
        end

        it 'pushes further values' do
          subject[:some_thing] = :foo
          subject[:some_thing] = :bar
          expect(subject[:some_thing]).to eq %i[foo bar]
        end

        it 'can handle array values' do
          subject[:some_thing] = :foo
          subject[:some_thing] = %i[bar more]
          expect(subject[:some_thing]).to eq [:foo, %i[bar more]]

          parent[:some_thing_else] = %i[foo bar]
          subject[:some_thing_else] = %i[some bar foo]

          expect(subject[:some_thing_else]).to eq [%i[some bar foo], %i[foo bar]]
        end
      end

      describe '#to_hash' do
        it 'returns a Hash representation' do
          parent[:some_thing] = :foo
          subject[:some_thing] = %i[bar more]
          subject[:some_thing_more] = :foo_bar
          expect(subject.to_hash).to eq(
            some_thing: [%i[bar more], :foo],
            some_thing_more: [:foo_bar]
          )
        end
      end

      describe '#clone' do
        let(:obj_cloned) { subject.clone }

        it 'copies all values' do
          parent = described_class.new
          child = described_class.new parent
          grandchild = described_class.new child

          parent[:some_thing] = :foo
          child[:some_thing] = %i[bar more]
          grandchild[:some_thing] = :grand_foo_bar
          grandchild[:some_thing_more] = :foo_bar

          expect(grandchild.clone.to_hash).to eq(
            some_thing: [:grand_foo_bar, %i[bar more], :foo],
            some_thing_more: [:foo_bar]
          )
        end

        context 'complex (i.e. not primitive) data types (ex. middleware, please see bug #930)' do
          let(:middleware) { double }

          before { subject[:middleware] = middleware }

          it 'copies values; does not duplicate them' do
            expect(obj_cloned[:middleware]).to eq [middleware]
          end
        end
      end
    end
  end
end