~singpolyma/dhall-ruby

ref: 0.2.0 dhall-ruby/test/test_coder.rb -rw-r--r-- 6.0 KiB View raw
a49a8991Stephen Paul Weber Customizable cache 1 year, 1 month 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
# frozen_string_literal: true

require "minitest/autorun"

require "dhall"

class TestCoder < Minitest::Test
	def test_dump_integer
		assert_equal "\x82\x0F\x01".b, Dhall::Coder.dump(1)
	end

	def test_dump_integer_negative
		assert_equal "\x82\x10 ".b, Dhall::Coder.dump(-1)
	end

	def test_dump_float
		assert_equal "\xFA?\x80\x00\x00".b, Dhall::Coder.dump(1.0)
	end

	def test_dump_string
		assert_equal "\x82\x12ehello".b, Dhall::Coder.dump("hello")
	end

	def test_dump_nil
		assert_nil Dhall::Coder.dump(nil)
	end

	def test_dump_array
		assert_equal(
			"\x84\x04\xF6\x82\x0F\x01\x82\x0F\x02".b,
			Dhall::Coder.dump([1, 2])
		)
	end

	def test_dump_array_with_nil
		assert_equal(
			"\x84\x04\xF6\x83\x05\xF6\x82\x0F\x01\x83\x00dNonegNatural".b,
			Dhall::Coder.dump([1, nil])
		)
	end

	def test_dump_array_heterogenous
		assert_equal(
			"\x86\x04\xF6\x83\x00\x83\t\x82\v\xA4fDoublefDoublegNatural" \
			"gNaturaldNone\xF6dTextdTextgNatural\x82\x0F\x01\x83\t\x82\v\xA4" \
			"fDoublefDoublegNaturalgNaturaldNone\xF6dTextdTextdNone\x83\x00\x83" \
			"\t\x82\v\xA4fDoublefDoublegNaturalgNaturaldNone\xF6dTextdText" \
			"fDouble\xFA?\x80\x00\x00\x83\x00\x83\t\x82\v\xA4fDoublefDoubleg" \
			"NaturalgNaturaldNone\xF6dTextdTextdText\x82\x12ehello".b,
			Dhall::Coder.dump([1, nil, 1.0, "hello"])
		)
	end

	def test_dump_hash
		assert_equal(
			"\x82\b\xA2aa\x82\x0F\x01ab\xFA?\x80\x00\x00".b,
			Dhall::Coder.dump(a: 1, b: 1.0)
		)
	end

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

	def test_load_loaded
		assert_equal 1, Dhall::Coder.load(1)
	end

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

	def test_load_integer_negative
		assert_equal(-1, Dhall::Coder.load("\x82\x10 ".b))
	end

	def test_load_float
		assert_equal 1.0, Dhall::Coder.load("\xFA?\x80\x00\x00".b)
	end

	def test_load_string
		assert_equal "hello", Dhall::Coder.load("\x82\x12ehello".b)
	end

	def test_load_nil
		assert_nil Dhall::Coder.load(nil)
	end

	def test_load_array
		assert_equal(
			[1, 2],
			Dhall::Coder.load("\x84\x04\xF6\x82\x0F\x01\x82\x0F\x02".b)
		)
	end

	def test_load_array_with_nil
		assert_equal(
			[1, nil],
			Dhall::Coder.load(
				"\x84\x04\xF6\x83\x05\xF6\x82\x0F\x01\x83\x00dNonegNatural".b
			)
		)
	end

	def test_load_array_heterogenous
		assert_equal(
			[1, nil, 1.0, "hello"],
			Dhall::Coder.load(
				"\x86\x04\xF6\x83\x00\x83\t\x82\v\xA4fDoublefDoublegNatural" \
				"gNaturaldNone\xF6dTextdTextgNatural\x82\x0F\x01\x83\t\x82\v\xA4" \
				"fDoublefDoublegNaturalgNaturaldNone\xF6dTextdTextdNone\x83\x00\x83" \
				"\t\x82\v\xA4fDoublefDoublegNaturalgNaturaldNone\xF6dTextdText" \
				"fDouble\xFA?\x80\x00\x00\x83\x00\x83\t\x82\v\xA4fDoublefDoubleg" \
				"NaturalgNaturaldNone\xF6dTextdTextdText\x82\x12ehello".b
			)
		)
	end

	def test_load_hash
		assert_equal(
			{ "a" => 1, "b" => 1.0 },
			Dhall::Coder.load("\x82\b\xA2aa\x82\x0F\x01ab\xFA?\x80\x00\x00".b)
		)
	end

	def test_load_hash_symbolize
		assert_equal(
			{ a: 1, b: 1.0 },
			Dhall::Coder.load(
				"\x82\b\xA2aa\x82\x0F\x01ab\xFA?\x80\x00\x00".b,
				transform_keys: :to_sym
			)
		)
	end

	def test_load_object
		assert_raises ArgumentError do
			Dhall::Coder.load(
				"\x83\x00\x83\t\x82\v\xA1fObject\x82\a\xA0fObject\x82\b\xA0".b
			)
		end
	end

	class Custom
		attr_reader :a, :b

		def initialize
			@a = true
			@b = "true"
		end

		def ==(other)
			a == other.a && b == other.b
		end
	end

	def test_bad_default
		assert_raises ArgumentError do
			Dhall::Coder.new(safe: Custom)
		end
	end

	def test_dump_custom
		assert_equal(
			"\x83\x00\x83\t\x82\v\xA1qTestCoder::Custom\x82\a\xA2aadBoolabd" \
			"TextqTestCoder::Custom\x82\b\xA2aa\xF5ab\x82\x12dtrue".b,
			Dhall::Coder.new(default: Custom.new, safe: Custom).dump(Custom.new)
		)
	end

	def test_load_custom
		coder = Dhall::Coder.new(
			default: Custom.new,
			safe:    Dhall::Coder::JSON_LIKE + [Custom]
		)
		assert_equal(
			Custom.new,
			coder.load(
				"\x83\x00\x83\t\x82\v\xA1qTestCoder::Custom\x82\a\xA2aadBoolabd" \
				"TextqTestCoder::Custom\x82\b\xA2aa\xF5ab\x82\x12dtrue".b
			)
		)
	end

	class CustomCoding
		attr_reader :a, :b

		def initialize
			@a = true
			@b = "true"
		end

		def ==(other)
			a == other.a && b == other.b
		end

		def init_with(coder)
			@a = coder["abool"]
			@b = coder["astring"]
		end

		def encode_with(coder)
			coder["abool"] = @a
			coder["astring"] = @b
		end
	end

	def test_dump_custom_coding
		assert_equal(
			"\x83\x00\x83\t\x82\v\xA1wTestCoder::CustomCoding\x82\a\xA2" \
			"eabooldBoolgastringdTextwTestCoder::CustomCoding\x82\b\xA2" \
			"eabool\xF5gastring\x82\x12dtrue".b,
			Dhall::Coder.new(
				default: CustomCoding.new,
				safe:    CustomCoding
			).dump(CustomCoding.new)
		)
	end

	def test_load_custom_coding
		coder = Dhall::Coder.new(
			default: CustomCoding.new,
			safe:    Dhall::Coder::JSON_LIKE + [CustomCoding]
		)
		assert_equal(
			CustomCoding.new,
			coder.load(
				"\x83\x00\x83\t\x82\v\xA1wTestCoder::CustomCoding\x82\a\xA2" \
				"eabooldBoolgastringdTextwTestCoder::CustomCoding\x82\b\xA2" \
				"eabool\xF5gastring\x82\x12dtrue".b
			)
		)
	end

	class CustomDhall
		using Dhall::AsDhall

		attr_reader :str

		def self.from_dhall(expr)
			new(expr.to_s)
		end

		def initialize(str="test")
			@str = str
		end

		def ==(other)
			str == other.str
		end

		def as_dhall
			Dhall::Union.from(
				Dhall::UnionType.new(
					alternatives: { self.class.name => Dhall::Text.as_dhall }
				),
				self.class.name,
				@str.as_dhall
			)
		end
	end

	def test_dump_custom_dhall
		assert_equal(
			"\x83\x00\x83\t\x82\v\xA1vTestCoder::CustomDhalldText" \
			"vTestCoder::CustomDhall\x82\x12dtest".b,
			Dhall::Coder.new(
				default: CustomDhall.new,
				safe:    CustomDhall
			).dump(CustomDhall.new)
		)
	end

	def test_load_custom_dhall
		coder = Dhall::Coder.new(
			default: CustomDhall.new,
			safe:    Dhall::Coder::JSON_LIKE + [CustomDhall]
		)
		assert_equal(
			CustomDhall.new,
			coder.load(
				"\x83\x00\x83\t\x82\v\xA1vTestCoder::CustomDhalldText" \
				"vTestCoder::CustomDhall\x82\x12dtest".b
			)
		)
	end
end