Fix various issues with subjects

All tests in crystal spec pass!
This commit is contained in:
Michael Miller 2021-01-30 20:38:09 -07:00
parent 948e29a8b7
commit 078058ad05
No known key found for this signature in database
GPG key ID: FB9F12F7C646A4AD
2 changed files with 21 additions and 12 deletions

View file

@ -1,6 +1,7 @@
require "../source"
require "./builder"
require "./tags"
require "./values"
module Spectator::DSL
# DSL methods and macros for creating example groups.
@ -89,13 +90,13 @@ module Spectator::DSL
{{described_type}}
end
private def _spectator_implicit_subject
{% if described_type < Reference || described_type < Value %}
described_class.new
{% else %}
{% if described_type < Reference || described_type < Value %}
subject { described_class.new }
{% else %}
private def subject
described_class
{% end %}
end
end
{% end %}
{% else %}
private def _spectator_implicit_subject
{{what}}

View file

@ -53,11 +53,15 @@ module Spectator::DSL
# The block is evaluated only the first time the subject is referenced
# and the return value is saved for subsequent calls.
macro subject(name, &block)
subject {{block}}
{% 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 %}
let({{name.id}}) {{block}}
{% if name.id != :subject.id %}
def {{name.id}}
subject
def subject
{{name.id}}
end
{% end %}
end
@ -80,11 +84,15 @@ module Spectator::DSL
# The block is evaluated once before the example runs
# and the return value is saved for subsequent calls.
macro subject!(name, &block)
subject! {{block}}
{% 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 %}
let!({{name.id}}) {{block}}
{% if name.id != :subject.id %}
def {{name.id}}
subject
def subject
{{name.id}}
end
{% end %}
end