mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Implement #let functionality
This commit is contained in:
parent
623033623a
commit
709b226f8e
4 changed files with 23 additions and 37 deletions
|
@ -46,7 +46,19 @@ module Spectator
|
||||||
|
|
||||||
macro let(name, &block)
|
macro let(name, &block)
|
||||||
module Context
|
module Context
|
||||||
|
@_%proxy : ValueProxy?
|
||||||
|
|
||||||
def {{name.id}}
|
def {{name.id}}
|
||||||
|
if (proxy = @_%proxy)
|
||||||
|
proxy.as(TypedValueProxy(typeof({{name.id}}!))).value
|
||||||
|
else
|
||||||
|
{{name.id}}!.tap do |value|
|
||||||
|
@_%proxy = TypedValueProxy(typeof({{name.id}}!)).new(value)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def {{name.id}}!
|
||||||
{{block.body}}
|
{{block.body}}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,36 +0,0 @@
|
||||||
require "./value_proxy"
|
|
||||||
|
|
||||||
module Spectator
|
|
||||||
# Lazy initialization of a value.
|
|
||||||
# Constructs a value only once by calling a `Proc`.
|
|
||||||
# The value is then stored and reused - the `Proc` is only called once.
|
|
||||||
class LazyValueProxy(T) < ValueProxy
|
|
||||||
@value_or_block : Proc(T) | T
|
|
||||||
|
|
||||||
# Creates a lazy instance.
|
|
||||||
# The block provided to this method will be called
|
|
||||||
# when `#value` is invoked.
|
|
||||||
# The block will only be called once,
|
|
||||||
# and the result of the block will be cached.
|
|
||||||
def initialize(&block : -> T)
|
|
||||||
@value_or_block = block
|
|
||||||
end
|
|
||||||
|
|
||||||
# Retrieves the lazy initialized value.
|
|
||||||
# The first call to this method will create the value.
|
|
||||||
# Subsequent calls will return the same value.
|
|
||||||
def value
|
|
||||||
if value = @value_or_block.as?(T)
|
|
||||||
return value
|
|
||||||
else
|
|
||||||
@value_or_block = construct
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Calls the block used to construct the value.
|
|
||||||
# This method can only be called once per instance.
|
|
||||||
private def construct : T
|
|
||||||
@value_or_block.as(Proc(T)).call
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
10
src/spectator/typed_value_proxy.cr
Normal file
10
src/spectator/typed_value_proxy.cr
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
require "./value_proxy"
|
||||||
|
|
||||||
|
module Spectator
|
||||||
|
class TypedValueProxy(T) < ValueProxy
|
||||||
|
getter value : T
|
||||||
|
|
||||||
|
def initialize(@value : T)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -2,7 +2,7 @@ module Spectator
|
||||||
# Base class for proxying test values to examples.
|
# Base class for proxying test values to examples.
|
||||||
# This abstraction is required for inferring types.
|
# This abstraction is required for inferring types.
|
||||||
# The `DSL#let` macro makes heavy use of this.
|
# The `DSL#let` macro makes heavy use of this.
|
||||||
protected abstract class ValueProxy
|
private abstract class ValueProxy
|
||||||
# Retrieves the underlying value.
|
# Retrieves the underlying value.
|
||||||
abstract def value
|
abstract def value
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue