~singpolyma/dhall-ruby

ref: 43890aec4916d92fd9f40b523d1024c824ebb7ac dhall-ruby/test/test_readme.rb -rw-r--r-- 8.0 KiB View raw
43890aecStephen Paul Weber Typecheck while resolving 11 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
# frozen_string_literal: true

require "minitest/autorun"

require "dhall"

class TestReadme < Minitest::Test
	# Load, as_dhall, and resolver tests are in their own files

	FUNCTION = Dhall.load("\\(x: Natural) -> x + 1").sync
	CURRIED_FUNCTION = Dhall.load(
		"\\(x: Natural) -> \\(y: Natural) -> x + y"
	).sync
	BOOL = Dhall.load("True").sync
	NAT = Dhall.load("1").sync
	INT = Dhall.load("+1").sync
	DOUBLE = Dhall.load("1.0").sync
	TEXT = Dhall.load("\"abc\"").sync
	SOME = Dhall.load("Some 1").sync
	NONE = Dhall.load("None Natural").sync
	LIST = Dhall.load("[1,2]").sync
	REC = Dhall.load("{ a = 1 }").sync
	ENUM = Dhall.load("< one | two >.one").sync
	UNION = Dhall.load("< Natural: Natural | Text: Text >.Natural 1").sync

	def test_function_send_call
		assert_equal Dhall::Natural.new(value: 2), FUNCTION.call(1)
	end

	def test_function_send_staples
		assert_equal Dhall::Natural.new(value: 2), FUNCTION[1]
	end

	def test_function_use_as_proc
		assert_equal(
			[Dhall::Natural.new(value: 2), Dhall::Natural.new(value: 3)],
			[1, 2].map(&FUNCTION)
		)
	end

	def test_function_send_to_proc
		assert_kind_of Proc, FUNCTION.to_proc
		assert_equal Dhall::Natural.new(value: 2), FUNCTION.to_proc.call(1)
	end

	def test_curried_function_call_curried
		assert_equal(
			Dhall::Natural.new(value: 2),
			CURRIED_FUNCTION.call(1).call(1)
		)
	end

	def test_curried_function_call_uncurried
		assert_equal(
			Dhall::Natural.new(value: 2),
			CURRIED_FUNCTION.call(1, 1)
		)
	end

	def test_bool_and_false
		assert_equal false, BOOL & false
	end

	def test_bool_or_false
		assert_equal Dhall::Bool.new(value: true), BOOL | false
	end

	def test_not_bool
		assert_equal Dhall::Bool.new(value: false), !BOOL
	end

	def test_bool_match_true
		assert_equal true, BOOL === true
	end

	def test_bool_reduce_true_false
		assert_equal true, BOOL.reduce(true, false)
	end

	def test_bool_to_s
		assert_equal "True", BOOL.to_s
	end

	def test_nat_plus_1
		assert_equal Dhall::Natural.new(value: 2), NAT + 1
	end

	def test_1_plus_nat
		assert_equal Dhall::Natural.new(value: 2), 1 + NAT
	end

	def test_nat_times_2
		assert_equal Dhall::Natural.new(value: 2), NAT * 2
	end

	def test_2_times_nat
		assert_equal Dhall::Natural.new(value: 2), 2 * NAT
	end

	def test_nat_match_1
		assert_equal true, NAT === 1
	end

	def test_nat_zero
		assert_equal false, NAT.zero?
	end

	def test_nat_even
		assert_equal false, NAT.even?
	end

	def test_nat_odd
		assert_equal true, NAT.odd?
	end

	def test_nat_pred
		assert_equal Dhall::Natural.new(value: 0), NAT.pred
	end

	def test_nat_to_s
		assert_equal "1", NAT.to_s
	end

	def test_nat_to_i
		assert_equal 1, NAT.to_i
	end

	def test_int_match_1
		assert_equal true, INT === 1
	end

	def test_int_to_s
		assert_equal "+1", INT.to_s
	end

	def test_int_to_i
		assert_equal 1, INT.to_i
	end

	def test_double_match_one
		assert_equal true, DOUBLE === 1.0
	end

	def test_double_to_s
		assert_equal "1.0", DOUBLE.to_s
	end

	def test_double_to_f
		assert_equal 1.0, DOUBLE.to_f
	end

	def test_text_match_abc
		assert_equal true, TEXT === "abc"
	end

	def test_text_to_s
		assert_equal "abc", TEXT.to_s
	end

	def test_some_map
		assert_equal(
			Dhall::Optional.new(
				value:      Dhall::Natural.new(value: 2),
				value_type: nil
			),
			SOME.map { |x| x + 1 }
		)
	end

	def test_some_map_with_type
		assert_equal(
			Dhall::Optional.new(
				value:      Dhall::Natural.new(value: 2),
				value_type: Dhall::Builtins[:Natural]
			),
			SOME.map(type: Dhall::Builtins[:Natural]) { |x| x + 1 }
		)
	end

	def test_some_reduce
		assert_equal Dhall::Natural.new(value: 1), SOME.reduce(nil) { |x| x }
	end

	def test_some_to_s
		assert_equal "1", SOME.to_s
	end

	def test_none_map
		assert_equal(
			Dhall::OptionalNone.new(value_type: Dhall::Builtins[:Natural]),
			NONE.map { |x| x + 1 }
		)
	end

	def test_none_map_with_type
		assert_equal(
			Dhall::OptionalNone.new(value_type: Dhall::Builtins[:Natural]),
			NONE.map(type: Dhall::Builtins[:Natural]) { |x| x + 1 }
		)
	end

	def test_none_reduce
		assert_equal nil, NONE.reduce(nil) { |x| x }
	end

	def test_none_to_s
		assert_equal "", NONE.to_s
	end

	def test_list_map
		assert_equal(
			Dhall::List.new(elements: [
				Dhall::Natural.new(value: 2),
				Dhall::Natural.new(value: 3)
			], element_type: nil),
			LIST.map { |x| x + 1 }
		)
	end

	def test_list_map_with_type
		assert_equal(
			Dhall::List.new(elements: [
				Dhall::Natural.new(value: 2),
				Dhall::Natural.new(value: 3)
			], element_type: Dhall::Builtins[:Natural]),
			LIST.map(type: Dhall::Builtins[:Natural]) { |x| x + 1 }
		)
	end

	def test_list_reduce
		assert_equal(
			Dhall::Natural.new(value: 1),
			LIST.reduce(nil) { |x, _| x }
		)
	end

	def test_list_first
		assert_equal(
			Dhall::Optional.new(
				value:      Dhall::Natural.new(value: 1),
				value_type: Dhall::Builtins[:Natural]
			),
			LIST.first
		)
	end

	def test_list_last
		assert_equal(
			Dhall::Optional.new(
				value:      Dhall::Natural.new(value: 2),
				value_type: Dhall::Builtins[:Natural]
			),
			LIST.last
		)
	end

	def test_list_index_0
		assert_equal(
			Dhall::Optional.new(
				value:      Dhall::Natural.new(value: 1),
				value_type: Dhall::Builtins[:Natural]
			),
			LIST[0]
		)
	end

	def test_list_index_100
		assert_equal(
			Dhall::OptionalNone.new(value_type: Dhall::Builtins[:Natural]),
			LIST[100]
		)
	end

	def test_list_reverse
		assert_equal(
			Dhall::List.new(elements: [
				Dhall::Natural.new(value: 2),
				Dhall::Natural.new(value: 1)
			], element_type: Dhall::Builtins[:Natural]),
			LIST.reverse
		)
	end

	def test_list_join
		assert_equal "1,2", LIST.join(",")
	end

	def test_list_to_a
		assert_equal(
			[Dhall::Natural.new(value: 1), Dhall::Natural.new(value: 2)],
			LIST.to_a
		)
	end

	def test_rec_index_a_string
		assert_equal Dhall::Natural.new(value: 1), REC["a"]
	end

	def test_rec_index_a_symbol
		assert_equal Dhall::Natural.new(value: 1), REC[:a]
	end

	def test_rec_index_b_string
		assert_equal nil, REC["b"]
	end

	def test_rec_fetch_a_string
		assert_equal Dhall::Natural.new(value: 1), REC.fetch("a")
	end

	def test_rec_fetch_a_symbol
		assert_equal Dhall::Natural.new(value: 1), REC.fetch(:a)
	end

	def test_rec_fetch_b_string
		assert_raises KeyError do
			REC.fetch("b")
		end
	end

	def test_rec_dig_a
		assert_equal Dhall::Natural.new(value: 1), REC.dig(:a)
	end

	def test_rec_dig_b
		assert_equal nil, REC.dig(:b)
	end

	def test_rec_slice_a
		assert_equal(
			Dhall::Record.new(record: { "a" => Dhall::Natural.new(value: 1) }),
			REC.slice(:a)
		)
	end

	def test_rec_slice
		assert_equal Dhall::EmptyRecord.new, REC.slice
	end

	def test_rec_keys
		assert_equal ["a"], REC.keys
	end

	def test_rec_values
		assert_equal [Dhall::Natural.new(value: 1)], REC.values
	end

	def test_rec_map
		assert_equal(
			Dhall::Record.new(record: { "a" => Dhall::Natural.new(value: 2) }),
			REC.map { |k, v| [k, v + 1] }
		)
	end

	def test_rec_merge
		assert_equal(
			Dhall::Record.new(
				record: {
					"a" => Dhall::Natural.new(value: 1),
					"b" => Dhall::Natural.new(value: 2)
				}
			),
			REC.merge(b: 2)
		)
	end

	def test_rec_deep_merge
		assert_equal(
			Dhall::Record.new(
				record: {
					"a" => Dhall::Natural.new(value: 1),
					"b" => Dhall::Natural.new(value: 2)
				}
			),
			REC.deep_merge(b: 2)
		)
	end

	def test_enum_to_s
		assert_equal "one", ENUM.to_s
	end

	def test_enum_reduce
		assert_equal 1, ENUM.reduce(one: 1, two: 2)
	end

	def test_enum_extract
		assert_equal :one, ENUM.extract
	end

	def test_union_to_s
		assert_equal "1", UNION.to_s
	end

	def test_union_reduce
		assert_equal 1, UNION.reduce(Natural: :to_i, Text: :to_i)
	end

	def test_union_extract
		assert_equal Dhall::Natural.new(value: 1), UNION.extract
	end

	def test_coder_dump
		assert_equal "\x82\x0F\x01".b, Dhall::Coder.dump(1)
	end

	def test_coder_load
		assert_equal 1, Dhall::Coder.load("\x82\x0F\x01".b)
	end

	def test_coder_dump_object
		assert_raises ArgumentError do
			Dhall::Coder.dump(Object.new)
		end
	end

	def test_coder_roundtrip
		coder = Dhall::Coder.new(safe: Object)
		assert_equal Object, coder.load_async(coder.dump(Object.new)).sync.class
	end
end