diff --git a/CHANGELOG.md b/CHANGELOG.md index 3e6215e..b224148 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/spectator/dsl.cr b/src/spectator/dsl.cr index 6a98e85..51d6f38 100644 --- a/src/spectator/dsl.cr +++ b/src/spectator/dsl.cr @@ -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 diff --git a/src/spectator/dsl/values.cr b/src/spectator/dsl/values.cr index fdff02f..09b9b7a 100644 --- a/src/spectator/dsl/values.cr +++ b/src/spectator/dsl/values.cr @@ -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}}