let

package
v0.178.2 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2025 License: Apache-2.0 Imports: 9 Imported by: 2

README

Package let

Package let contains Common Testcase variable #Let declarations for testing purpose.

var (
	ctx  = let.Context(s)
	name = let.FirstName(s)
)
act := func(t *testcase.T) error {
	return MyFunc(ctx.Get(t), name.Get(t))
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Act added in v0.171.0

func Act[A any](fn func(t *testcase.T) A) func(t *testcase.T) A

Act is a syntax shortcut that improves auto-completion in code editors like VS Code or IntelliJ IDEA. It represents an immutable testing act, where the closure retrieves input argument variables. This ensures that the test scenario properly arranges the variables beforehand since Act itself remains immutable.

Example
package main

import (
	"testing"

	"go.llib.dev/testcase"
	"go.llib.dev/testcase/let"
)

func main() {
	// in production code
	var MyFunc = func(n int) bool {
		return n%2 == 0
	}

	// TestMyFunc(t *testing.T)
	var t *testing.T
	s := testcase.NewSpec(t)

	var (
		n = let.Int(s)
	)
	act := let.Act(func(t *testcase.T) bool {
		return MyFunc(n.Get(t))
	})

	s.Then("...", func(t *testcase.T) {
		var got bool = act(t)
		_ = got // assert
	})
}

func Act0 added in v0.177.0

func Act0(fn func(t *testcase.T)) func(t *testcase.T)

Act0 is a syntax shortcut that improves auto-completion in code editors like VS Code or IntelliJ IDEA. It represents an immutable testing action, where the closure retrieves input argument variables. This ensures that the test scenario properly arranges the variables beforehand since Act itself remains immutable.

Example
package main

import (
	"testing"

	"go.llib.dev/testcase"
	"go.llib.dev/testcase/let"
)

func main() {
	// in production code
	var MyFunc = func(n int) {
		// something something...
	}

	// TestMyFunc(t *testing.T)
	var t *testing.T
	s := testcase.NewSpec(t)

	var (
		n = let.Int(s)
	)
	act := let.Act0(func(t *testcase.T) {
		MyFunc(n.Get(t))
	})

	s.Then("...", func(t *testcase.T) {
		act(t) // act

		// assert outcome
	})
}

func Act1 added in v0.177.0

func Act1[A any](fn func(t *testcase.T) A) func(t *testcase.T) A

Act1 is a syntax shortcut that improves auto-completion in code editors like VS Code or IntelliJ IDEA. It represents an immutable testing act, where the closure retrieves input argument variables. This ensures that the test scenario properly arranges the variables beforehand since Act itself remains immutable.

Example
package main

import (
	"testing"

	"go.llib.dev/testcase"
	"go.llib.dev/testcase/let"
)

func main() {
	// in production code
	var MyFunc = func(n int) bool {
		return n%2 == 0
	}

	// TestMyFunc(t *testing.T)
	var t *testing.T
	s := testcase.NewSpec(t)

	var (
		n = let.Int(s)
	)
	act := let.Act1(func(t *testcase.T) bool {
		return MyFunc(n.Get(t))
	})

	s.Then("...", func(t *testcase.T) {
		var got bool = act(t)
		_ = got // assert
	})
}

func Act2 added in v0.171.0

func Act2[A, B any](fn func(t *testcase.T) (A, B)) func(t *testcase.T) (A, B)

Act2 is a syntax shortcut that improves auto-completion in code editors like VS Code or IntelliJ IDEA. It represents an immutable testing act, where the closure retrieves input argument variables. This ensures that the test scenario properly arranges the variables beforehand since Act itself remains immutable.

Example
package main

import (
	"strconv"
	"testing"

	"go.llib.dev/testcase"
	"go.llib.dev/testcase/let"
	"go.llib.dev/testcase/random"
)

func main() {
	var t *testing.T
	s := testcase.NewSpec(t)

	var (
		str = let.StringNC(s, 3, random.CharsetDigit())
	)
	act := let.Act2(func(t *testcase.T) (int, error) {
		return strconv.Atoi(str.Get(t))
	})

	s.Then("...", func(t *testcase.T) {
		_, _ = act(t)
	})
}

func Act3 added in v0.171.0

func Act3[A, B, C any](fn func(t *testcase.T) (A, B, C)) func(t *testcase.T) (A, B, C)

Act3 is a syntax shortcut that improves auto-completion in code editors like VS Code or IntelliJ IDEA. It represents an immutable testing act, where the closure retrieves input argument variables. This ensures that the test scenario properly arranges the variables beforehand since Act itself remains immutable.

Example
package main

import (
	"strconv"
	"testing"

	"go.llib.dev/testcase"
	"go.llib.dev/testcase/let"
	"go.llib.dev/testcase/random"
)

func main() {
	// package mypkg
	var MyFunc = func(str string) (string, int, error) {
		n, err := strconv.Atoi(str)
		return str, n, err
	}

	// package mypkg_test
	var t *testing.T
	s := testcase.NewSpec(t)

	var (
		str = let.StringNC(s, 3, random.CharsetDigit())
	)
	act := let.Act3(func(t *testcase.T) (string, int, error) {
		return MyFunc(str.Get(t))
	})

	s.Then("...", func(t *testcase.T) {
		_, _, _ = act(t)
	})
}

func As

func As[To, From any](Var testcase.Var[From]) testcase.Var[To]
Example
package main

import (
	"testing"

	"go.llib.dev/testcase"
	"go.llib.dev/testcase/let"
)

func main() {
	s := testcase.NewSpec((testing.TB)(nil))

	type MyString string
	str := let.As[MyString](let.String(s))

	s.Test("", func(t *testcase.T) {
		t.Log(str.Get(t))
	})
}

func Bool

func Bool(s *testcase.Spec) testcase.Var[bool]
Example
package main

import (
	"testing"

	"go.llib.dev/testcase"
	"go.llib.dev/testcase/let"
)

func main() {
	s := testcase.NewSpec((testing.TB)(nil))

	b := let.Bool(s)

	s.Test("", func(t *testcase.T) {
		t.Log(b.Get(t))
	})
}

func Context

func Context(s *testcase.Spec) testcase.Var[context.Context]
Example
package main

import (
	"testing"

	"go.llib.dev/testcase"
	"go.llib.dev/testcase/let"
)

func main() {
	s := testcase.NewSpec((testing.TB)(nil))

	ctx := let.Context(s)

	s.Test("", func(t *testcase.T) {
		t.Logf("%#v", ctx.Get(t))
	})
}

func ContextWithCancel added in v0.165.0

func ContextWithCancel(s *testcase.Spec) (testcase.Var[context.Context], testcase.Var[func()])

func DurationBetween added in v0.164.0

func DurationBetween(s *testcase.Spec, min, max time.Duration) testcase.Var[time.Duration]

func ElementFrom deprecated

func ElementFrom[V any](s *testcase.Spec, vs ...V) testcase.Var[V]

ElementFrom

Deprecated: use let.OneOf instead

func Email

func Email(s *testcase.Spec) testcase.Var[string]
Example
package main

import (
	"testing"

	"go.llib.dev/testcase"
	"go.llib.dev/testcase/let"
)

func main() {
	s := testcase.NewSpec((testing.TB)(nil))

	email := let.Email(s)

	s.Test("", func(t *testcase.T) {
		t.Log(email.Get(t))
	})
}

func Error

func Error(s *testcase.Spec) testcase.Var[error]
Example
package main

import (
	"testing"

	"go.llib.dev/testcase"
	"go.llib.dev/testcase/let"
)

func main() {
	s := testcase.NewSpec((testing.TB)(nil))

	expectedErr := let.Error(s)

	s.Test("", func(t *testcase.T) {
		t.Log(expectedErr.Get(t))
	})
}

func FirstName

func FirstName(s *testcase.Spec, opts ...internal.ContactOption) testcase.Var[string]
Example
package main

import (
	"testing"

	"go.llib.dev/testcase"
	"go.llib.dev/testcase/let"
)

func main() {
	s := testcase.NewSpec((testing.TB)(nil))

	firstName := let.FirstName(s)

	s.Test("", func(t *testcase.T) {
		t.Log(firstName.Get(t))
	})
}

func HTTPTestResponseRecorder added in v0.170.0

func HTTPTestResponseRecorder(s *testcase.Spec) testcase.Var[*httptest.ResponseRecorder]

func Int

func Int(s *testcase.Spec) testcase.Var[int]
Example
package main

import (
	"testing"

	"go.llib.dev/testcase"
	"go.llib.dev/testcase/let"
)

func main() {
	s := testcase.NewSpec((testing.TB)(nil))

	n := let.Int(s)

	s.Test("", func(t *testcase.T) {
		t.Log(n.Get(t))
	})
}

func IntB

func IntB(s *testcase.Spec, min, max int) testcase.Var[int]
Example
package main

import (
	"testing"

	"go.llib.dev/testcase"
	"go.llib.dev/testcase/let"
)

func main() {
	s := testcase.NewSpec((testing.TB)(nil))

	n := let.IntB(s, 7, 42)

	s.Test("", func(t *testcase.T) {
		t.Log(n.Get(t))
	})
}

func IntN

func IntN(s *testcase.Spec, n int) testcase.Var[int]
Example
package main

import (
	"testing"

	"go.llib.dev/testcase"
	"go.llib.dev/testcase/let"
)

func main() {
	s := testcase.NewSpec((testing.TB)(nil))

	n := let.IntN(s, 42)

	s.Test("", func(t *testcase.T) {
		t.Log(n.Get(t))
	})
}

func LastName

func LastName(s *testcase.Spec) testcase.Var[string]
Example
package main

import (
	"testing"

	"go.llib.dev/testcase"
	"go.llib.dev/testcase/let"
)

func main() {
	s := testcase.NewSpec((testing.TB)(nil))

	lastName := let.LastName(s)

	s.Test("", func(t *testcase.T) {
		t.Log(lastName.Get(t))
	})
}

func OneOf added in v0.172.0

func OneOf[V any](s *testcase.Spec, vs ...V) testcase.Var[V]
Example
package main

import (
	"testing"

	"go.llib.dev/testcase"
	"go.llib.dev/testcase/let"
)

func main() {
	s := testcase.NewSpec((testing.TB)(nil))

	v := let.OneOf(s, "foo", "bar", "baz")

	s.Test("", func(t *testcase.T) {
		t.Log(v.Get(t))
	})
}

func String

func String(s *testcase.Spec) testcase.Var[string]
Example
package main

import (
	"testing"

	"go.llib.dev/testcase"
	"go.llib.dev/testcase/let"
)

func main() {
	s := testcase.NewSpec((testing.TB)(nil))

	str := let.String(s)

	s.Test("", func(t *testcase.T) {
		t.Log(str.Get(t))
	})
}

func StringNC

func StringNC(s *testcase.Spec, length int, charset string) testcase.Var[string]
Example
package main

import (
	"testing"

	"go.llib.dev/testcase"
	"go.llib.dev/testcase/let"
	"go.llib.dev/testcase/random"
)

func main() {
	s := testcase.NewSpec((testing.TB)(nil))

	str := let.StringNC(s, 42, random.CharsetASCII())

	s.Test("", func(t *testcase.T) {
		t.Log(str.Get(t))
	})
}

func Time

func Time(s *testcase.Spec) testcase.Var[time.Time]
Example
package main

import (
	"testing"
	"time"

	"go.llib.dev/testcase"
	"go.llib.dev/testcase/let"
)

func main() {
	s := testcase.NewSpec((testing.TB)(nil))

	tm := let.Time(s)

	s.Test("", func(t *testcase.T) {
		t.Log(tm.Get(t).Format(time.RFC3339))
	})
}

func TimeB

func TimeB(s *testcase.Spec, from, to time.Time) testcase.Var[time.Time]
Example
package main

import (
	"testing"
	"time"

	"go.llib.dev/testcase"
	"go.llib.dev/testcase/let"
)

func main() {
	s := testcase.NewSpec((testing.TB)(nil))

	tm := let.TimeB(s, time.Now().AddDate(-1, 0, 0), time.Now())

	s.Test("", func(t *testcase.T) {
		t.Log(tm.Get(t).Format(time.RFC3339))
	})
}

func UUID

func UUID(s *testcase.Spec) testcase.Var[string]
Example
package main

import (
	"testing"

	"go.llib.dev/testcase"
	"go.llib.dev/testcase/let"
)

func main() {
	s := testcase.NewSpec((testing.TB)(nil))

	uuid := let.UUID(s)

	s.Test("", func(t *testcase.T) {
		t.Log(uuid.Get(t))
	})
}

func Var added in v0.170.0

func Var[V any](s *testcase.Spec, blk func(t *testcase.T) V) testcase.Var[V]

Var creates a stateless specification variable, serving as a blueprint for test cases to construct test runtime values. It also functions as both a getter and setter for the value associated to the test runtime.

Example
package main

import (
	"go.llib.dev/testcase"
	"go.llib.dev/testcase/let"
)

func main() {
	var t *testcase.T
	s := testcase.NewSpec(t)

	v1 := let.Var(s, func(t *testcase.T) int {
		return t.Random.IntB(1, 7)
	})

	s.Test("", func(t *testcase.T) {
		v1.Get(t) // the random value
		v1.Get(t) // the same random value
	})
}

func Var2 added in v0.170.0

func Var2[V1, V2 any](s *testcase.Spec, blk func(t *testcase.T) (V1, V2)) (testcase.Var[V1], testcase.Var[V2])

Var2 creates a stateless specification variable, serving as a blueprint for test cases to construct test runtime values. It also functions as both a getter and setter for the value associated to the test runtime.

Example
package main

import (
	"go.llib.dev/testcase"
	"go.llib.dev/testcase/let"
)

func main() {
	var t *testcase.T
	s := testcase.NewSpec(t)

	v1, v2 := let.Var2(s, func(t *testcase.T) (int, string) {
		return t.Random.IntB(1, 7), t.Random.String()
	})

	s.Test("", func(t *testcase.T) {
		v1.Get(t) // the random value constructed in the init block of Var
		v1.Get(t) // the same random value

		v2.Get(t) // the random string we made in the init block
		v2.Get(t) // the same value
	})
}

func Var3 added in v0.170.0

func Var3[V1, V2, V3 any](s *testcase.Spec, blk func(t *testcase.T) (V1, V2, V3)) (testcase.Var[V1], testcase.Var[V2], testcase.Var[V3])

Var3 creates a stateless specification variable, serving as a blueprint for test cases to construct test runtime values. It also functions as both a getter and setter for the value associated to the test runtime.

Example
package main

import (
	"go.llib.dev/testcase"
	"go.llib.dev/testcase/let"
)

func main() {
	var t *testcase.T
	s := testcase.NewSpec(t)

	v1, v2, v3 := let.Var3(s, func(t *testcase.T) (int, string, bool) {
		return t.Random.IntB(1, 7), t.Random.String(), t.Random.Bool()
	})

	s.Test("", func(t *testcase.T) {
		v1.Get(t) // the random value constructed in the init block of Var
		v1.Get(t) // the same random value

		v2.Get(t) // the random string we made in the init block
		v2.Get(t) // the same value

		v3.Get(t) // the random string we made in the init block
		v3.Get(t) // the same value
	})
}

func VarOf added in v0.175.0

func VarOf[V any](s *testcase.Spec, v V) testcase.Var[V]

VarOf is a shorthand for defining a testcase.Var[V] using an immutable value. So the function blocks can be skipped, which makes tests more readable.

func With

func With[V any, FN withFN[V]](s *testcase.Spec, fn FN) testcase.Var[V]
Example (Func)
package main

import (
	"testing"

	"go.llib.dev/testcase"
	"go.llib.dev/testcase/let"
)

func main() {
	s := testcase.NewSpec((testing.TB)(nil))

	v := let.With[int](s, func() int {
		return 42
	})

	s.Test("", func(t *testcase.T) {
		t.Log(v.Get(t))
	})
}
Example (TestcaseTFunc)
package main

import (
	"testing"

	"go.llib.dev/testcase"
	"go.llib.dev/testcase/let"
)

func main() {
	s := testcase.NewSpec((testing.TB)(nil))

	v := let.With[int](s, func(t *testcase.T) int {
		return t.Random.Int()
	})

	s.Test("", func(t *testcase.T) {
		t.Log(v.Get(t))
	})
}
Example (TestingTBFunc)
package main

import (
	"testing"

	"go.llib.dev/testcase"
	"go.llib.dev/testcase/let"
)

func main() {
	s := testcase.NewSpec((testing.TB)(nil))

	v := let.With[int](s, func(tb testing.TB) int {
		return 42
	})

	s.Test("", func(t *testcase.T) {
		t.Log(v.Get(t))
	})
}

Types

This section is empty.

Jump to

Keyboard shortcuts

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