Skip to content

Commit b1c3b97

Browse files
✨ Initial Commit
0 parents  commit b1c3b97

File tree

9 files changed

+193
-0
lines changed

9 files changed

+193
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
coverage

app/Application.go

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package app
2+
3+
import "pjm.dev/mock/calculator"
4+
5+
type Application struct {
6+
Calculator calculator.Calculatorer
7+
}

app/CoolAlgorithm.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package app
2+
3+
func (application *Application) CoolAlgorithm(x int) int {
4+
var output int = x
5+
6+
output = application.Calculator.Add(output, 0)
7+
output = application.Calculator.Subtract(output, 0)
8+
output = application.Calculator.Multiply(output, 1)
9+
output = application.Calculator.Divide(output, 1)
10+
11+
return output
12+
}

app/CoolAlgorithm_test.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package app_test
2+
3+
import (
4+
"testing"
5+
6+
"go.uber.org/mock/gomock"
7+
"pjm.dev/mock/app"
8+
"pjm.dev/mock/mock_calculator"
9+
)
10+
11+
func TestCoolAlgorithm(t *testing.T) {
12+
ctrl := gomock.NewController(t)
13+
mockCalculator := mock_calculator.NewMockCalculatorer(ctrl)
14+
15+
number := 100
16+
17+
mockCalculator.
18+
EXPECT().
19+
Add(number, 0).
20+
Times(1).
21+
Return(number)
22+
23+
mockCalculator.
24+
EXPECT().
25+
Subtract(number, 0).
26+
Times(1).
27+
Return(number)
28+
29+
mockCalculator.
30+
EXPECT().
31+
Multiply(number, 1).
32+
Times(1).
33+
Return(number)
34+
35+
mockCalculator.
36+
EXPECT().
37+
Divide(number, 1).
38+
Times(1).
39+
Return(number)
40+
41+
application := app.Application{Calculator: mockCalculator}
42+
if application.CoolAlgorithm(number) != number {
43+
t.Fail()
44+
}
45+
}

calculator/Calculatorer.go

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package calculator
2+
3+
type Calculatorer interface {
4+
Add(int, int) int
5+
Subtract(int, int) int
6+
Multiply(int, int) int
7+
Divide(int, int) int
8+
}

calculator/Client.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package calculator
2+
3+
type Client struct{}
4+
5+
func (client *Client) Add(a int32, b int32) int32 {
6+
panic("Can't call this in unit tests!")
7+
}
8+
9+
func (client *Client) Subtract(a int32, b int32) int32 {
10+
panic("Can't call this in unit tests!")
11+
}
12+
13+
func (client *Client) Multiply(a int32, b int32) int32 {
14+
panic("Can't call this in unit tests!")
15+
}
16+
17+
func (client *Client) Divide(a int32, b int32) int32 {
18+
panic("Can't call this in unit tests!")
19+
}

go.mod

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module pjm.dev/mock
2+
3+
go 1.21.1
4+
5+
require go.uber.org/mock v0.3.0

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
go.uber.org/mock v0.3.0 h1:3mUxI1No2/60yUYax92Pt8eNOEecx2D3lcXZh2NEZJo=
2+
go.uber.org/mock v0.3.0/go.mod h1:a6FSlNadKUHUa9IP5Vyt1zh4fC7uAwxMutEAscFbkZc=

mock_calculator/Client.go

+94
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)