-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added additional Boolean, Union and Nilable types
fixes #6
- Loading branch information
Showing
7 changed files
with
159 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ | |
|
||
module Delivered | ||
autoload :Signature, 'delivered/signature' | ||
autoload :Types, 'delivered/types' | ||
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
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,41 @@ | ||
# frozen_string_literal: true | ||
|
||
module Delivered | ||
class UnionType | ||
def initialize(*types) | ||
@types = types | ||
end | ||
|
||
def ===(value) | ||
@types.any? { |type| type === value } | ||
end | ||
end | ||
|
||
class NilableType | ||
def initialize(type = nil) | ||
@type = type | ||
end | ||
|
||
def ===(value) | ||
(@type.nil? ? true : nil === value) || @type === value | ||
end | ||
end | ||
|
||
class BooleanType | ||
def initialize | ||
freeze | ||
end | ||
|
||
def ===(value) | ||
[true, false].include?(value) | ||
end | ||
end | ||
|
||
module Types | ||
module_function | ||
|
||
def Nilable(type = nil) = NilableType.new(type) | ||
def Union(*types) = UnionType.new(*types) | ||
def Boolean = BooleanType.new | ||
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
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,47 @@ | ||
# frozen_string_literal: true | ||
|
||
describe Delivered::Types do | ||
T = Delivered::Types | ||
|
||
describe 'Union' do | ||
it 'is within union' do | ||
assert T.Union(:one, :two) === :one | ||
end | ||
|
||
it 'raises when value is not in union' do | ||
expect { :three => ^(T.Union(:one, :two)) }.to raise_exception NoMatchingPatternError | ||
expect { :one => ^(T.Union(:one, :two)) }.not.to raise_exception | ||
end | ||
end | ||
|
||
describe 'Boolean' do | ||
it 'should be true or false' do | ||
assert T.Boolean === true | ||
assert T.Boolean === false | ||
end | ||
|
||
it 'raises when not boolean' do | ||
expect { 0 => ^(T.Boolean) }.to raise_exception NoMatchingPatternError | ||
end | ||
end | ||
|
||
describe 'Nilable' do | ||
it 'can be nil' do | ||
assert T.Nilable === nil | ||
end | ||
|
||
with 'no given type' do | ||
it 'can be any type' do | ||
assert T.Nilable === 'hello' | ||
expect { 'hello' => ^(T.Nilable) }.not.to raise_exception NoMatchingPatternError | ||
end | ||
end | ||
|
||
it 'can receive another type' do | ||
assert T.Nilable(String) === 'hello' | ||
assert T.Nilable(String) === nil | ||
expect { 'hello' => ^(T.Nilable(String)) }.not.to raise_exception | ||
expect { 1 => ^(T.Nilable(String)) }.to raise_exception NoMatchingPatternError | ||
end | ||
end | ||
end |