rubype 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +7 -16
- data/Rakefile +7 -2
- data/ext/rubype/rubype.c +31 -2
- data/lib/rubype/version.rb +1 -1
- data/lib/rubype.rb +6 -5
- data/rubype.gemspec +1 -1
- data/test/test_rubype.rb +46 -52
- metadata +3 -4
- data/lib/rubype/type_pair.rb +0 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6c7e1779556b1e9829b3b5043bc96aba5caaacb1
|
4
|
+
data.tar.gz: b2c48bd090e006e27a176803c0833d3a39883226
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 165eeb9c15017ab93dfefcf66236ff8096274bbd65e87fa082e0ae0fd62c10712c6636085fdd46dc96d06a59b1ddf318f9e97dcacd7df7bb78052441459bbcd6
|
7
|
+
data.tar.gz: 5983be0188e80c963ce8994949ff461abc99ea9217e8cde4265c088b2c4f565b58ee4146762e29a2ed76ac8b098436b3203c0b7caef1f7b4fea2be74ad45f2b9
|
data/README.md
CHANGED
@@ -12,12 +12,12 @@ class MyClass
|
|
12
12
|
def sum(x, y)
|
13
13
|
x + y
|
14
14
|
end
|
15
|
-
|
15
|
+
typesig sum: [Numeric, Numeric => Numeric]
|
16
16
|
|
17
17
|
def wrong_sum(x, y)
|
18
18
|
'string'
|
19
19
|
end
|
20
|
-
|
20
|
+
typesig wrong_sum: [Numeric, Numeric => Numeric]
|
21
21
|
end
|
22
22
|
|
23
23
|
MyClass.new.sum(1, 2)
|
@@ -29,23 +29,14 @@ MyClass.new.sum(1, 'string')
|
|
29
29
|
MyClass.new.wrong_sum(1, 2)
|
30
30
|
#=> TypeError: Expected wrong_sum to return Numeric but got "str" instead
|
31
31
|
|
32
|
-
# ex2: (Ruby 2.1.0+)
|
33
|
-
class MyClass
|
34
|
-
type Numeric, Numeric >= Numeric, def sum(x, y)
|
35
|
-
x + y
|
36
|
-
end
|
37
|
-
|
38
|
-
type Numeric, Numeric >= Numeric, def wrong_sum(x, y)
|
39
|
-
'string'
|
40
|
-
end
|
41
|
-
end
|
42
32
|
|
43
|
-
#
|
33
|
+
# ex2
|
44
34
|
class People
|
45
35
|
type People >= Any, def marry(people)
|
46
36
|
# Your Ruby code as usual
|
47
37
|
end
|
48
38
|
end
|
39
|
+
typesig marry: [People => Any]
|
49
40
|
|
50
41
|
People.new.marry(People.new)
|
51
42
|
#=> no error
|
@@ -63,9 +54,9 @@ class MyClass
|
|
63
54
|
def sum(x, y)
|
64
55
|
x + y
|
65
56
|
end
|
66
|
-
|
57
|
+
typesig sum: [Numeric, Numeric => Numeric]
|
67
58
|
|
68
|
-
def
|
59
|
+
def sum_without_type(x, y)
|
69
60
|
'string'
|
70
61
|
end
|
71
62
|
end
|
@@ -79,7 +70,7 @@ class MyClass
|
|
79
70
|
def foo(any_obj)
|
80
71
|
1
|
81
72
|
end
|
82
|
-
|
73
|
+
typesig sum: [Any => Numeric]
|
83
74
|
end
|
84
75
|
|
85
76
|
# It's totally OK!!
|
data/Rakefile
CHANGED
@@ -5,10 +5,15 @@ Rake::TestTask.new(:test) do |t|
|
|
5
5
|
t.libs << "test"
|
6
6
|
end
|
7
7
|
|
8
|
-
task :default => :test
|
9
|
-
|
10
8
|
require "rake/extensiontask"
|
11
9
|
|
12
10
|
Rake::ExtensionTask.new("rubype") do |ext|
|
13
11
|
ext.lib_dir = "lib/rubype"
|
14
12
|
end
|
13
|
+
|
14
|
+
task :compile_and_test do
|
15
|
+
Rake::Task['compile'].invoke
|
16
|
+
Rake::Task['test'].invoke
|
17
|
+
end
|
18
|
+
|
19
|
+
task default: :compile_and_test
|
data/ext/rubype/rubype.c
CHANGED
@@ -1,9 +1,38 @@
|
|
1
1
|
#include "rubype.h"
|
2
2
|
|
3
|
-
VALUE rb_mRubype;
|
3
|
+
VALUE rb_mRubype, rb_cAny, rb_mBoolean, rb_cTypePair;
|
4
|
+
|
5
|
+
#define STR2SYM(x) ID2SYM(rb_intern(x))
|
6
|
+
|
7
|
+
static VALUE
|
8
|
+
rb_mod_prepend(int argc, VALUE *argv, VALUE module)
|
9
|
+
{
|
10
|
+
int i;
|
11
|
+
ID id_prepend_features, id_prepended;
|
12
|
+
|
13
|
+
CONST_ID(id_prepend_features, "prepend_features");
|
14
|
+
CONST_ID(id_prepended, "prepended");
|
15
|
+
for (i = 0; i < argc; i++)
|
16
|
+
Check_Type(argv[i], T_MODULE);
|
17
|
+
while (argc--) {
|
18
|
+
rb_funcall(argv[argc], id_prepend_features, 1, module);
|
19
|
+
rb_funcall(argv[argc], id_prepended, 1, module);
|
20
|
+
}
|
21
|
+
|
22
|
+
return module;
|
23
|
+
}
|
4
24
|
|
5
25
|
void
|
6
26
|
Init_rubype(void)
|
7
27
|
{
|
8
|
-
rb_mRubype
|
28
|
+
// rb_mRubype = rb_define_module("Rubype");
|
29
|
+
// rb_cAny = rb_define_class("Any", rb_cObject);
|
30
|
+
// rb_mBoolean = rb_define_module("Boolean");
|
31
|
+
// rb_include_module(rb_cTrueClass, rb_mBoolean);
|
32
|
+
// rb_include_module(rb_cFalseClass, rb_mBoolean);
|
33
|
+
// rb_define_class(
|
34
|
+
// "TypePair",
|
35
|
+
// rb_funcall(rb_cStruct, rb_intern("new"), 2, STR2SYM("last_arg_type"), STR2SYM("rtn_type"))
|
36
|
+
// );
|
37
|
+
rb_define_method(rb_cModule, "prepend", rb_mod_prepend, -1);
|
9
38
|
}
|
data/lib/rubype/version.rb
CHANGED
data/lib/rubype.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require "rubype/rubype"
|
2
2
|
|
3
3
|
# Builtin Contracts
|
4
4
|
class Any; end
|
@@ -13,13 +13,14 @@ class Module
|
|
13
13
|
@__rubype__
|
14
14
|
end
|
15
15
|
|
16
|
-
def
|
17
|
-
|
16
|
+
def typesig(hash)
|
17
|
+
meth = hash.keys.first
|
18
|
+
*arg_types, type_pair = hash.values.first
|
18
19
|
|
19
20
|
__rubype__.send(:define_method, meth) do |*args, &block|
|
20
|
-
::Rubype.assert_arg_type(meth, args, arg_types << type_pair.
|
21
|
+
::Rubype.assert_arg_type(meth, args, arg_types << type_pair.keys.first)
|
21
22
|
rtn = super(*args, &block)
|
22
|
-
::Rubype.assert_trn_type(meth, rtn, type_pair.
|
23
|
+
::Rubype.assert_trn_type(meth, rtn, type_pair.values.first)
|
23
24
|
rtn
|
24
25
|
end
|
25
26
|
self
|
data/rubype.gemspec
CHANGED
@@ -11,7 +11,7 @@ Gem::Specification.new do |spec|
|
|
11
11
|
spec.extensions = ["ext/rubype/extconf.rb"]
|
12
12
|
spec.summary = %q{Ruby with type.}
|
13
13
|
spec.description = %q{Ruby with type.}
|
14
|
-
spec.homepage = "http://
|
14
|
+
spec.homepage = "http://gogotanaka.me/"
|
15
15
|
spec.license = "MIT"
|
16
16
|
|
17
17
|
spec.files = `git ls-files -z`.split("\x0")
|
data/test/test_rubype.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'minitest_helper'
|
2
2
|
class TypePair
|
3
3
|
def to_s
|
4
|
-
"#{last_arg_type}
|
4
|
+
"#{last_arg_type} => #{rtn_type}"
|
5
5
|
end
|
6
6
|
end
|
7
7
|
class TestRubype < MiniTest::Unit::TestCase
|
@@ -13,66 +13,60 @@ class TestRubype < MiniTest::Unit::TestCase
|
|
13
13
|
@hash = { test: :hash }
|
14
14
|
end
|
15
15
|
|
16
|
-
def test_type_list
|
17
|
-
assert_equal_to_s "Numeric >= Numeric", Numeric >= Numeric
|
18
|
-
assert_equal_to_s "Numeric >= Array", Numeric >= Array
|
19
|
-
assert_equal_to_s "Array >= String", Array >= String
|
20
|
-
end
|
21
|
-
|
22
16
|
def test_correct_type
|
23
|
-
assert_correct_type [Numeric
|
24
|
-
assert_correct_type [Numeric
|
25
|
-
assert_correct_type [Numeric
|
26
|
-
assert_correct_type [Numeric
|
27
|
-
assert_correct_type [Numeric
|
28
|
-
assert_correct_type [Numeric
|
29
|
-
assert_correct_type [Numeric
|
30
|
-
|
31
|
-
assert_correct_type [Boolean, Numeric
|
32
|
-
assert_correct_type [Boolean, Array
|
33
|
-
assert_correct_type [Boolean, String
|
34
|
-
assert_correct_type [Boolean, Hash
|
35
|
-
assert_correct_type [Boolean, Symbol
|
17
|
+
assert_correct_type [Numeric => Numeric], [@numeric], @numeric
|
18
|
+
assert_correct_type [Numeric => Array ], [@numeric], @array
|
19
|
+
assert_correct_type [Numeric => String ], [@numeric], @string
|
20
|
+
assert_correct_type [Numeric => Hash ], [@numeric], @hash
|
21
|
+
assert_correct_type [Numeric => Symbol ], [@numeric], @symbol
|
22
|
+
assert_correct_type [Numeric => Boolean], [@numeric], true
|
23
|
+
assert_correct_type [Numeric => Boolean], [@numeric], false
|
24
|
+
|
25
|
+
assert_correct_type [Boolean, Numeric => Numeric], [true, @numeric], @numeric
|
26
|
+
assert_correct_type [Boolean, Array => Array ], [true, @array ], @array
|
27
|
+
assert_correct_type [Boolean, String => String ], [true, @string ], @string
|
28
|
+
assert_correct_type [Boolean, Hash => Hash ], [true, @hash ], @hash
|
29
|
+
assert_correct_type [Boolean, Symbol => Symbol ], [true, @symbol ], @symbol
|
36
30
|
end
|
37
31
|
|
38
32
|
def test_wrong_return_type
|
39
|
-
assert_wrong_rtn [Numeric
|
40
|
-
assert_wrong_rtn [Numeric
|
41
|
-
assert_wrong_rtn [Numeric
|
42
|
-
assert_wrong_rtn [Numeric
|
43
|
-
assert_wrong_rtn [Numeric
|
44
|
-
|
45
|
-
assert_wrong_rtn [Numeric, Numeric
|
46
|
-
assert_wrong_rtn [Numeric, Numeric
|
47
|
-
assert_wrong_rtn [Numeric, Numeric
|
48
|
-
assert_wrong_rtn [Numeric, Numeric
|
49
|
-
assert_wrong_rtn [Numeric, Numeric
|
33
|
+
assert_wrong_rtn [Numeric => Numeric], [@numeric], @array
|
34
|
+
assert_wrong_rtn [Numeric => Numeric], [@numeric], @string
|
35
|
+
assert_wrong_rtn [Numeric => Numeric], [@numeric], @hash
|
36
|
+
assert_wrong_rtn [Numeric => Numeric], [@numeric], @symbol
|
37
|
+
assert_wrong_rtn [Numeric => Numeric], [@numeric], true
|
38
|
+
|
39
|
+
assert_wrong_rtn [Numeric, Numeric => Numeric], [@numeric, @numeric], @array
|
40
|
+
assert_wrong_rtn [Numeric, Numeric => Numeric], [@numeric, @numeric], @string
|
41
|
+
assert_wrong_rtn [Numeric, Numeric => Numeric], [@numeric, @numeric], @hash
|
42
|
+
assert_wrong_rtn [Numeric, Numeric => Numeric], [@numeric, @numeric], @symbol
|
43
|
+
assert_wrong_rtn [Numeric, Numeric => Numeric], [@numeric, @numeric], true
|
50
44
|
end
|
51
45
|
|
52
46
|
def test_wrong_args_type
|
53
|
-
assert_wrong_arg [Numeric
|
54
|
-
assert_wrong_arg [Numeric
|
55
|
-
assert_wrong_arg [Numeric
|
56
|
-
assert_wrong_arg [Numeric
|
57
|
-
assert_wrong_arg [Numeric
|
58
|
-
|
59
|
-
assert_wrong_arg [Numeric, Numeric
|
60
|
-
assert_wrong_arg [Numeric, Numeric
|
61
|
-
assert_wrong_arg [Numeric, Numeric
|
62
|
-
assert_wrong_arg [Numeric, Numeric
|
63
|
-
assert_wrong_arg [Numeric, Numeric
|
47
|
+
assert_wrong_arg [Numeric => Numeric], [@array ], @numeric
|
48
|
+
assert_wrong_arg [Numeric => Numeric], [@string], @numeric
|
49
|
+
assert_wrong_arg [Numeric => Numeric], [@hash ], @numeric
|
50
|
+
assert_wrong_arg [Numeric => Numeric], [@symbol], @numeric
|
51
|
+
assert_wrong_arg [Numeric => Numeric], [true ], @numeric
|
52
|
+
|
53
|
+
assert_wrong_arg [Numeric, Numeric => Numeric], [@numeric, @array ], @numeric
|
54
|
+
assert_wrong_arg [Numeric, Numeric => Numeric], [@numeric, @string], @numeric
|
55
|
+
assert_wrong_arg [Numeric, Numeric => Numeric], [@numeric, @hash ], @numeric
|
56
|
+
assert_wrong_arg [Numeric, Numeric => Numeric], [@numeric, @symbol], @numeric
|
57
|
+
assert_wrong_arg [Numeric, Numeric => Numeric], [@numeric, true ], @numeric
|
64
58
|
end
|
65
59
|
|
66
60
|
def test_any
|
67
|
-
assert_correct_type [Any
|
68
|
-
assert_correct_type [Any
|
69
|
-
assert_correct_type [Any
|
70
|
-
assert_correct_type [Any
|
71
|
-
|
72
|
-
assert_correct_type [Any, Any
|
73
|
-
assert_correct_type [Any, Any
|
74
|
-
assert_correct_type [Any, Any
|
75
|
-
assert_correct_type [Any, Any
|
61
|
+
assert_correct_type [Any => Any], [@array ], @numeric
|
62
|
+
assert_correct_type [Any => Any], [@string], @numeric
|
63
|
+
assert_correct_type [Any => Any], [@hash ], @numeric
|
64
|
+
assert_correct_type [Any => Any], [@symbol], @numeric
|
65
|
+
|
66
|
+
assert_correct_type [Any, Any => Any], [@numeric, @array ], @numeric
|
67
|
+
assert_correct_type [Any, Any => Any], [@numeric, @string], @numeric
|
68
|
+
assert_correct_type [Any, Any => Any], [@numeric, @hash ], @numeric
|
69
|
+
assert_correct_type [Any, Any => Any], [@numeric, @symbol], @numeric
|
76
70
|
end
|
77
71
|
|
78
72
|
private
|
@@ -97,7 +91,7 @@ class TestRubype < MiniTest::Unit::TestCase
|
|
97
91
|
def call(#{arg_literal(args.count)})
|
98
92
|
#{obj_literal(val)}
|
99
93
|
end
|
100
|
-
|
94
|
+
typesig call: #{obj_literal(type_list)}
|
101
95
|
RUBY_CODE
|
102
96
|
|
103
97
|
klass.new
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubype
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- gogotanaka
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-12-
|
11
|
+
date: 2014-12-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -86,12 +86,11 @@ files:
|
|
86
86
|
- ext/rubype/rubype.c
|
87
87
|
- ext/rubype/rubype.h
|
88
88
|
- lib/rubype.rb
|
89
|
-
- lib/rubype/type_pair.rb
|
90
89
|
- lib/rubype/version.rb
|
91
90
|
- rubype.gemspec
|
92
91
|
- test/minitest_helper.rb
|
93
92
|
- test/test_rubype.rb
|
94
|
-
homepage: http://
|
93
|
+
homepage: http://gogotanaka.me/
|
95
94
|
licenses:
|
96
95
|
- MIT
|
97
96
|
metadata: {}
|