-
-
Save danielnelson/79908f0bc7145d3feb7a91e0ef56d88c to your computer and use it in GitHub Desktop.
Mocking library that does not use interfaces
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
// main shows how to run with production mgr library | |
package main | |
import "github.com/influxdata/sandbox/cmd/mock/win_services" | |
func main() { | |
plugin := &win_services.WinServices{win_services.Connect} | |
plugin.Gather() | |
} |
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
// mgr is a standin for library code that cannot be modified. | |
package mgr | |
import "fmt" | |
func Connect() (*Mgr, error) { | |
return &Mgr{}, nil | |
} | |
type Mgr struct{} | |
func (m *Mgr) Disconnect() error { | |
fmt.Println("Mgr: disconnecting") | |
return nil | |
} | |
func (m *Mgr) OpenService(name string) (*Svc, error) { | |
fmt.Println("Mgr: opening service") | |
return &Svc{}, nil | |
} | |
type Svc struct{} | |
func (s *Svc) Close() error { | |
fmt.Println("Svc: closing") | |
return nil | |
} |
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
// win_services is the production code | |
package win_services | |
import "github.com/influxdata/sandbox/cmd/mock/mgr" | |
type Manager interface { | |
Disconnect() error | |
OpenService(name string) (Service, error) | |
} | |
type Service interface { | |
Close() error | |
} | |
type WinServices struct { | |
Connect func() (Manager, error) | |
} | |
func Connect() (Manager, error) { | |
manager, err := mgr.Connect() | |
if err != nil { | |
return nil, err | |
} | |
return &Mgr{manager}, nil | |
} | |
type Mgr struct { | |
manager *mgr.Mgr | |
} | |
type Svc struct { | |
service *mgr.Svc | |
} | |
func (m *Mgr) Disconnect() error { | |
return m.manager.Disconnect() | |
} | |
func (m *Mgr) OpenService(name string) (Service, error) { | |
return m.manager.OpenService(name) | |
} | |
func (s *Svc) Close() error { | |
return s.service.Close() | |
} | |
func (w *WinServices) Gather() error { | |
manager, err := w.Connect() | |
if err != nil { | |
return err | |
} | |
defer manager.Disconnect() | |
service, err := manager.OpenService("foo") | |
if err != nil { | |
return err | |
} | |
err = service.Close() | |
if err != nil { | |
return err | |
} | |
return nil | |
} |
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
// win_services_test tests win_services with mock mgr library | |
package win_services_test | |
import ( | |
"fmt" | |
"testing" | |
"github.com/influxdata/sandbox/cmd/mock/win_services" | |
"github.com/stretchr/testify/require" | |
) | |
type MockManager struct { | |
DisconnectF func() error | |
OpenServiceF func(name string) (win_services.Service, error) | |
} | |
type MockService struct { | |
CloseF func() error | |
} | |
func (m *MockManager) Disconnect() error { | |
fmt.Println("MockMgr: disconnecting") | |
return m.DisconnectF() | |
} | |
func (m *MockManager) OpenService(name string) (win_services.Service, error) { | |
fmt.Println("MockMgr: opening service") | |
return m.OpenServiceF(name) | |
} | |
func (s *MockService) Close() error { | |
fmt.Println("MockSvc: closing") | |
return s.CloseF() | |
} | |
func TestGather(t *testing.T) { | |
connect := func() (win_services.Manager, error) { | |
return &MockManager{ | |
DisconnectF: func() error { | |
return nil | |
}, | |
OpenServiceF: func(name string) (win_services.Service, error) { | |
return &MockService{ | |
CloseF: func() error { | |
return nil | |
}, | |
}, nil | |
}, | |
}, nil | |
} | |
plugin := &win_services.WinServices{connect} | |
err := plugin.Gather() | |
require.NoError(t, err) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment