diff --git a/src/spectator/lazy.cr b/src/spectator/lazy_value_proxy.cr similarity index 92% rename from src/spectator/lazy.cr rename to src/spectator/lazy_value_proxy.cr index f76d30e..83d3a64 100644 --- a/src/spectator/lazy.cr +++ b/src/spectator/lazy_value_proxy.cr @@ -1,8 +1,10 @@ +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. - struct Lazy(T) + class LazyValueProxy(T) < ValueProxy @value_or_block : Proc(T) | T # Creates a lazy instance. @@ -17,7 +19,7 @@ module Spectator # Retrieves the lazy initialized value. # The first call to this method will create the value. # Subsequent calls will return the same value. - def value : T + def value if value = @value_or_block.as?(T) return value else diff --git a/src/spectator/value_proxy.cr b/src/spectator/value_proxy.cr new file mode 100644 index 0000000..8766e11 --- /dev/null +++ b/src/spectator/value_proxy.cr @@ -0,0 +1,9 @@ +module Spectator + # Base class for proxying test values to examples. + # This abstraction is required for inferring types. + # The `DSL#let` macro makes heavy use of this. + protected abstract class ValueProxy + # Retrieves the underlying value. + abstract def value + end +end