Skip to content

Commit 8253f79

Browse files
author
Kevin Paulisse
committed
Fix const_get naming and write test for filter
1 parent afbd14f commit 8253f79

2 files changed

Lines changed: 44 additions & 3 deletions

File tree

lib/octocatalog-diff/catalog-diff/filter.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ def self.apply_filters(result, filter_names, options = {})
2222
# @param filter_class_name [String] Filter class name (from `filter` subdirectory)
2323
# @param options [Hash] Additional options (optional) to pass to filtered? method
2424
def self.filter(result, filter_class_name, options = {})
25-
filter_class_name = [self.class.name.to_s, filter_class_name].join('::')
26-
clazz = Kernel.get_const(filter_class_name)
25+
filter_class_name = [name.to_s, filter_class_name].join('::')
26+
clazz = Kernel.const_get(filter_class_name)
2727
result.reject! { |item| clazz.filtered?(item, options) }
2828
end
2929

3030
# Inherited: Construct a default `filtered?` method for the subclass via inheritance.
3131
# Each subclass must implement this method, so the default method errors.
32-
def self.filtered?(_item, _options)
32+
def self.filtered?(_item, _options = {})
3333
raise "No `filtered?` method is implemented in #{self.class.name}"
3434
end
3535
end
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../spec_helper'
4+
require OctocatalogDiff::Spec.require_path('/catalog-diff/filter')
5+
6+
describe OctocatalogDiff::CatalogDiff::Filter do
7+
before(:each) do
8+
@class_1 = double
9+
@class_2 = double
10+
allow(Kernel).to receive(:const_get).with('OctocatalogDiff::CatalogDiff::Filter::Fake1').and_return(@class_1)
11+
allow(Kernel).to receive(:const_get).with('OctocatalogDiff::CatalogDiff::Filter::Fake2').and_return(@class_2)
12+
end
13+
14+
describe '#apply_filters' do
15+
it 'should call self.filter() with appropriate options for each class' do
16+
result = [false]
17+
options = { 'Fake1' => { foo: 'bar' } }
18+
classes = %w(Fake1 Fake2)
19+
expect(@class_1).to receive(:'filtered?').with(false, foo: 'bar').and_return(false)
20+
expect(@class_2).to receive(:'filtered?').with(false, {}).and_return(false)
21+
expect { described_class.apply_filters(result, classes, options) }.not_to raise_error
22+
expect(result).to eq([false])
23+
end
24+
end
25+
26+
describe '#filter' do
27+
it 'should call .filtered?() in a class and remove matching items' do
28+
result = [false, true]
29+
expect(@class_1).to receive(:'filtered?').with(false, {}).and_return(false)
30+
expect(@class_1).to receive(:'filtered?').with(true, {}).and_return(true)
31+
expect { described_class.filter(result, 'Fake1') }.not_to raise_error
32+
expect(result).to eq([false])
33+
end
34+
end
35+
36+
describe '#filtered?' do
37+
it 'should raise error' do
38+
expect { described_class.filtered?([]) }.to raise_error(RuntimeError, /No `filtered\?` method is implemented/)
39+
end
40+
end
41+
end

0 commit comments

Comments
 (0)