-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bd16e7d
commit 89f6f7a
Showing
3 changed files
with
163 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
}) | ||
} | ||
} |