~singpolyma/dhall-ruby

ref: ebd4b252393c58b8d82af9d886bb059df73db1a6 dhall-ruby/test/test_resolve.rb -rw-r--r-- 5.4 KiB
ebd4b252Stephen Paul Weber Always encode OptionalNone in new style 2 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
# frozen_string_literal: true

require "base64"
require "webmock/minitest"
require "minitest/autorun"

require "dhall/resolve"
require "dhall/normalize"
require "dhall/binary"

class TestResolve < Minitest::Test
	DIRPATH = Pathname.new(File.dirname(__FILE__))
	TESTS = DIRPATH + "normalization/"

	def setup
		@resolver = Dhall::Resolvers::Default.new(
			path_reader: lambda do |sources|
				sources.map do |source|
					Promise.resolve(Base64.decode64({
						"var"      => "AA",
						"import"   => "hRgY9gADY3Zhcg",
						"a"        => "hRgY9gADYWI",
						"b"        => "hRgY9gADYWE",
						"self"     => "hRgY9gADZHNlbGY",
						"text"     => "aGFp",
						"moretext" => "hRgY9gEDZHRleHQ",
						"2text"    => "hAMGhRgY9gEDZHRleHSFGBj2AANobW9yZXRleHQ",
						"using"    => "iRgY9gAAhRgY9gADZ2hlYWRlcnNkZS50ZGF09vY",
						"headers"  => "gwT2ggiiZmhlYWRlcoISYnRoZXZhbHVlghJidHY"
					}.fetch(source.pathname.to_s)))
				end
			end
		)
	end

	def test_nothing_to_resolve
		expr = Dhall::Function.of_arguments(
			Dhall::Variable["Natural"],
			body: Dhall::Variable["_"]
		)

		assert_equal expr, expr.resolve(Dhall::Resolvers::None.new).sync
	end

	def test_import_as_text
		expr = Dhall::Import.new(
			nil,
			Dhall::Import::Text,
			Dhall::Import::RelativePath.new("text")
		)

		assert_equal(
			Dhall::Text.new(value: "hai"),
			expr.resolve(@resolver).sync
		)
	end

	def test_one_level_to_resolve
		expr = Dhall::Function.of_arguments(
			Dhall::Variable["Natural"],
			body: Dhall::Import.new(
				nil,
				Dhall::Import::Expression,
				Dhall::Import::RelativePath.new("var")
			)
		)

		assert_equal(
			expr.with(body: Dhall::Variable["_"]),
			expr.resolve(@resolver).sync
		)
	end

	def test_two_levels_to_resolve
		expr = Dhall::Function.of_arguments(
			Dhall::Variable["Natural"],
			body: Dhall::Import.new(
				nil,
				Dhall::Import::Expression,
				Dhall::Import::RelativePath.new("import")
			)
		)

		assert_equal(
			expr.with(body: Dhall::Variable["_"]),
			expr.resolve(@resolver).sync
		)
	end

	def test_self_loop
		expr = Dhall::Function.of_arguments(
			Dhall::Variable["Natural"],
			body: Dhall::Import.new(
				nil,
				Dhall::Import::Expression,
				Dhall::Import::RelativePath.new("self")
			)
		)

		assert_raises Dhall::ImportLoopException do
			expr.resolve(@resolver).sync
		end
	end

	def test_two_level_loop
		expr = Dhall::Function.of_arguments(
			Dhall::Variable["Natural"],
			body: Dhall::Import.new(
				nil,
				Dhall::Import::Expression,
				Dhall::Import::RelativePath.new("a")
			)
		)

		assert_raises Dhall::ImportLoopException do
			expr.resolve(@resolver).sync
		end
	end

	def test_two_references_no_loop
		expr = Dhall::Import.new(
			nil,
			Dhall::Import::Expression,
			Dhall::Import::RelativePath.new("2text")
		)

		assert_equal(
			Dhall::Operator::TextConcatenate.new(
				lhs: Dhall::Text.new(value: "hai"),
				rhs: Dhall::Text.new(value: "hai")
			),
			expr.resolve(@resolver).sync
		)
	end

	def test_missing
		expr = Dhall::Import.new(
			nil,
			Dhall::Import::Expression,
			Dhall::Import::MissingImport.new
		)

		assert_raises Dhall::ImportFailedException do
			expr.resolve(@resolver).sync
		end
	end

	def test_fallback_to_expr
		expr = Dhall::Operator::ImportFallback.new(
			lhs: Dhall::Import.new(
				nil,
				Dhall::Import::Expression,
				Dhall::Import::MissingImport.new
			),
			rhs: Dhall::Variable["fallback"]
		)

		assert_equal Dhall::Variable["fallback"], expr.resolve(@resolver).sync
	end

	def test_fallback_to_import
		expr = Dhall::Operator::ImportFallback.new(
			lhs: Dhall::Import.new(
				nil,
				Dhall::Import::Expression,
				Dhall::Import::MissingImport.new
			),
			rhs: Dhall::Import.new(
				nil,
				Dhall::Import::Expression,
				Dhall::Import::RelativePath.new("import")
			)
		)

		assert_equal Dhall::Variable["_"], expr.resolve(@resolver).sync
	end

	def test_headers
		stub_request(:get, "http://e.td/t")
			.with(headers: { "Th" => "tv" })
			.to_return(status: 200, body: "\x00")

		expr = Dhall::Import.new(
			nil,
			Dhall::Import::Expression,
			Dhall::Import::RelativePath.new("using")
		)

		assert_equal Dhall::Variable["_"], expr.resolve(@resolver).sync
	end

	def test_ipfs
		stub_request(:get, "http://localhost:8000/ipfs/TESTCID")
			.to_return(status: 200, body: "\x00")

		expr = Dhall::Import.new(
			nil,
			Dhall::Import::Expression,
			Dhall::Import::AbsolutePath.new("ipfs", "TESTCID")
		)

		assert_equal Dhall::Variable["_"], expr.resolve.sync
	end

	def test_ipfs_public_gateway
		stub_request(:get, "http://localhost:8000/ipfs/TESTCID")
			.to_return(status: 500)

		stub_request(:get, "https://cloudflare-ipfs.com/ipfs/TESTCID")
			.to_return(status: 200, body: "\x00")

		expr = Dhall::Import.new(
			nil,
			Dhall::Import::Expression,
			Dhall::Import::AbsolutePath.new("ipfs", "TESTCID")
		)

		assert_equal Dhall::Variable["_"], expr.resolve.sync
	end

	# Sanity check that all expressions can pass through the resolver
	Pathname.glob(TESTS + "**/*A.dhallb").each do |path|
		test = path.relative_path_from(TESTS).to_s.sub(/A\.dhallb$/, "")
		next if test =~ /prelude\//
		next if test =~ /remoteSystems/

		define_method("test_#{test.gsub(/\//, "_")}") do
			Dhall::Function.disable_alpha_normalization! if test =~ /^standard\//
			assert_equal(
				Dhall.from_binary((TESTS + "#{test}B.dhallb").binread),
				Dhall.from_binary(path.binread).resolve.sync.normalize
			)
			Dhall::Function.enable_alpha_normalization! if test =~ /^standard\//
		end
	end
end