~mna/snow

ref: 51726a5ef2959dd97add58914fe45d829880e258 snow/pkg/token/token_test.go -rw-r--r-- 1.9 KiB
51726a5eMartin Angers Merge branch 'wip-static-func-return' 1 year, 11 months ago
                                                                                
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package token

import "testing"

func TestToken(t *testing.T) {
	ignoreTokens := map[Token]bool{
		litStart: true,
		litEnd:   true,
		opStart:  true,
		opEnd:    true,
		kwStart:  true,
		kwEnd:    true,
	}

	t.Run("string", func(t *testing.T) {
		// all tokens have a string representation
		for i := Illegal; i < kwEnd; i++ {
			if ignoreTokens[i] {
				continue
			}
			if s := i.String(); s == "" {
				t.Fatalf("token %d has no string representation", i)
			}
		}
	})

	t.Run("LookupKw", func(t *testing.T) {
		for i := kwStart + 1; i < kwEnd; i++ {
			if tok := LookupKw(i.String()); !tok.Keyword() {
				t.Fatalf("token %s does not return a keyword token", i)
			}
		}

		for i := opStart + 1; i < opEnd; i++ {
			if tok := LookupKw(i.String()); tok.Keyword() {
				t.Fatalf("token %s should not return a keyword token", tok)
			}
		}

		ids := []string{"a", "notAKeyword"}
		for _, id := range ids {
			if tok := LookupKw(id); tok != Ident {
				t.Fatalf("identifier %s does not return an Ident token, returns: %s", id, tok)
			}
		}
	})

	t.Run("LookupOp", func(t *testing.T) {
		for i := opStart + 1; i < opEnd; i++ {
			if tok := LookupOp(i.String()); !tok.Operator() {
				t.Fatalf("token %s does not return an operator token", i)
			}
		}

		for i := kwStart + 1; i < kwEnd; i++ {
			if tok := LookupOp(i.String()); tok != Illegal {
				t.Fatalf("token %s does not return an Illegal token, returns: %s", i, tok)
			}
		}
	})

	t.Run("Literal", func(t *testing.T) {
		for i := litStart + 1; i < litEnd; i++ {
			if !i.Literal() {
				t.Fatalf("token %s should be a literal", i)
			}
		}
	})

	t.Run("Operator", func(t *testing.T) {
		for i := opStart + 1; i < opEnd; i++ {
			if !i.Operator() {
				t.Fatalf("token %s should be an operator", i)
			}
		}
	})

	t.Run("Keyword", func(t *testing.T) {
		for i := kwStart + 1; i < kwEnd; i++ {
			if !i.Keyword() {
				t.Fatalf("token %s should be a keyword", i)
			}
		}
	})
}