-
Notifications
You must be signed in to change notification settings - Fork 57
/
base_lexer_test.go
105 lines (92 loc) · 2.04 KB
/
base_lexer_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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT
package sdp
import (
"fmt"
"testing"
)
func TestLexer(t *testing.T) {
t.Run("single field", func(t *testing.T) {
for k, value := range map[string]string{
"clean": "aaa",
"with extra space": "aaa ",
"with linebreak": "aaa \n",
"with linebreak 2": "aaa \r\n",
} {
l := &baseLexer{value: value}
field, err := l.readField()
if err != nil {
t.Fatal(err)
}
if field != "aaa" {
t.Errorf("%s: aaa not parsed, got: '%v'", k, field)
}
}
})
t.Run("syntax error", func(t *testing.T) {
l := &baseLexer{value: "12NaN"}
_, err := l.readUint64Field()
if err != nil {
fmt.Println("error message:", err.Error())
} else {
t.Fatal("no error")
}
})
t.Run("many fields", func(t *testing.T) {
l := &baseLexer{value: "aaa 123\nf1 f2\nlast"}
t.Run("first line", func(t *testing.T) {
field, err := l.readField()
if err != nil {
t.Fatal(err)
}
if field != "aaa" {
t.Errorf("aaa not parsed, got: '%v'", field)
}
value, err := l.readUint64Field()
if err != nil {
t.Fatal(err)
}
if value != 123 {
t.Errorf("aaa not parsed, got: '%v'", field)
}
if err := l.nextLine(); err != nil {
t.Fatal(err)
}
})
t.Run("second line", func(t *testing.T) {
field, err := l.readField()
if err != nil {
t.Fatal(err)
}
if field != "f1" {
t.Errorf("value not parsed, got: '%v'", field)
}
field, err = l.readField()
if err != nil {
t.Fatal(err)
}
if field != "f2" {
t.Errorf("value not parsed, got: '%v'", field)
}
field, err = l.readField()
if err != nil {
t.Fatal(err)
}
if field != "" {
t.Errorf("value not parsed, got: '%v'", field)
}
if err := l.nextLine(); err != nil {
t.Fatal(err)
}
})
t.Run("last line", func(t *testing.T) {
field, err := l.readField()
if err != nil {
t.Fatal(err)
}
if field != "last" {
t.Errorf("value not parsed, got: '%v'", field)
}
})
})
}