Initial implementation of tag filtering

This commit is contained in:
Michael Miller 2021-08-17 20:52:06 -06:00
parent 38ea2e7f96
commit b79dd4361e
No known key found for this signature in database
GPG key ID: F9A0C5C65B162436
3 changed files with 30 additions and 0 deletions

View file

@ -5,6 +5,7 @@ require "../line_node_filter"
require "../location"
require "../location_node_filter"
require "../name_node_filter"
require "../tag_node_filter"
module Spectator
class Config
@ -105,6 +106,7 @@ module Spectator
example_option(parser, builder)
line_option(parser, builder)
location_option(parser, builder)
tag_option(parser, builder)
end
# Adds the example filter option to the parser.
@ -135,6 +137,17 @@ module Spectator
end
end
# Adds the tag filter option to the parser.
private def tag_option(parser, builder)
parser.on("--tag TAG", "run examples with the specified TAG, or exclude examples by adding ~ before the TAG.") do |tag|
negated = tag.starts_with?('~')
tag = tag.lchop('~')
Log.debug { "Filtering for example with tag #{tag}" }
filter = TagNodeFilter.new(tag)
builder.add_node_filter(filter)
end
end
# Adds options to the parser for changing output.
private def output_parser_options(parser, builder)
verbose_option(parser, builder)

View file

@ -51,6 +51,7 @@ require "./runner_events"
require "./runner"
require "./spec_builder"
require "./spec"
require "./tag_node_filter"
require "./test_context"
require "./value"
require "./wrapper"

View file

@ -0,0 +1,16 @@
require "./node_filter"
module Spectator
# Filter that matches nodes with a given tag.
class TagNodeFilter < NodeFilter
# Creates the filter.
# The *tag* indicates which tag the node must have in its metadata.
def initialize(@tag : String)
end
# Checks whether the node satisfies the filter.
def includes?(node) : Bool
node.metadata.each_key.any? { |key| key.to_s == @tag }
end
end
end