Skip to content

Commit

Permalink
added tests for loop.go
Browse files Browse the repository at this point in the history
  • Loading branch information
TangiresH committed May 5, 2023
1 parent 78cb033 commit ec5ca86
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 0 deletions.
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ require (

require (
dmitri.shuralyov.com/gpu/mtl v0.0.0-20221208032759-85de2813cf6b // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4 // indirect
github.com/jezek/xgb v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/stretchr/testify v1.8.2 // indirect
golang.org/x/sys v0.5.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
17 changes: 17 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
dmitri.shuralyov.com/gpu/mtl v0.0.0-20221208032759-85de2813cf6b h1:a26Bdkl2B9PmYN6vGXnnfB2UGKjz0Moif1aEg+xTd7M=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20221208032759-85de2813cf6b/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4 h1:WtGNWLvXpe6ZudgnXrq0barxBImvnnJoMEhXAzcbM0I=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/jezek/xgb v1.0.0 h1:s2rRzAV8KQRlpsYA7Uyxoidv1nodMF0m6dIG6FhhVLQ=
github.com/jezek/xgb v1.0.0/go.mod h1:nrhwO0FX/enq75I7Y7G8iN1ubpSGZEiA3v9e9GyRFlk=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
Expand Down Expand Up @@ -58,3 +71,7 @@ golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
139 changes: 139 additions & 0 deletions test/loop_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package test

import (
"image"
"image/color"
"image/draw"
"testing"
"time"

"github.com/Illyakravchuk/lab_3/painter"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"golang.org/x/exp/shiny/screen"
)

type receiverMock struct {
mock.Mock
}

func (rm *receiverMock) Update(t screen.Texture) {
rm.Called(t)
}

type screenMock struct {
mock.Mock
}

func (sm *screenMock) NewBuffer(size image.Point) (screen.Buffer, error) {
return nil, nil
}

func (sm *screenMock) NewWindow(opts *screen.NewWindowOptions) (screen.Window, error) {
return nil, nil
}

func (sm *screenMock) NewTexture(size image.Point) (screen.Texture, error) {
args := sm.Called(size)
return args.Get(0).(screen.Texture), args.Error(1)
}

type textureMock struct {
mock.Mock
}

func (tm *textureMock) Release() {
tm.Called()
}

func (tm *textureMock) Upload(dp image.Point, src screen.Buffer, sr image.Rectangle) {
tm.Called(dp, src, sr)
}

func (tm *textureMock) Bounds() image.Rectangle {
args := tm.Called()
return args.Get(0).(image.Rectangle)
}

func (tm *textureMock) Fill(dr image.Rectangle, src color.Color, op draw.Op) {
tm.Called(dr, src, op)
}

func (tm *textureMock) Size() image.Point {
args := tm.Called()
return args.Get(0).(image.Point)
}

type operationMock struct {
mock.Mock
}

func (om *operationMock) Do(t screen.Texture) bool {
args := om.Called(t)
return args.Bool(0)
}

type operationQueueMock struct{}

func (m *operationQueueMock) Do(t screen.Texture) (ready bool) {
return false
}

func TestLoop_Post(t *testing.T) {
screenMock := new(screenMock)
textureMock := new(textureMock)
receiverMock := new(receiverMock)
tx := image.Pt(800, 800)
l := painter.Loop{
Receiver: receiverMock,
}

screenMock.On("NewTexture", tx).Return(textureMock, nil)
receiverMock.On("Update", textureMock).Return()

l.Start(screenMock)

op1Mock := new(operationMock)
op2Mock := new(operationMock)
op3Mock := new(operationMock)

textureMock.On("Bounds").Return(image.Rectangle{})
op1Mock.On("Do", textureMock).Return(false)
op2Mock.On("Do", textureMock).Return(true)
op3Mock.On("Do", textureMock).Return(true)

assert.Empty(t, l.Mq.Operations)
l.Post(op1Mock)
l.Post(op2Mock)
l.Post(op3Mock)
time.Sleep(1 * time.Second)
assert.Empty(t, l.Mq.Operations)

op1Mock.AssertCalled(t, "Do", textureMock)
op2Mock.AssertCalled(t, "Do", textureMock)
op3Mock.AssertCalled(t, "Do", textureMock)
receiverMock.AssertCalled(t, "Update", textureMock)
screenMock.AssertCalled(t, "NewTexture", image.Pt(800, 800))
}

func TestMessageQueue_Push(t *testing.T) {
mq := &painter.MessageQueue{}

op1Mock := &operationQueueMock{}
mq.Push(op1Mock)
if len(mq.Operations) != 1 {
t.Errorf("Expected 1 operation in the queue, but got %d", len(mq.Operations))
}

op2Mock := &operationQueueMock{}
mq.Push(op2Mock)
if len(mq.Operations) != 2 {
t.Errorf("Expected 2 operations in the queue, but got %d", len(mq.Operations))
}

op3Mock := &operationQueueMock{}
mq.Push(op3Mock)
if len(mq.Operations) != 3 {
t.Errorf("Expected 3 operations in the queue, but got %d", len(mq.Operations))
}
}

0 comments on commit ec5ca86

Please sign in to comment.