Treat any tokens starting with _spectator as reserved

This commit is contained in:
Michael Miller 2022-03-12 19:17:38 -07:00
parent 5ad29f486f
commit 20087f1c57
No known key found for this signature in database
GPG key ID: AC78B32D30CE34A2
3 changed files with 9 additions and 9 deletions

View file

@ -9,6 +9,6 @@ module Spectator
module DSL
# Keywords that cannot be used in specs using the DSL.
# These are either problematic or reserved for internal use.
RESERVED_KEYWORDS = %i[initialize _spectator_stubbed_name _spectator_find_stub _spectator_define_stub]
RESERVED_KEYWORDS = %i[initialize]
end
end

View file

@ -12,7 +12,7 @@ module Spectator::DSL
{% raise "Missing block for 'let'" unless block %}
{% raise "Expected zero or one arguments for 'let', but got #{block.args.size}" if block.args.size > 1 %}
{% raise "Cannot use 'let' inside of an example block" if @def %}
{% raise "Cannot use '#{name.id}' for 'let'" if ::Spectator::DSL::RESERVED_KEYWORDS.includes?(name.id.symbolize) %}
{% raise "Cannot use '#{name.id}' for 'let'" if name.id.starts_with?("_spectator") || ::Spectator::DSL::RESERVED_KEYWORDS.includes?(name.id.symbolize) %}
@%value = ::Spectator::LazyWrapper.new
@ -34,7 +34,7 @@ module Spectator::DSL
{% raise "Missing block for 'let!'" unless block %}
{% raise "Expected zero or one arguments for 'let!', but got #{block.args.size}" if block.args.size > 1 %}
{% raise "Cannot use 'let!' inside of an example block" if @def %}
{% raise "Cannot use '#{name.id}' for 'let!'" if ::Spectator::DSL::RESERVED_KEYWORDS.includes?(name.id.symbolize) %}
{% raise "Cannot use '#{name.id}' for 'let!'" if name.id.starts_with?("_spectator") || ::Spectator::DSL::RESERVED_KEYWORDS.includes?(name.id.symbolize) %}
let({{name}}) {{block}}
before_each { {{name.id}} }
@ -61,7 +61,7 @@ module Spectator::DSL
{% raise "Missing block for 'subject'" unless block %}
{% raise "Expected zero or one arguments for 'subject', but got #{block.args.size}" if block.args.size > 1 %}
{% raise "Cannot use 'subject' inside of an example block" if @def %}
{% raise "Cannot use '#{name.id}' for 'subject'" if ::Spectator::DSL::RESERVED_KEYWORDS.includes?(name.id.symbolize) %}
{% raise "Cannot use '#{name.id}' for 'subject'" if name.id.starts_with?("_spectator") || ::Spectator::DSL::RESERVED_KEYWORDS.includes?(name.id.symbolize) %}
let({{name.id}}) {{block}}
@ -93,7 +93,7 @@ module Spectator::DSL
{% raise "Missing block for 'subject'" unless block %}
{% raise "Expected zero or one arguments for 'subject!', but got #{block.args.size}" if block.args.size > 1 %}
{% raise "Cannot use 'subject!' inside of an example block" if @def %}
{% raise "Cannot use '#{name.id}' for 'subject!'" if ::Spectator::DSL::RESERVED_KEYWORDS.includes?(name.id.symbolize) %}
{% raise "Cannot use '#{name.id}' for 'subject!'" if name.id.starts_with?("_spectator") || ::Spectator::DSL::RESERVED_KEYWORDS.includes?(name.id.symbolize) %}
let!({{name.id}}) {{block}}

View file

@ -46,7 +46,7 @@ module Spectator
# Stubbed methods will call `#_spectator_find_stub` with the method call information.
private macro stub(method)
{% raise "stub requires a method definition" if !method.is_a?(Def) %}
{% raise "Cannot stub method with reserved keyword as name - #{method.name}" if ::Spectator::DSL::RESERVED_KEYWORDS.includes?(method.name.symbolize) %}
{% raise "Cannot stub method with reserved keyword as name - #{method.name}" if method.name.starts_with?("_spectator") || ::Spectator::DSL::RESERVED_KEYWORDS.includes?(method.name.symbolize) %}
{% original = ((@type.has_method?(method.name) || !@type.ancestors.any? { |a| a.has_method?(method.name) }) ? :previous_def : :super).id %}
@ -109,7 +109,7 @@ module Spectator
# Stubbed methods will call `#_spectator_find_stub` with the method call information.
private macro abstract_stub(method)
{% raise "abstract_stub requires a method definition" if !method.is_a?(Def) %}
{% raise "Cannot stub method with reserved keyword as name - #{method.name}" if ::Spectator::DSL::RESERVED_KEYWORDS.includes?(method.name.symbolize) %}
{% raise "Cannot stub method with reserved keyword as name - #{method.name}" if method.name.starts_with?("_spectator") || ::Spectator::DSL::RESERVED_KEYWORDS.includes?(method.name.symbolize) %}
{% if method.visibility != :public %}{{method.visibility.id}}{% end %} def {{method.receiver}}{{method.name}}(
{% for arg, i in method.args %}{% if i == method.splat_index %}*{% end %}{{arg}}, {% end %}
@ -154,8 +154,8 @@ module Spectator
stub_all({{type.superclass}}, with: {{style}})
{% end %}
{% for meth in type.methods.reject { |m| DSL::RESERVED_KEYWORDS.includes?(m.name.symbolize) } %}
{{style.id}} {{meth}}
{% for method in type.methods.reject { |meth| meth.name.starts_with?("_spectator") || DSL::RESERVED_KEYWORDS.includes?(meth.name.symbolize) } %}
{{style.id}} {{method}}
{% end %}
end
end