|
1 | 1 | describe "reader" do
|
2 |
| - subject do |
3 |
| - class Test::Foo |
4 |
| - extend Dry::Initializer::Mixin |
| 2 | + shared_examples "it has no public attr_reader" do |
| 3 | + it "does not define a public attr_reader" do |
| 4 | + expect(subject).not_to respond_to :foo |
| 5 | + expect(subject).not_to respond_to :bar |
| 6 | + end |
| 7 | + end |
| 8 | + |
| 9 | + context "with reader: :public or no reader: option" do |
| 10 | + subject do |
| 11 | + class Test::Foo |
| 12 | + extend Dry::Initializer::Mixin |
| 13 | + |
| 14 | + param :foo |
| 15 | + param :foo2, reader: :public |
| 16 | + option :bar |
| 17 | + option :bar2, reader: :public |
| 18 | + end |
| 19 | + |
| 20 | + Test::Foo.new 1, 2, bar: 3, bar2: 4 |
| 21 | + end |
5 | 22 |
|
6 |
| - param :foo, reader: false |
7 |
| - option :bar, reader: false |
| 23 | + it "defines a public attr_reader by default" do |
| 24 | + expect(subject).to respond_to(:foo, :foo2) |
| 25 | + expect(subject).to respond_to :bar |
| 26 | + expect(subject).to respond_to :bar2 |
8 | 27 | end
|
| 28 | + end |
| 29 | + |
| 30 | + context "with reader: false" do |
| 31 | + subject do |
| 32 | + class Test::Foo |
| 33 | + extend Dry::Initializer::Mixin |
| 34 | + |
| 35 | + param :foo, reader: false |
| 36 | + option :bar, reader: false |
| 37 | + end |
| 38 | + |
| 39 | + Test::Foo.new 1, bar: 2 |
| 40 | + end |
| 41 | + |
| 42 | + it_behaves_like "it has no public attr_reader" |
9 | 43 |
|
10 |
| - Test::Foo.new 1, bar: 2 |
| 44 | + it "keeps assigning variables" do |
| 45 | + expect(subject.instance_variable_get(:@foo)).to eql 1 |
| 46 | + expect(subject.instance_variable_get(:@bar)).to eql 2 |
| 47 | + end |
11 | 48 | end
|
12 | 49 |
|
13 |
| - it "skips attr_reader" do |
14 |
| - expect(subject).not_to respond_to :foo |
15 |
| - expect(subject).not_to respond_to :bar |
| 50 | + context "with reader: :private" do |
| 51 | + subject do |
| 52 | + class Test::Foo |
| 53 | + extend Dry::Initializer::Mixin |
| 54 | + |
| 55 | + param :foo, reader: :private |
| 56 | + option :bar, reader: :private |
| 57 | + end |
| 58 | + |
| 59 | + Test::Foo.new 1, bar: 2 |
| 60 | + end |
| 61 | + |
| 62 | + it_behaves_like "it has no public attr_reader" |
| 63 | + |
| 64 | + it "adds a private attr_reader" do |
| 65 | + expect(subject.send(:foo)).to eql 1 |
| 66 | + expect(subject.send(:bar)).to eql 2 |
| 67 | + end |
16 | 68 | end
|
17 | 69 |
|
18 |
| - it "keeps assigning variables" do |
19 |
| - expect(subject.instance_variable_get(:@foo)).to eql 1 |
20 |
| - expect(subject.instance_variable_get(:@bar)).to eql 2 |
| 70 | + context "with reader: :protected" do |
| 71 | + subject do |
| 72 | + class Test::Foo |
| 73 | + extend Dry::Initializer::Mixin |
| 74 | + |
| 75 | + param :foo, reader: :protected |
| 76 | + option :bar, reader: :protected |
| 77 | + end |
| 78 | + |
| 79 | + Test::Foo.new 1, bar: 2 |
| 80 | + end |
| 81 | + |
| 82 | + it "adds a protected attr_reader" do |
| 83 | + protected_instance_methods = subject.class.protected_instance_methods |
| 84 | + expect(protected_instance_methods).to match_array([:foo, :bar]) |
| 85 | + end |
21 | 86 | end
|
22 | 87 | end
|
0 commit comments