Skip to content

Commit

Permalink
[WIP] Add the Given() addon
Browse files Browse the repository at this point in the history
  • Loading branch information
serradura committed Jan 1, 2024
1 parent fb820c5 commit a01591e
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 16 deletions.
3 changes: 2 additions & 1 deletion lib/bcdd/result/config/switchers/addons.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ module Addons
].freeze

OPTIONS = {
continue: { default: false, affects: AFFECTS }
continue: { default: false, affects: AFFECTS },
given: { default: true, affects: AFFECTS }
}.transform_values!(&:freeze).freeze

def self.switcher
Expand Down
10 changes: 9 additions & 1 deletion lib/bcdd/result/context/expectations/mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,15 @@ module Continue
end
end

OPTIONS = { continue: Continue }.freeze
module Given
private def Given(*values)
value = values.map(&:to_h).reduce({}) { |acc, val| acc.merge(val) }

Success.new(type: :initial_input, value: value, subject: self)
end
end

OPTIONS = { continue: Continue, given: Given }.freeze

def self.options(config_flags)
::BCDD::Result::Config::Options.addon(map: config_flags, from: OPTIONS)
Expand Down
10 changes: 9 additions & 1 deletion lib/bcdd/result/context/mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,15 @@ def Success(type, **value)
end
end

OPTIONS = { continue: Continue }.freeze
module Given
private def Given(*values)
value = values.map(&:to_h).reduce({}) { |acc, val| acc.merge(val) }

_ResultAs(Success, :initial_input, value)
end
end

OPTIONS = { continue: Continue, given: Given }.freeze

def self.options(config_flags)
::BCDD::Result::Config::Options.addon(map: config_flags, from: OPTIONS)
Expand Down
8 changes: 7 additions & 1 deletion lib/bcdd/result/expectations/mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ module Continue
end
end

OPTIONS = { continue: Continue }.freeze
module Given
private def Given(value)
Success.new(type: :initial_input, value: value, subject: self)
end
end

OPTIONS = { continue: Continue, given: Given }.freeze

def self.options(config_flags)
Config::Options.addon(map: config_flags, from: OPTIONS)
Expand Down
8 changes: 7 additions & 1 deletion lib/bcdd/result/mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,13 @@ def Success(type, value = nil)
end
end

OPTIONS = { continue: Continue }.freeze
module Given
private def Given(value)
_ResultAs(Success, :initial_input, value)
end
end

OPTIONS = { continue: Continue, given: Given }.freeze

def self.options(config_flags)
Config::Options.addon(map: config_flags, from: OPTIONS)
Expand Down
1 change: 1 addition & 0 deletions sig/bcdd/result/config.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class BCDD::Result::Config::Switcher
end

module BCDD::Result::Config::Addons
AFFECTS: Array[String]
OPTIONS: Hash[String, Hash[Symbol, untyped]]

def self.switcher: -> BCDD::Result::Config::Switcher
Expand Down
12 changes: 12 additions & 0 deletions sig/bcdd/result/context.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ class BCDD::Result::Context
def Continue: (**untyped) -> BCDD::Result::Context::Success
end

module Given
include BCDD::Result::Context::Mixin::Methods

private

def Given: (*untyped) -> BCDD::Result::Context::Success
end

OPTIONS: Hash[Symbol, Module]

def self.options: (Hash[Symbol, Hash[Symbol, bool]]) -> Hash[Symbol, Module]
Expand Down Expand Up @@ -95,6 +103,10 @@ module BCDD::Result::Context::Expectations::Mixin
private def Continue: (**untyped) -> BCDD::Result::Context::Success
end

module Given
private def Given: (*untyped) -> BCDD::Result::Context::Success
end

OPTIONS: Hash[Symbol, Module]

def self.options: (Hash[Symbol, Hash[Symbol, bool]]) -> Hash[Symbol, Module]
Expand Down
4 changes: 4 additions & 0 deletions sig/bcdd/result/expectations.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ module BCDD::Result::Expectations::Mixin
private def Continue: (untyped) -> BCDD::Result::Success
end

module Given
private def Given: (untyped) -> BCDD::Result::Success
end

OPTIONS: Hash[Symbol, Module]

def self.options: (Hash[Symbol, Hash[Symbol, bool]]) -> Hash[Symbol, Module]
Expand Down
8 changes: 8 additions & 0 deletions sig/bcdd/result/mixin.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ class BCDD::Result
def Continue: (untyped) -> BCDD::Result::Success
end

module Given
include BCDD::Result::Mixin::Methods

private

def Given: (untyped) -> BCDD::Result::Success
end

OPTIONS: Hash[Symbol, Module]

def self.options: (Hash[Symbol, Hash[Symbol, bool]]) -> Hash[Symbol, Module]
Expand Down
3 changes: 2 additions & 1 deletion test/bcdd/result/config/addon_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class AddonTest < Minitest::Test

assert_equal(
{
continue: { enabled: false, affects: AFFECTS }
continue: { enabled: false, affects: AFFECTS },
given: { enabled: true, affects: AFFECTS }
},
config.options
)
Expand Down
2 changes: 1 addition & 1 deletion test/bcdd/result/config_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class Test < Minitest::Test
test '#to_h' do
config_values = BCDD::Result.config.to_h

assert_equal({ continue: false }, config_values[:addon])
assert_equal({ continue: false, given: true }, config_values[:addon])
assert_equal({ expectations: true, transitions: true }, config_values[:feature])
assert_equal({ nil_as_valid_value_checking: false }, config_values[:pattern_matching])
assert_equal({ 'Result' => false, 'Context' => false, 'BCDD::Context' => false }, config_values[:constant_alias])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,8 @@
class BCDD::Result::TransitionsEnabledWithSubjectSingletonNestedTest < Minitest::Test
include BCDDResultTransitionAssertions

module Giveable
def Given(value)
_ResultAs(BCDD::Result::Success, :initial_input, value)
end
end

module CheckForZeros
extend self, Giveable, BCDD::Result.mixin(config: { addon: { continue: true } })
extend self, BCDD::Result.mixin(config: { addon: { continue: true } })

def call(numbers)
BCDD::Result.transitions(name: 'CheckForZeros') do
Expand All @@ -33,7 +27,7 @@ def detect_zero(numbers, ref)
end

module Division
extend self, Giveable, BCDD::Result.mixin(config: { addon: { continue: true } })
extend self, BCDD::Result.mixin(config: { addon: { continue: true } })

def call(num1, num2)
BCDD::Result.transitions(name: 'Division') do
Expand Down Expand Up @@ -67,7 +61,7 @@ def divide((num1, num2))
end

module SumDivisionsByTwo
extend self, Giveable, BCDD::Result.mixin(config: { addon: { continue: true } })
extend self, BCDD::Result.mixin(config: { addon: { continue: true } })

def call(*numbers)
BCDD::Result.transitions(name: 'SumDivisionsByTwo') do
Expand Down

0 comments on commit a01591e

Please sign in to comment.