open Hcfg include Token include Lexer include Parser let token_testable = Alcotest.testable Prettyprint.Token.pretty_print Token.equal let hvalue_testable = Alcotest.testable Prettyprint.Parser.pretty_print Parser.equal let read_test_file = (* open_in_bin works correctly on Unix and Windows *) let ch = Stdlib.open_in_bin "test.hcfg" in let s = Stdlib.really_input_string ch (Stdlib.in_channel_length ch) in Stdlib.close_in ch; s let test_lexer() = Alcotest.(check (list token_testable)) "same token types" [ Token.LBRACE ; Token.IDENT "key" ; Token.COLON ; Token.STRING "value" ; Token.COMMA ; Token.IDENT "other_key" ; Token.COLON ; Token.INTEGER 100 ; Token.COMMA ; Token.IDENT "nulled" ; Token.COLON ; Token.NULL ; Token.COMMA ; Token.IDENT "floating" ; Token.COLON ; Token.FLOAT 123.0 ; Token.COMMA ; Token.IDENT "other_float" ; Token.COLON ; Token.FLOAT 123.3 ; Token.COMMA ; Token.IDENT "bin" ; Token.COLON ; Token.BINARY (Bytes.of_string "test") ; Token.COMMA ; Token.IDENT "arr" ; Token.COLON ; Token.LBRACKET ; Token.INTEGER 1 ; Token.COMMA ; Token.INTEGER 2; Token.RBRACKET ; Token.COMMA ; Token.IDENT "bool1"; Token.COLON; Token.BOOL true; Token.COMMA ; Token.IDENT "bool2"; Token.COLON; Token.BOOL false; Token.COMMA ; Token.IDENT "nested" ; Token.COLON ; Token.LBRACE ; Token.IDENT "key" ; Token.COLON ; Token.STRING "trailing" ; Token.COMMA ; Token.RBRACE ; Token.COMMA ; Token.RBRACE ; Token.EOF ] (Lexer.generate_tokens read_test_file) module Obj = Map.Make(String) let test_ast() = Alcotest.(check (hvalue_testable)) "same token types" (Parser.OBJECT (Obj.of_list [("a", Parser.FLOAT 1.); ("b", Parser.ARRAY [Parser.INTEGER 1; Parser.INTEGER 2]); ("c", Parser.OBJECT (Obj.of_list [("a", Parser.INTEGER 2)])); ("d", Parser.BOOL false)])) (Lexer.generate_tokens {|{a: 1f,b: [1,2], c: { a: 2}, d: false}|} |> Parser.parse) let () = Alcotest.run "Lexer" [ ( "lex test file", [ Alcotest.test_case "" `Slow test_lexer] ); ( "parse test file", [ Alcotest.test_case "" `Slow test_ast]); ]