~singpolyma/dhall-ruby

20640b783cf278432500783550abcdd63b4cfc1b — Stephen Paul Weber 2 years ago b72a988 0.3.0
Decode unions the same way as_dhall makes them
2 files changed, 28 insertions(+), 2 deletions(-)

M lib/dhall/coder.rb
M test/test_coder.rb
M lib/dhall/coder.rb => lib/dhall/coder.rb +4 -2
@@ 129,8 129,10 @@ module Dhall

			refine Union do
				def to_ruby
					if tag.match(/\A\p{Upper}/) && Object.const_defined?(tag)
						yield extract, Object.const_get(tag)
					rtag = tag.sub(/_[0-9a-f]{64}\Z/, "")
					if tag.match(/\A\p{Upper}/) &&
					   Object.const_defined?(rtag) && !Dhall.const_defined?(rtag, false)
						yield extract, Object.const_get(rtag)
					else
						yield extract
					end

M test/test_coder.rb => test/test_coder.rb +24 -0
@@ 306,4 306,28 @@ class TestCoder < Minitest::Test
			)
		)
	end

	class SomeTestClass
		attr_reader :a, :b

		def initialize(a: 1, b: "hai")
			@a = a
			@b = b
		end

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

	def test_mixed_array_roundtrip
		coder = Dhall::Coder.new(safe: Object) # unsafe coder

		array = [
			1, "hai", nil, :boop, true, false, { "a" => 1 }, [1],
			SomeTestClass.new, SomeTestClass.new(a: "hai", b: 1)
		]

		assert_equal(array, coder.load(coder.dump(array)))
	end
end