rubype 0.2.3 → 0.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +13 -0
- data/lib/rubype.rb +18 -10
- data/lib/rubype/version.rb +1 -1
- data/test/minitest_helper.rb +1 -1
- data/test/test_rubype.rb +12 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e7c7fb90c7b7bb6d84aed99e298cfd92344c334b
|
4
|
+
data.tar.gz: 66a55c98516f1d8b2f4b1a1454516532406c3e24
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 56da79236105ebf887d96f031d596efdff6272ee1e1a548f3d87c23b63fde00bc49eb77c18de1146177e6157f0acf92bb46669f1b89026a147ab65f1b5b41c81
|
7
|
+
data.tar.gz: b2d3b9172ec56df5b2b5f1488c2b8022048d699202e0eadb26399aad46078d4b7da70d699ea6e3694e709819cd7ce5de9ed61a0ede214858c92fe60039e3ade4
|
data/README.md
CHANGED
@@ -125,6 +125,19 @@ MyClass.new.sum(1, 2)
|
|
125
125
|
MyClass.new.sum('1', 2)
|
126
126
|
```
|
127
127
|
|
128
|
+
### Check type info everywhere!
|
129
|
+
```ruby
|
130
|
+
class MyClass
|
131
|
+
def sum(x, y)
|
132
|
+
x.to_i + y
|
133
|
+
end
|
134
|
+
typesig :sum, [:to_i, Numeric] => Numeric
|
135
|
+
end
|
136
|
+
|
137
|
+
MyClass.new.method(:foo).type_info
|
138
|
+
# => [:to_i, Numeric] => Numeric
|
139
|
+
|
140
|
+
```
|
128
141
|
|
129
142
|
## Installation
|
130
143
|
|
data/lib/rubype.rb
CHANGED
@@ -27,6 +27,14 @@ class Method
|
|
27
27
|
methods_hash[name]
|
28
28
|
end
|
29
29
|
end
|
30
|
+
|
31
|
+
def arg_types
|
32
|
+
type_info.first.first if type_info
|
33
|
+
end
|
34
|
+
|
35
|
+
def return_type
|
36
|
+
type_info.first.last if type_info
|
37
|
+
end
|
30
38
|
end
|
31
39
|
|
32
40
|
module Rubype
|
@@ -43,15 +51,15 @@ module Rubype
|
|
43
51
|
# @param caller [Object]
|
44
52
|
# @param type_info_hash [Hash] { [ArgInfo_1, ArgInfo_2, ... ArgInfo_n] => RtnInfo }
|
45
53
|
# @param module [Module]
|
46
|
-
def define_typed_method(
|
54
|
+
def define_typed_method(owner, meth, type_info_hash, __rubype__)
|
47
55
|
arg_types, rtn_type = *strip_type_info(type_info_hash)
|
48
|
-
@@typed_method_info[
|
56
|
+
@@typed_method_info[owner][meth] = {
|
49
57
|
arg_types => rtn_type
|
50
58
|
}
|
51
59
|
__rubype__.send(:define_method, meth) do |*args, &block|
|
52
|
-
::Rubype.send(:assert_arg_type,
|
60
|
+
::Rubype.send(:assert_arg_type, self, meth, args, arg_types)
|
53
61
|
rtn = super(*args, &block)
|
54
|
-
::Rubype.send(:assert_trn_type,
|
62
|
+
::Rubype.send(:assert_trn_type, self, meth, rtn, rtn_type)
|
55
63
|
rtn
|
56
64
|
end
|
57
65
|
end
|
@@ -67,15 +75,15 @@ module Rubype
|
|
67
75
|
# @param meth [Symbol]
|
68
76
|
# @param args [Array<Object>]
|
69
77
|
# @param type_infos [Array<Class, Symbol>]
|
70
|
-
def assert_arg_type(
|
78
|
+
def assert_arg_type(meth_caller, meth, args, type_infos)
|
71
79
|
args.zip(type_infos).each.with_index(1) do |(arg, type_info), i|
|
72
80
|
case type_check(arg, type_info)
|
73
81
|
when :need_correct_class
|
74
82
|
raise ArgumentTypeError,
|
75
|
-
"Expected #{
|
83
|
+
"Expected #{meth_caller.class}##{meth}'s #{i}th argument to be #{type_info} but got #{arg.inspect} instead"
|
76
84
|
when :need_correct_method
|
77
85
|
raise ArgumentTypeError,
|
78
|
-
"Expected #{
|
86
|
+
"Expected #{meth_caller.class}##{meth}'s #{i}th argument to have method ##{type_info} but got #{arg.inspect} instead"
|
79
87
|
end
|
80
88
|
end
|
81
89
|
end
|
@@ -83,14 +91,14 @@ module Rubype
|
|
83
91
|
# @param caller [Module]
|
84
92
|
# @param rtn [Object]
|
85
93
|
# @param type_info [Class, Symbol]
|
86
|
-
def assert_trn_type(
|
94
|
+
def assert_trn_type(meth_caller, meth, rtn, type_info)
|
87
95
|
case type_check(rtn, type_info)
|
88
96
|
when :need_correct_class
|
89
97
|
raise ReturnTypeError,
|
90
|
-
"Expected #{
|
98
|
+
"Expected #{meth_caller.class}##{meth} to return #{type_info} but got #{rtn.inspect} instead"
|
91
99
|
when :need_correct_method
|
92
100
|
raise ReturnTypeError,
|
93
|
-
"Expected #{
|
101
|
+
"Expected #{meth_caller.class}##{meth} to return object which has method ##{type_info} but got #{rtn.inspect} instead"
|
94
102
|
end
|
95
103
|
end
|
96
104
|
|
data/lib/rubype/version.rb
CHANGED
data/test/minitest_helper.rb
CHANGED
data/test/test_rubype.rb
CHANGED
@@ -91,12 +91,22 @@ class TestRubype < MiniTest::Unit::TestCase
|
|
91
91
|
|
92
92
|
def test_type_info
|
93
93
|
klass = Class.new.class_eval <<-RUBY_CODE
|
94
|
-
def test_mth
|
94
|
+
def test_mth(n1, n2)
|
95
95
|
end
|
96
96
|
typesig :test_mth, [Numeric, Numeric] => String
|
97
97
|
RUBY_CODE
|
98
|
+
Object.const_set('MyClass', klass)
|
98
99
|
|
99
|
-
|
100
|
+
meth = klass.new.method(:test_mth)
|
101
|
+
assert_equal meth.type_info, { [Numeric, Numeric] => String }
|
102
|
+
assert_equal meth.arg_types, [Numeric, Numeric]
|
103
|
+
assert_equal meth.return_type, String
|
104
|
+
|
105
|
+
err = assert_raises(Rubype::ReturnTypeError) { meth.(1,2) }
|
106
|
+
assert_equal err.message, "Expected MyClass#test_mth to return String but got nil instead"
|
107
|
+
|
108
|
+
err = assert_raises(Rubype::ArgumentTypeError) { meth.(1,'2') }
|
109
|
+
assert_equal err.message, "Expected MyClass#test_mth's 2th argument to be Numeric but got \"2\" instead"
|
100
110
|
end
|
101
111
|
|
102
112
|
private
|