Skip to content

Commit

Permalink
feat: cleaned up code (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
tkrop committed Oct 13, 2022
1 parent 0b89729 commit f94be2f
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 192 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
build
run

mock_*_test.go
99 changes: 0 additions & 99 deletions build/test-unit.cover

This file was deleted.

60 changes: 0 additions & 60 deletions mock/mock_iface_test.go

This file was deleted.

36 changes: 18 additions & 18 deletions mock/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ func MockValidate(
}
}

func SetupPermTestABC(mocks *mock.Mocks) *perm.PermTest {
func SetupPermTestABC(mocks *mock.Mocks) *perm.Test {
iface := mock.Get(mocks, mock.NewMockIFace)
return perm.NewPermTest(mocks,
return perm.NewTest(mocks,
perm.TestMap{
"a": func(t *test.TestingT) { iface.CallA("a") },
"b1": func(t *test.TestingT) {
Expand All @@ -82,9 +82,9 @@ func SetupPermTestABC(mocks *mock.Mocks) *perm.PermTest {

}

func SetupPermTestABCD(mocks *mock.Mocks) *perm.PermTest {
func SetupPermTestABCD(mocks *mock.Mocks) *perm.Test {
iface := mock.Get(mocks, mock.NewMockIFace)
return perm.NewPermTest(mocks,
return perm.NewTest(mocks,
perm.TestMap{
"a": func(t *test.TestingT) { iface.CallA("a") },
"b": func(t *test.TestingT) { iface.CallA("b") },
Expand All @@ -98,9 +98,9 @@ func SetupPermTestABCD(mocks *mock.Mocks) *perm.PermTest {

}

func SetupPermTestABCDEF(mocks *mock.Mocks) *perm.PermTest {
func SetupPermTestABCDEF(mocks *mock.Mocks) *perm.Test {
iface := mock.Get(mocks, mock.NewMockIFace)
return perm.NewPermTest(mocks,
return perm.NewTest(mocks,
perm.TestMap{
"a": func(t *test.TestingT) { iface.CallA("a") },
"b": func(t *test.TestingT) { iface.CallA("b") },
Expand All @@ -116,7 +116,7 @@ func SetupPermTestABCDEF(mocks *mock.Mocks) *perm.PermTest {

}

var testSetupParams = perm.PermMap{
var testSetupParams = perm.ExpectMap{
"b2-b1-a-c": test.ExpectFailure,
"b2-b1-c-a": test.ExpectFailure,
"b2-c-b1-a": test.ExpectFailure,
Expand All @@ -132,7 +132,7 @@ var testSetupParams = perm.PermMap{
}

func TestSetup(t *testing.T) {
perms := perm.PermRemain(testSetupParams, test.ExpectSuccess)
perms := perm.Remain(testSetupParams, test.ExpectSuccess)
for message, expect := range perms {
t.Run(message, test.Run(expect, func(t *test.TestingT) {
require.NotEmpty(t, message)
Expand All @@ -158,12 +158,12 @@ func TestSetup(t *testing.T) {
}
}

var testChainParams = perm.PermMap{
var testChainParams = perm.ExpectMap{
"a-b1-b2-c": test.ExpectSuccess,
}

func TestChain(t *testing.T) {
perms := perm.PermRemain(testChainParams, test.ExpectFailure)
perms := perm.Remain(testChainParams, test.ExpectFailure)
for message, expect := range perms {
t.Run(message, test.Run(expect, func(t *test.TestingT) {
require.NotEmpty(t, message)
Expand All @@ -189,7 +189,7 @@ func TestChain(t *testing.T) {
}
}

var testSetupChainParams = perm.PermMap{
var testSetupChainParams = perm.ExpectMap{
"a-b-c-d": test.ExpectSuccess,
"a-c-b-d": test.ExpectSuccess,
"a-c-d-b": test.ExpectSuccess,
Expand All @@ -199,7 +199,7 @@ var testSetupChainParams = perm.PermMap{
}

func TestSetupChain(t *testing.T) {
perms := perm.PermRemain(testSetupChainParams, test.ExpectFailure)
perms := perm.Remain(testSetupChainParams, test.ExpectFailure)
for message, expect := range perms {
t.Run(message, test.Run(expect, func(t *test.TestingT) {
require.NotEmpty(t, message)
Expand Down Expand Up @@ -230,7 +230,7 @@ func TestSetupChain(t *testing.T) {
}

func TestChainSetup(t *testing.T) {
perms := perm.PermRemain(testSetupChainParams, test.ExpectFailure)
perms := perm.Remain(testSetupChainParams, test.ExpectFailure)
for message, expect := range perms {
t.Run(message, test.Run(expect, func(t *test.TestingT) {
require.NotEmpty(t, message)
Expand Down Expand Up @@ -260,7 +260,7 @@ func TestChainSetup(t *testing.T) {
}
}

var testParallelChainParams = perm.PermMap{
var testParallelChainParams = perm.ExpectMap{
"a-b-c-d-e-f": test.ExpectSuccess,
"a-b-c-e-d-f": test.ExpectSuccess,
"a-b-e-c-d-f": test.ExpectSuccess,
Expand All @@ -276,7 +276,7 @@ var testParallelChainParams = perm.PermMap{
}

func TestParallelChain(t *testing.T) {
perms := perm.PermRemain(testParallelChainParams, test.ExpectFailure)
perms := perm.Remain(testParallelChainParams, test.ExpectFailure)
for message, expect := range perms {
t.Run(message, test.Run(expect, func(t *test.TestingT) {
require.NotEmpty(t, message)
Expand Down Expand Up @@ -308,7 +308,7 @@ func TestParallelChain(t *testing.T) {
}
}

var testChainSubParams = perm.PermMap{
var testChainSubParams = perm.ExpectMap{
"a-b-c-d-e-f": test.ExpectSuccess,
"a-c-b-d-e-f": test.ExpectSuccess,
"a-c-d-b-e-f": test.ExpectSuccess,
Expand Down Expand Up @@ -359,7 +359,7 @@ func TestChainSub(t *testing.T) {
}
}

var testDetachParams = perm.PermMap{
var testDetachParams = perm.ExpectMap{
"a-b-c-d": test.ExpectSuccess,
"a-b-d-c": test.ExpectSuccess,
"a-d-b-c": test.ExpectSuccess,
Expand All @@ -371,7 +371,7 @@ var testDetachParams = perm.PermMap{
}

func TestDetach(t *testing.T) {
perms := perm.PermRemain(testDetachParams, test.ExpectFailure)
perms := perm.Remain(testDetachParams, test.ExpectFailure)
for message, expect := range perms {
t.Run(message, test.Run(expect, func(t *test.TestingT) {
require.NotEmpty(t, message)
Expand Down
35 changes: 20 additions & 15 deletions perm/perm.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,31 @@ import (
"github.com/tkrop/testing/test"
)

// ExpectMap defines a map of permutation tests that are expected to either
// fail or succeed to succeed depending on expressed expectation.
type ExpectMap map[string]test.Expect

// TestMap defines a map of test functions that is subject of the actual
// permutation.
type TestMap map[string]func(t *test.TestingT)
type PermMap map[string]test.Expect

// PermTest permutation test.
type PermTest struct {
// Test permutation test.
type Test struct {
mocks *mock.Mocks
tests TestMap
}

// NewPermTest creates a new permutation test with given mock and given
// NewTest creates a new permutation test with given mock and given
// permutation test map.
func NewPermTest(mocks *mock.Mocks, tests TestMap) *PermTest {
return &PermTest{
func NewTest(mocks *mock.Mocks, tests TestMap) *Test {
return &Test{
mocks: mocks,
tests: tests,
}
}

// TestPerm tests a single permutation given by the string slice.
func (p *PermTest) TestPerm(t *test.TestingT, perm []string) {
func (p *Test) TestPerm(t *test.TestingT, perm []string) {
require.Equal(t, len(p.tests), len(perm),
"permutation needs to cover all tests")
for _, value := range perm {
Expand All @@ -37,7 +42,7 @@ func (p *PermTest) TestPerm(t *test.TestingT, perm []string) {
}

// Test executes a permutation test with given permutation and expected result.
func (p *PermTest) Test(t *test.TestingT, perm []string, expect test.Expect) {
func (p *Test) Test(t *test.TestingT, perm []string, expect test.Expect) {
switch expect {
case test.ExpectSuccess:
// Test proper usage of `WaitGroup` on non-failing validation.
Expand All @@ -50,11 +55,11 @@ func (p *PermTest) Test(t *test.TestingT, perm []string, expect test.Expect) {
}
}

// PermRemain calculate and add the missing permutations and add it with
// Remain calculate and add the missing permutations and add it with
// expected result to the given permmutation map.
func PermRemain(perms PermMap, expect test.Expect) PermMap {
func Remain(perms ExpectMap, expect test.Expect) ExpectMap {
for key := range perms {
PermSlice(strings.Split(key, "-"), func(perm []string) {
Slice(strings.Split(key, "-"), func(perm []string) {
key := strings.Join(perm, "-")
if _, ok := perms[key]; !ok {
perms[key] = expect
Expand All @@ -65,15 +70,15 @@ func PermRemain(perms PermMap, expect test.Expect) PermMap {
return perms
}

// PermSlice permutates the given slice starting at the position given by and
// Slice permutates the given slice starting at the position given by and
// call the `do` function on each permutation to collect the result. For a full
// permutation the `index` must start with `0`.
func PermSlice[T any](slice []T, do func([]T), index int) {
func Slice[T any](slice []T, do func([]T), index int) {
if index <= len(slice) {
PermSlice(slice, do, index+1)
Slice(slice, do, index+1)
for offset := index + 1; offset < len(slice); offset++ {
slice[index], slice[offset] = slice[offset], slice[index]
PermSlice(slice, do, index+1)
Slice(slice, do, index+1)
slice[index], slice[offset] = slice[offset], slice[index]
}
} else {
Expand Down
Loading

0 comments on commit f94be2f

Please sign in to comment.