objective-ruby 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1 +1,8 @@
1
1
  require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs.push "lib"
6
+ t.test_files = FileList['spec/*_spec.rb']
7
+ t.verbose = true
8
+ end
@@ -1,3 +1,3 @@
1
1
  module ObjectiveRuby
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -2,45 +2,106 @@ require "objective-ruby/version"
2
2
 
3
3
  module ObjectiveRuby
4
4
  class << self
5
- def included(base)
6
- base.extend ClassMethods
7
- end
5
+ def included(base)
6
+ base.extend ClassMethods
8
7
  end
8
+ end
9
9
 
10
10
  module ClassMethods
11
- def obj method, input = {}, &block
12
- define_method(method) do |method_input = {}|
13
- block_input = {}
14
- method_input.each do |k,v|
15
- new_key = k.to_s.sub!(/(with_|and_)/, '').to_sym
16
- if input[new_key].nil?
17
- raise ArgumentError.new "invalid input field #{k}"
18
- end
19
- unless v.is_a? input[new_key]
20
- raise ArgumentError.new "invalid type for #{new_key} expected #{input[new_key]}"
21
- end
22
- block_input[new_key] = v
11
+ def objr method, input = {}, &block
12
+ define_method(method) do |method_input = {}|
13
+ block_input = {}
14
+ method_input.each do |k,v|
15
+ new_key = k.to_s.sub!(/(with_|and_)/, '').to_sym
16
+
17
+ if input[new_key].nil?
18
+ raise ArgumentError.new "invalid input field #{new_key}"
19
+ end
20
+
21
+ unless v.is_a? input[new_key]
22
+ raise ArgumentError.new "invalid type for #{new_key} expected #{input[new_key]}"
23
+ end
24
+
25
+ block_input[new_key] = v
26
+ end
27
+
28
+ input.each do |k,v|
29
+ if block_input[k].nil?
30
+ raise ArgumentError.new "missing input field #{k}"
31
+ end
32
+ end
33
+
34
+ block.call block_input
35
+ end
36
+ end
37
+
38
+ def objr_d method, input = {}, &block
39
+ define_method(method) do |method_input = {}|
40
+ block_input = {}
41
+ method_input.each do |k,v|
42
+ new_key = k.to_s.sub!(/(with_|and_)/, '').to_sym
43
+
44
+ if input.index(new_key).nil?
45
+ raise ArgumentError.new "invalid input field #{new_key}"
46
+ end
47
+
48
+ block_input[new_key] = v
49
+ end
50
+
51
+ input.each do |k,v|
52
+ if block_input[k].nil?
53
+ raise ArgumentError.new "missing input field #{k}"
23
54
  end
24
-
25
- block.call block_input
26
55
  end
56
+
57
+ block.call block_input
58
+ end
27
59
  end
60
+
61
+ def objc method, input = {}, &block
62
+ define_method(method) do |method_input = {}|
63
+ block_input = {}
64
+ method_input.each do |k,v|
65
+ if input[k].nil?
66
+ raise ArgumentError.new "invalid input field #{k}"
67
+ end
68
+
69
+ unless v.is_a? input[k][1]
70
+ raise ArgumentError.new "invalid type for #{k} expected #{input[k][1]}"
71
+ end
72
+
73
+ block_input[input[k][0]] = v
74
+ end
28
75
 
29
- def objr method, input = {}, &block
30
- define_method(method) do |method_input = {}|
31
- block_input = {}
32
- method_input.each do |k,v|
33
- if input[k].nil?
34
- raise ArgumentError.new "invalid input field #{k}"
35
- end
36
- unless v.is_a? input[k][1]
37
- raise ArgumentError.new "invalid type for #{new_key} expected #{input[new_key]}"
38
- end
39
- block_input[input[k][0]] = v
76
+ input.each do |k,v|
77
+ if block_input[v[0]].nil?
78
+ raise ArgumentError.new "missing input field #{k}"
79
+ end
80
+ end
81
+
82
+ block.call block_input
83
+ end
84
+ end
85
+
86
+ def objc_d method, input = {}, &block
87
+ define_method(method) do |method_input = {}|
88
+ block_input = {}
89
+ method_input.each do |k,v|
90
+ if input[k].nil?
91
+ raise ArgumentError.new "invalid input field #{k}"
92
+ end
93
+
94
+ block_input[input[k]] = v
95
+ end
96
+
97
+ input.each do |k,v|
98
+ if block_input[v].nil?
99
+ raise ArgumentError.new "missing input field #{k}"
40
100
  end
