~singpolyma/dhall-ruby

ref: 2a3d4e2a5a2024f679162666427676293c1b851a dhall-ruby/test/test_as_dhall.rb -rw-r--r-- 5.6 KiB
2a3d4e2aStephen Paul Weber Hoist single-label enum into array 4 years 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
# frozen_string_literal: true

require "minitest/autorun"

require "dhall"

class TestAsDhall < Minitest::Test
	using Dhall::AsDhall

	def test_string
		assert_equal Dhall::Text.new(value: "hai"), "hai".as_dhall
	end

	def test_string_encoding
		assert_equal(
			Dhall::Text.new(value: "hai"),
			"hai".encode("UTF-16BE").as_dhall
		)
	end

	def test_string_binary
		assert_equal(
			Dhall::List.new(elements: [Dhall::Natural.new(value: 0xff)]),
			"\xff".b.as_dhall
		)
	end

	def test_symbol
		assert_equal(
			Dhall::Enum.new(
				tag:          "hai",
				alternatives: Dhall::UnionType.new(alternatives: {})
			),
			:hai.as_dhall
		)
	end

	def test_natural
		assert_equal Dhall::Natural.new(value: 1), 1.as_dhall
	end

	def test_big_natural
		assert_equal(
			Dhall::Natural.new(value: 10000000000000000000000000000000000),
			10000000000000000000000000000000000.as_dhall
		)
	end

	def test_negative_integer
		assert_equal Dhall::Integer.new(value: -1), -1.as_dhall
	end

	def test_double
		assert_equal Dhall::Double.new(value: 1.0), 1.0.as_dhall
	end

	def test_double_infinity
		assert_equal(
			Dhall::Double.new(value: Float::INFINITY),
			Float::INFINITY.as_dhall
		)
	end

	def test_true
		assert_equal Dhall::Bool.new(value: true), true.as_dhall
	end

	def test_false
		assert_equal Dhall::Bool.new(value: false), false.as_dhall
	end

	def test_nil
		assert_raises RuntimeError do
			nil.as_dhall
		end
	end

	def test_empty_array
		assert_equal(
			Dhall::EmptyList.new(element_type: Dhall::UnionType.new(alternatives: {})),
			[].as_dhall
		)
	end

	def test_array_one_natural
		assert_equal(
			Dhall::List.new(elements: [Dhall::Natural.new(value: 1)]),
			[1].as_dhall
		)
	end

	def test_array_natural_and_nil
		assert_equal(
			Dhall::List.new(elements: [
				Dhall::Optional.new(value: Dhall::Natural.new(value: 1)),
				Dhall::OptionalNone.new(value_type: Dhall::Builtins[:Natural])
			]),
			[1, nil].as_dhall
		)
	end

	def test_array_natural_and_bignum_and_nil
		assert_equal(
			Dhall::List.new(elements: [
				Dhall::Optional.new(value: Dhall::Natural.new(value: 1)),
				Dhall::Optional.new(value: Dhall::Natural.new(
					value: 10000000000000000000000000000000000
				)),
				Dhall::OptionalNone.new(value_type: Dhall::Builtins[:Natural])
			]),
			[1, 10000000000000000000000000000000000, nil].as_dhall
		)
	end

	def test_array_mixed
		array_key = "Array_f256441295d38d19e84f2de0596f5ae2377" \
		            "c923c4162351d88f7648d741cdd0c"
		hash_key = "Hash_76cf2d18fa656820d79d13cad11bf3e613fdb0" \
		           "6ff80f968ba1755d27cdf5eab3"
		union_type = Dhall::UnionType.new(
			alternatives: {
				"Natural" => Dhall::Builtins[:Natural],
				"Text"    => Dhall::Builtins[:Text],
				"None"    => nil,
				"boop"    => nil,
				"Object"  => Dhall::EmptyRecordType.new,
				"Bool"    => Dhall::Builtins[:Bool],
				hash_key  => Dhall::RecordType.new(
					record: {
						"a" => Dhall::Builtins[:Natural]
					}
				),
				array_key => Dhall::Application.new(
					function: Dhall::Builtins[:List],
					argument: Dhall::Builtins[:Natural]
				)
			}
		)

		assert_equal(
			Dhall::List.new(elements: [
				Dhall::Union.from(union_type, "Natural", Dhall::Natural.new(value: 1)),
				Dhall::Union.from(union_type, "Text", Dhall::Text.new(value: "hai")),
				Dhall::Union.from(union_type, "None", nil),
				Dhall::Union.from(union_type, "boop", nil),
				Dhall::Union.from(union_type, "Object", Dhall::EmptyRecord.new),
				Dhall::Union.from(union_type, "Bool", Dhall::Bool.new(value: true)),
				Dhall::Union.from(union_type, "Bool", Dhall::Bool.new(value: false)),
				Dhall::Union.from(union_type, hash_key, Dhall::Record.new(
					record: { "a" => Dhall::Natural.new(value: 1) }
				)),
				Dhall::Union.from(union_type, array_key, Dhall::List.new(
					elements: [Dhall::Natural.new(value: 1)]
				))
			]),
			[1, "hai", nil, :boop, Object.new, true, false, { a: 1 }, [1]].as_dhall
		)
	end

	def test_empty_hash
		assert_equal Dhall::EmptyRecord.new, {}.as_dhall
	end

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

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

	def test_hash_mixed
		assert_equal(
			Dhall::Record.new(
				record: {
					"a" => Dhall::Natural.new(value: 1),
					"b" => Dhall::Text.new(value: "hai"),
					"c" => Dhall::Bool.new(value: true)
				}
			),
			{ a: 1, b: "hai", c: true }.as_dhall
		)
	end

	def test_hash_nested
		assert_equal(
			Dhall::Record.new(
				record: { "a" => Dhall::Record.new(
					record: { "b" => Dhall::Natural.new(value: 1) }
				) }
			),
			{ a: { b: 1 } }.as_dhall
		)
	end

	def test_openstruct
		assert_equal(
			Dhall::Union.new(
				tag:          "OpenStruct",
				value:        Dhall::TypeAnnotation.new(
					type:  Dhall::EmptyRecordType.new,
					value: Dhall::EmptyRecord.new
				),
				alternatives: Dhall::UnionType.new(alternatives: {})
			),
			OpenStruct.new({}).as_dhall
		)
	end

	class SomeTestClass
		def initialize
			@a = 1
			@b = "hai"
		end
	end

	def test_object
		assert_equal(
			Dhall::Union.new(
				tag:          "TestAsDhall::SomeTestClass",
				value:        Dhall::TypeAnnotation.new(
					type:  Dhall::RecordType.new(
						record: {
							"a" => Dhall::Builtins[:Natural],
							"b" => Dhall::Builtins[:Text]
						}
					),
					value: Dhall::Record.new(
						record: {
							"a" => Dhall::Natural.new(value: 1),
							"b" => Dhall::Text.new(value: "hai")
						}
					)
				),
				alternatives: Dhall::UnionType.new(alternatives: {})
			),
			SomeTestClass.new.as_dhall
		)
	end
end