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

     1  package gopter_test
     2  
     3  import (
     4  	"reflect"
     5  
     6  	"github.com/leanovate/gopter"
     7  	"github.com/leanovate/gopter/arbitrary"
     8  	"github.com/leanovate/gopter/gen"
     9  )
    10  
    11  type TestBook struct {
    12  	Title   string
    13  	Content string
    14  }
    15  
    16  func genTestBook() gopter.Gen {
    17  	return gen.Struct(reflect.TypeOf(&TestBook{}), map[string]gopter.Gen{
    18  		"Title":   gen.AlphaString(),
    19  		"Content": gen.AlphaString(),
    20  	})
    21  }
    22  
    23  type TestLibrary struct {
    24  	Name       string
    25  	Librarians uint8
    26  	Books      []TestBook
    27  }
    28  
    29  func genTestLibrary() gopter.Gen {
    30  	return gen.Struct(reflect.TypeOf(&TestLibrary{}), map[string]gopter.Gen{
    31  		"Name": gen.AlphaString().SuchThat(func(s string) bool {
    32  			// Non-empty string
    33  			return s != ""
    34  		}),
    35  		"Librarians": gen.UInt8Range(1, 255),
    36  		"Books":      gen.SliceOf(genTestBook()),
    37  	})
    38  }
    39  
    40  type CityName = string
    41  type TestCities struct {
    42  	Libraries map[CityName][]TestLibrary
    43  }
    44  
    45  func genTestCities() gopter.Gen {
    46  	return gen.StructPtr(reflect.TypeOf(&TestCities{}), map[string]gopter.Gen{
    47  		"Libraries": (gen.MapOf(gen.AlphaString(), gen.SliceOf(genTestLibrary()))),
    48  	})
    49  }
    50  func Example_libraries() {
    51  	parameters := gopter.DefaultTestParameters()
    52  	parameters.Rng.Seed(1234) // Just for this example to generate reproducible results
    53  	parameters.MaxSize = 5
    54  	arbitraries := arbitrary.DefaultArbitraries()
    55  	arbitraries.RegisterGen(genTestCities())
    56  
    57  	properties := gopter.NewProperties(parameters)
    58  
    59  	properties.Property("no unsupervised libraries", arbitraries.ForAll(
    60  		func(tc *TestCities) bool {
    61  			for _, libraries := range tc.Libraries {
    62  				for _, library := range libraries {
    63  					if library.Librarians == 0 {
    64  						return false
    65  					}
    66  				}
    67  			}
    68  			return true
    69  		},
    70  	))
    71  
    72  	// When using testing.T you might just use: properties.TestingRun(t)
    73  	properties.Run(gopter.ConsoleReporter(false))
    74  	// Output:
    75  	// + no unsupervised libraries: OK, passed 100 tests.
    76  }
    77  
    78  func Example_libraries2() {
    79  	parameters := gopter.DefaultTestParameters()
    80  	parameters.Rng.Seed(1234) // Just for this example to generate reproducible results
    81  
    82  	arbitraries := arbitrary.DefaultArbitraries()
    83  	// All string are alphanumeric
    84  	arbitraries.RegisterGen(gen.AlphaString())
    85  
    86  	properties := gopter.NewProperties(parameters)
    87  
    88  	properties.Property("libraries always empty", arbitraries.ForAll(
    89  		func(tc *TestCities) bool {
    90  			return len(tc.Libraries) == 0
    91  		},
    92  	))
    93  
    94  	// When using testing.T you might just use: properties.TestingRun(t)
    95  	properties.Run(gopter.ConsoleReporter(false))
    96  	// Output:
    97  	// ! libraries always empty: Falsified after 2 passed tests.
    98  	// ARG_0: &{Libraries:map[z:[]]}
    99  }