Skip to content

Commit

Permalink
added testify and build a better unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
deemount committed Mar 18, 2024
1 parent 2fc99d2 commit 45eb2a8
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 51 deletions.
19 changes: 2 additions & 17 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,31 +1,16 @@
{
// Verwendet IntelliSense zum Ermitteln möglicher Attribute.
// Zeigen Sie auf vorhandene Attribute, um die zugehörigen Beschreibungen anzuzeigen.
// Weitere Informationen finden Sie unter https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "TestInject",
"name": "TestInjectConfig",
"type": "go",
"request": "launch",
"mode": "test",
"program": "${workspaceFolder}",
"showLog": true,
"args": [
"-test.v",
"^TestInject$"
]
},
{
"name": "TestEmbeddedInject",
"type": "go",
"request": "launch",
"mode": "test",
"program": "${workspaceFolder}",
"showLog": true,
"args": [
"-test.v",
"^TestEmbeddedInject$"
"^TestInjectConfig$"
]
}
]
Expand Down
9 changes: 8 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ go 1.21.7
require (
github.com/deemount/gobpmnModels v0.0.0-20240316085912-7831ff39ffa4
github.com/deemount/gobpmnReflection v0.0.0-20240315112013-036495e4c08d
github.com/stretchr/testify v1.9.0
)

require github.com/deemount/gobpmnTypes v0.0.0-20240315111519-43046016ad9f // indirect
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/deemount/gobpmnTypes v0.0.0-20240315111519-43046016ad9f // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.5.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
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/deemount/gobpmnModels v0.0.0-20240316085912-7831ff39ffa4 h1:xAqg4ZIeMDxME0O8O4UpwpcS3AeXB+3IspauNX2G2AA=
github.com/deemount/gobpmnModels v0.0.0-20240316085912-7831ff39ffa4/go.mod h1:bv2zkkqCzzaGV5gYP3gDQfMXuqjxMBjSl9GjHFo2mY8=
github.com/deemount/gobpmnReflection v0.0.0-20240315112013-036495e4c08d h1:yQY4ikw/HuvVlPu3hGc0X8gntQnSbLqMnUw/nHRRCKU=
github.com/deemount/gobpmnReflection v0.0.0-20240315112013-036495e4c08d/go.mod h1:j8WCmdzEkTq/FVsWJAdoPtjTiRryLZp/WjOLcIqtt5c=
github.com/deemount/gobpmnTypes v0.0.0-20240315111519-43046016ad9f h1:0gux6KtY8XgrN/L9tiZ7B+wx3th7x4hBYu1+wtUkHYY=
github.com/deemount/gobpmnTypes v0.0.0-20240315111519-43046016ad9f/go.mod h1:Dw7Jo8/vr0DzNHNMnbwQu0JSMWm2klNdo00rcxHCzJs=
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.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
106 changes: 73 additions & 33 deletions inject_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,70 +3,110 @@ package gobpmn_hash_test
import (
"testing"

"github.com/stretchr/testify/assert"

gobpmn_hash "github.com/deemount/gobpmnHash"
"github.com/deemount/gobpmnModels/pkg/core"
)

type Injection struct {
Suffix string
}
/*
type TestProcess struct {
Def core.DefinitionsRepository
IsExecutable bool
Process gobpmn_hash.Injection
StartEvent gobpmn_hash.Injection
FromStartEvent gobpmn_hash.Injection
Task gobpmn_hash.Injection
FromTask gobpmn_hash.Injection
EndEvent gobpmn_hash.Injection
}
Note:
Test Process and TestEmbeddedProcess are two structs copied from the
gobpmnByExample package.
*/

// TestEmbeddedProcess is a struct that contains fields of type
// core.DefinitionsRepository and two anonymous embedded structs.
// It is a copy of the gobpmnByExample/example/01 package.
type TestEmbeddedProcess struct {
Def core.DefinitionsRepository
TestEmbeddedPool
TestEmbeddedTenant
}

// TestEmbeddedPool is a struct that contains fields of type
// gobpmn_hash.Injection and bool.
// It is embedded in the TestEmbeddedProcess struct.
type TestEmbeddedPool struct {
XYZIsExecutable bool
XYZProcess gobpmn_hash.Injection
}

