TSpec
TSpec add simple type check of method into Ruby.
:construction: Recommended for use only in hobby programming. Do not use this in production apps. :construction:
Installation
Add this line to your application's Gemfile:
gem 'tspec'
And then execute:
$ bundle
Or install it yourself as:
$ gem install tspec
Usage
tspec can use #receive
and #return
method.
receive
receive
define the type of method arguments.
require 'tspec'
def echo(str)
puts str
end.receive(str: String)
specify multiple type.
require 'tspec'
def echo(val)
puts val
end.receive(val: [String, Float])
echo('hello')
echo(3.14)
if method arguments is single. it can skip keyword.
require 'tspec'
def join_array(arr)
arr.join(' ')
end.receive([String])
puts join_array(%w(hello world))
if specify Array content type. writing is strange.
require 'tspec'
def receive_string_array(arr)
arr.join
end.receive(arr: [[String]])
puts receive_string_array(['hello', 'world'])
return
return
define the type of method return value.
require 'tspec'
def
'hello world'
end.return(String)
multiple return value.
require 'tspec'
def random_val
[1.0, '1', :hello].sample
end.return(Float, String, Symbol)
10.times do
v = random_val
end
specify Array content type.
require 'tspec'
def
%w(hello ruby world)
end.return([String])
p
Example
combination receive
and return
method.
require 'tspec'
def string2symbol(str)
str.to_sym
end.receive(str: String).return(Symbol)
p string2symbol('hello') #=> :hello
p string2symbol(123) #=> TSpec::ArgumentTypeError
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/siman-man/tspec. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
License
The gem is available as open source under the terms of the MIT License.