rtype-legacy-native 0.0.2 → 0.0.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 +50 -24
- data/Rakefile +1 -1
- data/ext/rtype/legacy/c/rtype_legacy.h +1 -1
- data/spec/rtype_legacy_spec.rb +146 -0
- data/spec/spec_helper.rb +1 -0
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fbcad8735dfcab9290ac1583500134355040592b
|
4
|
+
data.tar.gz: 8540f84a446ea302a6128cb081eb9b63ee6a61d9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 98331f96aa08994c35f1e8996813965353d6f396cdd70ebbb8c68d5cf9968ff2ff7996c676ade051cebcdcaf9b608da68792af4ea098053600229fff16d1237a
|
7
|
+
data.tar.gz: ce85db1f8e1693972625086c4dc6bf666573eabd8f9a83217b53eb24b93847696f1ff1efb12a4ec1f7486ffbe2a4c58eaf24100eb2a1b11593d93f5c5843a9ce
|
data/README.md
CHANGED
@@ -12,7 +12,9 @@ class Test
|
|
12
12
|
a.to_i + b
|
13
13
|
end
|
14
14
|
|
15
|
-
|
15
|
+
# Second hash is keyword argument signature
|
16
|
+
# (syntax compatibility with 'rtype' gem)
|
17
|
+
rtype [{state: Boolean}, {}] => Boolean
|
16
18
|
def self.invert(opts)
|
17
19
|
!opts[:state]
|
18
20
|
end
|
@@ -22,7 +24,7 @@ Test.new.sum(123, "asd")
|
|
22
24
|
# (Rtype::ArgumentTypeError) for 2nd argument:
|
23
25
|
# Expected "asd" to be a Numeric
|
24
26
|
|
25
|
-
Test
|
27
|
+
Test.invert(state: 0)
|
26
28
|
# (Rtype::ArgumentTypeError) for 1st argument:
|
27
29
|
# Expected {:state=>0} to be a hash with 1 elements:
|
28
30
|
# - state : Expected 0 to be a Boolean
|
@@ -47,8 +49,10 @@ Test::invert(state: 0)
|
|
47
49
|
- Provides type checking for arguments and return
|
48
50
|
- [Type checking for hash elements](#hash)
|
49
51
|
- [Duck Typing](#duck-typing)
|
50
|
-
- [Typed Array](#typed-array)
|
52
|
+
- [Typed Array](#typed-array), Typed Set, Typed Hash
|
51
53
|
- [Numeric check](#special-behaviors). e.g. `Int >= 0`
|
54
|
+
- [Type checking for getter and setter](#attr_accessor-with-rtype)
|
55
|
+
- [float_accessor](#float_accessor), [bool_accessor](#bool_accessor)
|
52
56
|
- Custom type behavior
|
53
57
|
- ...
|
54
58
|
|
@@ -74,16 +78,8 @@ gem 'rtype-legacy-native'
|
|
74
78
|
```
|
75
79
|
then, Rtype Legacy uses it. (**Do not** `require 'rtype-legacy-native'`)
|
76
80
|
|
77
|
-
#### Java extension for JRuby
|
78
|
-
|
79
|
-
```ruby
|
80
|
-
gem install rtype-legacy-java
|
81
|
-
```
|
82
|
-
or add to your `Gemfile`:
|
83
|
-
```ruby
|
84
|
-
gem 'rtype-legacy-java'
|
85
|
-
```
|
86
|
-
then, Rtype Legacy uses it. (**Do not** `require 'rtype-legacy-java'`)
|
81
|
+
#### Java extension for JRuby is automatic
|
82
|
+
**Do not** `require 'rtype-java'`
|
87
83
|
|
88
84
|
## Usage
|
89
85
|
|
@@ -113,7 +109,7 @@ then, Rtype Legacy uses it. (**Do not** `require 'rtype-legacy-java'`)
|
|
113
109
|
- Example: [Hash](#hash)
|
114
110
|
|
115
111
|
- [Special Behaviors](#special-behaviors)
|
116
|
-
- `TypedArray`, `Num, Int, Flo`, `And`, `Xor`, `Not`, `Nilable`
|
112
|
+
- `TypedArray, TypedSet, TypedHash`, `Num, Int, Flo`, `And`, `Xor`, `Not`, `Nilable`
|
117
113
|
|
118
114
|
### Examples
|
119
115
|
|
@@ -209,10 +205,12 @@ func({"msg" => "hello hash"})
|
|
209
205
|
func({msg: "hello hash"}) # hello hash
|
210
206
|
```
|
211
207
|
|
212
|
-
####
|
213
|
-
`rtype_accessor` : calls `attr_accessor` if the accessor
|
208
|
+
#### attr_accessor with rtype
|
209
|
+
- `rtype_accessor(*names, type)` : calls `attr_accessor` if the accessor methods(getter/setter) are not defined. and makes it typed
|
210
|
+
- `rtype_reader(*names, type)` : calls `attr_reader` if the getters are not defined. and makes it typed
|
211
|
+
- `rtype_writer(*names, type)` : calls `attr_writer` if the setters are not defined. and makes it typed
|
214
212
|
|
215
|
-
You can use `rtype_accessor_self` for static accessor.
|
213
|
+
You can use `rtype_accessor_self` for static accessor. (`rtype_reader_self`, `rtype_writer_self` also exist)
|
216
214
|
|
217
215
|
```ruby
|
218
216
|
require 'rtype'
|
@@ -266,6 +264,29 @@ sum([1, 2, 3]) # => 6
|
|
266
264
|
sum([1.0, 2, 3]) # => 6.0
|
267
265
|
```
|
268
266
|
|
267
|
+
#### float_accessor
|
268
|
+
```ruby
|
269
|
+
class Point
|
270
|
+
float_accessor :x, :y
|
271
|
+
end
|
272
|
+
|
273
|
+
v = Point.new
|
274
|
+
v.x = 1
|
275
|
+
v.x # => 1.0 (always Float)
|
276
|
+
```
|
277
|
+
|
278
|
+
#### bool_accessor
|
279
|
+
```ruby
|
280
|
+
class Human
|
281
|
+
bool_accessor :hungry
|
282
|
+
end
|
283
|
+
|
284
|
+
a = Human.new
|
285
|
+
a.hungry = true
|
286
|
+
a.hungry? # => true
|
287
|
+
a.hungry # NoMethodError
|
288
|
+
```
|
289
|
+
|
269
290
|
#### `rtype`
|
270
291
|
```ruby
|
271
292
|
require 'rtype'
|
@@ -358,9 +379,14 @@ Example.new.method(:test).return_type
|
|
358
379
|
|
359
380
|
#### Special Behaviors
|
360
381
|
- `TypedArray` : Ensures value is an array with the type (type signature)
|
361
|
-
- `Array
|
362
|
-
- or `Rtype::Behavior::TypedArray[type]`
|
382
|
+
- `Array.of(type)` (recommended)
|
363
383
|
- Example: [TypedArray](#typed-array)
|
384
|
+
|
385
|
+
- `TypedSet` : Ensures value is a set with the type (type signature)
|
386
|
+
- `Set.of(type)` (recommended)
|
387
|
+
|
388
|
+
- `TypedHash` : Ensures value is a hash with the type (type signature)
|
389
|
+
- `Hash.of(key_type, value_type)` (recommended)
|
364
390
|
|
365
391
|
- `Num, Int, Flo` : Numeric check
|
366
392
|
- `Num/Int/Flo >/>=/</<=/== x`
|
@@ -369,19 +395,19 @@ Example.new.method(:test).return_type
|
|
369
395
|
- e.g. `Flo >= 2` means value must be a `Float` and >= 2
|
370
396
|
|
371
397
|
- `And` : Ensures value is valid for all given types
|
372
|
-
- `Rtype
|
398
|
+
- `Rtype.and(*types)`, `Rtype::Behavior::And[*types]`
|
373
399
|
- or `Array#comb`, `Object#and(*others)`
|
374
400
|
|
375
401
|
- `Xor` : Ensures value is valid for only one of given types
|
376
|
-
- `Rtype
|
402
|
+
- `Rtype.xor(*types)`, `Rtype::Behavior::Xor[*types]`
|
377
403
|
- or `Object#xor(*others)`
|
378
404
|
|
379
405
|
- `Not` : Ensures value is not valid for all given types
|
380
|
-
- `Rtype
|
406
|
+
- `Rtype.not(*types)`, `Rtype::Behavior::Not[*types]`
|
381
407
|
- or `Object#not`
|
382
408
|
|
383
409
|
- `Nilable` : Value can be nil
|
384
|
-
- `Rtype
|
410
|
+
- `Rtype.nilable(type)`, `Rtype::Behavior::Nilable[type]`
|
385
411
|
- or `Object#nilable`, `Object#or_nil`
|
386
412
|
|
387
413
|
- You can create custom behaviors by extending `Rtype::Behavior::Base`
|
@@ -392,7 +418,7 @@ Example.new.method(:test).return_type
|
|
392
418
|
## Benchmarks
|
393
419
|
Result of `rake benchmark` ([source](https://github.com/sputnikgugja/rtype-legacy/tree/master/benchmark/benchmark.rb))
|
394
420
|
|
395
|
-
Rubype and Sig don't support 1.9 ruby. Typecheck
|
421
|
+
Rubype and Sig don't support 1.9 ruby. Typecheck raises an error in my environment
|
396
422
|
|
397
423
|
### MRI
|
398
424
|
```
|
data/Rakefile
CHANGED
data/spec/rtype_legacy_spec.rb
CHANGED
@@ -152,6 +152,93 @@ describe Rtype::Legacy do
|
|
152
152
|
expect {TestClass::value2}.to raise_error Rtype::ReturnTypeError
|
153
153
|
end
|
154
154
|
|
155
|
+
it 'Kernel#rtype_reader' do
|
156
|
+
class ReaderTestClass
|
157
|
+
rtype_reader :value, :value2, String
|
158
|
+
|
159
|
+
def initialize
|
160
|
+
@value = 123
|
161
|
+
@value2 = 123
|
162
|
+
end
|
163
|
+
end
|
164
|
+
expect {ReaderTestClass.new.value}.to raise_error Rtype::ReturnTypeError
|
165
|
+
expect {ReaderTestClass.new.value2}.to raise_error Rtype::ReturnTypeError
|
166
|
+
end
|
167
|
+
|
168
|
+
it 'Kernel#rtype_reader_self' do
|
169
|
+
class ReaderTestClass
|
170
|
+
@@value = 123
|
171
|
+
@@value2 = 123
|
172
|
+
rtype_reader_self :value, :value2, String
|
173
|
+
end
|
174
|
+
expect {ReaderTestClass::value}.to raise_error Rtype::ReturnTypeError
|
175
|
+
expect {ReaderTestClass::value2}.to raise_error Rtype::ReturnTypeError
|
176
|
+
end
|
177
|
+
|
178
|
+
it 'Kernel#rtype_writer' do
|
179
|
+
class WriterTestClass
|
180
|
+
rtype_writer :value, :value2, String
|
181
|
+
|
182
|
+
def initialize
|
183
|
+
@value = 123
|
184
|
+
@value2 = 123
|
185
|
+
end
|
186
|
+
end
|
187
|
+
expect {WriterTestClass.new.value = 123}.to raise_error Rtype::ArgumentTypeError
|
188
|
+
expect {WriterTestClass.new.value2 = 123}.to raise_error Rtype::ArgumentTypeError
|
189
|
+
end
|
190
|
+
|
191
|
+
it 'Kernel#rtype_writer_self' do
|
192
|
+
class WriterTestClass
|
193
|
+
@@value = 123
|
194
|
+
@@value2 = 123
|
195
|
+
rtype_writer_self :value, :value2, String
|
196
|
+
end
|
197
|
+
expect {WriterTestClass::value = 123}.to raise_error Rtype::ArgumentTypeError
|
198
|
+
expect {WriterTestClass::value2 = 123}.to raise_error Rtype::ArgumentTypeError
|
199
|
+
end
|
200
|
+
|
201
|
+
it 'Kernel#float_accessor' do
|
202
|
+
class FloatAccessorTestClass
|
203
|
+
float_accessor :float, :int
|
204
|
+
|
205
|
+
def initialize
|
206
|
+
@float = 10.0
|
207
|
+
@int = 10
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
float_accessor_test = FloatAccessorTestClass.new
|
212
|
+
|
213
|
+
float_accessor_test.float
|
214
|
+
expect {float_accessor_test.int}.to raise_error Rtype::ReturnTypeError
|
215
|
+
float_accessor_test.float = 5.0
|
216
|
+
float_accessor_test.float = 5
|
217
|
+
expect(float_accessor_test.float).to eql(5.0) # be(expected) => passes if actual.eql?(expected)
|
218
|
+
expect(float_accessor_test.float).not_to eql(5)
|
219
|
+
end
|
220
|
+
|
221
|
+
it 'Kernel#bool_accessor' do
|
222
|
+
class BoolAccessorTestClass
|
223
|
+
bool_accessor :state, :invalid_var
|
224
|
+
|
225
|
+
def initialize
|
226
|
+
@state = false
|
227
|
+
@invalid_var = 123
|
228
|
+
end
|
229
|
+
end
|
230
|
+
|
231
|
+
bool_accessor_test = BoolAccessorTestClass.new
|
232
|
+
|
233
|
+
bool_accessor_test.state?
|
234
|
+
expect {bool_accessor_test.state}.to raise_error NoMethodError
|
235
|
+
expect(bool_accessor_test.state?).to eql(false)
|
236
|
+
bool_accessor_test.state = true
|
237
|
+
expect(bool_accessor_test.state?).to eql(true)
|
238
|
+
expect {bool_accessor_test.state = 123}.to raise_error Rtype::ArgumentTypeError
|
239
|
+
expect {bool_accessor_test.invalid_var?}.to raise_error Rtype::ReturnTypeError
|
240
|
+
end
|
241
|
+
|
155
242
|
describe 'Test type behaviors' do
|
156
243
|
describe 'Module' do
|
157
244
|
it "is right" do
|
@@ -451,6 +538,65 @@ describe Rtype::Legacy do
|
|
451
538
|
end
|
452
539
|
end
|
453
540
|
|
541
|
+
describe 'Rtype::Behavior::TypedSet' do
|
542
|
+
it 'class singleton [] method' do
|
543
|
+
klass.send :rtype, :return_nil, [ Rtype::Behavior::TypedSet[Integer] ] => nil
|
544
|
+
instance.return_nil( Set.new([123]) )
|
545
|
+
expect {instance.return_nil(123)}.to raise_error Rtype::ArgumentTypeError
|
546
|
+
expect { instance.return_nil(Set.new([1.0])) }.to raise_error Rtype::ArgumentTypeError
|
547
|
+
end
|
548
|
+
|
549
|
+
it 'core extension method (Set::of)' do
|
550
|
+
klass.send :rtype, :return_nil, [ Set.of(Integer) ] => nil
|
551
|
+
instance.return_nil( Set.new([123]) )
|
552
|
+
expect {instance.return_nil(123)}.to raise_error Rtype::ArgumentTypeError
|
553
|
+
expect { instance.return_nil(Set.new([1.0])) }.to raise_error Rtype::ArgumentTypeError
|
554
|
+
end
|
555
|
+
|
556
|
+
it 'complicated type sig' do
|
557
|
+
klass.send :rtype, :return_nil, [ Set.of(:to_i.and(:chars)) ] => nil
|
558
|
+
instance.return_nil( Set.new(["hello"]) )
|
559
|
+
expect {instance.return_nil("hello")}.to raise_error Rtype::ArgumentTypeError
|
560
|
+
expect { instance.return_nil(Set.new([123])) }.to raise_error Rtype::ArgumentTypeError
|
561
|
+
end
|
562
|
+
|
563
|
+
it 'allows empty set' do
|
564
|
+
klass.send :rtype, :return_nil, [ Set.of(Integer) ] => nil
|
565
|
+
instance.return_nil(Set.new)
|
566
|
+
end
|
567
|
+
end
|
568
|
+
|
569
|
+
describe 'Rtype::Behavior::TypedHash' do
|
570
|
+
it 'class singleton [] method' do
|
571
|
+
klass.send :rtype, :return_nil, [ Rtype::Behavior::TypedHash[Symbol, Integer] ] => nil
|
572
|
+
instance.return_nil( {key: 123} )
|
573
|
+
expect {instance.return_nil(:key)}.to raise_error Rtype::ArgumentTypeError
|
574
|
+
expect {instance.return_nil(123)}.to raise_error Rtype::ArgumentTypeError
|
575
|
+
expect {instance.return_nil( {"key" => 123} )}.to raise_error Rtype::ArgumentTypeError
|
576
|
+
end
|
577
|
+
|
578
|
+
it 'core extension method (Hash::of)' do
|
579
|
+
klass.send :rtype, :return_nil, [ Hash.of(Symbol, Integer) ] => nil
|
580
|
+
instance.return_nil( {key: 123} )
|
581
|
+
expect {instance.return_nil(:key)}.to raise_error Rtype::ArgumentTypeError
|
582
|
+
expect {instance.return_nil(123)}.to raise_error Rtype::ArgumentTypeError
|
583
|
+
expect {instance.return_nil( {"key" => 123} )}.to raise_error Rtype::ArgumentTypeError
|
584
|
+
end
|
585
|
+
|
586
|
+
it 'complicated type sig' do
|
587
|
+
klass.send :rtype, :return_nil, [ Hash.of(:to_i.and(:chars), :to_i.and(:chars)) ] => nil
|
588
|
+
instance.return_nil( {"key" => "val"} )
|
589
|
+
expect {instance.return_nil("hello")}.to raise_error Rtype::ArgumentTypeError
|
590
|
+
expect {instance.return_nil( {key: "val"} )}.to raise_error Rtype::ArgumentTypeError
|
591
|
+
expect {instance.return_nil( {"key" => :val} )}.to raise_error Rtype::ArgumentTypeError
|
592
|
+
end
|
593
|
+
|
594
|
+
it 'allows empty hash' do
|
595
|
+
klass.send :rtype, :return_nil, [ Hash.of(Symbol, Integer) ] => nil
|
596
|
+
instance.return_nil({})
|
597
|
+
end
|
598
|
+
end
|
599
|
+
|
454
600
|
describe 'Numeric check' do
|
455
601
|
it 'Num (Numeric)' do
|
456
602
|
klass.send :rtype, :return_nil, [Num >= 0] => Any
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rtype-legacy-native
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.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-08-
|
11
|
+
date: 2016-08-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rtype-legacy
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - '='
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 0.0.
|
19
|
+
version: 0.0.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.0.
|
26
|
+
version: 0.0.4
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|