gent

package module
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 15, 2025 License: MIT Imports: 5 Imported by: 30

README

gent

Personal reusable code for my Go projects.

Name

Roughly: "denarced" -> "den" -> (this is for Go so "g") -> "gen" -> "gen" + "t(ools)" -> "gent".

Documentation

Overview

Package gent contains miscellaneous tools that the author keeps rewriting in various projects.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Filter

func Filter[T any](s []T, f func(T) bool) []T

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

func Map[T any, U any](s []T, f func(T) U) []U

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

func OrPanic2[T any](value T, err error) func(message string) T

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

func ReadLines(filep string) (lines []string, err error)

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

func ToSafeFilename(s string) string

ToSafeFilename replaces all non-safe characters with underscore.

func Tri

func Tri[T any](condition bool, a, b T) T

Tri returns one of the two values based on the condition. I.e. this is a ternary "operator".

Types

type Pair

type Pair[T any, U any] struct {
	First  T
	Second U
}

Pair is a pair of values.

func NewPair

func NewPair[T any, U any](first T, second U) Pair[T, U]

NewPair create an initialized gent.Pair.

type Set

type Set[T comparable] struct {
	// contains filtered or unexported fields
}

Set is a naive map backed set.

func NewSet

func NewSet[T comparable](items ...T) *Set[T]

NewSet creates a new gent.Set.

func (*Set[T]) Add

func (v *Set[T]) Add(item T) (added bool)

Add item to the set, return true if it was added. Otherwise it already existed and wasn't added.

func (*Set[T]) Clear

func (v *Set[T]) Clear()

Clear the set, remove all items.

func (*Set[T]) Contains

func (v *Set[T]) Contains(item T) bool

Contains checks if item exists in the set. Alias for gent.Set.Has.

func (*Set[T]) Count added in v0.2.0

func (v *Set[T]) Count() int

Count returns the number of items in the set. Alias for gent.Set.Len.

func (*Set[T]) Equal

func (v *Set[T]) Equal(s *Set[T]) bool

Equal returns true when the sets contain the exact same items.

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.

func (*Set[T]) Has

func (v *Set[T]) Has(item T) bool

Has checks if item exists in the set.

func (*Set[T]) Len

func (v *Set[T]) Len() int

Len returns the number of items in the set.

func (*Set[T]) Remove

func (v *Set[T]) Remove(item T) (existed bool)

Remove removes an item in the set, returns true if it was. I.e. if it existed.

func (*Set[T]) ToSlice

func (v *Set[T]) ToSlice() []T

ToSlice returns a slice with all set items. Set itself doesn't change.

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

func (v *Snapshot) Run(view string) error

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL