Skip to content

Commit

Permalink
test layerFile
Browse files Browse the repository at this point in the history
  • Loading branch information
MetalBlueberry committed Nov 29, 2020
1 parent bd16e7d commit 89f6f7a
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 11 deletions.
9 changes: 8 additions & 1 deletion cmd/chkb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"log"
"net/http"
"os"
"os/user"
"path/filepath"
"strings"
"time"

Expand Down Expand Up @@ -51,7 +53,12 @@ func main() {
"base",
)

lf, err := layerFile.NewLayerFile(afero.NewOsFs(), kb, ".chkb_layout")
usr, err := user.Current()
if err != nil {
log.Fatal(err)
}

lf, err := layerFile.NewLayerFile(afero.NewOsFs(), kb, filepath.Join(usr.HomeDir, ".chkb_layout"))
if err != nil {
panic(err)
}
Expand Down
13 changes: 3 additions & 10 deletions pkg/deliverers/layerFile/layerFile.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ package layerFile
import (
"MetalBlueberry/cheap-keyboard/pkg/chkb"
"fmt"
"log"
"os/user"
"path/filepath"
"strings"

"github.com/spf13/afero"
Expand All @@ -22,12 +19,7 @@ func NewLayerFile(fs afero.Fs, kb *chkb.Mapper, fileName string) (*LayerFile, er
mapper: kb,
}

usr, err := user.Current()
if err != nil {
log.Fatal(err)
}

file, err := fs.Create(filepath.Join(usr.HomeDir, fileName))
file, err := fs.Create(fileName)
if err != nil {
return nil, err
}
Expand All @@ -41,7 +33,8 @@ func (lf *LayerFile) Deliver(event chkb.MapEvent) (handled bool, err error) {
switch event.Action {
case chkb.KbActionPushLayer, chkb.KbActionPopLayer:
str := layerString(lf.mapper)
fmt.Fprint(lf, str)
_, err := fmt.Fprint(lf, str)
return true, err
}
return false, nil
}
Expand Down
152 changes: 152 additions & 0 deletions pkg/deliverers/layerFile/layerFile_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package layerFile

import (
"MetalBlueberry/cheap-keyboard/pkg/chkb"
"io/ioutil"
"testing"

"github.com/spf13/afero"
)

func TestLayerFile_Deliver(t *testing.T) {
book := chkb.Book{
"l0": &chkb.Layer{},
"l1": &chkb.Layer{},
"l2": &chkb.Layer{},
}
mfs := afero.NewMemMapFs()

type fields struct {
File func() *LayerFile
}
type args struct {
event chkb.MapEvent
}
tests := []struct {
name string
fields fields
args args
wantHandled bool
wantErr bool
wantFileContent string
}{
{
name: "simple",
args: args{
event: chkb.MapEvent{
Action: chkb.KbActionPushLayer,
LayerName: "test",
},
},
fields: fields{
File: func() *LayerFile {
f, err := NewLayerFile(mfs, chkb.NewMapper(book, "l0"), "file")
if err != nil {
panic(err)
}
return f
},
},
wantErr: false,
wantHandled: true,
wantFileContent: `l0 >
`,
},
{
name: "two layers",
args: args{
event: chkb.MapEvent{
Action: chkb.KbActionPushLayer,
LayerName: "test",
},
},
fields: fields{
File: func() *LayerFile {
f, err := NewLayerFile(mfs, chkb.NewMapper(book, "l0"), "file")
if err != nil {
panic(err)
}
f.mapper.PushLayer("l2")
return f
},
},
wantErr: false,
wantHandled: true,
wantFileContent: `l0 > l2 >
`,
},
{
name: "handle pop",
args: args{
event: chkb.MapEvent{
Action: chkb.KbActionPopLayer,
LayerName: "test",
},
},
fields: fields{
File: func() *LayerFile {
f, err := NewLayerFile(mfs, chkb.NewMapper(book, "l0"), "file")
if err != nil {
panic(err)
}
f.mapper.PushLayer("l2")
return f
},
},
wantErr: false,
wantHandled: true,
wantFileContent: `l0 > l2 >
`,
},
{
name: "ignore event",
args: args{
event: chkb.MapEvent{
Action: chkb.KbActionDown,
LayerName: "test",
},
},
fields: fields{
File: func() *LayerFile {
f, err := NewLayerFile(mfs, chkb.NewMapper(book, "l0"), "file")
if err != nil {
panic(err)
}
return f
},
},
wantErr: false,
wantHandled: false,
wantFileContent: ``,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotHandled, err := tt.fields.File().Deliver(tt.args.event)
if (err != nil) != tt.wantErr {
t.Errorf("LayerFile.Deliver() error = %v, wantErr %v", err, tt.wantErr)
return
}

if gotHandled != tt.wantHandled {
t.Errorf("LayerFile.Deliver() = %v, want %v", gotHandled, tt.wantHandled)
return
}
f, err := mfs.Open("file")
if err != nil {
t.Errorf("Cannot open file %v", err)
return
}
defer f.Close()
content, err := ioutil.ReadAll(f)
if err != nil {
t.Errorf("Cannot read file %v", err)
return
}
if string(content) != tt.wantFileContent {
t.Errorf("LayerFile.Deliver() out content %v, want %v", string(content), tt.wantFileContent)
return
}
})
}
}

0 comments on commit 89f6f7a

Please sign in to comment.