Code Monkey home page Code Monkey logo

combinator's Introduction

combinator

Latest Release GoDoc Build Status Coverage Status Go ReportCard

combinator generates a slice of all possible value combinations for any given struct and a set of its potential member values. This can be used to generate extensive test matrixes among other things.

Installation

go get github.com/muesli/combinator

Example

type User struct {
    Name  string
    Age   uint
    Admin bool
}

/*
  Define potential test values. Make sure the struct's fields share the name and
  type of the structs you want to generate.
*/
testData := struct {
    Name  []string
    Age   []uint
    Admin []bool
}{
    Name:  []string{"Alice", "Bob"},
    Age:   []uint{23, 42, 99},
    Admin: []bool{false, true},
}

// Generate all possible combinations
var users []User
combinator.Generate(&users, testData)

for i, u := range users {
    fmt.Printf("Combination %2d | Name: %-5s | Age: %d | Admin: %v\n", i, u.Name, u.Age, u.Admin)
}
Combination  0 | Name: Alice | Age: 23 | Admin: false
Combination  1 | Name: Bob   | Age: 23 | Admin: false
Combination  2 | Name: Alice | Age: 42 | Admin: false
Combination  3 | Name: Bob   | Age: 42 | Admin: false
Combination  4 | Name: Alice | Age: 99 | Admin: false
Combination  5 | Name: Bob   | Age: 99 | Admin: false
Combination  6 | Name: Alice | Age: 23 | Admin: true
Combination  7 | Name: Bob   | Age: 23 | Admin: true
Combination  8 | Name: Alice | Age: 42 | Admin: true
Combination  9 | Name: Bob   | Age: 42 | Admin: true
Combination 10 | Name: Alice | Age: 99 | Admin: true
Combination 11 | Name: Bob   | Age: 99 | Admin: true

License

MIT

combinator's People

Contributors

muesli avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

sthagen cosby5

combinator's Issues

add support for single-valued fields

Firstly, thank you for this awesome module! It's really helpful and has both reduced the amount of test code that I have to write and increased my test coverage :)

I have a sum type switched on a field:

type T struct {
	IsIntsNotStrings bool
	Ints             []int
	Strings          []string
}

Depending on the value of IsIntsNotStrings, either Ints or Strings will be filled, but never both.

I can use muesli/combinator to generate a bunch of combinations like this:

var ts []T
combinator.Generate(&ts, struct {
	IsIntsNotStrings []bool
	Ints             []int
	Strings          []string
}, {
	IsIntsNotStrings: []bool{true},
	Ints:             []int{1, 2, 3},
})
combinator.Generate(&ts, struct {
	IsIntsNotStrings []bool
	Ints             []int
	Strings          []string
}, {
	IsIntsNotBools: []bool{false},
	Strings:        []string{"a", "b", "c"},
})

Thanks to combinator's appending of options, I end up with a nice test suite, but I have to use arrays with one element for IsIntsNotStrings and add fields to my struct that always take the zero value in my test data.

What I would like to write is this:

var ts []T
combinator.Generate(&ts, struct {
	IsIntsNotStrings bool
	Ints             []int
}, {
	IsIntNotStrings: true,
	Ints:            []int{1, 2, 3},
})
combinator.Generate(&ts, struct {
	IsIntsNotStrings bool
	Strings          []string
}, {
	IsIntsNotStrings: false,
	Strings:          []string{"a", "b", "c"},
})

What changed:

  • There is less code.
  • IsIntsNotBool changed from a slice of []bool to a single-valued bool, with only that single value used to generate combinations.
  • Ints and Strings are only specified if their values change, otherwise the zero value is used.

(apologies for any errors in the above code - it was a manually simplified example and may contain errors, but I hope the intent is clear)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.