41
-
42
- block.call block_input
43
101
  end
102
+
103
+ block.call block_input
104
+ end
44
105
  end
45
106
  end
46
107
  end
@@ -0,0 +1,148 @@
1
+ require 'minitest/spec'
2
+ require 'minitest/autorun'
3
+ require 'date'
4
+
5
+ require_relative '../lib/objective-ruby'
6
+
7
+ class Test
8
+ include ObjectiveRuby
9
+
10
+ objr :objr_method, foo: String, bar: Fixnum do |input|
11
+ input
12
+ end
13
+
14
+ objr_d :objr_d_method, [:foo, :bar] do |input|
15
+ input
16
+ end
17
+
18
+ objc :objc_method, with_foo: [:foo, String], also_bar: [:bar, Fixnum] do |input|
19
+ input
20
+ end
21
+
22
+ objc_d :objc_d_method, with_foo: :foo, also_bar: :bar do |input|
23
+ input
24
+ end
25
+ end
26
+
27
+ describe ObjectiveRuby do
28
+
29
+ before do
30
+ @test = Test.new
31
+ end
32
+
33
+ describe 'self.objr' do
34
+ it 'should do the right thing with correct input' do
35
+ result = @test.objr_method with_foo: 'hello', and_bar: 12
36
+ result.must_equal foo: 'hello', bar: 12
37
+ end
38
+
39
+ it 'should allow for interchangable prefixes' do
40
+ results = Array.new
41
+ results << (@test.objr_method with_foo: 'hello', and_bar: 12)
42
+ results << (@test.objr_method and_foo: 'hello', with_bar: 12)
43
+ results << (@test.objr_method with_foo: 'hello', with_bar: 12)
44
+ results << (@test.objr_method and_foo: 'hello', and_bar: 12)
45
+ results.each { |result| result.must_equal foo: 'hello', bar: 12 }
46
+ end
47
+
48
+ it 'should raise an error for invalid types' do
49
+ error = ( -> { @test.objr_method(with_foo: 1, and_bar: 12) }.must_raise ArgumentError )
50
+ error.message.must_equal 'invalid type for foo expected String'
51
+
52
+ error = ( -> { @test.objr_method(with_foo: 'hello', and_bar: nil) }.must_raise ArgumentError )
53
+ error.message.must_equal 'invalid type for bar expected Fixnum'
54
+ end
55
+
56
+ it 'should raise an error for missing parameters' do
57
+ error = ( -> { @test.objr_method(with_foo: 'hello') }.must_raise ArgumentError )
58
+ error.message.must_equal 'missing input field bar'
59
+ end
60
+
61
+ it 'should raise an error for extra parameters' do
62
+ error = ( -> { @test.objr_method(with_foo: 'hello', and_bar: 12, and_extra: 'this is extra!') }.must_raise ArgumentError )
63
+ error.message.must_equal 'invalid input field extra'
64
+ end
65
+ end
66
+
67
+ describe 'self.objr_d' do
68
+ it 'should do the right thing with correct input' do
69
+ result = @test.objr_d_method with_foo: 'hello', and_bar: 12
70
+ result.must_equal foo: 'hello', bar: 12
71
+ end
72
+
73
+ it 'should allow for duck typing' do
74
+ now = DateTime.now
75
+ result = @test.objr_d_method with_foo: now , and_bar: 'something'
76
+ result.must_equal foo: now, bar: 'something'
77
+ end
78
+
79
+ it 'should allow for interchangable prefixes' do
80
+ results = Array.new
81
+ results << (@test.objr_d_method with_foo: 'hello', and_bar: 12)
82
+ results << (@test.objr_d_method and_foo: 'hello', with_bar: 12)
83
+ results << (@test.objr_d_method with_foo: 'hello', with_bar: 12)
84
+ results << (@test.objr_d_method and_foo: 'hello', and_bar: 12)
85
+ results.each { |result| result.must_equal foo: 'hello', bar: 12 }
86
+ end
87
+
88
+ it 'should raise an error for missing parameters' do
89
+ error = ( -> { @test.objr_d_method(with_foo: 'hello') }.must_raise ArgumentError )
90
+ error.message.must_equal 'missing input field bar'
91
+ end
92
+
93
+ it 'should raise an error for extra parameters' do
94
+ error = ( -> { @test.objr_d_method(with_foo: 'hello', and_bar: 12, and_extra: 'this is extra!') }.must_raise ArgumentError )
95
+ error.message.must_equal 'invalid input field extra'
96
+ end
97
+ end
98
+
99
+ describe 'self.objc' do
100
+ it 'should do the right thing with correct input' do
101
+ result = @test.objc_method with_foo: 'hello', also_bar: 12
102
+ result.must_equal foo: 'hello', bar: 12
103
+ end
104
+
105
+ it 'should raise an error for missing parameters' do
106
+ error = ( -> { @test.objc_method(with_foo: 'hello') }.must_raise ArgumentError )
107
+ error.message.must_equal 'missing input field also_bar'
108
+ end
109
+
110
+ it 'should raise an error for extra parameters' do
111
+ error = ( -> { @test.objc_method(with_foo: 'hello', also_bar: 12, and_extra: 'this is extra!') }.must_raise ArgumentError )
112
+ error.message.must_equal 'invalid input field and_extra'
113
+ end
114
+
115
+ it 'should raise an error for invalid types' do
116
+ error = ( -> { @test.objc_method(with_foo: 1, also_bar: 12) }.must_raise ArgumentError )
117
+ error.message.must_equal 'invalid type for with_foo expected String'
118
+
119
+ error = ( -> { @test.objc_method(with_foo: 'hello', also_bar: nil) }.must_raise ArgumentError )
120
+ error.message.must_equal 'invalid type for also_bar expected Fixnum'
121
+ end
122
+ end
123
+
124
+ describe 'self.objc_d' do
125
+ it 'should do the right thing with correct input' do
126
+ result = @test.objc_d_method with_foo: 'hello', also_bar: 12
127
+ result.must_equal foo: 'hello', bar: 12
128
+ end
129
+
130
+ it 'should allow for duck typing' do
131
+ now = DateTime.now
132
+ result = @test.objc_d_method with_foo: now , also_bar: 'something'
133
+ result.must_equal foo: now, bar: 'something'
134
+ end
135
+
136
+ it 'should raise an error for missing parameters' do
137
+ error = ( -> { @test.objc_d_method(with_foo: 'hello') }.must_raise ArgumentError )
138
+ error.message.must_equal 'missing input field also_bar'
139
+ end
140
+
141
+ it 'should raise an error for extra parameters' do
142
+ error = ( -> { @test.objc_d_method(with_foo: 'hello', also_bar: 12, and_extra: 'this is extra!') }.must_raise ArgumentError )
143
+ error.message.must_equal 'invalid input field and_extra'
144
+ end
145
+
146
+ end
147
+
148
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: objective-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -26,6 +26,7 @@ files:
26
26
  - lib/objective-ruby.rb
27
27
  - lib/objective-ruby/version.rb
28
28
  - objective-ruby.gemspec
29
+ - spec/objective-ruby_spec.rb
29
30
  homepage: ''
30
31
  licenses: []
31
32
  post_install_message:
@@ -50,5 +51,6 @@ rubygems_version: 1.8.17
50
51
  signing_key:
51
52
  specification_version: 3
52
53
  summary: Objective-C style Ruby.
53
- test_files: []
54
+ test_files:
55
+ - spec/objective-ruby_spec.rb
54
56
  has_rdoc: