File: inheritable_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 (80 lines) | stat: -rw-r--r-- 2,256 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
# frozen_string_literal: true

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

      let(:parent) { described_class.new }

      describe '#delete' do
        it 'deletes a key' do
          subject[:some_thing] = :new_foo_bar
          subject.delete :some_thing
          expect(subject[:some_thing]).to be_nil
        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 a value' 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 'overwrites parent value with the current one' do
          parent[:some_thing] = :foo
          subject[:some_thing] = :foo_bar
          expect(subject[:some_thing]).to eq :foo_bar
        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
      end

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

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

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

          before { subject[:description] = description }

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