File: README.md

package info (click to toggle)
golang-github-muesli-combinator 0.3.0-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 96 kB
  • sloc: makefile: 3
file content (68 lines) | stat: -rw-r--r-- 2,278 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
# combinator

[![Latest Release](https://img.shields.io/github/release/muesli/combinator.svg)](https://github.com/muesli/combinator/releases)
[![GoDoc](https://godoc.org/github.com/golang/gddo?status.svg)](https://godoc.org/github.com/muesli/combinator)
[![Build Status](https://github.com/muesli/combinator/workflows/build/badge.svg)](https://github.com/muesli/combinator/actions)
[![Coverage Status](https://coveralls.io/repos/github/muesli/combinator/badge.svg?branch=master)](https://coveralls.io/github/muesli/combinator?branch=master)
[![Go ReportCard](http://goreportcard.com/badge/muesli/combinator)](http://goreportcard.com/report/muesli/combinator)

`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

```bash
go get github.com/muesli/combinator
```

## Example

```go
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](https://github.com/muesli/combinator/raw/master/LICENSE)