@ -74,83 +74,87 @@ func TestParseKeyValueWithInvalidEntry(t *testing.T) {
type extractResult struct {
type extractResult struct {
FrontMatter parser . FrontMatter
FrontMatter parser . FrontMatter
Content [ ] string
Error error
Error error
}
}
func TestExtractFrontMatterWithValidContent ( t * testing . T ) {
func TestExtractFrontMatterWithValidContent ( t * testing . T ) {
asrt := assert . New ( t )
asrt := assert . New ( t )
empty := [ ] string { }
fm , e := parser . ExtractFrontMatter ( [ ] string { } )
fm , rest, e := parser . ExtractFrontMatter ( empty )
asrt . EqualValues (
asrt . EqualValues (
extractResult { map [ string ] string { } , nil } ,
extractResult { map [ string ] string { } , empty , nil } ,
extractResult { fm , e} ,
extractResult { fm , rest, e} ,
"parsing empty input yields unexpected result" ,
"parsing empty input yields unexpected result" ,
)
)
fm , e = parser . ExtractFrontMatter ( [ ] string { "# Content" , "..." } )
inp := [ ] string { "# Content" , "..." }
fm , rest , e = parser . ExtractFrontMatter ( inp )
asrt . EqualValues (
asrt . EqualValues (
extractResult { map [ string ] string { } , nil } ,
extractResult { map [ string ] string { } , inp , nil } ,
extractResult { fm , e} ,
extractResult { fm , rest, e} ,
"parsing empty input yields unexpected result" ,
"parsing empty input yields unexpected result" ,
)
)
fm , e = parser . ExtractFrontMatter ( [ ] string { "Key: Value" , "---" } )
fm , rest, e = parser . ExtractFrontMatter ( [ ] string { "Key: Value" , "---" } )
asrt . EqualValues (
asrt . EqualValues (
extractResult { map [ string ] string { "Key" : "Value" } , nil } ,
extractResult { map [ string ] string { "Key" : "Value" } , empty , nil } ,
extractResult { fm , e} ,
extractResult { fm , rest, e} ,
"parsing valid FrontMatter yields invalid result" ,
"parsing valid FrontMatter yields invalid result" ,
)
)
fm , e = parser . ExtractFrontMatter ( [ ] string { "Key: Value" , "---" , "# Content" , "Other content" } )
fm , rest, e = parser . ExtractFrontMatter ( [ ] string { "Key: Value" , "---" , "# Content" , "Other content" } )
asrt . EqualValues (
asrt . EqualValues (
extractResult { map [ string ] string { "Key" : "Value" } , nil } ,
extractResult { map [ string ] string { "Key" : "Value" } , [ ] string { "# Content" , "Other content" } , nil } ,
extractResult { fm , e} ,
extractResult { fm , rest, e} ,
"parsing valid FrontMatter yields invalid result" ,
"parsing valid FrontMatter yields invalid result" ,
)
)
fm , e = parser . ExtractFrontMatter ( [ ] string { "---" , "Key: Value" , "---" } )
fm , rest, e = parser . ExtractFrontMatter ( [ ] string { "---" , "Key: Value" , "---" } )
asrt . EqualValues (
asrt . EqualValues (
extractResult { map [ string ] string { "Key" : "Value" } , nil } ,
extractResult { map [ string ] string { "Key" : "Value" } , empty , nil } ,
extractResult { fm , e} ,
extractResult { fm , rest, e} ,
"parsing valid FrontMatter yields invalid result" ,
"parsing valid FrontMatter yields invalid result" ,
)
)
fm , e = parser . ExtractFrontMatter ( [ ] string { "Key: Value" , "Another Key: Another Value" , "---" } )
fm , rest, e = parser . ExtractFrontMatter ( [ ] string { "Key: Value" , "Another Key: Another Value" , "---" } )
asrt . EqualValues (
asrt . EqualValues (
extractResult { map [ string ] string { "Key" : "Value" , "Another Key" : "Another Value" } , nil } ,
extractResult { map [ string ] string { "Key" : "Value" , "Another Key" : "Another Value" } , empty , nil } ,
extractResult { fm , e} ,
extractResult { fm , rest, e} ,
"parsing valid FrontMatter yields invalid result" ,
"parsing valid FrontMatter yields invalid result" ,
)
)
}
}
func TestExtractFrontMatterWithBadKeys ( t * testing . T ) {
func TestExtractFrontMatterWithBadKeys ( t * testing . T ) {
asrt := assert . New ( t )
asrt := assert . New ( t )
empty := [ ] string { }
fm , e := parser . ExtractFrontMatter ( [ ] string { "---" , "Key Value" , "---" } )
fm , rest, e := parser . ExtractFrontMatter ( [ ] string { "---" , "Key Value" , "---" } )
asrt . EqualValues (
asrt . EqualValues (
extractResult { map [ string ] string { } , fmt . Errorf ( "error parsing line 2: %w" , parser . ErrInvalidKeyValuePair ) } ,
extractResult { map [ string ] string { } , [ ] string { "---" } , fmt . Errorf ( "error parsing line 2: %w" , parser . ErrInvalidKeyValuePair ) } ,
extractResult { fm , e} ,
extractResult { fm , rest, e} ,
"parsing invalid FrontMatter with no delimiter yields invalid result" ,
"parsing invalid FrontMatter with no delimiter yields invalid result" ,
)
)
fm , e = parser . ExtractFrontMatter ( [ ] string { "Key: Value" , ": Another Value" , "---" } )
fm , _, e = parser . ExtractFrontMatter ( [ ] string { "Key: Value" , ": Another Value" , "---" } )
asrt . EqualValues (
asrt . EqualValues (
extractResult { map [ string ] string { "Key" : "Value" } , fmt. Errorf ( "error parsing line 2: %w" , parser . ErrBlankKey ) } ,
extractResult { map [ string ] string { "Key" : "Value" } , empty, fmt. Errorf ( "error parsing line 2: %w" , parser . ErrBlankKey ) } ,
extractResult { fm , e } ,
extractResult { fm , e mpty, e } ,
"parsing invalid FrontMatter with blank key yields invalid result" ,
"parsing invalid FrontMatter with blank key yields invalid result" ,
)
)
fm , e = parser . ExtractFrontMatter ( [ ] string { "Key: Value" , "Key: Dupe Value" , "---" } )
fm , _, e = parser . ExtractFrontMatter ( [ ] string { "Key: Value" , "Key: Dupe Value" , "---" } )
asrt . EqualValues (
asrt . EqualValues (
extractResult { map [ string ] string { "Key" : "Value" } , fmt. Errorf ( "error on parsing line 2: %w" , parser . ErrDuplicateKey ) } ,
extractResult { map [ string ] string { "Key" : "Value" } , empty, fmt. Errorf ( "error on parsing line 2: %w" , parser . ErrDuplicateKey ) } ,
extractResult { fm , e } ,
extractResult { fm , e mpty, e } ,
"parsing invalid FrontMatter with duplicate key entry yields invalid result" ,
"parsing invalid FrontMatter with duplicate key entry yields invalid result" ,
)
)
fm , e = parser . ExtractFrontMatter ( [ ] string { "Key: Value" , "Another Key: Another Value" } )
fm , rest, e = parser . ExtractFrontMatter ( [ ] string { "Key: Value" , "Another Key: Another Value" } )
asrt . EqualValues (
asrt . EqualValues (
extractResult { map [ string ] string { "Key" : "Value" , "Another Key" : "Another Value" } , fmt . Errorf ( "error on parsing: %w" , parser . ErrEOF ) } ,
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 , e} ,
extractResult { fm , rest, e} ,
"parsing invalid FrontMatter with no final dashes yields invalid result" ,
"parsing invalid FrontMatter with no final dashes yields invalid result" ,
)
)
}
}