github.com/wtfutil/wtf@v0.43.0/app/module_validator_test.go (about) 1 package app 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/logrusorgru/aurora/v4" 8 "github.com/olebedev/config" 9 "github.com/stretchr/testify/assert" 10 "github.com/wtfutil/wtf/wtf" 11 ) 12 13 const ( 14 valid = ` 15 wtf: 16 mods: 17 clocks: 18 enabled: true 19 position: 20 top: 0 21 left: 0 22 height: 1 23 width: 1 24 refreshInterval: 30` 25 26 invalid = ` 27 wtf: 28 mods: 29 clocks: 30 enabled: true 31 position: 32 top: abc 33 left: 0 34 height: 1 35 width: 1 36 refreshInterval: 30` 37 ) 38 39 func Test_NewModuleValidator(t *testing.T) { 40 assert.IsType(t, &ModuleValidator{}, NewModuleValidator()) 41 } 42 43 func Test_validate(t *testing.T) { 44 tests := []struct { 45 name string 46 moduleName string 47 config *config.Config 48 expected []string 49 }{ 50 { 51 name: "valid config", 52 moduleName: "clocks", 53 config: func() *config.Config { 54 cfg, _ := config.ParseYaml(valid) 55 return cfg 56 }(), 57 expected: []string{}, 58 }, 59 { 60 name: "invalid config", 61 moduleName: "clocks", 62 config: func() *config.Config { 63 cfg, _ := config.ParseYaml(invalid) 64 return cfg 65 }(), 66 expected: []string{ 67 fmt.Sprintf("%s in %s configuration", aurora.Red("Errors"), aurora.Yellow("clocks.position")), 68 fmt.Sprintf( 69 " - Invalid value for %s: 0 %s strconv.ParseInt: parsing \"abc\": invalid syntax", 70 aurora.Yellow("top"), 71 aurora.Red("Error:"), 72 ), 73 }, 74 }, 75 } 76 77 for _, tt := range tests { 78 t.Run(tt.name, func(t *testing.T) { 79 widget := MakeWidget(nil, nil, tt.moduleName, tt.config, make(chan bool)) 80 81 if widget == nil { 82 t.Logf("Failed to create widget %s", tt.moduleName) 83 t.FailNow() 84 } 85 86 errs := validate([]wtf.Wtfable{widget}) 87 88 if len(tt.expected) == 0 { 89 assert.Empty(t, errs) 90 } else { 91 assert.NotEmpty(t, errs) 92 93 var actual []string 94 for _, err := range errs { 95 actual = append(actual, err.errorMessages()...) 96 } 97 98 assert.Equal(t, tt.expected, actual) 99 } 100 }) 101 } 102 }