1import birdie
 2import gleam/int
 3import gleam/list
 4import gleeunit
 5import nibble.{Expected}
 6import nibble/lexer.{Span, Token}
 7import node
 8import parser
 9import quasi_lexer
10
11pub fn main() -> Nil {
12  gleeunit.main()
13}
14
15pub fn simple_quasi_lexer_test() {
16  quasi_lexer.chars()
17  |> quasi_lexer.run(on: "let x1 = e1")
18  |> list.index_map(fn(token, index) {
19    let Token(span, lexeme, value) = token
20    assert lexeme == value
21
22    let Span(row_start, col_start, row_end, col_end) = span
23    assert row_start == row_end && row_start == 1
24    assert col_start == index + 1
25    assert col_end == col_start + 1
26  })
27}
28
29pub fn quasi_lexer_off_by_one_test() {
30  let input = "let x1 =\n e1"
31  let tokens = quasi_lexer.chars() |> quasi_lexer.run(on: input)
32
33  let snap =
34    tokens
35    |> list.index_map(fn(token, index) {
36      let Token(Span(rs, cs, re, ce), lexeme, _) = token
37      "Token "
38      <> int.to_string(index)
39      <> ": '"
40      <> lexeme
41      <> "' at Span(row_start: "
42      <> int.to_string(rs)
43      <> ", col_start: "
44      <> int.to_string(cs)
45      <> ", row_end: "
46      <> int.to_string(re)
47      <> ", col_end: "
48      <> int.to_string(ce)
49      <> ")\n"
50    })
51    |> list.fold("", fn(acc, line) { acc <> line })
52
53  birdie.snap(snap, title: "Quasi lexer spans with multiline input")
54}
55
56pub fn parse_let_successfully_test() {
57  let input = "let"
58  let tokens = quasi_lexer.chars() |> quasi_lexer.run(on: input)
59  let parser = parser.keyword("let", node.Let)
60  let assert Ok(_) = nibble.run(tokens, parser)
61}
62
63pub fn parse_let_failing_test() {
64  let input = "lt"
65  let tokens = quasi_lexer.chars() |> quasi_lexer.run(on: input)
66  let parser = parser.keyword("let", node.Let)
67  let assert Error(error) = nibble.run(tokens, parser)
68  let assert [nibble.DeadEnd(Span(_, cs, _, _), Expected(msg, got: got), [])] =
69    error
70
71  let snap =
72    "Msg: "
73    <> msg
74    <> "\n"
75    <> "Got: "
76    <> got
77    <> "\n"
78    <> "At column: "
79    <> int.to_string(cs)
80    <> "\n"
81
82  birdie.snap(snap, title: "Should fail to parse 'lt' as node.Let")
83}