2020-11-27 14:56:31 +07:00
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
2020-11-28 04:28:30 +07:00
Content [ ] string
2020-11-27 14:56:31 +07:00
Error error
}
func TestExtractFrontMatterWithValidContent ( t * testing . T ) {
asrt := assert . New ( t )
2020-11-28 04:28:30 +07:00
empty := [ ] string { }
2020-11-27 14:56:31 +07:00
2020-11-28 04:28:30 +07:00
fm , rest , e := parser . ExtractFrontMatter ( empty )
2020-11-27 14:56:31 +07:00
asrt . EqualValues (
2020-11-28 04:28:30 +07:00
extractResult { map [ string ] string { } , empty , nil } ,
extractResult { fm , rest , e } ,
2020-11-27 14:56:31 +07:00
"parsing empty input yields unexpected result" ,
)
2020-11-28 04:28:30 +07:00
inp := [ ] string { "# Content" , "..." }
fm , rest , e = parser . ExtractFrontMatter ( inp )
2020-11-27 14:56:31 +07:00
asrt . EqualValues (
2020-11-28 04:28:30 +07:00
extractResult { map [ string ] string { } , inp , nil } ,
extractResult { fm , rest , e } ,
2020-11-27 14:56:31 +07:00
"parsing empty input yields unexpected result" ,
)
2020-11-28 04:28:30 +07:00
fm , rest , e = parser . ExtractFrontMatter ( [ ] string { "Key: Value" , "---" } )
2020-11-27 14:56:31 +07:00
asrt . EqualValues (
2020-11-28 04:28:30 +07:00
extractResult { map [ string ] string { "Key" : "Value" } , empty , nil } ,
extractResult { fm , rest , e } ,
2020-11-27 14:56:31 +07:00
"parsing valid FrontMatter yields invalid result" ,
)
2020-11-28 04:28:30 +07:00
fm , rest , e = parser . ExtractFrontMatter ( [ ] string { "Key: Value" , "---" , "# Content" , "Other content" } )
2020-11-27 14:56:31 +07:00
asrt . EqualValues (
2020-11-28 04:28:30 +07:00
extractResult { map [ string ] string { "Key" : "Value" } , [ ] string { "# Content" , "Other content" } , nil } ,
extractResult { fm , rest , e } ,
2020-11-27 14:56:31 +07:00
"parsing valid FrontMatter yields invalid result" ,
)
2020-11-28 04:28:30 +07:00
fm , rest , e = parser . ExtractFrontMatter ( [ ] string { "---" , "Key: Value" , "---" } )
2020-11-27 14:56:31 +07:00
asrt . EqualValues (
2020-11-28 04:28:30 +07:00
extractResult { map [ string ] string { "Key" : "Value" } , empty , nil } ,
extractResult { fm , rest , e } ,
2020-11-27 14:56:31 +07:00
"parsing valid FrontMatter yields invalid result" ,
)
2020-11-28 04:28:30 +07:00
fm , rest , e = parser . ExtractFrontMatter ( [ ] string { "Key: Value" , "Another Key: Another Value" , "---" } )
2020-11-27 14:56:31 +07:00
asrt . EqualValues (
2020-11-28 04:28:30 +07:00
extractResult { map [ string ] string { "Key" : "Value" , "Another Key" : "Another Value" } , empty , nil } ,
extractResult { fm , rest , e } ,
2020-11-27 14:56:31 +07:00
"parsing valid FrontMatter yields invalid result" ,
)
2020-11-28 04:34:54 +07:00
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" ,
)
2020-11-27 14:56:31 +07:00
}
func TestExtractFrontMatterWithBadKeys ( t * testing . T ) {
asrt := assert . New ( t )
2020-11-28 04:28:30 +07:00
empty := [ ] string { }
2020-11-27 14:56:31 +07:00
2020-11-28 04:28:30 +07:00
fm , rest , e := parser . ExtractFrontMatter ( [ ] string { "---" , "Key Value" , "---" } )
2020-11-27 14:56:31 +07:00
asrt . EqualValues (
2020-11-28 04:28:30 +07:00
extractResult { map [ string ] string { } , [ ] string { "---" } , fmt . Errorf ( "error parsing line 2: %w" , parser . ErrInvalidKeyValuePair ) } ,
extractResult { fm , rest , e } ,
2020-11-27 14:56:31 +07:00
"parsing invalid FrontMatter with no delimiter yields invalid result" ,
)
2020-11-28 04:28:30 +07:00
fm , _ , e = parser . ExtractFrontMatter ( [ ] string { "Key: Value" , ": Another Value" , "---" } )
2020-11-27 14:56:31 +07:00
asrt . EqualValues (
2020-11-28 04:28:30 +07:00
extractResult { map [ string ] string { "Key" : "Value" } , empty , fmt . Errorf ( "error parsing line 2: %w" , parser . ErrBlankKey ) } ,
extractResult { fm , empty , e } ,
2020-11-27 14:56:31 +07:00
"parsing invalid FrontMatter with blank key yields invalid result" ,
)
2020-11-28 04:28:30 +07:00
fm , _ , e = parser . ExtractFrontMatter ( [ ] string { "Key: Value" , "Key: Dupe Value" , "---" } )
2020-11-27 14:56:31 +07:00
asrt . EqualValues (
2020-11-28 04:28:30 +07:00
extractResult { map [ string ] string { "Key" : "Value" } , empty , fmt . Errorf ( "error on parsing line 2: %w" , parser . ErrDuplicateKey ) } ,
extractResult { fm , empty , e } ,
2020-11-27 14:56:31 +07:00
"parsing invalid FrontMatter with duplicate key entry yields invalid result" ,
2020-11-27 14:56:31 +07:00
)
2020-11-28 04:28:30 +07:00
fm , rest , e = parser . ExtractFrontMatter ( [ ] string { "Key: Value" , "Another Key: Another Value" } )
2020-11-27 14:56:31 +07:00
asrt . EqualValues (
2020-11-28 04:28:30 +07:00
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 } ,
2020-11-27 14:56:31 +07:00
"parsing invalid FrontMatter with no final dashes yields invalid result" ,
)
}