From 7274a86b64cd761f0c0c3d0bddb790f2d5aba1fc Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Mon, 8 Apr 2019 21:16:06 -0500 Subject: [PATCH] Add Dhall.load --- lib/dhall.rb | 8 +++++++ test/test_load.rb | 57 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 test/test_load.rb diff --git a/lib/dhall.rb b/lib/dhall.rb index 0c8014a..6e4e114 100644 --- a/lib/dhall.rb +++ b/lib/dhall.rb @@ -1,6 +1,14 @@ # frozen_string_literal: true module Dhall + def self.load(source, resolver: Resolvers::Default.new) + Promise.resolve(nil).then { + load_raw(source).resolve(resolver: resolver) + }.then do |resolved| + TypeChecker.for(resolved).annotate(TypeChecker::Context.new).normalize + end + end + def self.load_raw(source) begin return from_binary(source) if source.encoding == Encoding::BINARY diff --git a/test/test_load.rb b/test/test_load.rb new file mode 100644 index 0000000..1feb16a --- /dev/null +++ b/test/test_load.rb @@ -0,0 +1,57 @@ +# frozen_string_literal: true + +require "minitest/autorun" + +require "dhall" + +class TestLoad < Minitest::Test + def test_load_natural_source + assert_equal Dhall::Natural.new(value: 1), Dhall.load("1").sync + end + + def test_load_natural_source_mislabeled_encoding + assert_equal Dhall::Natural.new(value: 1), Dhall.load("1".b).sync + end + + def test_load_natural_binary + assert_equal( + Dhall::Natural.new(value: 1), + Dhall.load("\x82\x0f\x01".b).sync + ) + end + + def test_load_normalizes + assert_equal( + Dhall::Natural.new(value: 2), + Dhall.load("1 + 1").sync + ) + end + + def test_load_resolves + assert_equal( + Dhall::Natural.new(value: 2), + Dhall.load( + "/path/to/source.dhall", + resolver: Dhall::Resolvers::Default.new( + path_reader: ->(s) { s.map { "1 + 1" } } + ) + ).sync + ) + end + + def test_load_typechecks + assert_raises TypeError do + Dhall.load("1 + \"hai\"").sync + end + end + + def test_load_raw_not_normalizes_or_typechecks + assert_equal( + Dhall::Operator::Plus.new( + lhs: Dhall::Natural.new(value: 1), + rhs: Dhall::Text.new(value: "hai") + ), + Dhall.load_raw("1 + \"hai\"") + ) + end +end -- 2.34.2