eye-of-newt 0.0.8 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/eye_of_newt/parser.rb +132 -96
- data/lib/eye_of_newt/parser.y +18 -7
- data/lib/eye_of_newt/quantity.rb +13 -6
- data/lib/eye_of_newt/tokenizer.rb +2 -2
- data/lib/eye_of_newt/units.rb +1 -0
- data/lib/eye_of_newt/version.rb +1 -1
- data/test/examples.txt +3 -15
- data/test/eye_of_newt/quantity_test.rb +11 -0
- data/test/eye_of_newt/tokenizer_test.rb +10 -0
- data/test/eye_of_newt_test.rb +2 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a593b4f889ec78c7e789daba91965f94f9419810
|
4
|
+
data.tar.gz: f341eda16d664a480c92dbb5efbf65bfdf64873c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 00a299e06e879fdf92dc3ff06899494575fa77df6b549726a6bb4c4756b0680be936e765c2529da3a6f9d04a1566989002fc2df5a95106faf7683945a7b49ec5
|
7
|
+
data.tar.gz: 23bbf21df97753bbfe355adbf9a95c4177baa764ab1adf7e9c64fcb21f3620009c552de652eae07356e25eb7116750e3892b73ec78d36fb5002242546d5d6252
|
data/lib/eye_of_newt/parser.rb
CHANGED
@@ -37,105 +37,117 @@ module EyeOfNewt
|
|
37
37
|
##### State transition tables begin ###
|
38
38
|
|
39
39
|
racc_action_table = [
|
40
|
-
-
|
41
|
-
-
|
42
|
-
|
43
|
-
|
44
|
-
|
40
|
+
-45, 42, 18, 20, 11, 40, 19, 10, -45, 12,
|
41
|
+
-45, 13, 18, 11, -33, 33, 19, 23, 34, 24,
|
42
|
+
13, 18, 11, 46, 55, 19, 23, 28, 24, 29,
|
43
|
+
30, 37, 38, 29, 30, 44, 45, 29, 30, 36,
|
44
|
+
11, 30, 40, 40, 21, 56, 57, 20, 20, 60,
|
45
|
+
44, 61, 62, 30, 30, 65, 66 ]
|
45
46
|
|
46
47
|
racc_action_check = [
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
48
|
+
10, 14, 0, 0, 0, 13, 0, 0, 10, 0,
|
49
|
+
10, 0, 2, 5, 12, 5, 2, 2, 5, 2,
|
50
|
+
5, 17, 31, 21, 31, 17, 17, 3, 17, 3,
|
51
|
+
3, 8, 8, 22, 22, 20, 20, 25, 25, 7,
|
52
|
+
6, 26, 29, 30, 1, 32, 35, 37, 38, 39,
|
53
|
+
42, 44, 45, 47, 49, 53, 54 ]
|
52
54
|
|
53
55
|
racc_action_pointer = [
|
54
|
-
0,
|
55
|
-
nil, 0,
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
nil, nil, nil, nil, nil, nil
|
56
|
+
0, 44, 10, 19, nil, 9, 36, 33, 18, nil,
|
57
|
+
0, nil, 10, 0, -2, nil, nil, 19, nil, nil,
|
58
|
+
20, 23, 23, nil, nil, 27, 30, nil, nil, 37,
|
59
|
+
38, 18, 39, nil, nil, 40, nil, 44, 45, 37,
|
60
|
+
nil, nil, 35, nil, 48, 49, nil, 42, nil, 43,
|
61
|
+
nil, nil, nil, 43, 50, nil, nil, nil, nil, nil,
|
62
|
+
nil, nil, nil, nil, nil, nil, nil ]
|
60
63
|
|
61
64
|
racc_action_default = [
|
62
|
-
-
|
63
|
-
-
|
64
|
-
-
|
65
|
-
-
|
66
|
-
|
67
|
-
-
|
65
|
+
-51, -51, -51, -12, -13, -21, -51, -25, -26, -27,
|
66
|
+
-28, -29, -47, -51, -37, -39, -40, -42, -44, -46,
|
67
|
+
-48, -51, -8, -45, -47, -10, -11, -6, -30, -51,
|
68
|
+
-51, -19, -17, -20, -33, -23, -24, -51, -51, -51,
|
69
|
+
-43, -38, -51, -41, -51, -51, 67, -7, -2, -9,
|
70
|
+
-4, -5, -31, -51, -15, -18, -16, -22, -35, -36,
|
71
|
+
-34, -49, -50, -1, -3, -32, -14 ]
|
68
72
|
|
69
73
|
racc_goto_table = [
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
nil, nil, nil, nil, nil,
|
74
|
+
27, 8, 7, 5, 39, 26, 6, 32, 35, 43,
|
75
|
+
3, 31, 22, 25, 1, 9, 2, 41, nil, 48,
|
76
|
+
52, 53, 50, 51, 47, nil, nil, 49, nil, nil,
|
77
|
+
nil, nil, nil, 54, nil, nil, nil, nil, 58, 59,
|
78
|
+
nil, nil, nil, nil, 63, nil, 64 ]
|
74
79
|
|
75
80
|
racc_goto_check = [
|
76
|
-
5,
|
77
|
-
|
78
|
-
|
79
|
-
nil, nil, nil, nil, nil,
|
81
|
+
5, 11, 10, 8, 13, 4, 9, 10, 10, 7,
|
82
|
+
3, 9, 3, 6, 1, 12, 2, 15, nil, 5,
|
83
|
+
13, 13, 5, 5, 4, nil, nil, 4, nil, nil,
|
84
|
+
nil, nil, nil, 10, nil, nil, nil, nil, 11, 11,
|
85
|
+
nil, nil, nil, nil, 5, nil, 5 ]
|
80
86
|
|
81
87
|
racc_goto_pointer = [
|
82
|
-
nil,
|
83
|
-
1,
|
88
|
+
nil, 14, 16, 10, 2, -3, 10, -8, 3, 6,
|
89
|
+
2, 1, 15, -9, nil, 3, nil, nil ]
|
84
90
|
|
85
91
|
racc_goto_default = [
|
86
92
|
nil, nil, nil, nil, nil, nil, nil, 4, nil, nil,
|
87
|
-
nil, nil, nil, nil,
|
93
|
+
nil, nil, nil, nil, 14, 15, 16, 17 ]
|
88
94
|
|
89
95
|
racc_reduce_table = [
|
90
96
|
0, 0, :racc_error,
|
91
|
-
4,
|
92
|
-
3,
|
93
|
-
4,
|
94
|
-
3,
|
95
|
-
3,
|
96
|
-
2,
|
97
|
-
3,
|
98
|
-
2,
|
99
|
-
3,
|
100
|
-
2,
|
101
|
-
2,
|
102
|
-
1,
|
103
|
-
1,
|
104
|
-
4,
|
105
|
-
3,
|
106
|
-
3,
|
107
|
-
2,
|
108
|
-
3,
|
109
|
-
2,
|
110
|
-
2,
|
111
|
-
1,
|
112
|
-
3,
|
113
|
-
2,
|
114
|
-
2,
|
115
|
-
1,
|
116
|
-
1,
|
117
|
-
|
118
|
-
1,
|
119
|
-
1,
|
97
|
+
4, 18, :_reduce_none,
|
98
|
+
3, 18, :_reduce_none,
|
99
|
+
4, 18, :_reduce_none,
|
100
|
+
3, 18, :_reduce_none,
|
101
|
+
3, 18, :_reduce_none,
|
102
|
+
2, 18, :_reduce_none,
|
103
|
+
3, 18, :_reduce_none,
|
104
|
+
2, 18, :_reduce_none,
|
105
|
+
3, 18, :_reduce_none,
|
106
|
+
2, 18, :_reduce_none,
|
107
|
+
2, 18, :_reduce_none,
|
108
|
+
1, 18, :_reduce_none,
|
109
|
+
1, 20, :_reduce_13,
|
110
|
+
4, 19, :_reduce_none,
|
111
|
+
3, 19, :_reduce_none,
|
112
|
+
3, 19, :_reduce_none,
|
113
|
+
2, 19, :_reduce_none,
|
114
|
+
3, 19, :_reduce_none,
|
115
|
+
2, 19, :_reduce_none,
|
116
|
+
2, 19, :_reduce_none,
|
117
|
+
1, 19, :_reduce_none,
|
118
|
+
3, 19, :_reduce_none,
|
119
|
+
2, 19, :_reduce_none,
|
120
|
+
2, 19, :_reduce_none,
|
121
|
+
1, 19, :_reduce_none,
|
122
|
+
1, 25, :_reduce_26,
|
123
|
+
1, 25, :_reduce_27,
|
124
|
+
1, 25, :_reduce_28,
|
125
|
+
1, 27, :_reduce_29,
|
120
126
|
1, 23, :_reduce_30,
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
1,
|
127
|
+
2, 21, :_reduce_31,
|
128
|
+
3, 22, :_reduce_32,
|
129
|
+
1, 26, :_reduce_33,
|
130
|
+
3, 26, :_reduce_34,
|
131
|
+
3, 29, :_reduce_35,
|
132
|
+
3, 29, :_reduce_36,
|
133
|
+
1, 28, :_reduce_none,
|
134
|
+
2, 28, :_reduce_38,
|
135
|
+
1, 28, :_reduce_none,
|
136
|
+
1, 28, :_reduce_none,
|
137
|
+
2, 24, :_reduce_41,
|
138
|
+
1, 24, :_reduce_none,
|
128
139
|
1, 30, :_reduce_none,
|
129
|
-
1,
|
130
|
-
1,
|
131
|
-
1,
|
132
|
-
1,
|
133
|
-
|
134
|
-
3,
|
140
|
+
1, 34, :_reduce_none,
|
141
|
+
1, 34, :_reduce_none,
|
142
|
+
1, 34, :_reduce_none,
|
143
|
+
1, 34, :_reduce_none,
|
144
|
+
1, 31, :_reduce_48,
|
145
|
+
3, 32, :_reduce_49,
|
146
|
+
3, 33, :_reduce_50 ]
|
135
147
|
|
136
|
-
racc_reduce_n =
|
148
|
+
racc_reduce_n = 51
|
137
149
|
|
138
|
-
racc_shift_n =
|
150
|
+
racc_shift_n = 67
|
139
151
|
|
140
152
|
racc_token_table = {
|
141
153
|
false => 0,
|
@@ -151,10 +163,12 @@ racc_token_table = {
|
|
151
163
|
"," => 10,
|
152
164
|
"(" => 11,
|
153
165
|
")" => 12,
|
154
|
-
"
|
155
|
-
"
|
166
|
+
"-" => 13,
|
167
|
+
"\u{2013}" => 14,
|
168
|
+
"/" => 15,
|
169
|
+
"." => 16 }
|
156
170
|
|
157
|
-
racc_nt_base =
|
171
|
+
racc_nt_base = 17
|
158
172
|
|
159
173
|
racc_use_result_var = true
|
160
174
|
|
@@ -188,6 +202,8 @@ Racc_token_to_s_table = [
|
|
188
202
|
"\",\"",
|
189
203
|
"\"(\"",
|
190
204
|
"\")\"",
|
205
|
+
"\"-\"",
|
206
|
+
"\"\u{2013}\"",
|
191
207
|
"\"/\"",
|
192
208
|
"\".\"",
|
193
209
|
"$start",
|
@@ -201,10 +217,12 @@ Racc_token_to_s_table = [
|
|
201
217
|
"amount",
|
202
218
|
"unit_modifier",
|
203
219
|
"unit",
|
220
|
+
"numerical_amount",
|
221
|
+
"numerical_range",
|
222
|
+
"text",
|
204
223
|
"number",
|
205
224
|
"fraction",
|
206
225
|
"decimal",
|
207
|
-
"text",
|
208
226
|
"word" ]
|
209
227
|
|
210
228
|
Racc_debug_parser = false
|
@@ -272,76 +290,94 @@ def _reduce_26(val, _values, result)
|
|
272
290
|
end
|
273
291
|
|
274
292
|
def _reduce_27(val, _values, result)
|
275
|
-
@ingredient.amount =
|
293
|
+
@ingredient.amount = result
|
276
294
|
result
|
277
295
|
end
|
278
296
|
|
279
297
|
def _reduce_28(val, _values, result)
|
280
|
-
@ingredient.amount =
|
298
|
+
@ingredient.amount = 1
|
281
299
|
result
|
282
300
|
end
|
283
301
|
|
284
302
|
def _reduce_29(val, _values, result)
|
285
|
-
@ingredient.
|
303
|
+
@ingredient.unit = to_unit(result)
|
286
304
|
result
|
287
305
|
end
|
288
306
|
|
289
307
|
def _reduce_30(val, _values, result)
|
290
|
-
@ingredient.
|
308
|
+
@ingredient.unit = to_unit(result)
|
291
309
|
result
|
292
310
|
end
|
293
311
|
|
294
312
|
def _reduce_31(val, _values, result)
|
295
|
-
@ingredient.
|
313
|
+
@ingredient.style = val[1]
|
296
314
|
result
|
297
315
|
end
|
298
316
|
|
299
317
|
def _reduce_32(val, _values, result)
|
300
|
-
@ingredient.
|
318
|
+
@ingredient.note = val[1]
|
301
319
|
result
|
302
320
|
end
|
303
321
|
|
304
322
|
def _reduce_33(val, _values, result)
|
305
|
-
@ingredient.
|
323
|
+
@ingredient.unit_modifier = val[0]
|
306
324
|
result
|
307
325
|
end
|
308
326
|
|
309
327
|
def _reduce_34(val, _values, result)
|
310
|
-
|
328
|
+
@ingredient.unit_modifier = val[1]
|
311
329
|
result
|
312
330
|
end
|
313
331
|
|
314
|
-
|
332
|
+
def _reduce_35(val, _values, result)
|
333
|
+
result = [val[0], val[2]]
|
334
|
+
result
|
335
|
+
end
|
315
336
|
|
316
337
|
def _reduce_36(val, _values, result)
|
317
|
-
|
338
|
+
result = [val[0], val[2]]
|
318
339
|
result
|
319
340
|
end
|
320
341
|
|
321
342
|
# reduce 37 omitted
|
322
343
|
|
323
|
-
|
344
|
+
def _reduce_38(val, _values, result)
|
345
|
+
result = val[0] + val[1]
|
346
|
+
result
|
347
|
+
end
|
324
348
|
|
325
349
|
# reduce 39 omitted
|
326
350
|
|
327
351
|
# reduce 40 omitted
|
328
352
|
|
329
353
|
def _reduce_41(val, _values, result)
|
330
|
-
|
354
|
+
result = val.join(' ')
|
331
355
|
result
|
332
356
|
end
|
333
357
|
|
334
|
-
|
358
|
+
# reduce 42 omitted
|
359
|
+
|
360
|
+
# reduce 43 omitted
|
361
|
+
|
362
|
+
# reduce 44 omitted
|
363
|
+
|
364
|
+
# reduce 45 omitted
|
365
|
+
|
366
|
+
# reduce 46 omitted
|
367
|
+
|
368
|
+
# reduce 47 omitted
|
369
|
+
|
370
|
+
def _reduce_48(val, _values, result)
|
335
371
|
result = val[0].to_i
|
336
372
|
result
|
337
373
|
end
|
338
374
|
|
339
|
-
def
|
375
|
+
def _reduce_49(val, _values, result)
|
340
376
|
result = val[0].to_f / val[2].to_f
|
341
377
|
result
|
342
378
|
end
|
343
379
|
|
344
|
-
def
|
380
|
+
def _reduce_50(val, _values, result)
|
345
381
|
result = val.join.to_f
|
346
382
|
result
|
347
383
|
end
|
data/lib/eye_of_newt/parser.y
CHANGED
@@ -33,23 +33,34 @@ rule
|
|
33
33
|
| unit
|
34
34
|
;
|
35
35
|
amount
|
36
|
-
:
|
37
|
-
|
|
38
|
-
| fraction { @ingredient.amount = result }
|
39
|
-
| decimal { @ingredient.amount = result }
|
36
|
+
: numerical_amount { @ingredient.amount = result }
|
37
|
+
| numerical_range { @ingredient.amount = result }
|
40
38
|
| A { @ingredient.amount = 1 }
|
41
39
|
;
|
40
|
+
unit : UNIT { @ingredient.unit = to_unit(result) } ;
|
42
41
|
to_taste : TO_TASTE { @ingredient.unit = to_unit(result) } ;
|
43
42
|
style : ',' text { @ingredient.style = val[1] } ;
|
44
43
|
note : '(' text ')' { @ingredient.note = val[1] } ;
|
44
|
+
unit_modifier
|
45
|
+
: UNIT_MODIFIER { @ingredient.unit_modifier = val[0] }
|
46
|
+
| '(' text ')' { @ingredient.unit_modifier = val[1] }
|
47
|
+
;
|
48
|
+
numerical_range
|
49
|
+
: numerical_amount '-' numerical_amount { result = [val[0], val[2]] }
|
50
|
+
| numerical_amount '–' numerical_amount { result = [val[0], val[2]] }
|
51
|
+
;
|
52
|
+
numerical_amount
|
53
|
+
: number
|
54
|
+
| number fraction { result = val[0] + val[1] }
|
55
|
+
| fraction
|
56
|
+
| decimal
|
57
|
+
;
|
45
58
|
words
|
46
59
|
: word words { result = val.join(' ') }
|
47
60
|
| word
|
48
61
|
;
|
49
|
-
unit_modifier : UNIT_MODIFIER { @ingredient.unit_modifier = val[0] } ;
|
50
62
|
text : TEXT ;
|
51
|
-
word : WORD | A | OF ;
|
52
|
-
unit : UNIT { @ingredient.unit = to_unit(result) } ;
|
63
|
+
word : WORD | A | OF | UNIT_MODIFIER ;
|
53
64
|
number : NUMBER { result = val[0].to_i } ;
|
54
65
|
fraction : NUMBER '/' NUMBER { result = val[0].to_f / val[2].to_f } ;
|
55
66
|
decimal : NUMBER '.' NUMBER { result = val.join.to_f } ;
|
data/lib/eye_of_newt/quantity.rb
CHANGED
@@ -16,18 +16,25 @@ module EyeOfNewt
|
|
16
16
|
|
17
17
|
def in(new_unit)
|
18
18
|
rate = units.conversion_rate(unit, new_unit)
|
19
|
-
|
19
|
+
new_amount = range? ? amount.map{|a| a * rate} : amount * rate
|
20
|
+
self.class.new(new_amount, new_unit, units: units)
|
20
21
|
end
|
21
22
|
|
22
23
|
def to_s
|
23
|
-
|
24
|
+
amount_str = range? ? amount.map{|a| fraction_str(a)}.join('–') : fraction_str(amount)
|
25
|
+
[amount_str, modifier, unit_str].compact.join(' ')
|
24
26
|
end
|
25
27
|
alias :inspect :to_s
|
26
28
|
|
27
29
|
private
|
28
30
|
|
29
|
-
def
|
31
|
+
def range?
|
32
|
+
amount.is_a?(Array)
|
33
|
+
end
|
34
|
+
|
35
|
+
def fraction_str(a)
|
30
36
|
return nil if units.unquantified?(unit)
|
37
|
+
fraction = to_fraction(a)
|
31
38
|
whole = fraction.to_i
|
32
39
|
fractional = fraction - whole
|
33
40
|
[whole, fractional].reject(&:zero?).join(' ')
|
@@ -35,12 +42,12 @@ module EyeOfNewt
|
|
35
42
|
|
36
43
|
def unit_str
|
37
44
|
return nil if unit == units.default
|
38
|
-
singular =
|
45
|
+
singular = range? ? amount.last <= 1 : amount <= 1
|
39
46
|
singular ? unit.singularize : unit
|
40
47
|
end
|
41
48
|
|
42
|
-
def
|
43
|
-
|
49
|
+
def to_fraction(a)
|
50
|
+
signif(a, SIGNIFICANT_DIGITS).to_r.rationalize(DELTA)
|
44
51
|
end
|
45
52
|
|
46
53
|
def signif(value, digits)
|
@@ -6,12 +6,11 @@ module EyeOfNewt
|
|
6
6
|
|
7
7
|
WHITESPACE = /\s+/
|
8
8
|
ANYTHING = /[^()]+/
|
9
|
-
WORD =
|
9
|
+
WORD = /\w+(-\w+)*/
|
10
10
|
NUMBER = /\d+/
|
11
11
|
OF = /of/
|
12
12
|
A = /an?/
|
13
13
|
TO_TASTE = /to taste/
|
14
|
-
COMMA = ','
|
15
14
|
|
16
15
|
attr_reader :string, :units, :unit_modifiers
|
17
16
|
|
@@ -29,6 +28,7 @@ module EyeOfNewt
|
|
29
28
|
|
30
29
|
case
|
31
30
|
when @scan_text && text = @ss.scan(/#{ANYTHING}\b/)
|
31
|
+
@scan_text = false
|
32
32
|
[:TEXT, text]
|
33
33
|
when text = @ss.scan(NUMBER)
|
34
34
|
[:NUMBER, text]
|
data/lib/eye_of_newt/units.rb
CHANGED
data/lib/eye_of_newt/version.rb
CHANGED
data/test/examples.txt
CHANGED
@@ -24,18 +24,6 @@ an apple, sliced (I like Fuji, but any variety will do) | apple | 1 | units | sl
|
|
24
24
|
big pinch of salt | salt | 1 | pinches | | | big
|
25
25
|
heaping tablespoon of sugar | sugar | 1 | tbsp | | | heaping
|
26
26
|
1 large yellow onion | yellow onion | 1 | units | | | large
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
#3 tablespoons sunflower oil, ghee, or clarified butter
|
33
|
-
#1/2 cup crumbled feta, fresh ricotta, or fresh paneer
|
34
|
-
|
35
|
-
#1 (46 fluid ounce) can tomato juice' => ["1 (46 fluid ounce) can", "tomato juice"],
|
36
|
-
#1 (29 ounce) can tomato sauce' => ["1 (29 ounce) can", "tomato sauce"],
|
37
|
-
#1 (15 ounce) can kidney beans, drained and rinsed' => ["1 (29 ounce) can", "kidney beans", "drained and rinsed"],
|
38
|
-
#1 (15 ounce) can pinto beans, drained and rinsed' => ["1 (29 ounce) can", "pinto beans", "drained and rinsed"],
|
39
|
-
|
40
|
-
#1 1/2 cups chopped onion' => ["1 1/2 cups", "onion", "chopped"],
|
41
|
-
#1/4 cup chopped green bell pepper' => ["1/4 cup", "green bell pepper", "chopped"],
|
27
|
+
1 pound of large beans | large beans | 1 | lb
|
28
|
+
4-5 handfuls of spinach, very roughly chopped | spinach | 4-5 | handfuls | very roughly chopped
|
29
|
+
1 (46 oz) can tomato juice | tomato juice | 1 | cans | | | 46 oz
|
@@ -17,15 +17,26 @@ class EyeOfNewt::QuantityTest < ActiveSupport::TestCase
|
|
17
17
|
assert_equal "foo", foo.unit
|
18
18
|
end
|
19
19
|
|
20
|
+
test "#in returns a range in the new unit" do
|
21
|
+
bar = EyeOfNewt::Quantity.new([1, 2], "bar", units: units)
|
22
|
+
foo = bar.in("foo")
|
23
|
+
assert_equal [0.5, 1], foo.amount
|
24
|
+
assert_equal "foo", foo.unit
|
25
|
+
end
|
26
|
+
|
20
27
|
test "#to_s makes appropriate strings" do
|
21
28
|
assert_equal "1", EyeOfNewt::Quantity.new(1, "unit").to_s
|
22
29
|
assert_equal "1/2", EyeOfNewt::Quantity.new(0.5, "units").to_s
|
23
30
|
assert_equal "1/2 tbsp", EyeOfNewt::Quantity.new(0.5, "tbs").to_s
|
31
|
+
assert_equal "2/3 tbsp", EyeOfNewt::Quantity.new(0.66, "tbs").to_s
|
24
32
|
assert_equal "1 1/2 tbsp", EyeOfNewt::Quantity.new(1.5, "tbs").to_s
|
25
33
|
assert_equal "1000 g", EyeOfNewt::Quantity.new(1001, "grams").to_s
|
34
|
+
assert_equal "100 g", EyeOfNewt::Quantity.new(100.1, "grams").to_s
|
35
|
+
assert_equal "50 1/2 g", EyeOfNewt::Quantity.new(50.5, "grams").to_s
|
26
36
|
assert_equal "1/3 g", EyeOfNewt::Quantity.new(0.33, "grams").to_s
|
27
37
|
assert_equal "to taste", EyeOfNewt::Quantity.new(1, "to taste").to_s
|
28
38
|
assert_equal "1 heaping tsp", EyeOfNewt::Quantity.new(1, "teaspoon", modifier: "heaping").to_s
|
39
|
+
assert_equal "1–2 handfuls", EyeOfNewt::Quantity.new([1, 2], "handfuls").to_s
|
29
40
|
end
|
30
41
|
|
31
42
|
end
|
@@ -24,6 +24,16 @@ class EyeOfNewt::TokenizerTest < ActiveSupport::TestCase
|
|
24
24
|
assert_nil t.next_token
|
25
25
|
end
|
26
26
|
|
27
|
+
test "tokenizes TEXT between number and unit" do
|
28
|
+
t = tok("1 (46 oz) can", ["can"])
|
29
|
+
assert_equal [:NUMBER, "1"], t.next_token
|
30
|
+
assert_equal ['(', "("], t.next_token
|
31
|
+
assert_equal [:TEXT, "46 oz"], t.next_token
|
32
|
+
assert_equal [')', ')'], t.next_token
|
33
|
+
assert_equal [:UNIT, 'can'], t.next_token
|
34
|
+
assert_nil t.next_token
|
35
|
+
end
|
36
|
+
|
27
37
|
test "tokenizes TEXT after a comma, with parens" do
|
28
38
|
t = tok(", sliced (this is a note)")
|
29
39
|
assert_equal [',', ","], t.next_token
|
data/test/eye_of_newt_test.rb
CHANGED
@@ -12,7 +12,8 @@ class EyeOfNewtTest < ActiveSupport::TestCase
|
|
12
12
|
tokens = line.split('|').map(&:strip)
|
13
13
|
line = tokens.shift
|
14
14
|
name = tokens.shift
|
15
|
-
amount = tokens.shift.to_f
|
15
|
+
amount = tokens.shift.split('-').map(&:to_f)
|
16
|
+
amount = amount.first unless amount.many?
|
16
17
|
unit = tokens.shift.presence
|
17
18
|
style = tokens.shift.presence
|
18
19
|
note = tokens.shift.presence
|