Skip to content

Commit 6ef2787

Browse files
committed
Matcher can accept instance or class, fix ClosureTree#110
Matcher is included in all models by default.
1 parent 359ef9f commit 6ef2787

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

lib/closure_tree/test/matcher.rb

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,39 +7,39 @@ def be_a_closure_tree
77

88
class ClosureTree
99
def matches?(subject)
10-
@subject = subject
10+
@subject = subject.is_a?(Class) ? subject : subject.class
1111
# OPTIMIZE
1212
if @subject.respond_to?(:_ct)
1313

1414
unless @subject.column_names.include?(@subject._ct.parent_column_name)
15-
@message = "expected #{@subject.class.name} to respond to #{@subject._ct.parent_column_name}"
15+
@message = "expected #{@subject.name} to respond to #{@subject._ct.parent_column_name}"
1616
return false
1717
end
1818

1919
# Checking if hierarchy table exists (common error)
2020
unless @subject.hierarchy_class.table_exists?
21-
@message = "expected #{@subject.class.name}'s hierarchy table '#{@subject.hierarchy_class.table_name}' to exist"
21+
@message = "expected #{@subject.name}'s hierarchy table '#{@subject.hierarchy_class.table_name}' to exist"
2222
return false
2323
end
2424

2525
if @ordered
2626
unless @subject._ct.options.include?(:order)
27-
@message = "expected #{@subject.class.name} to be an ordered closure tree"
27+
@message = "expected #{@subject.name} to be an ordered closure tree"
2828
return false
2929
end
3030
unless @subject.column_names.include?(@subject._ct.options[:order].to_s)
31-
@message = "expected #{@subject.class.name} to have #{@subject._ct.options[:order]} as column"
31+
@message = "expected #{@subject.name} to have #{@subject._ct.options[:order]} as column"
3232
return false
3333
end
3434
end
3535

3636
if @with_advisory_lock && !@subject._ct.options[:with_advisory_lock]
37-
@message = "expected #{@subject.class.name} to have advisory lock"
37+
@message = "expected #{@subject.name} to have advisory lock"
3838
return false
3939
end
4040

4141
if @without_advisory_lock && @subject._ct.options[:with_advisory_lock]
42-
@message = "expected #{@subject.class.name} to not have advisory lock"
42+
@message = "expected #{@subject.name} to not have advisory lock"
4343
return false
4444
end
4545

@@ -65,13 +65,13 @@ def without_advisory_lock
6565
end
6666

6767
def failure_message
68-
@message || "expected #{@subject.class.name} to #{description}"
68+
@message || "expected #{@subject.name} to #{description}"
6969
end
7070

7171
alias_method :failure_message_for_should, :failure_message
7272

7373
def failure_message_when_negated
74-
"expected #{@subject.class.name} not be a closure tree, but it is."
74+
"expected #{@subject.name} not be a closure tree, but it is."
7575
end
7676

7777
alias_method :failure_message_for_should_not, :failure_message_when_negated
@@ -83,3 +83,7 @@ def description
8383
end
8484
end
8585
end
86+
87+
RSpec.configure do |c|
88+
c.include ClosureTree::Test::Matcher, type: :model
89+
end

0 commit comments

Comments
 (0)