Documentation
¶
Overview ¶
Package gent contains miscellaneous tools that the author keeps rewriting in various projects.
Index ¶
- func Filter[T any](s []T, f func(T) bool) []T
- func Map[T any, U any](s []T, f func(T) U) []U
- func OrPanic2[T any](value T, err error) func(message string) T
- func ReadLines(filep string) (lines []string, err error)
- func ToSafeFilename(s string) string
- func Tri[T any](condition bool, a, b T) T
- type Pair
- type Set
- func (v *Set[T]) Add(item T) (added bool)
- func (v *Set[T]) Clear()
- func (v *Set[T]) Contains(item T) bool
- func (v *Set[T]) Count() int
- func (v *Set[T]) Equal(s *Set[T]) bool
- func (v *Set[T]) ForEach(f func(each T, stop func()))
- func (v *Set[T]) ForEachAll(f func(each T))
- func (v *Set[T]) Has(item T) bool
- func (v *Set[T]) Len() int
- func (v *Set[T]) Remove(item T) (existed bool)
- func (v *Set[T]) ToSlice() []T
- type Snapshot
- type SnapshotSuite
- type VerifyFunc
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Filter ¶
Filter values in s with f. When f returns true, item is included in the response slice.
Example ¶
filterOdd := func(i int) bool { return i%2 != 0 }
fmt.Print(
Filter(
[]int{1, 2, 3, 4, 5},
filterOdd))
Output: [1 3 5]
func Map ¶
Map a slice into another slice of the same size.
Example ¶
fmt.Print(
Map(
[]int{1, 2, 4},
func(i int) string {
return fmt.Sprintf("item: %d", i)
}))
Output: [item: 1 item: 2 item: 4]
func OrPanic2 ¶
OrPanic2 returns function that returns value if err is nil, else panics with message. Useful for cases where failure should result in panic and you don't want to deal with the returned error.
Example ¶
defer func() {
if r := recover(); r != nil {
fmt.Print(r)
}
}()
divide := func(a, b int) (int, error) {
if b == 0 {
return 0, fmt.Errorf("can't divide with zero")
}
return a / b, nil
}
// The usual case where the call succeeds and you don't need to deal with the error.
result := OrPanic2(divide(10, 2))("oops")
fmt.Println(result)
// Failing case where you still don't have to deal with the error.
OrPanic2(divide(1, 0))("nope")
Output: 5 Message: nope. Error: can't divide with zero.
func ReadLines ¶
ReadLines read all lines in file filep. Empty lines are included. Returned lines do not contain newlines at the end.
func ToSafeFilename ¶ added in v0.4.0
ToSafeFilename replaces all non-safe characters with underscore.
Types ¶
type Set ¶
type Set[T comparable] struct { // contains filtered or unexported fields }
Set is a naive map backed set.
func (*Set[T]) Add ¶
Add item to the set, return true if it was added. Otherwise it already existed and wasn't added.
func (*Set[T]) Contains ¶
Contains checks if item exists in the set. Alias for gent.Set.Has.
func (*Set[T]) Count ¶ added in v0.2.0
Count returns the number of items in the set. Alias for gent.Set.Len.
func (*Set[T]) ForEach ¶
func (v *Set[T]) ForEach(f func(each T, stop func()))
ForEach iterates all items in the set, calls f for each item, stops if stop is called. Use gent.ForEachAll if there's no need to stop iteration.
func (*Set[T]) ForEachAll ¶ added in v0.3.0
func (v *Set[T]) ForEachAll(f func(each T))
ForEachAll iterates all items in the set and calls f for each item. Use gent.ForEach if you need to stop iteration.
type Snapshot ¶ added in v0.4.0
type Snapshot struct {
// Name of the test that's also the last part of the snapshot file's filepath.
Name string
// contains filtered or unexported fields
}
Snapshot represents a single test with a snapshot file.
func (*Snapshot) Run ¶ added in v0.4.0
Run the snapshot process according to parameters set in gent.SnapshotSuite.NewSnapshot. Error is returned when something unexpected fails, not when the test itself fails. Determining whether any given test fails is left for "equal" function defined in gent.SnapshotSuite.NewSnapshot.
type SnapshotSuite ¶ added in v0.4.0
type SnapshotSuite struct {
// contains filtered or unexported fields
}
A SnapshotSuite is a suite of snapshot tests with a shared directory for the snapshot files. It is made of [gent.Snapshot]s.
func NewSnapshotSuite ¶ added in v0.4.0
func NewSnapshotSuite(rootDir string) *SnapshotSuite
NewSnapshotSuite creates a gent.SnapshotSuite with a root directory. Usually it's under "testdata".
func (*SnapshotSuite) NewSnapshot ¶ added in v0.4.0
func (v *SnapshotSuite) NewSnapshot(name string, verify bool, equal VerifyFunc) *Snapshot
NewSnapshot creates a snapshot. Name is gent.Snapshot.Name and with [gent.SnapshotSuite.rootDir], becomes the full filepath of the snapshot file. When verify is false, snapshots are written, and tests won't fail. That's how you initialize or update snapshots. When verify is true and snapshot file doesn't exist or it's empty, content produced by the tested code is written. And finally, when verify is true and the snapshot file exists, equal function is used to assert equality.
type VerifyFunc ¶ added in v0.4.0
type VerifyFunc func(expected, actual, message string)
VerifyFunc is used to assert that snapshot matches to the string that code produced. This is your standard "assertEqual" function in any unit test library.