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