Skip to content

Commit

Permalink
Spec: let with type declaration
Browse files Browse the repository at this point in the history
  • Loading branch information
ysbaddaden committed Oct 28, 2023
1 parent 329a33e commit 190b465
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/spec.cr
Expand Up @@ -3,9 +3,21 @@ require "./expectations"
module Minitest
class Spec < Test
macro let(name, &block)
def {{ name.id }}
@{{ name.id }} ||= begin; {{ yield }}; end
end
{% if name.is_a?(TypeDeclaration) %}
{% if name.value && block %}
{% raise "A let declaration MUST have a default value OR an initializer block but NOT both" %}
{% end %}

@{{name.var}} : {{name.type}} | Nil

def {{name.var}} : {{name.type}}
@{{name.var}} ||= {% if block %} {{yield}} {% else %} {{name.value}} {% end %}
end
{% else %}
def {{name.id}}
@{{name.id}} ||= {{yield}}
end
{% end %}
end

macro before(&block)
Expand Down
7 changes: 7 additions & 0 deletions test/spec_test.cr
Expand Up @@ -49,6 +49,8 @@ describe Minitest::Spec do

describe "let" do
let(:foo) { Foo.new }
let(utc : Time) { Time.local }
let(local : Time = Time.local)

it "memoizes the object for the duration of the test" do
foo.bar = "baz"
Expand All @@ -58,6 +60,11 @@ describe Minitest::Spec do
it "regenerates the object on each teardown" do
assert_nil foo.bar
end

it "declares the object type" do
assert utc.is_a?(Time)
assert local.is_a?(Time)
end
end

describe "expect" do
Expand Down

0 comments on commit 190b465

Please sign in to comment.