diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4ebc8ae --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +coverage diff --git a/app/Application.go b/app/Application.go new file mode 100644 index 0000000..7e040e7 --- /dev/null +++ b/app/Application.go @@ -0,0 +1,7 @@ +package app + +import "pjm.dev/mock/calculator" + +type Application struct { + Calculator calculator.Calculatorer +} diff --git a/app/CoolAlgorithm.go b/app/CoolAlgorithm.go new file mode 100644 index 0000000..d5026c6 --- /dev/null +++ b/app/CoolAlgorithm.go @@ -0,0 +1,12 @@ +package app + +func (application *Application) CoolAlgorithm(x int) int { + var output int = x + + output = application.Calculator.Add(output, 0) + output = application.Calculator.Subtract(output, 0) + output = application.Calculator.Multiply(output, 1) + output = application.Calculator.Divide(output, 1) + + return output +} diff --git a/app/CoolAlgorithm_test.go b/app/CoolAlgorithm_test.go new file mode 100644 index 0000000..ca7b78c --- /dev/null +++ b/app/CoolAlgorithm_test.go @@ -0,0 +1,45 @@ +package app_test + +import ( + "testing" + + "go.uber.org/mock/gomock" + "pjm.dev/mock/app" + "pjm.dev/mock/mock_calculator" +) + +func TestCoolAlgorithm(t *testing.T) { + ctrl := gomock.NewController(t) + mockCalculator := mock_calculator.NewMockCalculatorer(ctrl) + + number := 100 + + mockCalculator. + EXPECT(). + Add(number, 0). + Times(1). + Return(number) + + mockCalculator. + EXPECT(). + Subtract(number, 0). + Times(1). + Return(number) + + mockCalculator. + EXPECT(). + Multiply(number, 1). + Times(1). + Return(number) + + mockCalculator. + EXPECT(). + Divide(number, 1). + Times(1). + Return(number) + + application := app.Application{Calculator: mockCalculator} + if application.CoolAlgorithm(number) != number { + t.Fail() + } +} diff --git a/calculator/Calculatorer.go b/calculator/Calculatorer.go new file mode 100644 index 0000000..9ca8ab1 --- /dev/null +++ b/calculator/Calculatorer.go @@ -0,0 +1,8 @@ +package calculator + +type Calculatorer interface { + Add(int, int) int + Subtract(int, int) int + Multiply(int, int) int + Divide(int, int) int +} diff --git a/calculator/Client.go b/calculator/Client.go new file mode 100644 index 0000000..6aaa666 --- /dev/null +++ b/calculator/Client.go @@ -0,0 +1,19 @@ +package calculator + +type Client struct{} + +func (client *Client) Add(a int32, b int32) int32 { + panic("Can't call this in unit tests!") +} + +func (client *Client) Subtract(a int32, b int32) int32 { + panic("Can't call this in unit tests!") +} + +func (client *Client) Multiply(a int32, b int32) int32 { + panic("Can't call this in unit tests!") +} + +func (client *Client) Divide(a int32, b int32) int32 { + panic("Can't call this in unit tests!") +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..0ccf459 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module pjm.dev/mock + +go 1.21.1 + +require go.uber.org/mock v0.3.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..fd2f035 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +go.uber.org/mock v0.3.0 h1:3mUxI1No2/60yUYax92Pt8eNOEecx2D3lcXZh2NEZJo= +go.uber.org/mock v0.3.0/go.mod h1:a6FSlNadKUHUa9IP5Vyt1zh4fC7uAwxMutEAscFbkZc= diff --git a/mock_calculator/Client.go b/mock_calculator/Client.go new file mode 100644 index 0000000..c34526e --- /dev/null +++ b/mock_calculator/Client.go @@ -0,0 +1,94 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: ./calculator/Calculatorer.go +// +// Generated by this command: +// +// mockgen -source=./calculator/Calculatorer.go -destination=./mock_calculator/Client.go +// +// Package mock_calculator is a generated GoMock package. +package mock_calculator + +import ( + reflect "reflect" + + gomock "go.uber.org/mock/gomock" +) + +// MockCalculatorer is a mock of Calculatorer interface. +type MockCalculatorer struct { + ctrl *gomock.Controller + recorder *MockCalculatorerMockRecorder +} + +// MockCalculatorerMockRecorder is the mock recorder for MockCalculatorer. +type MockCalculatorerMockRecorder struct { + mock *MockCalculatorer +} + +// NewMockCalculatorer creates a new mock instance. +func NewMockCalculatorer(ctrl *gomock.Controller) *MockCalculatorer { + mock := &MockCalculatorer{ctrl: ctrl} + mock.recorder = &MockCalculatorerMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockCalculatorer) EXPECT() *MockCalculatorerMockRecorder { + return m.recorder +} + +// Add mocks base method. +func (m *MockCalculatorer) Add(arg0, arg1 int) int { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Add", arg0, arg1) + ret0, _ := ret[0].(int) + return ret0 +} + +// Add indicates an expected call of Add. +func (mr *MockCalculatorerMockRecorder) Add(arg0, arg1 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Add", reflect.TypeOf((*MockCalculatorer)(nil).Add), arg0, arg1) +} + +// Divide mocks base method. +func (m *MockCalculatorer) Divide(arg0, arg1 int) int { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Divide", arg0, arg1) + ret0, _ := ret[0].(int) + return ret0 +} + +// Divide indicates an expected call of Divide. +func (mr *MockCalculatorerMockRecorder) Divide(arg0, arg1 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Divide", reflect.TypeOf((*MockCalculatorer)(nil).Divide), arg0, arg1) +} + +// Multiply mocks base method. +func (m *MockCalculatorer) Multiply(arg0, arg1 int) int { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Multiply", arg0, arg1) + ret0, _ := ret[0].(int) + return ret0 +} + +// Multiply indicates an expected call of Multiply. +func (mr *MockCalculatorerMockRecorder) Multiply(arg0, arg1 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Multiply", reflect.TypeOf((*MockCalculatorer)(nil).Multiply), arg0, arg1) +} + +// Subtract mocks base method. +func (m *MockCalculatorer) Subtract(arg0, arg1 int) int { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Subtract", arg0, arg1) + ret0, _ := ret[0].(int) + return ret0 +} + +// Subtract indicates an expected call of Subtract. +func (mr *MockCalculatorerMockRecorder) Subtract(arg0, arg1 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Subtract", reflect.TypeOf((*MockCalculatorer)(nil).Subtract), arg0, arg1) +}