Prevent using reserved keywords in let and subject

This commit is contained in:
Michael Miller 2021-02-10 16:58:17 -07:00
parent 86a85c0946
commit 3cd569e639
No known key found for this signature in database
GPG key ID: F9A0C5C65B162436
3 changed files with 8 additions and 0 deletions

View file

@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Overhaul storage of test values.
- Cleanup and simplify DSL implementation.
- Better error messages and detection when DSL methods are used when they shouldn't (i.e. `describe` inside `it`).
- Prevent usage of reserved keywords in DSL (such as `initialize`).
- Other minor internal improvements and cleanup.
### Removed

View file

@ -7,5 +7,8 @@ module Spectator
# This also helps keep error traces small.
# Documentation only useful for debugging is included in generated code.
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]
end
end

View file

@ -11,6 +11,7 @@ module Spectator::DSL
{% raise "Block required for 'let'" unless block %}
{% raise "Cannot use 'let' inside of a test block" if @def %}
{% raise "Block argument count for 'let' must be 0..1" if block.args.size > 1 %}
{% raise "Cannot use '#{name.id}' for 'let'" if ::Spectator::DSL::RESERVED_KEYWORDS.includes?(name.id.symbolize) %}
@%value = ::Spectator::LazyWrapper.new
@ -32,6 +33,7 @@ module Spectator::DSL
{% raise "Block required for 'let!'" unless block %}
{% raise "Cannot use 'let!' inside of a test block" if @def %}
{% raise "Block argument count for 'let!' must be 0..1" if block.args.size > 1 %}
{% raise "Cannot use '#{name.id}' for 'let!'" if ::Spectator::DSL::RESERVED_KEYWORDS.includes?(name.id.symbolize) %}
let({{name}}) {{block}}
before_each { {{name.id}} }
@ -58,6 +60,7 @@ module Spectator::DSL
{% raise "Block required for 'subject'" unless block %}
{% raise "Cannot use 'subject' inside of a test block" if @def %}
{% raise "Block argument count for 'subject' must be 0..1" if block.args.size > 1 %}
{% raise "Cannot use '#{name.id}' for 'subject'" if ::Spectator::DSL::RESERVED_KEYWORDS.includes?(name.id.symbolize) %}
let({{name.id}}) {{block}}
@ -89,6 +92,7 @@ module Spectator::DSL
{% raise "Block required for 'subject!'" unless block %}
{% raise "Cannot use 'subject!' inside of a test block" if @def %}
{% raise "Block argument count for 'subject!' must be 0..1" if block.args.size > 1 %}
{% raise "Cannot use '#{name.id}' for 'subject!'" if ::Spectator::DSL::RESERVED_KEYWORDS.includes?(name.id.symbolize) %}
let!({{name.id}}) {{block}}