github.com/leanovate/gopter@v0.2.9/prop/example_invalidconcat_test.go (about)

     1  package prop_test
     2  
     3  import (
     4  	"strings"
     5  	"unicode"
     6  
     7  	"github.com/leanovate/gopter"
     8  	"github.com/leanovate/gopter/gen"
     9  	"github.com/leanovate/gopter/prop"
    10  )
    11  
    12  func MisimplementedConcat(a, b string) string {
    13  	if strings.IndexFunc(a, unicode.IsDigit) > 5 {
    14  		return b
    15  	}
    16  	return a + b
    17  }
    18  
    19  // Example_invalidconcat demonstrates shrinking of string
    20  // Kudos to @exarkun and @itamarst for finding this issue
    21  func Example_invalidconcat() {
    22  	parameters := gopter.DefaultTestParametersWithSeed(1234) // Example should generate reproducible results, otherwise DefaultTestParameters() will suffice
    23  
    24  	properties := gopter.NewProperties(parameters)
    25  
    26  	properties.Property("length is sum of lengths", prop.ForAll(
    27  		func(a, b string) bool {
    28  			return MisimplementedConcat(a, b) == a+b
    29  		},
    30  		gen.Identifier(),
    31  		gen.Identifier(),
    32  	))
    33  
    34  	// When using testing.T you might just use: properties.TestingRun(t)
    35  	properties.Run(gopter.ConsoleReporter(false))
    36  	// Output:
    37  	// ! length is sum of lengths: Falsified after 17 passed tests.
    38  	// ARG_0: bahbxh6
    39  	// ARG_0_ORIGINAL (2 shrinks): pkpbahbxh6
    40  	// ARG_1: l
    41  	// ARG_1_ORIGINAL (1 shrinks): dl
    42  }