Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed action name in config #468

Merged
merged 2 commits into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
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)
}
})
}
}