1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
package commonmark
import (
"fmt"
"testing"
)
func TestValidateConfig_Empty(t *testing.T) {
cfg := fillInDefaultConfig(&config{})
if cfg.HeadingStyle != "atx" {
t.Error("the config value was not filled with the default value")
}
err := validateConfig(&cfg)
if err != nil {
t.Errorf("expected no error but got %+v", err)
}
}
func TestValidateConfig_Success(t *testing.T) {
cfg := fillInDefaultConfig(&config{
HeadingStyle: "setext",
})
if cfg.HeadingStyle != "setext" {
t.Error("the config value was overridden")
}
err := validateConfig(&cfg)
if err != nil {
t.Errorf("expected no error but got %+v", err)
}
}
func TestValidateConfig_RandomValue(t *testing.T) {
cfg := fillInDefaultConfig(&config{
HeadingStyle: "random",
})
err := validateConfig(&cfg)
if err == nil {
t.Error("expected an error")
}
e, ok := err.(*ValidateConfigError)
if !ok {
t.Error("expected an error of type ValidateConfigError")
}
if e.Key != "HeadingStyle" {
t.Errorf("expected a different value for 'key' but got %q", e.Key)
}
if e.Value != "random" {
t.Errorf("expected a different value for 'actual' but got %q", e.Value)
}
formatted := err.Error()
if formatted != "invalid value for HeadingStyle:\"random\" must be one of \"atx\" or \"setext\"" {
t.Errorf("expected a different formatted message but got %q", formatted)
}
}
func TestValidateConfig_KeyWithValue(t *testing.T) {
cfg := fillInDefaultConfig(&config{
StrongDelimiter: "*",
})
err := validateConfig(&cfg)
if err == nil {
t.Error("expected an error")
}
e, ok := err.(*ValidateConfigError)
if !ok {
t.Fatal("expected an error of type ValidateConfigError")
}
// The default error message for the golang api
formatted1 := err.Error()
expected1 := `invalid value for StrongDelimiter:"*" must be exactly 2 characters of "**" or "__"`
if formatted1 != expected1 {
t.Errorf("expected a different formatted message but got %q", formatted1)
}
// The error message for the cli
if e.Key == "StrongDelimiter" {
e.KeyWithValue = fmt.Sprintf("--%s=%q", "strong_delimiter", e.Value)
}
formatted2 := err.Error()
expected2 := `invalid value for --strong_delimiter="*" must be exactly 2 characters of "**" or "__"`
if formatted2 != expected2 {
t.Errorf("expected a different formatted message but got %q", formatted2)
}
}
|