Skip to content

Commit

Permalink
✨ Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
peterjamesmatthews committed Nov 15, 2023
0 parents commit b1c3b97
Show file tree
Hide file tree
Showing 9 changed files with 193 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
coverage
7 changes: 7 additions & 0 deletions app/Application.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package app

import "pjm.dev/mock/calculator"

type Application struct {
Calculator calculator.Calculatorer
}
12 changes: 12 additions & 0 deletions app/CoolAlgorithm.go
Original file line number Diff line number Diff line change
@@ -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
}
45 changes: 45 additions & 0 deletions app/CoolAlgorithm_test.go
Original file line number Diff line number Diff line change
@@ -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()
}
}
8 changes: 8 additions & 0 deletions calculator/Calculatorer.go
Original file line number Diff line number Diff line change
@@ -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
}
19 changes: 19 additions & 0 deletions calculator/Client.go
Original file line number Diff line number Diff line change
@@ -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!")
}
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module pjm.dev/mock

go 1.21.1

require go.uber.org/mock v0.3.0
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
94 changes: 94 additions & 0 deletions mock_calculator/Client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b1c3b97

Please sign in to comment.