-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathparse_test.go
50 lines (42 loc) · 1.01 KB
/
parse_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
package parse_test
import (
"context"
"testing"
"github.com/modern-go/parse"
"github.com/modern-go/test"
"github.com/modern-go/test/must"
)
func TestString(t *testing.T) {
t.Run("no error", test.Case(func(ctx context.Context) {
parsed := must.Call(parse.String, "abc", &myLexer{})[0]
must.Equal(uint8('a'), parsed)
}))
t.Run("can not parse", test.Case(func(ctx context.Context) {
parsed, err := parse.String("bc", &myLexer{})
must.NotNil(err)
must.Nil(parsed)
}))
t.Run("EOF", test.Case(func(ctx context.Context) {
parsed := must.Call(parse.String, "a", &myLexer{})[0]
must.Equal(uint8('a'), parsed)
}))
}
type myLexer struct {
}
func (lexer *myLexer) PrefixToken(src *parse.Source) parse.PrefixToken {
switch src.Peek1() {
case 'a':
return &myToken{}
default:
return nil
}
}
func (lexer *myLexer) InfixToken(src *parse.Source) (parse.InfixToken, int) {
return nil, 0
}
type myToken struct {
}
func (token *myToken) PrefixParse(src *parse.Source) interface{} {
b := src.Read1()
return b
}