lib/minitest/filesystem/matcher.rb in minitest-filesystem-0.0.1 vs lib/minitest/filesystem/matcher.rb in minitest-filesystem-0.1.0
- old
+ new
@@ -12,22 +12,15 @@
def file(file)
entry(:file, file) && is_a?(:file, file)
end
def dir(dir, &block)
- matcher = self.class.new(@actual_tree.root + dir, &block) if block_given?
+ matcher = self.class.new(@actual_tree.expand_path(dir), &block) if block_given?
entry(:directory, dir) && is_a?(:directory, dir) && subtree(matcher)
end
- def entry(kind = :entry, entry)
- @is_matching = @is_matching && @actual_tree.include?(entry)
- set_failure_msg_for(kind, entry) unless @is_matching
-
- @is_matching
- end
-
def match_found?
instance_eval(&@expected_tree)
@is_matching
end
@@ -35,60 +28,68 @@
@failure_msg || ""
end
private
- def subtree(matcher)
- @is_matching = @is_matching && matcher.match_found? if matcher
- set_failure_msg(matcher.message) unless @is_matching
+ def entry(kind = :entry, entry)
+ update_matching_status(
+ @actual_tree.include?(entry),
+ not_found_msg_for(kind, entry))
+ end
- @is_matching
+ def subtree(matcher)
+ update_matching_status(matcher.match_found?, matcher.message) if matcher
end
def is_a?(kind, entry)
- @is_matching = @is_matching && (@actual_tree.is_a?(kind, entry))
- set_mismatch_failure_msg_for(kind, entry) unless @is_matching
+ update_matching_status(
+ @actual_tree.is_a?(kind, entry),
+ mismatch_msg_for(kind, entry))
+ end
+ def update_matching_status(check, msg)
+ @is_matching = @is_matching && check
+ set_failure_msg(msg) unless @is_matching
+
@is_matching
end
- def set_failure_msg_for(kind, entry)
- set_failure_msg "Expected `#{@actual_tree.root}` to contain #{kind} `#{entry}`."
+ def not_found_msg_for(kind, entry)
+ "Expected `#{@actual_tree.root}` to contain #{kind} `#{entry}`."
end
+ def mismatch_msg_for(kind, entry)
+ "Expected `#{entry}` to be a #{kind}, but it was not."
+ end
+
def set_failure_msg(msg)
@failure_msg ||= msg
end
- def set_mismatch_failure_msg_for(kind, entry)
- @failure_msg ||=
- "Expected `#{entry}` to be a #{kind}, but it was not."
- end
-
class MatchingTree
attr_reader :root
def initialize(root)
@root = Pathname.new(root)
@tree = expand_tree_under @root
end
def include?(entry)
- @tree.include?(root_slash(entry))
+ @tree.include?(expand_path(entry))
end
def is_a?(kind, entry)
- (root_slash entry).send("#{kind}?")
+ (expand_path entry).send("#{kind}?")
end
+ def expand_path(file)
+ @root + Pathname.new(file)
+ end
+
private
def expand_tree_under(dir)
Pathname.glob(dir.join("**/*"))
- end
-
- def root_slash(file)
- @root + Pathname.new(file)
end
end
end
end
end