File: commands_test.go

package info (click to toggle)
golang-github-leanovate-gopter 0.2.9%2Bgit20210201.bbbf00e-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 696 kB
  • sloc: makefile: 37
file content (112 lines) | stat: -rw-r--r-- 2,780 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package commands_test

import (
	"testing"

	"github.com/leanovate/gopter"
	"github.com/leanovate/gopter/commands"
	"github.com/leanovate/gopter/gen"
)

type counter struct {
	value int
}

func (c *counter) Get() int {
	return c.value
}

func (c *counter) Inc() int {
	c.value++
	return c.value
}

func (c *counter) Dec() int {
	c.value--
	return c.value
}

var GetCommand = &commands.ProtoCommand{
	Name: "GET",
	RunFunc: func(systemUnderTest commands.SystemUnderTest) commands.Result {
		return systemUnderTest.(*counter).Get()
	},
	PreConditionFunc: func(state commands.State) bool {
		_, ok := state.(int)
		return ok
	},
	PostConditionFunc: func(state commands.State, result commands.Result) *gopter.PropResult {
		if state.(int) != result.(int) {
			return &gopter.PropResult{Status: gopter.PropFalse}
		}
		return &gopter.PropResult{Status: gopter.PropTrue}
	},
}

var IncCommand = &commands.ProtoCommand{
	Name: "INC",
	RunFunc: func(systemUnderTest commands.SystemUnderTest) commands.Result {
		return systemUnderTest.(*counter).Inc()
	},
	NextStateFunc: func(state commands.State) commands.State {
		return state.(int) + 1
	},
	PostConditionFunc: func(state commands.State, result commands.Result) *gopter.PropResult {
		if state.(int) != result.(int) {
			return &gopter.PropResult{Status: gopter.PropFalse}
		}
		return &gopter.PropResult{Status: gopter.PropTrue}
	},
}

var DecCommand = &commands.ProtoCommand{
	Name: "DEC",
	RunFunc: func(systemUnderTest commands.SystemUnderTest) commands.Result {
		return systemUnderTest.(*counter).Dec()
	},
	PreConditionFunc: func(state commands.State) bool {
		return state.(int) > 0
	},
	NextStateFunc: func(state commands.State) commands.State {
		return state.(int) - 1
	},
	PostConditionFunc: func(state commands.State, result commands.Result) *gopter.PropResult {
		if state.(int) != result.(int) {
			return &gopter.PropResult{Status: gopter.PropFalse}
		}
		return &gopter.PropResult{Status: gopter.PropTrue}
	},
}

type counterCommands struct {
}

func (c *counterCommands) NewSystemUnderTest(initialState commands.State) commands.SystemUnderTest {
	return &counter{value: initialState.(int)}
}

func (c *counterCommands) DestroySystemUnderTest(commands.SystemUnderTest) {
}

func (c *counterCommands) GenInitialState() gopter.Gen {
	return gen.Int()
}

func (c *counterCommands) InitialPreCondition(state commands.State) bool {
	return state.(int) >= 0
}

func (c *counterCommands) GenCommand(state commands.State) gopter.Gen {
	return gen.OneConstOf(GetCommand, IncCommand, DecCommand)
}

func TestCommands(t *testing.T) {
	parameters := gopter.DefaultTestParameters()

	prop := commands.Prop(&counterCommands{})

	result := prop.Check(parameters)
	if !result.Passed() {
		t.Errorf("Invalid result: %v", result)
	}
}