150 lines
4.4 KiB
Go
150 lines
4.4 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
|
||
|
Error error
|
||
|
}
|
||
|
|
||
|
func TestExtractFrontMatterWithValidContent(t *testing.T) {
|
||
|
asrt := assert.New(t)
|
||
|
|
||
|
fm, e := parser.ExtractFrontMatter([]string{})
|
||
|
asrt.EqualValues(
|
||
|
extractResult{map[string]string{}, nil},
|
||
|
extractResult{fm, e},
|
||
|
"parsing empty input yields unexpected result",
|
||
|
)
|
||
|
|
||
|
fm, e = parser.ExtractFrontMatter([]string{"Key: Value", "---"})
|
||
|
asrt.EqualValues(
|
||
|
extractResult{map[string]string{"Key": "Value"}, nil},
|
||
|
extractResult{fm, e},
|
||
|
"parsing valid FrontMatter yields invalid result",
|
||
|
)
|
||
|
|
||
|
fm, e = parser.ExtractFrontMatter([]string{"Key: Value", "---", "# Content", "Other content"})
|
||
|
asrt.EqualValues(
|
||
|
extractResult{map[string]string{"Key": "Value"}, nil},
|
||
|
extractResult{fm, e},
|
||
|
"parsing valid FrontMatter yields invalid result",
|
||
|
)
|
||
|
|
||
|
fm, e = parser.ExtractFrontMatter([]string{"---", "Key: Value", "---"})
|
||
|
asrt.EqualValues(
|
||
|
extractResult{map[string]string{"Key": "Value"}, nil},
|
||
|
extractResult{fm, e},
|
||
|
"parsing valid FrontMatter yields invalid result",
|
||
|
)
|
||
|
|
||
|
fm, e = parser.ExtractFrontMatter([]string{"Key: Value", "Another Key: Another Value", "---"})
|
||
|
asrt.EqualValues(
|
||
|
extractResult{map[string]string{"Key": "Value", "Another Key": "Another Value"}, nil},
|
||
|
extractResult{fm, e},
|
||
|
"parsing valid FrontMatter yields invalid result",
|
||
|
)
|
||
|
}
|
||
|
|
||
|
func TestExtractFrontMatterWithBadKeys(t *testing.T) {
|
||
|
asrt := assert.New(t)
|
||
|
|
||
|
fm, e := parser.ExtractFrontMatter([]string{"---", "Key Value", "---"})
|
||
|
asrt.EqualValues(
|
||
|
extractResult{map[string]string{}, fmt.Errorf("error parsing line 2: %w", parser.ErrInvalidKeyValuePair)},
|
||
|
extractResult{fm, 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"}, fmt.Errorf("error parsing line 2: %w", parser.ErrBlankKey)},
|
||
|
extractResult{fm, 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"}, fmt.Errorf("error on parsing line 2: %w", parser.ErrDuplicateKey)},
|
||
|
extractResult{fm, e},
|
||
|
"parsing invalid FrontMatter with no final dashes yields invalid result",
|
||
|
)
|
||
|
|
||
|
fm, e = parser.ExtractFrontMatter([]string{"Key: Value", "Another Key: Another Value"})
|
||
|
asrt.EqualValues(
|
||
|
extractResult{map[string]string{"Key": "Value", "Another Key": "Another Value"}, fmt.Errorf("error on parsing: %w", parser.ErrEOF)},
|
||
|
extractResult{fm, e},
|
||
|
"parsing invalid FrontMatter with no final dashes yields invalid result",
|
||
|
)
|
||
|
}
|