Skip to content

Commit

Permalink
Change error to bool comparison
Browse files Browse the repository at this point in the history
The error message changes depending on the environment,
so change to bool comparison.
  • Loading branch information
noborus committed Dec 27, 2023
1 parent cd37bcd commit a2e0ad5
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 10 deletions.
16 changes: 7 additions & 9 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"io"
"os"
"strings"
"testing"
)

Expand All @@ -13,22 +12,22 @@ func Test_initConfig(t *testing.T) {
tests := []struct {
name string
cfgFile string
errStr string
wantErr bool
}{
{
name: "test-ov.yaml",
cfgFile: "ov.yaml",
errStr: "",
wantErr: false,
},
{
name: "test-ov-less.yaml",
cfgFile: "ov-less.yaml",
errStr: "",
wantErr: false,
},
{
name: "no-file.yaml",
cfgFile: "no-file.yaml",
errStr: "no such file or directory",
wantErr: true,
},
}
for _, tt := range tests {
Expand All @@ -53,11 +52,10 @@ func Test_initConfig(t *testing.T) {

// Now you can assert capturedStderr
// For example, check if it contains a specific error message
if !strings.Contains(capturedStderr, tt.errStr) {
t.Errorf("initConfig() error = %v, wantErr %v", capturedStderr, tt.errStr)
got := len(capturedStderr) > 0
if got != tt.wantErr {
t.Errorf("initConfig() error = %v, wantErr %v", capturedStderr, tt.wantErr)
}
//keyBind := oviewer.GetKeyBinds(config)
//log.Println(oviewer.KeyBindString(keyBind))
})
}
}
2 changes: 1 addition & 1 deletion oviewer/keybind.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ const (
actionSkipLines = "skip_lines"
actionTabWidth = "tabwidth"
actionGoLine = "goto"
actionSectionNum = "section_num"
actionSectionNum = "section_header_num"
actionNextSearch = "next_search"
actionNextBackSearch = "next_backsearch"
actionNextDoc = "next_doc"
Expand Down
53 changes: 53 additions & 0 deletions oviewer/oviewer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"

"github.com/gdamore/tcell/v2"
"github.com/spf13/viper"
)

const cwd = ".."
Expand Down Expand Up @@ -231,3 +232,55 @@ func Test_applyStyle(t *testing.T) {
})
}
}

func TestRoot_setKeyConfig(t *testing.T) {
tcellNewScreen = fakeScreen
defer func() {
tcellNewScreen = tcell.NewScreen
}()
tests := []struct {
name string
cfgFile string
want []string
wantErr bool
}{
{
name: "test-ov.yaml",
cfgFile: filepath.Join(cwd, "ov.yaml"),
want: []string{"Enter", "Down", "ctrl+N"},
wantErr: false,
},
{
name: "test-ov-less.yaml",
cfgFile: filepath.Join(cwd, "ov-less.yaml"),
want: []string{"e", "ctrl+e", "j", "J", "ctrl+j", "Enter", "Down"},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
root, err := Open(filepath.Join(testdata, "test.txt"))
if err != nil {
t.Fatalf("NewOviewer error = %v", err)
}
root.Screen = tcell.NewSimulationScreen("")
viper.SetConfigFile(tt.cfgFile)
var config Config
viper.AutomaticEnv() // read in environment variables that match
if err := viper.ReadInConfig(); err != nil {
t.Fatal("failed to read config file:", err)
}
viper.Unmarshal(&config)
root.SetConfig(config)
got, err := root.setKeyConfig()
if (err != nil) != tt.wantErr {
t.Errorf("Root.setKeyConfig() error = %v, wantErr %v", err, tt.wantErr)
return
}
action := "down"
if !reflect.DeepEqual(got[action], tt.want) {
t.Errorf("Root.setKeyConfig() = %v, want %v", got[action], tt.want)
}
})
}
}

0 comments on commit a2e0ad5

Please sign in to comment.