github.com/leanovate/gopter@v0.2.9/commands/commands_test.go (about) 1 package commands_test 2 3 import ( 4 "testing" 5 6 "github.com/leanovate/gopter" 7 "github.com/leanovate/gopter/commands" 8 "github.com/leanovate/gopter/gen" 9 ) 10 11 type counter struct { 12 value int 13 } 14 15 func (c *counter) Get() int { 16 return c.value 17 } 18 19 func (c *counter) Inc() int { 20 c.value++ 21 return c.value 22 } 23 24 func (c *counter) Dec() int { 25 c.value-- 26 return c.value 27 } 28 29 var GetCommand = &commands.ProtoCommand{ 30 Name: "GET", 31 RunFunc: func(systemUnderTest commands.SystemUnderTest) commands.Result { 32 return systemUnderTest.(*counter).Get() 33 }, 34 PreConditionFunc: func(state commands.State) bool { 35 _, ok := state.(int) 36 return ok 37 }, 38 PostConditionFunc: func(state commands.State, result commands.Result) *gopter.PropResult { 39 if state.(int) != result.(int) { 40 return &gopter.PropResult{Status: gopter.PropFalse} 41 } 42 return &gopter.PropResult{Status: gopter.PropTrue} 43 }, 44 } 45 46 var IncCommand = &commands.ProtoCommand{ 47 Name: "INC", 48 RunFunc: func(systemUnderTest commands.SystemUnderTest) commands.Result { 49 return systemUnderTest.(*counter).Inc() 50 }, 51 NextStateFunc: func(state commands.State) commands.State { 52 return state.(int) + 1 53 }, 54 PostConditionFunc: func(state commands.State, result commands.Result) *gopter.PropResult { 55 if state.(int) != result.(int) { 56 return &gopter.PropResult{Status: gopter.PropFalse} 57 } 58 return &gopter.PropResult{Status: gopter.PropTrue} 59 }, 60 } 61 62 var DecCommand = &commands.ProtoCommand{ 63 Name: "DEC", 64 RunFunc: func(systemUnderTest commands.SystemUnderTest) commands.Result { 65 return systemUnderTest.(*counter).Dec() 66 }, 67 PreConditionFunc: func(state commands.State) bool { 68 return state.(int) > 0 69 }, 70 NextStateFunc: func(state commands.State) commands.State { 71 return state.(int) - 1 72 }, 73 PostConditionFunc: func(state commands.State, result commands.Result) *gopter.PropResult { 74 if state.(int) != result.(int) { 75 return &gopter.PropResult{Status: gopter.PropFalse} 76 } 77 return &gopter.PropResult{Status: gopter.PropTrue} 78 }, 79 } 80 81 type counterCommands struct { 82 } 83 84 func (c *counterCommands) NewSystemUnderTest(initialState commands.State) commands.SystemUnderTest { 85 return &counter{value: initialState.(int)} 86 } 87 88 func (c *counterCommands) DestroySystemUnderTest(commands.SystemUnderTest) { 89 } 90 91 func (c *counterCommands) GenInitialState() gopter.Gen { 92 return gen.Int() 93 } 94 95 func (c *counterCommands) InitialPreCondition(state commands.State) bool { 96 return state.(int) >= 0 97 } 98 99 func (c *counterCommands) GenCommand(state commands.State) gopter.Gen { 100 return gen.OneConstOf(GetCommand, IncCommand, DecCommand) 101 } 102 103 func TestCommands(t *testing.T) { 104 parameters := gopter.DefaultTestParameters() 105 106 prop := commands.Prop(&counterCommands{}) 107 108 result := prop.Check(parameters) 109 if !result.Passed() { 110 t.Errorf("Invalid result: %v", result) 111 } 112 }