From 623033623a91a493284d58507e0d2a501e2fe47f Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Sat, 25 Aug 2018 18:36:32 -0600 Subject: [PATCH] Add untyped ValueProxy base class --- src/spectator/{lazy.cr => lazy_value_proxy.cr} | 6 ++++-- src/spectator/value_proxy.cr | 9 +++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) rename src/spectator/{lazy.cr => lazy_value_proxy.cr} (92%) create mode 100644 src/spectator/value_proxy.cr 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