https://github.com/siman-man/tspec
TSpec add simple type check of method into Ruby.
https://github.com/siman-man/tspec
gem ruby
Last synced: 10 months ago
JSON representation
TSpec add simple type check of method into Ruby.
- Host: GitHub
- URL: https://github.com/siman-man/tspec
- Owner: siman-man
- License: mit
- Created: 2017-02-13T13:04:01.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2018-03-08T15:26:35.000Z (over 8 years ago)
- Last Synced: 2024-11-30T03:38:17.656Z (over 1 year ago)
- Topics: gem, ruby
- Language: Ruby
- Homepage: https://github.com/siman-man/tspec
- Size: 43.9 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# TSpec
[](https://travis-ci.org/siman-man/tspec)
TSpec adds a method of simple type check to 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:
```ruby
gem 'tspec'
```
And then execute:
$ bundle
Or install it by yourself:
$ gem install tspec
## Usage
TSpec can use `#receive` and `#return` method.
### receive
`receive` defines the type of method arguments.
```ruby
require 'tspec'
def echo(str)
puts str
end.receive(str: String)
echo('hello world') #=> ok
echo(123) #=> TSpec::ArgumentTypeError
```
You can specify multiple type, too.
```ruby
require 'tspec'
def echo(val)
puts val
end.receive(val: [String, Float])
echo('hello') #=> ok
echo(3.14) #=> ok
echo(123) #=> TSpec::ArgumentTypeError
```
If single method argument is given, you can skip keyword.
```ruby
require 'tspec'
def join_array(arr)
arr.join(' ')
end.receive([String])
puts join_array(%w(hello world)) #=> ok
puts join_array([1,2,3]) #=> TSpec::ArgumentTypeError
```
You can specify Array content type, although it may seem strange.
```ruby
require 'tspec'
def receive_string_array(arr)
arr.join
end.receive(arr: [[String]])
puts receive_string_array(['hello', 'world']) #=> ok
puts receive_string_array([:hello, :world]) #=> TSpec::ArgumentTypeError
```
### return
`return` defines the type of method return value.
```ruby
require 'tspec'
def message
'hello world'
end.return(String)
def dummy_message
'hello world'
end.return(Symbol)
puts message #=> ok
puts dummy_message #=> TSpec::ReturnValueTypeError
```
You can specify multiple return value, too.
```ruby
require 'tspec'
def random_val
[1.0, '1', :hello].sample
end.return(Float, String, Symbol)
10.times do
v = random_val
end
```
Also, you can specify Array content type.
```ruby
require 'tspec'
def message_list
%w(hello ruby world)
end.return([String])
p message_list
```
## Example
Combination of `receive` and `return` method.
```ruby
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](http://contributor-covenant.org) code of conduct.
## License
The gem is available under the terms of the [MIT License](http://opensource.org/licenses/MIT).