iba 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +5 -5
  2. data/lib/iba.rb +80 -45
  3. data/test/analyse_test.rb +21 -0
  4. metadata +4 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: ef434931c94289fcb2c724e2e50e31a169456682
4
- data.tar.gz: 9db3968b45d90cfb24c689852e70a71ee6aae61e
2
+ SHA256:
3
+ metadata.gz: aa34cf98d021bff67ebed44e9b62e2445c34793422660c0e221b06ca641c722d
4
+ data.tar.gz: c23f48c6e815351c9181efb572c98e0849e87d3c35a8b922fad6581e87090157
5
5
  SHA512:
6
- metadata.gz: d2639e24f72a837b9479a1444e0aa217f67d7d3e37392452c8faf13599df585101c10f80895528abfd8c6c02c1fefec3f2f81711b9f07a269705890366bd07fd
7
- data.tar.gz: 7be55792993087cd78d88e848158b60322705c9aeae157d8a76e091ed8256546ce6f1ba34ce7c79306bfccefda8588a7d83f69a78339332b905ca3e124b70664
6
+ metadata.gz: 992248f06af84ad50da540697617ce9980acc400adbf8fe1df08965d54345bd335a98c8071ec8e32920fb1454ae0820dbe74b7b32322125b8769a3d611c93b82
7
+ data.tar.gz: 4ae83722fa833dd949a6b0ec254f395afbe2bed6fcd26d92cb919932f2c5f8bb101dcce3bcf9082df188a5019e97ceb7348dd4f956bc9e36076983e6970b43e6
data/lib/iba.rb CHANGED
@@ -35,37 +35,59 @@ module Iba
35
35
  nil
36
36
  else
37
37
  str = expr._to_s
38
- "#{str} is #{eval(str, @block.binding).inspect}"
38
+ value = expr._evaluate(@block.binding).inspect
39
+ "#{str} is #{value}"
39
40
  end
40
41
  end
41
42
  end
42
43
 
43
- class EmptyExpression
44
+ class BaseExpression
44
45
  def method_missing method, *args
45
46
  super if method.to_s =~ /^_/
46
47
  MethodCallExpression.new self, method, args
47
48
  end
48
49
 
49
- def respond_to_missing? _method
50
+ def respond_to_missing? method
51
+ return false if method.to_s =~ /^_/
50
52
  true
51
53
  end
52
54
 
55
+ def to_s
56
+ method_missing :to_s
57
+ end
58
+
59
+ def == other
60
+ method_missing :==, other
61
+ end
62
+
63
+ def _wrap arg
64
+ if arg.is_a? BaseExpression
65
+ arg
66
+ else
67
+ LiteralExpression.new arg
68
+ end
69
+ end
70
+
71
+ def coerce other
72
+ [_wrap(other), self]
73
+ end
74
+ end
75
+
76
+ class EmptyExpression < BaseExpression
53
77
  def _parse &blk
54
- b = blk.binding
78
+ bnd = blk.binding
55
79
 
56
- vars = eval 'local_variables', b
57
- ivars = eval 'instance_variables', b
80
+ vars = bnd.local_variables
81
+ ivars = bnd.receiver.instance_variables
58
82
 
59
83
  _override_instance_variables ivars
60
84
 
61
- _override_local_variables vars, b
85
+ _override_local_variables vars, bnd
62
86
 
63
87
  result = instance_eval(&blk)
64
- unless result.class == MethodCallExpression
65
- result = LiteralExpression.new(result)
66
- end
88
+ result = _wrap(result)
67
89
 
68
- _restore_local_variables vars, b
90
+ _restore_local_variables vars, bnd
69
91
 
70
92
  result
71
93
  end
@@ -73,22 +95,22 @@ module Iba
73
95
  def _override_instance_variables vars
74
96
  vars.each do |v|
75
97
  next if v =~ /^@_/
76
- eval "#{v} = Iba::MethodCallExpression.new(Iba::EmptyExpression.new, :#{v}, [])"
98
+ instance_variable_set v, Iba::InstanceVariableExpression.new(v.to_sym)
77
99
  end
78
100
  end
79
101
 
80
- def _override_local_variables vars, b
102
+ def _override_local_variables vars, bnd
81
103
  vars.each do |v|
82
104
  next if v =~ /^_/
83
- eval "_#{v} = #{v}", b
84
- eval "#{v} = Iba::MethodCallExpression.new(Iba::EmptyExpression.new, :#{v}, [])", b
105
+ bnd.local_variable_set "_#{v}", bnd.local_variable_get(v)
106
+ bnd.local_variable_set v, LocalVariableExpression.new(v.to_sym)
85
107
  end
86
108
  end
87
109
 
88
- def _restore_local_variables vars, b
110
+ def _restore_local_variables vars, bnd
89
111
  vars.each do |v|
90
112
  next if v =~ /^_/
91
- eval "#{v} = _#{v}", b
113
+ bnd.local_variable_set v, bnd.local_variable_get("_#{v}")
92
114
  end
