diff --git a/main_test.go b/main_test.go new file mode 100644 index 00000000..ac0c93d4 --- /dev/null +++ b/main_test.go @@ -0,0 +1,61 @@ +package main + +import ( + "bytes" + "io" + "os" + "testing" +) + +func Test_initConfig(t *testing.T) { + t.Parallel() + tests := []struct { + name string + cfgFile string + wantErr bool + }{ + { + name: "test-ov.yaml", + cfgFile: "ov.yaml", + wantErr: false, + }, + { + name: "test-ov-less.yaml", + cfgFile: "ov-less.yaml", + wantErr: false, + }, + { + name: "no-file.yaml", + cfgFile: "no-file.yaml", + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + cfgFile = tt.cfgFile + // Backup original stderr + origStderr := os.Stderr + + // Create a buffer to capture stderr output + r, w, _ := os.Pipe() + os.Stderr = w + + initConfig() + w.Close() + // Restore original stderr + os.Stderr = origStderr + + // Read captured stderr output + var buf bytes.Buffer + io.Copy(&buf, r) + capturedStderr := buf.String() + + // Now you can assert capturedStderr + // For example, check if it contains a specific error message + got := len(capturedStderr) > 0 + if got != tt.wantErr { + t.Errorf("initConfig() error = %v, wantErr %v", capturedStderr, tt.wantErr) + } + }) + } +} diff --git a/oviewer/keybind.go b/oviewer/keybind.go index 8950371b..a91f1be0 100644 --- a/oviewer/keybind.go +++ b/oviewer/keybind.go @@ -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" diff --git a/oviewer/oviewer_test.go b/oviewer/oviewer_test.go index b831d790..24720315 100644 --- a/oviewer/oviewer_test.go +++ b/oviewer/oviewer_test.go @@ -8,6 +8,7 @@ import ( "testing" "github.com/gdamore/tcell/v2" + "github.com/spf13/viper" ) const cwd = ".." @@ -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) + } + }) + } +}