Skip to content

Commit

Permalink
Merge pull request #468 from noborus/add-config-test
Browse files Browse the repository at this point in the history
Fixed action name in config
  • Loading branch information
noborus authored Dec 27, 2023
2 parents 81f4de1 + a2e0ad5 commit 008a8d5
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 1 deletion.
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)
}
})
}
}

0 comments on commit 008a8d5

Please sign in to comment.