|
12 | 12 | end
|
13 | 13 |
|
14 | 14 | before do
|
15 |
| - Dir.glob("#{root}**/*.rb").each { |f| require(f) } |
| 15 | + Dir.glob("#{root}**/*.rb").each { |f| silence_warnings { require(f) } } |
16 | 16 | end
|
17 | 17 |
|
18 | 18 | def f(path)
|
@@ -82,4 +82,141 @@ def f(path)
|
82 | 82 | end
|
83 | 83 | end
|
84 | 84 | end
|
| 85 | + |
| 86 | + describe "#ignore_klass?" do |
| 87 | + self::DummyInteractor = Class.new do |
| 88 | + include Interactify |
| 89 | + end |
| 90 | + |
| 91 | + let(:klass) { self.class::DummyInteractor } |
| 92 | + let(:wiring) { described_class.new(root:, ignore:) } |
| 93 | + let(:result) { wiring.send(:ignore_klass?, wiring.ignore, klass) } |
| 94 | + |
| 95 | + def self.it_ignores |
| 96 | + it "ignores" do |
| 97 | + expect(result).to be true |
| 98 | + end |
| 99 | + end |
| 100 | + |
| 101 | + context "with an array of classes" do |
| 102 | + let(:ignore) { [klass] } |
| 103 | + |
| 104 | + it_ignores |
| 105 | + end |
| 106 | + |
| 107 | + context "with a regexp" do |
| 108 | + let(:ignore) { [/Dummy/] } |
| 109 | + |
| 110 | + it_ignores |
| 111 | + end |
| 112 | + |
| 113 | + context "with a string" do |
| 114 | + let(:ignore) { ["DummyInteractor"] } |
| 115 | + |
| 116 | + it_ignores |
| 117 | + end |
| 118 | + |
| 119 | + context "proc condition" do |
| 120 | + let(:ignore) { [->(k) { k.to_s =~ /DummyInteractor/ }] } |
| 121 | + |
| 122 | + it_ignores |
| 123 | + end |
| 124 | + |
| 125 | + context "empty ignore" do |
| 126 | + let(:ignore) { [] } |
| 127 | + |
| 128 | + it "does not ignore class" do |
| 129 | + expect(result).to be false |
| 130 | + end |
| 131 | + end |
| 132 | + end |
| 133 | + context "with errors" do |
| 134 | + let(:organizer1) { double("Organizer1", klass: "SomeOrganizer1") } |
| 135 | + let(:organizer2) { double("Organizer2", klass: "SomeOrganizer2") } |
| 136 | + |
| 137 | + let(:interactor1) { double("Interactor1", klass: "SomeInteractor1") } |
| 138 | + let(:interactor2) { double("Interactor2", klass: "SomeInteractor2") } |
| 139 | + let(:interactor3) { double("Interactor3", klass: "SomeInteractor3") } |
| 140 | + |
| 141 | + describe "#format_error" do |
| 142 | + let(:missing_keys) { %i[foo bar] } |
| 143 | + let(:interactor) { double("Interactor", klass: "SomeInteractor") } |
| 144 | + let(:organizer) { double("Organizer", klass: "SomeOrganizer") } |
| 145 | + let(:formatted_errors) { [] } |
| 146 | + |
| 147 | + it "formats the error message correctly" do |
| 148 | + subject.send(:format_error, missing_keys, interactor, organizer, formatted_errors) |
| 149 | + expect(formatted_errors.first).to include("Missing keys: :foo, :bar") |
| 150 | + expect(formatted_errors.first).to include("expected in: SomeInteractor") |
| 151 | + expect(formatted_errors.first).to include("called by: SomeOrganizer") |
| 152 | + end |
| 153 | + end |
| 154 | + |
| 155 | + describe "#format_errors" do |
| 156 | + it "formats and combines all errors" do |
| 157 | + all_errors = { |
| 158 | + organizer1 => double( |
| 159 | + "ErrorContext", |
| 160 | + missing_keys: { |
| 161 | + interactor1 => %i[key1 key2] |
| 162 | + } |
| 163 | + ), |
| 164 | + organizer2 => double( |
| 165 | + "ErrorContext", |
| 166 | + missing_keys: { |
| 167 | + interactor2 => [:key3] |
| 168 | + } |
| 169 | + ) |
| 170 | + } |
| 171 | + |
| 172 | + expect(subject).to receive(:format_error).twice.and_call_original |
| 173 | + formatted_error_string = subject.format_errors(all_errors) |
| 174 | + expect(formatted_error_string).to include("Missing keys: :key1, :key2") |
| 175 | + expect(formatted_error_string).to include("Missing keys: :key3") |
| 176 | + expect(formatted_error_string).to match(/expected in:.*\n\s+called by:/) |
| 177 | + end |
| 178 | + end |
| 179 | + |
| 180 | + describe "#each_error" do |
| 181 | + it "yields each error unless the class is ignored" do |
| 182 | + all_errors = { |
| 183 | + organizer1 => double( |
| 184 | + "ErrorContext", |
| 185 | + missing_keys: { |
| 186 | + interactor1 => [:key1], |
| 187 | + interactor2 => [:key2] |
| 188 | + } |
| 189 | + ), |
| 190 | + organizer2 => double( |
| 191 | + "ErrorContext", |
| 192 | + missing_keys: { |
| 193 | + interactor3 => [:key3] |
| 194 | + } |
| 195 | + ) |
| 196 | + } |
| 197 | + |
| 198 | + allow(subject).to receive(:ignore_klass?).and_return(false) |
| 199 | + |
| 200 | + expect { |b| subject.each_error(all_errors, &b) } |
| 201 | + .to yield_successive_args( |
| 202 | + [[:key1], anything, anything], |
| 203 | + [[:key2], anything, anything], |
| 204 | + [[:key3], anything, anything] |
| 205 | + ) |
| 206 | + end |
| 207 | + |
| 208 | + it "does not yield errors for ignored classes" do |
| 209 | + all_errors = { |
| 210 | + organizer1 => double( |
| 211 | + "ErrorContext", |
| 212 | + missing_keys: { |
| 213 | + interactor1 => [:key1] |
| 214 | + } |
| 215 | + ) |
| 216 | + } |
| 217 | + allow(subject).to receive(:ignore_klass?).and_return(true) |
| 218 | + expect { |b| subject.each_error(all_errors, &b) }.not_to yield_control |
| 219 | + end |
| 220 | + end |
| 221 | + end |
85 | 222 | end
|
0 commit comments