forked from Team-Ortix/blgo
168 lines
5.2 KiB
Go
168 lines
5.2 KiB
Go
package parser_test
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"gitea.teamortix.com/Team-Ortix/blgo/parser"
|
|
)
|
|
|
|
type keyValueResult struct {
|
|
Key string
|
|
Value string
|
|
Error error
|
|
}
|
|
|
|
func TestParseKeyValueLineWithValidContent(t *testing.T) {
|
|
asrt := assert.New(t)
|
|
|
|
k, v, e := parser.ParseKeyValueLine("A: B")
|
|
asrt.EqualValues(
|
|
keyValueResult{"A", "B", nil},
|
|
keyValueResult{k, v, e},
|
|
"parsing valid key-value line gave invalid result",
|
|
)
|
|
|
|
k, v, e = parser.ParseKeyValueLine(" A : B : C")
|
|
asrt.EqualValues(
|
|
keyValueResult{"A", "B : C", nil},
|
|
keyValueResult{k, v, e},
|
|
"parsing valid key-value line gave invalid result",
|
|
)
|
|
|
|
k, v, e = parser.ParseKeyValueLine(" A B: B")
|
|
asrt.EqualValues(
|
|
keyValueResult{"A B", "B", nil},
|
|
keyValueResult{k, v, e},
|
|
"parsing valid key-value line gave invalid result",
|
|
)
|
|
|
|
k, v, e = parser.ParseKeyValueLine("A:")
|
|
asrt.EqualValues(
|
|
keyValueResult{"A", "", nil},
|
|
keyValueResult{k, v, e},
|
|
"parsing valid key-value line gave invalid result",
|
|
)
|
|
}
|
|
|
|
func TestParseKeyValueWithInvalidEntry(t *testing.T) {
|
|
asrt := assert.New(t)
|
|
|
|
k, v, e := parser.ParseKeyValueLine("")
|
|
asrt.EqualValues(
|
|
keyValueResult{"", "", parser.ErrInvalidKeyValuePair},
|
|
keyValueResult{k, v, e},
|
|
"parsing invalid key-value with no content gave unexpected result",
|
|
)
|
|
|
|
k, v, e = parser.ParseKeyValueLine("A B")
|
|
asrt.EqualValues(
|
|
keyValueResult{"", "", parser.ErrInvalidKeyValuePair},
|
|
keyValueResult{k, v, e},
|
|
"parsing invalid key-value with no delimiter gave unexpected result",
|
|
)
|
|
|
|
k, v, e = parser.ParseKeyValueLine(": B")
|
|
asrt.EqualValues(
|
|
keyValueResult{"", "", parser.ErrBlankKey},
|
|
keyValueResult{k, v, e},
|
|
"parsing invalid key-value with no key gave unexpected result",
|
|
)
|
|
}
|
|
|
|
type extractResult struct {
|
|
FrontMatter parser.FrontMatter
|
|
Content []string
|
|
Error error
|
|
}
|
|
|
|
func TestExtractFrontMatterWithValidContent(t *testing.T) {
|
|
asrt := assert.New(t)
|
|
empty := []string{}
|
|
|
|
fm, rest, e := parser.ExtractFrontMatter(empty)
|
|
asrt.EqualValues(
|
|
extractResult{map[string]string{}, empty, nil},
|
|
extractResult{fm, rest, e},
|
|
"parsing empty input yields unexpected result",
|
|
)
|
|
|
|
inp := []string{"# Content", "..."}
|
|
fm, rest, e = parser.ExtractFrontMatter(inp)
|
|
asrt.EqualValues(
|
|
extractResult{map[string]string{}, inp, nil},
|
|
extractResult{fm, rest, e},
|
|
"parsing empty input yields unexpected result",
|
|
)
|
|
|
|
fm, rest, e = parser.ExtractFrontMatter([]string{"Key: Value", "---"})
|
|
asrt.EqualValues(
|
|
extractResult{map[string]string{"Key": "Value"}, empty, nil},
|
|
extractResult{fm, rest, e},
|
|
"parsing valid FrontMatter yields invalid result",
|
|
)
|
|
|
|
fm, rest, e = parser.ExtractFrontMatter([]string{"Key: Value", "---", "# Content", "Other content"})
|
|
asrt.EqualValues(
|
|
extractResult{map[string]string{"Key": "Value"}, []string{"# Content", "Other content"}, nil},
|
|
extractResult{fm, rest, e},
|
|
"parsing valid FrontMatter yields invalid result",
|
|
)
|
|
|
|
fm, rest, e = parser.ExtractFrontMatter([]string{"---", "Key: Value", "---"})
|
|
asrt.EqualValues(
|
|
extractResult{map[string]string{"Key": "Value"}, empty, nil},
|
|
extractResult{fm, rest, e},
|
|
"parsing valid FrontMatter yields invalid result",
|
|
)
|
|
|
|
fm, rest, e = parser.ExtractFrontMatter([]string{"Key: Value", "Another Key: Another Value", "---"})
|
|
asrt.EqualValues(
|
|
extractResult{map[string]string{"Key": "Value", "Another Key": "Another Value"}, empty, nil},
|
|
extractResult{fm, rest, e},
|
|
"parsing valid FrontMatter yields invalid result",
|
|
)
|
|
|
|
fm, rest, e = parser.ExtractFrontMatter([]string{"", "Key: Value", "", "", "Another Key: Another Value", "---"})
|
|
asrt.EqualValues(
|
|
extractResult{map[string]string{"Key": "Value", "Another Key": "Another Value"}, empty, nil},
|
|
extractResult{fm, rest, e},
|
|
"parsing valid FrontMatter yields invalid result",
|
|
)
|
|
}
|
|
|
|
func TestExtractFrontMatterWithBadKeys(t *testing.T) {
|
|
asrt := assert.New(t)
|
|
empty := []string{}
|
|
|
|
fm, rest, e := parser.ExtractFrontMatter([]string{"---", "Key Value", "---"})
|
|
asrt.EqualValues(
|
|
extractResult{map[string]string{}, []string{"---"}, fmt.Errorf("error parsing line 2: %w", parser.ErrInvalidKeyValuePair)},
|
|
extractResult{fm, rest, e},
|
|
"parsing invalid FrontMatter with no delimiter yields invalid result",
|
|
)
|
|
|
|
fm, _, e = parser.ExtractFrontMatter([]string{"Key: Value", ": Another Value", "---"})
|
|
asrt.EqualValues(
|
|
extractResult{map[string]string{"Key": "Value"}, empty, fmt.Errorf("error parsing line 2: %w", parser.ErrBlankKey)},
|
|
extractResult{fm, empty, e},
|
|
"parsing invalid FrontMatter with blank key yields invalid result",
|
|
)
|
|
|
|
fm, _, e = parser.ExtractFrontMatter([]string{"Key: Value", "Key: Dupe Value", "---"})
|
|
asrt.EqualValues(
|
|
extractResult{map[string]string{"Key": "Value"}, empty, fmt.Errorf("error on parsing line 2: %w", parser.ErrDuplicateKey)},
|
|
extractResult{fm, empty, e},
|
|
"parsing invalid FrontMatter with duplicate key entry yields invalid result",
|
|
)
|
|
|
|
fm, rest, e = parser.ExtractFrontMatter([]string{"Key: Value", "Another Key: Another Value"})
|
|
asrt.EqualValues(
|
|
extractResult{map[string]string{"Key": "Value", "Another Key": "Another Value"}, []string{"Key: Value", "Another Key: Another Value"}, fmt.Errorf("error on parsing: %w", parser.ErrEOF)},
|
|
extractResult{fm, rest, e},
|
|
"parsing invalid FrontMatter with no final dashes yields invalid result",
|
|
)
|
|
}
|