93
115
  end
94
116
 
@@ -97,7 +119,7 @@ module Iba
97
119
  end
98
120
  end
99
121
 
100
- class LiteralExpression
122
+ class LiteralExpression < BaseExpression
101
123
  def initialize val
102
124
  @value = val
103
125
  end
@@ -105,9 +127,41 @@ module Iba
105
127
  def _to_s
106
128
  @value.inspect
107
129
  end
130
+
131
+ def _evaluate _bnd
132
+ @value
133
+ end
134
+ end
135
+
136
+ class InstanceVariableExpression < BaseExpression
137
+ def initialize ivar_name
138
+ @_ivar_name = ivar_name
139
+ end
140
+
141
+ def _to_s
142
+ @_ivar_name.to_s
143
+ end
144
+
145
+ def _evaluate bnd
146
+ bnd.receiver.instance_variable_get @_ivar_name
147
+ end
148
+ end
149
+
150
+ class LocalVariableExpression < BaseExpression
151
+ def initialize lvar_name
152
+ @_lvar_name = lvar_name
153
+ end
154
+
155
+ def _to_s
156
+ @_lvar_name.to_s
157
+ end
158
+
159
+ def _evaluate bnd
160
+ bnd.local_variable_get @_lvar_name
161
+ end
108
162
  end
109
163
 
110
- class MethodCallExpression
164
+ class MethodCallExpression < BaseExpression
111
165
  attr_reader :_method, :_reciever, :_args
112
166
 
113
167
  def initialize reciever, methodname, args
@@ -122,7 +176,7 @@ module Iba
122
176
 
123
177
  if @_method == :[]
124
178
  "#{rcv}[#{args[0]}]"
125
- elsif method_is_operator?
179
+ elsif _method_is_operator?
126
180
  case @_args.length
127
181
  when 0
128
182
  "#{@_method.to_s.sub(/@$/, '')}#{rcv}"
@@ -139,35 +193,16 @@ module Iba
139
193
  end
140
194
  end
141
195
 
142
- def method_missing method, *args
143
- super if method.to_s =~ /^_/
144
- MethodCallExpression.new self, method, args
145
- end
146
-
147
- def respond_to_missing? _method
148
- true
149
- end
150
-
151
- def method_is_operator?
152
- @_method.to_s !~ /^[a-z]/
153
- end
154
-
155
- def to_s
156
- method_missing :to_s
157
- end
158
-
159
- def == other
160
- method_missing :==, other
196
+ def _evaluate bnd
197
+ rcv = @_reciever._evaluate(bnd)
198
+ args = @_args.map { |arg| arg._evaluate(bnd) }
199
+ rcv.send @_method, *args
161
200
  end
162
201
 
163
202
  private
164
203
 
165
- def _wrap arg
166
- if arg.class == MethodCallExpression
167
- arg
168
- else
169
- LiteralExpression.new arg
170
- end
204
+ def _method_is_operator?
205
+ @_method.to_s !~ /^[a-z]/
171
206
  end
172
207
  end
173
208
 
@@ -54,4 +54,25 @@ class AnalyseTest < Test::Unit::TestCase
54
54
  @foo = 23
55
55
  assert_equal '@foo is 23', combinator { @foo }.analyse
56
56
  end
57
+
58
+ def test_instance_variable_method_call
59
+ @foo = 23
60
+ assert_equal "(@foo == 23) is true\n@foo is 23", combinator { @foo == 23 }.analyse
61
+ end
62
+
63
+ def test_instance_variable_as_argument_of_operator
64
+ @foo = 23
65
+ assert_equal '(23 + @foo) is 46', combinator { 23 + @foo }.analyse
66
+ end
67
+
68
+ def test_complex_subexpressions
69
+ @foo = 23
70
+ bar = 42
71
+ baz = [42]
72
+ result = combinator { (@foo + baz.first) == (bar + 23) }.analyse
73
+ assert_equal \
74
+ "((@foo + baz.first) == (bar + 23)) is true\n" \
75
+ '(@foo + baz.first) is 65, (bar + 23) is 65',
76
+ result
77
+ end
57
78
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: iba
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matijs van Zuijlen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-12-15 00:00:00.000000000 Z
11
+ date: 2018-03-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -73,7 +73,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
73
73
  requirements:
74
74
  - - ">="
75
75
  - !ruby/object:Gem::Version
76
- version: 2.1.0
76
+ version: 2.2.0
77
77
  required_rubygems_version: !ruby/object:Gem::Requirement
78
78
  requirements:
79
79
  - - ">="
@@ -81,7 +81,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
81
81
  version: '0'
82
82
  requirements: []
83
83
  rubyforge_project:
84
- rubygems_version: 2.6.13
84
+ rubygems_version: 2.7.6
85
85
  signing_key:
86
86
  specification_version: 4
87
87
  summary: Introspective Block Assertions