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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d752aa79376b7981146f2f8b9f6ef94f8fc74b20
4
- data.tar.gz: de61c2ff8105a604256f0d89c1e6c65285a94ddf
3
+ metadata.gz: e7c7fb90c7b7bb6d84aed99e298cfd92344c334b
4
+ data.tar.gz: 66a55c98516f1d8b2f4b1a1454516532406c3e24
5
5
  SHA512:
6
- metadata.gz: 08e2f22b13c8138246daf7c94417b73be722c55310aee0e21d0a6799d8c36673ef5b598f3b73dd8b32cb9aa1f4a1d81afcb8e5065e44e6bda52d7af47cc7cec8
7
- data.tar.gz: 5792a3bc311e89a8ca0b5b5333819ce335969bda6f2c35d4ce64054d818070a03693af4340ee9d2597c840da97c0c0699ccfb86960afdd383fc47574df22d00a
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(meth_caller, meth, type_info_hash, __rubype__)
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[meth_caller][meth] = {
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, meth_caller, meth, args, arg_types)
60
+ ::Rubype.send(:assert_arg_type, self, meth, args, arg_types)
53
61
  rtn = super(*args, &block)
54
- ::Rubype.send(:assert_trn_type, meth_caller, meth, rtn, rtn_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(caller, meth, args, type_infos)
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 #{caller.class}##{meth}'s #{i}th argument to be #{type_info} but got #{arg.inspect} instead"
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 #{caller.class}##{meth}'s #{i}th argument to have method ##{type_info} but got #{arg.inspect} instead"
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(caller, meth, rtn, type_info)
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 #{caller.class}##{meth} to return #{type_info} but got #{rtn.inspect} instead"
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 #{caller.class}##{meth} to return object which has method ##{type_info} but got #{rtn.inspect} instead"
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
 
@@ -1,3 +1,3 @@
1
1
  module Rubype
2
- VERSION = "0.2.3"
2
+ VERSION = "0.2.4"
3
3
  end
@@ -1,4 +1,4 @@
1
1
  $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
2
  require 'rubype'
3
-
3
+ require 'pry'
4
4
  require 'minitest/autorun'
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
- assert_equal klass.new.method(:test_mth).type_info, { [Numeric, Numeric] => String }
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubype
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - gogotanaka