-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
734 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
|
||
class BCDD::Result::AddonsGivenExpectationsTest < Minitest::Test | ||
class DivideType | ||
include BCDD::Result::Expectations.mixin( | ||
config: { addon: { continue: true } }, | ||
success: :ok, | ||
failure: :err | ||
) | ||
|
||
def call(arg1, arg2) | ||
Given([arg1, arg2]) | ||
.and_then(:validate_numbers) | ||
.and_then(:validate_non_zero) | ||
.and_then(:divide) | ||
end | ||
|
||
private | ||
|
||
def validate_numbers(numbers) | ||
number1, number2 = numbers | ||
|
||
number1.is_a?(::Numeric) or return Failure(:err, 'arg1 must be numeric') | ||
number2.is_a?(::Numeric) or return Failure(:err, 'arg2 must be numeric') | ||
|
||
Continue(numbers) | ||
end | ||
|
||
def validate_non_zero(numbers) | ||
return Continue(numbers) unless numbers.last.zero? | ||
|
||
Failure(:err, 'arg2 must not be zero') | ||
end | ||
|
||
def divide((number1, number2)) | ||
Success(:ok, number1 / number2) | ||
end | ||
end | ||
|
||
class DivideTypes | ||
include BCDD::Result::Expectations.mixin( | ||
config: { addon: { continue: true } }, | ||
success: :division_completed, | ||
failure: %i[invalid_arg division_by_zero] | ||
) | ||
|
||
def call(arg1, arg2) | ||
Given([arg1, arg2]) | ||
.and_then(:validate_numbers) | ||
.and_then(:validate_non_zero) | ||
.and_then(:divide) | ||
end | ||
|
||
private | ||
|
||
def validate_numbers((arg1, arg2)) | ||
arg1.is_a?(::Numeric) or return Failure(:invalid_arg, 'arg1 must be numeric') | ||
arg2.is_a?(::Numeric) or return Failure(:invalid_arg, 'arg2 must be numeric') | ||
|
||
Continue([arg1, arg2]) | ||
end | ||
|
||
def validate_non_zero(numbers) | ||
return Continue(numbers) unless numbers.last.zero? | ||
|
||
Failure(:division_by_zero, 'arg2 must not be zero') | ||
end | ||
|
||
def divide((number1, number2)) | ||
Success(:division_completed, number1 / number2) | ||
end | ||
end | ||
|
||
module DivideTypeAndValue | ||
extend self, BCDD::Result::Expectations.mixin( | ||
config: { addon: { continue: true } }, | ||
success: { division_completed: Numeric }, | ||
failure: { invalid_arg: String, division_by_zero: String } | ||
) | ||
|
||
def call(arg1, arg2) | ||
Given([arg1, arg2]) | ||
.and_then(:validate_numbers) | ||
.and_then(:validate_non_zero) | ||
.and_then(:divide) | ||
end | ||
|
||
private | ||
|
||
def validate_numbers((arg1, arg2)) | ||
arg1.is_a?(::Numeric) or return Failure(:invalid_arg, 'arg1 must be numeric') | ||
arg2.is_a?(::Numeric) or return Failure(:invalid_arg, 'arg2 must be numeric') | ||
|
||
Continue([arg1, arg2]) | ||
end | ||
|
||
def validate_non_zero(numbers) | ||
return Continue(numbers) unless numbers.last.zero? | ||
|
||
Failure(:division_by_zero, 'arg2 must not be zero') | ||
end | ||
|
||
def divide((number1, number2)) | ||
Success(:division_completed, number1 / number2) | ||
end | ||
end | ||
|
||
test 'method chaining' do | ||
result1 = DivideType.new.call(10, 2) | ||
result2 = DivideTypes.new.call(10, 2) | ||
result3 = DivideTypeAndValue.call(10, 2) | ||
|
||
assert result1.success?(:ok) | ||
assert result2.success?(:division_completed) | ||
assert result3.success?(:division_completed) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
|
||
class BCDD::Result::AddonsGivenInstanceTest < Minitest::Test | ||
class Divide | ||
include BCDD::Result.mixin(config: { addon: { continue: true } }) | ||
|
||
def call(arg1, arg2) | ||
Given([arg1, arg2]) | ||
.and_then(:validate_numbers) | ||
.and_then(:validate_non_zero) | ||
.and_then(:divide) | ||
end | ||
|
||
private | ||
|
||
def validate_numbers((arg1, arg2)) | ||
arg1.is_a?(Numeric) or return Failure(:invalid_arg, 'arg1 must be numeric') | ||
arg2.is_a?(Numeric) or return Failure(:invalid_arg, 'arg2 must be numeric') | ||
|
||
Continue([arg1, arg2]) | ||
end | ||
|
||
def validate_non_zero(numbers) | ||
return Continue(numbers) unless numbers.last.zero? | ||
|
||
Failure(:division_by_zero, 'arg2 must not be zero') | ||
end | ||
|
||
def divide((number1, number2)) | ||
Success(:division_completed, number1 / number2) | ||
end | ||
end | ||
|
||
test 'method chaining' do | ||
success = Divide.new.call(10, 2) | ||
|
||
failure1 = Divide.new.call('10', 0) | ||
failure2 = Divide.new.call(10, '2') | ||
failure3 = Divide.new.call(10, 0) | ||
|
||
assert_predicate success, :success? | ||
assert_equal :division_completed, success.type | ||
assert_equal 5, success.value | ||
|
||
assert_predicate failure1, :failure? | ||
assert_equal :invalid_arg, failure1.type | ||
assert_equal 'arg1 must be numeric', failure1.value | ||
|
||
assert_predicate failure2, :failure? | ||
assert_equal :invalid_arg, failure2.type | ||
assert_equal 'arg2 must be numeric', failure2.value | ||
|
||
assert_predicate failure3, :failure? | ||
assert_equal :division_by_zero, failure3.type | ||
assert_equal 'arg2 must not be zero', failure3.value | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
|
||
class BCDD::Result::AddonsGivenSingletonTest < Minitest::Test | ||
module Divide | ||
extend self, BCDD::Result.mixin(config: { addon: { continue: true } }) | ||
|
||
def call(arg1, arg2) | ||
Given([arg1, arg2]) | ||
.and_then(:validate_numbers) | ||
.and_then(:validate_non_zero) | ||
.and_then(:divide) | ||
end | ||
|
||
private | ||
|
||
def validate_numbers((arg1, arg2)) | ||
arg1.is_a?(Numeric) or return Failure(:invalid_arg, 'arg1 must be numeric') | ||
arg2.is_a?(Numeric) or return Failure(:invalid_arg, 'arg2 must be numeric') | ||
|
||
Continue([arg1, arg2]) | ||
end | ||
|
||
def validate_non_zero(numbers) | ||
return Continue(numbers) unless numbers.last.zero? | ||
|
||
Failure(:division_by_zero, 'arg2 must not be zero') | ||
end | ||
|
||
def divide((number1, number2)) | ||
Success(:division_completed, number1 / number2) | ||
end | ||
end | ||
|
||
test 'method chaining using Continue' do | ||
success = Divide.call(10, 2) | ||
|
||
failure1 = Divide.call('10', 0) | ||
failure2 = Divide.call(10, '2') | ||
failure3 = Divide.call(10, 0) | ||
|
||
assert_predicate success, :success? | ||
assert_equal :division_completed, success.type | ||
assert_equal 5, success.value | ||
|
||
assert_predicate failure1, :failure? | ||
assert_equal :invalid_arg, failure1.type | ||
assert_equal 'arg1 must be numeric', failure1.value | ||
|
||
assert_predicate failure2, :failure? | ||
assert_equal :invalid_arg, failure2.type | ||
assert_equal 'arg2 must be numeric', failure2.value | ||
|
||
assert_predicate failure3, :failure? | ||
assert_equal :division_by_zero, failure3.type | ||
assert_equal 'arg2 must not be zero', failure3.value | ||
end | ||
end |
Oops, something went wrong.