File: actions.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 (140 lines) | stat: -rw-r--r-- 4,415 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package commands

import (
	"fmt"
	"reflect"

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

type shrinkableCommand struct {
	command  Command
	shrinker gopter.Shrinker
}

func (s shrinkableCommand) shrink() gopter.Shrink {
	return s.shrinker(s.command).Map(func(command Command) shrinkableCommand {
		return shrinkableCommand{
			command:  command,
			shrinker: s.shrinker,
		}
	})
}

func (s shrinkableCommand) String() string {
	return fmt.Sprintf("%v", s.command)
}

type actions struct {
	// initialStateProvider has to reset/recreate the initial state exactly the
	// same every time.
	initialStateProvider func() State
	sequentialCommands   []shrinkableCommand
	// parallel commands will come later
}

func (a *actions) String() string {
	return fmt.Sprintf("initialState=%v sequential=%s", a.initialStateProvider(), a.sequentialCommands)
}

func (a *actions) run(systemUnderTest SystemUnderTest) *gopter.PropResult {
	state := a.initialStateProvider()
	propResult := &gopter.PropResult{Status: gopter.PropTrue}
	for _, shrinkableCommand := range a.sequentialCommands {
		if !shrinkableCommand.command.PreCondition(state) {
			return &gopter.PropResult{Status: gopter.PropFalse}
		}
		result := shrinkableCommand.command.Run(systemUnderTest)
		state = shrinkableCommand.command.NextState(state)
		propResult = propResult.And(shrinkableCommand.command.PostCondition(state, result))
	}
	return propResult
}

type sizedCommands struct {
	state    State
	commands []shrinkableCommand
}

func actionsShrinker(v interface{}) gopter.Shrink {
	a := v.(*actions)
	elementShrinker := gopter.Shrinker(func(v interface{}) gopter.Shrink {
		return v.(shrinkableCommand).shrink()
	})
	return gen.SliceShrinker(elementShrinker)(a.sequentialCommands).Map(func(v []shrinkableCommand) *actions {
		return &actions{
			initialStateProvider: a.initialStateProvider,
			sequentialCommands:   v,
		}
	})
}

func genActions(commands Commands) gopter.Gen {
	genInitialState := commands.GenInitialState()
	genInitialStateProvider := gopter.Gen(func(params *gopter.GenParameters) *gopter.GenResult {
		seed := params.NextInt64()
		return gopter.NewGenResult(func() State {
			paramsWithSeed := params.CloneWithSeed(seed)
			if initialState, ok := genInitialState(paramsWithSeed).Retrieve(); ok {
				return initialState
			}
			return nil
		}, gopter.NoShrinker)
	}).SuchThat(func(initialStateProvoder func() State) bool {
		state := initialStateProvoder()
		return state != nil && commands.InitialPreCondition(state)
	})
	return genInitialStateProvider.FlatMap(func(v interface{}) gopter.Gen {
		initialStateProvider := v.(func() State)
		return genSizedCommands(commands, initialStateProvider).Map(func(v sizedCommands) *actions {
			return &actions{
				initialStateProvider: initialStateProvider,
				sequentialCommands:   v.commands,
			}
		}).SuchThat(func(actions *actions) bool {
			state := actions.initialStateProvider()
			for _, shrinkableCommand := range actions.sequentialCommands {
				if !shrinkableCommand.command.PreCondition(state) {
					return false
				}
				state = shrinkableCommand.command.NextState(state)
			}
			return true
		}).WithShrinker(actionsShrinker)
	}, reflect.TypeOf((*actions)(nil)))
}

func genSizedCommands(commands Commands, initialStateProvider func() State) gopter.Gen {
	return func(genParams *gopter.GenParameters) *gopter.GenResult {
		sizedCommandsGen := gen.Const(sizedCommands{
			state:    initialStateProvider(),
			commands: make([]shrinkableCommand, 0, genParams.MaxSize),
		})
		for i := 0; i < genParams.MaxSize; i++ {
			sizedCommandsGen = sizedCommandsGen.FlatMap(func(v interface{}) gopter.Gen {
				prev := v.(sizedCommands)
				return gen.RetryUntil(commands.GenCommand(prev.state), func(command Command) bool {
					return command.PreCondition(prev.state)
				}, 100).MapResult(func(result *gopter.GenResult) *gopter.GenResult {
					value, ok := result.Retrieve()
					if !ok {
						return gopter.NewEmptyResult(reflect.TypeOf(sizedCommands{}))
					}
					command := value.(Command)
					return gopter.NewGenResult(
						sizedCommands{
							state: command.NextState(prev.state),
							commands: append(prev.commands, shrinkableCommand{
								command:  command,
								shrinker: result.Shrinker,
							}),
						},
						gopter.NoShrinker,
					)
				})
			}, reflect.TypeOf(sizedCommands{}))
		}
		return sizedCommandsGen(genParams)
	}
}