From cd37bcd32fc0726771b41d9de3d9e5448e4f6190 Mon Sep 17 00:00:00 2001 From: Noboru Saito Date: Wed, 27 Dec 2023 13:34:01 +0900 Subject: [PATCH 1/2] Fixed action name in config Fixed the keybind action name to section_header_num. Added configuration yaml test. Added test for names in config. --- main_test.go | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 main_test.go diff --git a/main_test.go b/main_test.go new file mode 100644 index 00000000..97b5a875 --- /dev/null +++ b/main_test.go @@ -0,0 +1,63 @@ +package main + +import ( + "bytes" + "io" + "os" + "strings" + "testing" +) + +func Test_initConfig(t *testing.T) { + t.Parallel() + tests := []struct { + name string + cfgFile string + errStr string + }{ + { + name: "test-ov.yaml", + cfgFile: "ov.yaml", + errStr: "", + }, + { + name: "test-ov-less.yaml", + cfgFile: "ov-less.yaml", + errStr: "", + }, + { + name: "no-file.yaml", + cfgFile: "no-file.yaml", + errStr: "no such file or directory", + }, + } + 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 + if !strings.Contains(capturedStderr, tt.errStr) { + t.Errorf("initConfig() error = %v, wantErr %v", capturedStderr, tt.errStr) + } + //keyBind := oviewer.GetKeyBinds(config) + //log.Println(oviewer.KeyBindString(keyBind)) + }) + } +} From a2e0ad51dd650c60db64718589a3722ecc628fa3 Mon Sep 17 00:00:00 2001 From: Noboru Saito Date: Wed, 27 Dec 2023 13:42:36 +0900 Subject: [PATCH 2/2] Change error to bool comparison The error message changes depending on the environment, so change to bool comparison. --- main_test.go | 16 ++++++------- oviewer/keybind.go | 2 +- oviewer/oviewer_test.go | 53 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 10 deletions(-) diff --git a/main_test.go b/main_test.go index 97b5a875..ac0c93d4 100644 --- a/main_test.go +++ b/main_test.go @@ -4,7 +4,6 @@ import ( "bytes" "io" "os" - "strings" "testing" ) @@ -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 { @@ -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)) }) } } 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) + } + }) + } +}