Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Idea for some syntactic sugar #19

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,21 @@ def sum(x, y)
end
typesig :sum, [Numeric, Numeric] => String

# This type definition can also be written like this:

require 'rubype/syntactic_sugar'
[Numeric, Numeric, String] > def sum(x, y)
(x + y).to_s
end


# Assert first arg has method #to_i
def sum(x, y)
x.to_i + y
end
typesig :sum, [:to_i, Numeric] => Numeric
```


This gem brings you advantage of type without changing existing code's behavior.

## Good point:
Expand Down
20 changes: 20 additions & 0 deletions lib/rubype/syntactic_sugar.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require 'binding_of_caller'

class Array
def >(*args)
args_match = args.length == 1 && args.first.kind_of?(Symbol)
self_matches = length > 0 && all? { |e| e.kind_of?(Class) }

if args_match && self_matches
return_type = self.last
call_string = "typesig :#{args.first}, [#{self[0...-1].map(&:name).join(', ')}] => #{return_type}"
binding.of_caller(1).eval(call_string)
else
if !args_match
raise "You must pass a Symbol as the only argument to a type definition!"
else
raise "Wrong type definition #{self}"
end
end
end
end
2 changes: 2 additions & 0 deletions rubype.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ Gem::Specification.new do |spec|

spec.required_ruby_version = ">= 2.0.0"

spec.add_runtime_dependency "binding_of_caller", "~> 0.7.2"

spec.add_development_dependency "bundler", "~> 1.3"
spec.add_development_dependency "rake"
spec.add_development_dependency "minitest"
Expand Down
1 change: 1 addition & 0 deletions test/minitest_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
require 'rubype'
require 'rubype/syntactic_sugar'
require 'pry'
require 'minitest/autorun'
33 changes: 24 additions & 9 deletions test/test_rubype.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,17 @@ def test_mth(n1, n2)
end
typesig :test_mth, [Numeric, Numeric] => String
RUBY_CODE
Object.const_set('MyClass', klass)

meth = klass.new.method(:test_mth)
assert_equal meth.type_info, { [Numeric, Numeric] => String }
assert_equal meth.arg_types, [Numeric, Numeric]
assert_equal meth.return_type, String
assert_correct_checks_on_class(klass)
end

err = assert_raises(Rubype::ReturnTypeError) { meth.(1,2) }
assert_equal err.message, %|Expected MyClass#test_mth to return String but got nil instead|
def test_type_info_syntactic_sugar
klass = Class.new.class_eval <<-RUBY_CODE
[Numeric, Numeric, String] > def test_mth(n1, n2)
end
RUBY_CODE

err = assert_raises(Rubype::ArgumentTypeError) { meth.(1,'2') }
assert_equal err.message, %|Expected MyClass#test_mth's 2nd argument to be Numeric but got "2" instead|
assert_correct_checks_on_class(klass)
end

private
Expand All @@ -126,6 +125,22 @@ def assert_wrong_rtn(type_list, args, val)
assert_raises(Rubype::ReturnTypeError) { define_test_method(type_list, args, val).call(*args) }
end

def assert_correct_checks_on_class(klass)
Object.send(:remove_const, 'MyClass') if defined? MyClass
Object.const_set('MyClass', klass)

meth = klass.new.method(:test_mth)
assert_equal meth.type_info, { [Numeric, Numeric] => String }
assert_equal meth.arg_types, [Numeric, Numeric]
assert_equal meth.return_type, String

err = assert_raises(Rubype::ReturnTypeError) { meth.(1,2) }
assert_equal err.message, %|Expected MyClass#test_mth to return String but got nil instead|

err = assert_raises(Rubype::ArgumentTypeError) { meth.(1,'2') }
assert_equal err.message, %|Expected MyClass#test_mth's 2nd argument to be Numeric but got "2" instead|
end

def define_test_method(type_list, args, val)
klass = Class.new.class_eval <<-RUBY_CODE
def call(#{arg_literal(args.count)})
Expand Down