Skip to content

Commit

Permalink
improve mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
jleni committed Sep 2, 2024
1 parent 0d4e240 commit 585d9b1
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,5 @@ cmake-build-debug/
\.idea/workspace\.xml
\.idea/
vendor/

coverage.txt
18 changes: 18 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"go.testFlags": [
"-tags=ledger_mock",
"-v",
"-race",
"-coverprofile=coverage.txt",
"-covermode=atomic"
],
"go.coverOnSave": true,
"go.coverageOptions": "showUncoveredCodeOnly",
"go.coverageDecorator": {
"type": "gutter",
"coveredHighlightColor": "rgba(64,128,128,0.5)",
"uncoveredHighlightColor": "rgba(128,64,64,0.25)",
"coveredGutterStyle": "blockblue",
"uncoveredGutterStyle": "slashyellow"
}
}
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ lint:
golangci-lint run

test:
go test -tags ledger_mock -v -race ./...
go test -tags ledger_mock -v -race ./... -coverprofile=coverage.txt -covermode=atomic
2 changes: 1 addition & 1 deletion ledger_hid.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ var supportedLedgerProductID = map[uint16]int{
0x5: 0, // Ledger Nano S Plus
}

func NewLedgerAdmin() *LedgerAdminHID {
func NewLedgerAdmin() LedgerAdmin {
return &LedgerAdminHID{}
}

Expand Down
16 changes: 11 additions & 5 deletions ledger_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type LedgerDeviceMock struct {
commands map[string]string
}

func NewLedgerAdmin() *LedgerAdminMock {
func NewLedgerAdmin() LedgerAdmin {
return &LedgerAdminMock{}
}

Expand All @@ -44,15 +44,13 @@ func (admin *LedgerAdminMock) CountDevices() int {
return 1
}

func (admin *LedgerAdminMock) Connect(deviceIndex int) (*LedgerDeviceMock, error) {
func (admin *LedgerAdminMock) Connect(deviceIndex int) (LedgerDevice, error) {
return NewLedgerDeviceMock(), nil
}

func NewLedgerDeviceMock() *LedgerDeviceMock {
return &LedgerDeviceMock{
commands: map[string]string{
"e001000000": "311000040853706563756c6f73000b53706563756c6f734d4355",
},
commands: make(map[string]string),
}
}

Expand All @@ -64,6 +62,14 @@ func (ledger *LedgerDeviceMock) Exchange(command []byte) ([]byte, error) {
return nil, fmt.Errorf("unknown command: %s", hexCommand)
}

func (ledger *LedgerDeviceMock) SetCommandReplies(commands map[string]string) {
ledger.commands = commands
}

func (ledger *LedgerDeviceMock) ClearCommands() {
ledger.commands = make(map[string]string)
}

func (ledger *LedgerDeviceMock) Close() error {
// Nothing to do here
return nil
Expand Down
18 changes: 17 additions & 1 deletion ledger_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build ledger_mock
// +build ledger_mock

/*******************************************************************************
* (c) Zondax AG
*
Expand Down Expand Up @@ -96,6 +99,13 @@ func Test_BasicExchange(t *testing.T) {
}
defer ledger.Close()

// Set expected replies for the commands (only if using mock)
if mockLedger, ok := ledger.(*LedgerDeviceMock); ok {
mockLedger.SetCommandReplies(map[string]string{
"e001000000": "311000040853706563756c6f73000b53706563756c6f734d4355",
})
}

// Call device info (this should work in main menu and many apps)
message := []byte{0xE0, 0x01, 0, 0, 0}

Expand Down Expand Up @@ -132,11 +142,17 @@ func TestGetVersion(t *testing.T) {
}
defer ledger.Close()

// Set expected replies for the commands (only if using mock)
if mockLedger, ok := ledger.(*LedgerDeviceMock); ok {
mockLedger.SetCommandReplies(map[string]string{
"e001000000": "311000040853706563756c6f73000b53706563756c6f734d4355",
})
}

// Call device info (this should work in main menu and many apps)
message := []byte{0xE0, 0x01, 0, 0, 0}

response, err := ledger.Exchange(message)
assert.NoError(t, err)
assert.NotEmpty(t, response, "Response should not be empty")

}

0 comments on commit 585d9b1

Please sign in to comment.