// TestEmbeddedTenant is a struct that contains a field of type
// gobpmn_hash.Injection.
// It is embedded in the TestEmbeddedProcess struct.
type TestEmbeddedTenant struct {
XYZStartEvent gobpmn_hash.Injection
}

func TestInject(t *testing.T) {
t.Log("TestInjectEmbedded")
var hash gobpmn_hash.Injection
p := hash.Inject(TestProcess{}).(TestProcess)
p.Def = core.NewDefinitions()
t.Logf("p: %#+v", p)
// TestProcess is a struct that contains fields of type
// core.DefinitionsRepository, gobpmn_hash.Injection, and bool.
// It is a copy of the gobpmnByExample/example/02 package.
type TestProcess struct {
Def core.DefinitionsRepository
IsExecutable bool
Process gobpmn_hash.Injection
StartEvent gobpmn_hash.Injection
FromStartEvent gobpmn_hash.Injection
Task gobpmn_hash.Injection
FromTask gobpmn_hash.Injection
EndEvent gobpmn_hash.Injection
}

func TestInjectEmbedded(t *testing.T) {
t.Log("TestInjectEmbedded")
var hash gobpmn_hash.Injection
p := hash.Inject(TestEmbeddedProcess{}).(TestEmbeddedProcess)
p.Def = core.NewDefinitions()
t.Logf("p: %#+v", p)
// TestInjectConfig tests the injection of a bool type
// into the field IsExecutable of the struct TestProcess
// and TestEmbeddedPool.
// The field IsExecutable is set to false as default, but
// if reflected the field is set to true.
// In gobpmnHash, a Config consists of a reflected struct field
// which has a bool type.
// This test should pass if the field IsExecutable is expected
// set to true or false
func TestInjectConfig(t *testing.T) {
t.Run("p(TestProcess{}).IsExecutable expected true",
func(t *testing.T) {
hash := new(gobpmn_hash.Injection)
p := hash.Inject(TestProcess{}).(TestProcess)
if assert.NotNil(t, p) {
assert.Equal(t, bool(true), p.IsExecutable)
}
})
t.Run("p(TestProcess{}).IsExecutable expected false",
func(t *testing.T) {
hash := new(gobpmn_hash.Injection)
p := hash.Inject(TestProcess{}).(TestProcess)
if assert.NotNil(t, p) {
assert.NotEqual(t, bool(false), p.IsExecutable)
}
})
t.Run("p(TestEmbeddedProcess{}).IsExecutable expected true", func(t *testing.T) {
hash := new(gobpmn_hash.Injection)
p := hash.Inject(TestEmbeddedProcess{}).(TestEmbeddedProcess)
if assert.NotNil(t, p) {
assert.Equal(t, bool(true), p.XYZIsExecutable)
}
})
t.Run("p(TestEmbeddedProcess{}).IsExecutable expected false", func(t *testing.T) {
hash := new(gobpmn_hash.Injection)
p := hash.Inject(TestEmbeddedProcess{}).(TestEmbeddedProcess)
if assert.NotNil(t, p) {
assert.NotEqual(t, bool(false), p.XYZIsExecutable)
}
})
}

func BenchmarkInject(b *testing.B) {
var hash gobpmn_hash.Injection
for n := 0; n < b.N; n++ {
p := hash.Inject(TestProcess{}).(TestProcess)
p.Def = core.NewDefinitions()
b.Logf("p: %#+v", p)
hash := new(gobpmn_hash.Injection)
_ = hash.Inject(TestProcess{}).(TestProcess)
}
}

func BenchmarkInjectEmbedded(b *testing.B) {
var hash gobpmn_hash.Injection
for n := 0; n < b.N; n++ {
p := hash.Inject(TestEmbeddedProcess{}).(TestEmbeddedProcess)
p.Def = core.NewDefinitions()
b.Logf("p: %#+v", p)
hash := new(gobpmn_hash.Injection)
_ = hash.Inject(TestEmbeddedProcess{}).(TestEmbeddedProcess)
}
}

0 comments on commit 45eb2a8

Please sign in to comment.