def test_type_inference_success(input, expected): set_cache_class(InMemoryCache) with open(input) as f: termA = Dhall.p_parse(f.read()) with open(expected) as f: termB = Dhall.p_parse(f.read()) assert termA.resolve(input).type().quote() == termB
def test_alpha_normalization_unit(input, expected): set_cache_class(InMemoryCache) with open(input) as f: termA = Dhall.p_parse(f.read()) with open(expected) as f: termB = Dhall.p_parse(f.read()) termA = termA.eval().quote(normalize=True) assert termA == termB
def test_bnorm(input, expected): set_cache_class(InMemoryCache) with open(input) as f: termA = Dhall.p_parse(f.read()) with open(expected) as f: termB = Dhall.p_parse(f.read()) assert termA.resolve(input).eval().quote() == termB.resolve( expected).eval().quote()
def test_import(input, expected): set_cache_class(TestFSCache) try: print(input) with open(input) as f: termA = Dhall.p_parse(f.read()) with open(expected) as f: termB = Dhall.p_parse(f.read()) term = termA.resolve(LocalFile(input, None, 0)).eval().quote() assert term == termB finally: import_.CACHE.reset()
def test_bin_decode(input, expected): set_cache_class(InMemoryCache) with open(input, "rb") as f: binA = f.read() with open(expected) as f: termB = Dhall.p_parse(f.read()) termA = Term.from_cbor(binA) assert termA == termB
def test_type_inference_failure(input): set_cache_class(InMemoryCache) with open(input) as f: with pytest.raises(Exception) as info: term = Dhall.p_parse(f.read()) term.resolve(input).type() if info.type not in (DhallTypeError, ParserError): print(info.traceback) assert False, info.value
def test_parse_failure(input): set_cache_class(InMemoryCache) with open(input) as f: try: src = f.read() except UnicodeDecodeError: return with pytest.raises(ParserError) as exc: term = Dhall.p_parse(src)
def hash(args): if not args.file: src = sys.stdin.read() module = Dhall.p_parse(src) origin = LocalFile(Path(os.getcwd()).joinpath("<stdin>"), None, 0) else: with open(args.file) as f: src = f.read() origin = LocalFile(Path(args.file), None, 0) module = Dhall.p_parse(src) module = module.resolve(origin) try: module.type() except DhallTypeError as e: raise sys.stderr.write("Error: ") sys.stderr.write(str(e)) sys.stderr.write("\n") sys.exit(1) print(module.eval().quote(normalize=True).sha256())
def test_line_comment(): p = Dhall("-- hello\nlet foo = 1 in foo") result = p.LineComment() assert result == LineComment(" hello") p = Dhall("--\nlet foo = 1 in foo") result = p.LineComment() assert result == LineComment("")
def test_var(): p = Dhall("myvar") result = p.Variable() assert result == Var("myvar", 0) p = Dhall("_@2") result = p.Variable() assert result == Var("_", 2)
def test_url(): p = Dhall("http://example.com/") result = p.HttpRaw() assert result == urlparse("http://example.com/") p = Dhall("https://*****:*****@example.com/package.dhall?stuff=21&b=32") result = p.HttpRaw() assert result == urlparse( "https://*****:*****@example.com/package.dhall?stuff=21&b=32")
def benchit(src, filename): NUMBER = 1 REPEAT = 5 gc.collect() total_seconds = min(repeat(lambda: Dhall.p_parse(src), lambda: gc.enable(), repeat=REPEAT, number=NUMBER)) seconds_each = total_seconds / NUMBER kb = len(src) / 1024.0 print('%s: Took %.3fs to parse %.1fKB: %.0fKB/s' % ( filename, seconds_each, kb, kb / seconds_each)) return seconds_each
def normalize(args): if not args.file: src = sys.stdin.read() module = Dhall.p_parse(src) else: with open(src) as f: src = f.read() try: module.type() except DhallTypeError as e: raise sys.stderr.write("Error: ") sys.stderr.write(str(e)) sys.stderr.write("\n") sys.exit(1) print(module.eval().quote(normalize=True).dhall())
def test_parse_success(input, expected): set_cache_class(InMemoryCache) with open(input) as f: termA = Dhall.p_parse(f.read()) with open(expected, "rb") as f: assert isinstance(termA, Term) bin = termA.cbor() termB_bin = f.read() try: assert termA.cbor() == termB_bin except AssertionError: # print("") # print(input) # print(repr(termA)) # print(termA.cbor_values()) # with open(str(expected).replace(".dhallb", ".diag")) as f: # print(f.read()) raise
def test_if(input, expected): assert Dhall.p_parse(input).eval() == expected
def test_integer_lit(input, expected): assert Dhall.p_parse(input) == expected
def test_double_quote_text(): p = Dhall('"a string"') result = p.DoubleQuoteLiteral() assert result == TextLit(chunks=[], suffix='a string')
def test_bindings(input, expected): assert Dhall.p_parse(input) == expected
def test_label_fail(input): p = Dhall(input) result = p.Label() assert result is p.NoMatch
def test_label(input, expected): p = Dhall(input) result = p.Label() assert p.p_flatten(result) == expected
def test_non_empty_list(input, expected): result = Dhall.p_parse(input) assert result == expected
def test_block_comment(): p = Dhall("{- hop -}") result = p.BlockComment() assert result == BlockComment("{- hop -}")
def test_looong_comment(): "Long comments used to fail on recursion error" cmt = "{-" + (" " * 5000) + "-}" p = Dhall(cmt) result = p.BlockComment() assert result == BlockComment(cmt)
def test_bool_lit(input, expected): result = Dhall.p_parse(input) assert result == expected
def test_pydhall_schema(): p = Dhall("pydhall+schema:my.module::MyClass") result = p.PydhallSchema() assert result[1] == "my.module" assert result[2] == "MyClass"
def test_char_classes(): p = Dhall("Ā1") result = p.ValidNonAscii() assert result == "Ā"
def test_operator_expr(input, expected): result = Dhall.p_parse(input) assert result == expected
def test_universe(input, expected): assert Dhall.p_parse(input) == expected
def test_record_complete(input, expected): result = Dhall.p_parse(input) assert result == expected
def test_lambda_expr(input, expected): result = Dhall.p_parse(input) assert result == expected