2023-05-18 21:33:18 +08:00
|
|
|
package tokenizer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestTokenize(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
text string
|
|
|
|
tokens []*Token
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
text: "*Hello world!",
|
|
|
|
tokens: []*Token{
|
|
|
|
{
|
|
|
|
Type: Star,
|
|
|
|
Value: "*",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: Text,
|
|
|
|
Value: "Hello",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: Space,
|
|
|
|
Value: " ",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: Text,
|
2023-05-26 08:43:37 +08:00
|
|
|
Value: "world",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: ExclamationMark,
|
|
|
|
Value: "!",
|
2023-05-18 21:33:18 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-05-23 19:52:31 +08:00
|
|
|
{
|
|
|
|
text: `# hello
|
|
|
|
world`,
|
|
|
|
tokens: []*Token{
|
|
|
|
{
|
|
|
|
Type: Hash,
|
|
|
|
Value: "#",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: Space,
|
|
|
|
Value: " ",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: Text,
|
|
|
|
Value: "hello",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: Space,
|
|
|
|
Value: " ",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: Newline,
|
|
|
|
Value: "\n",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: Space,
|
|
|
|
Value: " ",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: Text,
|
|
|
|
Value: "world",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-05-18 21:33:18 +08:00
|
|
|
}
|
2023-05-23 19:52:31 +08:00
|
|
|
|
2023-05-18 21:33:18 +08:00
|
|
|
for _, test := range tests {
|
2023-05-23 19:52:31 +08:00
|
|
|
result := Tokenize(test.text)
|
2023-05-18 21:33:18 +08:00
|
|
|
require.Equal(t, test.tokens, result)
|
|
|
|
}
|
|
|
|
}
|