-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner_test.go
65 lines (50 loc) · 1.33 KB
/
scanner_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package minimalisp_test
import (
"bytes"
"fmt"
"testing"
. "bakku.dev/minimalisp"
)
func TestScanSourceCode_ShouldReturnEOFForEmptyString(t *testing.T) {
var buf bytes.Buffer
scanner := NewScanner("", &buf)
tokens, ok := scanner.Scan()
if buf.String() != "" {
t.Fatalf("Expected error messages to be empty, got %s", buf.String())
}
if !ok {
t.Fatalf("Expected everything to be ok")
}
if len(tokens) != 1 {
t.Fatalf("Expected token list size 1, got %v", len(tokens))
}
token := tokens[0]
if token.TokenType != EOF {
t.Fatalf("Expected EOF token, got %v", token.TokenType)
}
}
func TestScanSourceCode_ShouldReturnCorrectSequenceOfTokens(t *testing.T) {
sourceCode := `
; some comment
(defun say-hello (name)
(if name
(let (hello-name (str "Hello, " name))
(println hello-name))
(println "No name given")))
(defvar name "Steven")
(println '(123.456 "two" say-hello))
`
var buf bytes.Buffer
scanner := NewScanner(sourceCode, &buf)
tokens, ok := scanner.Scan()
if buf.String() != "" {
fmt.Println(tokens)
t.Fatalf("Expected error messages to be empty, got %s", buf.String())
}
if !ok {
t.Fatalf("Expected everything to be ok")
}
if len(tokens) != 45 {
t.Fatalf("Expected token list size 45, got %v", len(tokens))
}
}