rtype-java 0.6.3 → 0.6.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +56 -117
- data/Rakefile +2 -2
- data/ext/rtype/rtype_java.jar +0 -0
- data/spec/rtype_spec.rb +62 -0
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e3760485d15ecd4272d5e6d419579a3c1c837ea1
|
4
|
+
data.tar.gz: 4ba52f011231c59f011d9d5026008473138c13b8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 432e3b0890ed2bd5e50e77525e79ed062827e59845825dd34ee8817bc9409f163297693fd380314bcd914f846c4c5ced58d79eb639dab011aeed8a51e536dc42
|
7
|
+
data.tar.gz: 1ffb50b5d2e3cf5236406836a3cd2979eb64f1ecba01cf42f36f5b3f6b121eb97b988a3df9ee36fab5aa8a7614e5ad935a6da1a0028e6b488ce2af8140524e15
|
data/README.md
CHANGED
@@ -3,8 +3,6 @@
|
|
3
3
|
[![Build Status](https://travis-ci.org/sputnikgugja/rtype.svg?branch=master)](https://travis-ci.org/sputnikgugja/rtype)
|
4
4
|
[![Coverage Status](https://coveralls.io/repos/github/sputnikgugja/rtype/badge.svg?branch=master)](https://coveralls.io/github/sputnikgugja/rtype?branch=master)
|
5
5
|
|
6
|
-
You can do the type checking in Ruby with this gem!
|
7
|
-
|
8
6
|
```ruby
|
9
7
|
require 'rtype'
|
10
8
|
|
@@ -38,10 +36,12 @@ Test::invert(state: 0)
|
|
38
36
|
|
39
37
|
## Features
|
40
38
|
- Provides type checking for arguments and return
|
41
|
-
-
|
39
|
+
- Provides type checking for [Keyword Argument](#keyword-argument)
|
42
40
|
- [Type checking for hash elements](#hash)
|
43
|
-
- [Duck
|
41
|
+
- [Duck Typing](#duck-typing)
|
42
|
+
- [Typed Array](#typed-array)
|
44
43
|
- Custom type behavior
|
44
|
+
- ...
|
45
45
|
|
46
46
|
## Installation
|
47
47
|
Run `gem install rtype` or add `gem 'rtype'` to your `Gemfile`
|
@@ -55,7 +55,7 @@ require 'rtype'
|
|
55
55
|
Rtype itself is pure-ruby gem. but you can make it more faster by using native extension.
|
56
56
|
|
57
57
|
#### Native extension for MRI
|
58
|
-
|
58
|
+
Run
|
59
59
|
```ruby
|
60
60
|
gem install rtype-native
|
61
61
|
```
|
@@ -63,10 +63,10 @@ or add to your `Gemfile`:
|
|
63
63
|
```ruby
|
64
64
|
gem 'rtype-native'
|
65
65
|
```
|
66
|
-
then, Rtype
|
66
|
+
then, Rtype uses it. (**Do not** `require 'rtype-native'`)
|
67
67
|
|
68
68
|
#### Java extension for JRuby
|
69
|
-
|
69
|
+
Run
|
70
70
|
```ruby
|
71
71
|
gem install rtype-java
|
72
72
|
```
|
@@ -74,67 +74,71 @@ or add to your `Gemfile`:
|
|
74
74
|
```ruby
|
75
75
|
gem 'rtype-java'
|
76
76
|
```
|
77
|
-
then, Rtype
|
77
|
+
then, Rtype uses it. (**Do not** `require 'rtype-java'`)
|
78
78
|
|
79
79
|
## Usage
|
80
80
|
|
81
81
|
### Supported Type Behaviors
|
82
82
|
- `Module`
|
83
|
-
-
|
84
|
-
- `Any` :
|
83
|
+
- Value must be of this module (`is_a?`)
|
84
|
+
- `Any` : Alias for `BasicObject` (means Any Object)
|
85
85
|
- `Boolean` : `true` or `false`
|
86
86
|
- `Symbol`
|
87
|
-
-
|
87
|
+
- Value must respond to a method with this name
|
88
88
|
- `Regexp`
|
89
|
-
-
|
89
|
+
- Value must match this regexp pattern
|
90
90
|
- `Range`
|
91
|
-
-
|
91
|
+
- Value must be included in this range
|
92
92
|
- `Array`
|
93
|
-
-
|
93
|
+
- Value can be any type in this array
|
94
94
|
- `Hash`
|
95
|
-
-
|
96
|
-
- Each of
|
97
|
-
-
|
95
|
+
- Value must be a hash
|
96
|
+
- Each of elements must be valid
|
97
|
+
- Value's keys must be equal to this hash's keys
|
98
98
|
- **String** key is **different** from **symbol** key
|
99
99
|
- vs. Keyword arguments (e.g.)
|
100
|
-
- `[{}]` is **not** hash
|
101
|
-
- `[{}, {}]` is
|
102
|
-
- `[{}, {}, {}]` is two
|
100
|
+
- `[{}]` is **not** hash argument. it is keyword argument, because its position is last
|
101
|
+
- `[{}, {}]` is hash argument (first) and keyword argument (second)
|
102
|
+
- `[{}, {}, {}]` is two hash argument (first, second) and keyword argument (last)
|
103
103
|
- `{}` is keyword argument. non-keyword arguments must be in array.
|
104
104
|
- Of course, nested hash works
|
105
105
|
- Example: [Hash](#hash)
|
106
106
|
- `Proc`
|
107
|
-
-
|
107
|
+
- Value must return a truthy value for this proc
|
108
108
|
- `true`
|
109
|
-
-
|
109
|
+
- Value must be truthy
|
110
110
|
- `false`
|
111
|
-
-
|
111
|
+
- Value must be falsy
|
112
112
|
- `nil`
|
113
|
-
-
|
113
|
+
- Value must be nil
|
114
114
|
|
115
115
|
- Special Behaviors
|
116
|
-
- `
|
116
|
+
- `TypedArray` : Ensures value is an array with the type (type signature)
|
117
117
|
- `Array::of(type)` (recommended)
|
118
|
-
- `Rtype::Behavior::TypedArray[type]`
|
118
|
+
- or `Rtype::Behavior::TypedArray[type]`
|
119
119
|
- Example: [TypedArray](#typed-array)
|
120
|
+
|
121
|
+
- `Num, Int, Flo` : Numeric check
|
122
|
+
- `Num/Int/Flo >/>=/</<=/== x`
|
123
|
+
- e.g. `Num >= 2` means value must be a `Numeric` and >= 2
|
124
|
+
- e.g. `Int >= 2` means value must be a `Integer` and >= 2
|
125
|
+
- e.g. `Flo >= 2` means value must be a `Float` and >= 2
|
126
|
+
|
127
|
+
- `And` : Ensures value is valid for all given types
|
128
|
+
- `Rtype::and(*types)`, `Rtype::Behavior::And[*types]`
|
129
|
+
- or `Array#comb`, `Object#and(*others)`
|
120
130
|
|
121
|
-
- `
|
122
|
-
- `Rtype::
|
123
|
-
- `
|
124
|
-
- `Object#and(*others)`
|
125
|
-
|
126
|
-
- `Rtype::xor(*types)` : Ensures a value is valid for only one of the types
|
127
|
-
- `Rtype::xor(*types)`, `Rtype::Behavior::Xor[*types]`, `include Rtype::Behavior; Xor[...]`
|
128
|
-
- `Object#xor(*others)`
|
131
|
+
- `Xor` : Ensures value is valid for only one of given types
|
132
|
+
- `Rtype::xor(*types)`, `Rtype::Behavior::Xor[*types]`
|
133
|
+
- or `Object#xor(*others)`
|
129
134
|
|
130
|
-
- `
|
131
|
-
- `Rtype::not(*types)`, `Rtype::Behavior::Not[*types]
|
132
|
-
- `Object#not`
|
135
|
+
- `Not` : Ensures value is not valid for all given types
|
136
|
+
- `Rtype::not(*types)`, `Rtype::Behavior::Not[*types]`
|
137
|
+
- or `Object#not`
|
133
138
|
|
134
|
-
- `
|
135
|
-
- `Rtype::nilable(type)`, `Rtype::Behavior::Nilable[type]
|
136
|
-
- `Object#nilable`
|
137
|
-
- `Object#or_nil`
|
139
|
+
- `Nilable` : Value can be nil
|
140
|
+
- `Rtype::nilable(type)`, `Rtype::Behavior::Nilable[type]`
|
141
|
+
- or `Object#nilable`, `Object#or_nil`
|
138
142
|
|
139
143
|
- You can create custom behavior by extending `Rtype::Behavior::Base`
|
140
144
|
|
@@ -234,7 +238,7 @@ def func(hash)
|
|
234
238
|
puts hash[:msg]
|
235
239
|
end
|
236
240
|
|
237
|
-
# last hash is keyword arguments
|
241
|
+
# last hash is not hash argument but keyword arguments
|
238
242
|
func({}, {})
|
239
243
|
# (Rtype::ArgumentTypeError) for 1st argument:
|
240
244
|
# Expected {} to be a hash with 1 elements:
|
@@ -315,83 +319,18 @@ sum([1, 2, 3]) # => 6
|
|
315
319
|
sum([1.0, 2, 3]) # => 6.0
|
316
320
|
```
|
317
321
|
|
318
|
-
####
|
319
|
-
```ruby
|
320
|
-
### TEST 1 ###
|
321
|
-
require 'rtype'
|
322
|
-
|
323
|
-
class Example
|
324
|
-
rtype [[String, :func].comb] => Any
|
325
|
-
# also works:
|
326
|
-
# rtype [Rtype::and(String, :func)] => Any
|
327
|
-
def and_test(arg)
|
328
|
-
end
|
329
|
-
end
|
330
|
-
|
331
|
-
Example.new.and_test("A string")
|
332
|
-
# (Rtype::ArgumentTypeError) for 1st argument:
|
333
|
-
# Expected "A string" to be a String
|
334
|
-
# AND Expected "A string" to respond to :func
|
335
|
-
```
|
336
|
-
```ruby
|
337
|
-
### TEST 2 ###
|
338
|
-
# ... require rtype and define Example the same as above ...
|
339
|
-
|
340
|
-
class String
|
341
|
-
def func; end
|
342
|
-
end
|
343
|
-
|
344
|
-
Example.new.and_test("A string") # Works!
|
345
|
-
```
|
346
|
-
|
347
|
-
#### Combined duck type
|
348
|
-
Application of duck typing and combined type
|
349
|
-
|
350
|
-
```ruby
|
351
|
-
require 'rtype'
|
352
|
-
|
353
|
-
module Game
|
354
|
-
ENEMY = [
|
355
|
-
:name,
|
356
|
-
:level
|
357
|
-
].comb
|
358
|
-
|
359
|
-
class Player < Entity
|
360
|
-
include Rtype::Behavior
|
361
|
-
|
362
|
-
rtype [ENEMY] => Any
|
363
|
-
def attacks(enemy)
|
364
|
-
"Player attacks '#{enemy.name}' (level #{enemy.level})!"
|
365
|
-
end
|
366
|
-
end
|
367
|
-
|
368
|
-
class Slime < Entity
|
369
|
-
def name
|
370
|
-
"Powerful Slime"
|
371
|
-
end
|
372
|
-
|
373
|
-
def level
|
374
|
-
123
|
375
|
-
end
|
376
|
-
end
|
377
|
-
end
|
378
|
-
|
379
|
-
Game::Player.new.attacks Game::Slime.new
|
380
|
-
# Player attacks 'Powerful Slime' (level 123)!
|
381
|
-
```
|
382
|
-
|
383
|
-
#### Position of `rtype` && (specifying method name || annotation mode) && (symbol || string)
|
322
|
+
#### `rtype`
|
384
323
|
```ruby
|
385
324
|
require 'rtype'
|
386
325
|
|
387
326
|
class Example
|
388
|
-
# Recommended.
|
327
|
+
# Recommended. With annotation mode (no method name required)
|
389
328
|
rtype [Integer, String] => String
|
390
329
|
def hello_world(i, str)
|
391
330
|
puts "Hello? #{i} #{st
|
392
331
|
end
|
393
332
|
|
394
|
-
# Works (specifying method name)
|
333
|
+
# Works (with specifying method name)
|
395
334
|
rtype :hello_world, [Integer, String] => String
|
396
335
|
def hello_world(i, str)
|
397
336
|
puts "Hello? #{i} #{st
|
@@ -409,7 +348,7 @@ class Example
|
|
409
348
|
puts "Hello? #{i} #{str}"
|
410
349
|
end
|
411
350
|
|
412
|
-
# Doesn't work.
|
351
|
+
# Doesn't work. annotation mode works for following (next) method
|
413
352
|
def hello_world_four(i, str)
|
414
353
|
puts "Hello? #{i} #{str}"
|
415
354
|
end
|
@@ -417,8 +356,8 @@ class Example
|
|
417
356
|
end
|
418
357
|
```
|
419
358
|
|
420
|
-
####
|
421
|
-
In the outside of module, annotation mode
|
359
|
+
#### In the outside of module (root)
|
360
|
+
In the outside of module, annotation mode doesn't work. You must specify method name.
|
422
361
|
|
423
362
|
```ruby
|
424
363
|
rtype :say, [String] => Any
|
@@ -429,11 +368,11 @@ end
|
|
429
368
|
Test.new.say "Hello" # Hello
|
430
369
|
|
431
370
|
rtype [String] => Any
|
432
|
-
# (ArgumentError) Annotation mode not working
|
371
|
+
# (ArgumentError) Annotation mode not working in the outside of module
|
433
372
|
```
|
434
373
|
|
435
374
|
#### Class method
|
436
|
-
|
375
|
+
Annotation mode works for both instance method and class method
|
437
376
|
|
438
377
|
```ruby
|
439
378
|
require 'rtype'
|
@@ -463,8 +402,8 @@ end
|
|
463
402
|
Example::say_ya(3) #say ya ya ya
|
464
403
|
```
|
465
404
|
|
466
|
-
####
|
467
|
-
This is just
|
405
|
+
#### Type information
|
406
|
+
This is just 'information'
|
468
407
|
|
469
408
|
Any change of this doesn't affect type checking
|
470
409
|
|
data/Rakefile
CHANGED
@@ -3,10 +3,10 @@ require "rspec/core/rake_task"
|
|
3
3
|
# Default pattern is 'spec/**{,/*/**}/*_spec.rb'
|
4
4
|
RSpec::Core::RakeTask.new(:spec)
|
5
5
|
|
6
|
-
task :default => :spec
|
6
|
+
task :default => [:spec]
|
7
7
|
|
8
8
|
# Benchmark
|
9
9
|
desc "Compare with pure ruby and other gems"
|
10
10
|
task :benchmark do
|
11
11
|
ruby "benchmark/benchmark.rb"
|
12
|
-
end
|
12
|
+
end
|
data/ext/rtype/rtype_java.jar
CHANGED
Binary file
|
data/spec/rtype_spec.rb
CHANGED
@@ -474,6 +474,68 @@ describe Rtype do
|
|
474
474
|
instance.return_nil([])
|
475
475
|
end
|
476
476
|
end
|
477
|
+
|
478
|
+
describe 'Numeric check' do
|
479
|
+
it 'Num (Numeric)' do
|
480
|
+
klass.send :rtype, :return_nil, [Num >= 0] => Any
|
481
|
+
expect { instance.return_nil("hello") }.to raise_error Rtype::ArgumentTypeError
|
482
|
+
expect { instance.return_nil(-1) }.to raise_error Rtype::ArgumentTypeError
|
483
|
+
instance.return_nil(2)
|
484
|
+
instance.return_nil(2.0)
|
485
|
+
|
486
|
+
klass.send :rtype, :return_nil, [Num > 0] => Any
|
487
|
+
expect { instance.return_nil("non numeric") }.to raise_error Rtype::ArgumentTypeError
|
488
|
+
expect { instance.return_nil(0) }.to raise_error Rtype::ArgumentTypeError
|
489
|
+
|
490
|
+
klass.send :rtype, :return_nil, [Num < 0] => Any
|
491
|
+
expect { instance.return_nil("non numeric") }.to raise_error Rtype::ArgumentTypeError
|
492
|
+
expect { instance.return_nil(0) }.to raise_error Rtype::ArgumentTypeError
|
493
|
+
|
494
|
+
klass.send :rtype, :return_nil, [Num <= 0] => Any
|
495
|
+
expect { instance.return_nil("non numeric") }.to raise_error Rtype::ArgumentTypeError
|
496
|
+
expect { instance.return_nil(1) }.to raise_error Rtype::ArgumentTypeError
|
497
|
+
end
|
498
|
+
|
499
|
+
it 'Int (Integer)' do
|
500
|
+
klass.send :rtype, :return_nil, [Int >= 0] => Any
|
501
|
+
expect { instance.return_nil("hello") }.to raise_error Rtype::ArgumentTypeError
|
502
|
+
expect { instance.return_nil(1.0) }.to raise_error Rtype::ArgumentTypeError
|
503
|
+
expect { instance.return_nil(-1) }.to raise_error Rtype::ArgumentTypeError
|
504
|
+
instance.return_nil(2)
|
505
|
+
|
506
|
+
klass.send :rtype, :return_nil, [Int > 0] => Any
|
507
|
+
expect { instance.return_nil(1.0) }.to raise_error Rtype::ArgumentTypeError
|
508
|
+
expect { instance.return_nil(0) }.to raise_error Rtype::ArgumentTypeError
|
509
|
+
|
510
|
+
klass.send :rtype, :return_nil, [Int < 0] => Any
|
511
|
+
expect { instance.return_nil(-1.0) }.to raise_error Rtype::ArgumentTypeError
|
512
|
+
expect { instance.return_nil(0) }.to raise_error Rtype::ArgumentTypeError
|
513
|
+
|
514
|
+
klass.send :rtype, :return_nil, [Int <= 0] => Any
|
515
|
+
expect { instance.return_nil(0.0) }.to raise_error Rtype::ArgumentTypeError
|
516
|
+
expect { instance.return_nil(1) }.to raise_error Rtype::ArgumentTypeError
|
517
|
+
end
|
518
|
+
|
519
|
+
it 'Flo (Float)' do
|
520
|
+
klass.send :rtype, :return_nil, [Flo >= 0] => Any
|
521
|
+
expect { instance.return_nil("hello") }.to raise_error Rtype::ArgumentTypeError
|
522
|
+
expect { instance.return_nil(1) }.to raise_error Rtype::ArgumentTypeError
|
523
|
+
expect { instance.return_nil(-1.0) }.to raise_error Rtype::ArgumentTypeError
|
524
|
+
instance.return_nil(2.0)
|
525
|
+
|
526
|
+
klass.send :rtype, :return_nil, [Flo > 0] => Any
|
527
|
+
expect { instance.return_nil(1) }.to raise_error Rtype::ArgumentTypeError
|
528
|
+
expect { instance.return_nil(0.0) }.to raise_error Rtype::ArgumentTypeError
|
529
|
+
|
530
|
+
klass.send :rtype, :return_nil, [Flo < 0] => Any
|
531
|
+
expect { instance.return_nil(-1) }.to raise_error Rtype::ArgumentTypeError
|
532
|
+
expect { instance.return_nil(0.0) }.to raise_error Rtype::ArgumentTypeError
|
533
|
+
|
534
|
+
klass.send :rtype, :return_nil, [Flo <= 0] => Any
|
535
|
+
expect { instance.return_nil(0) }.to raise_error Rtype::ArgumentTypeError
|
536
|
+
expect { instance.return_nil(1.0) }.to raise_error Rtype::ArgumentTypeError
|
537
|
+
end
|
538
|
+
end
|
477
539
|
end
|
478
540
|
end
|
479
541
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rtype-java
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
4
|
+
version: 0.6.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sputnik Gugja
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-07-
|
11
|
+
date: 2016-07-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rtype
|
@@ -16,28 +16,28 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - '='
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 0.6.
|
19
|
+
version: 0.6.4
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - '='
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 0.6.
|
26
|
+
version: 0.6.4
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '11.0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
40
|
+
version: '11.0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|