testutils

package
v0.0.0-...-b160839 Latest Latest
Warning

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

Go to latest
Published: May 29, 2023 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Package testutils contains a minimal set of utils for testing. The package is inspired by testify.

Index

Constants

This section is empty.

Variables

View Source
var (
	ValueVactory = exec.NewValueFactory(gonja.StrictUndefined, map[reflect.Type]exec.ValueFunc{})
	NewValue     = ValueVactory.Value
	NewSafeValue = ValueVactory.SafeValue
	NewVarArgs   = func(args []exec.Value, kwargs []exec.KVPair) *exec.VarArgs {
		return exec.NewVarArgsWithValues(ValueVactory, args, kwargs)
	}
)
View Source
var Fixtures = map[string]any{
	"number": 11,
	"simple": map[string]any{
		"number":                   42,
		"name":                     "john doe",
		"included_file":            "INCLUDES.helper",
		"included_file_not_exists": "INCLUDES.helper.not_exists",
		"nil":                      nil,
		"uint":                     uint(8),
		"float":                    float64(3.1415),
		"str":                      "string",
		"chinese_hello_world":      "你好世界",
		"bool_true":                true,
		"bool_false":               false,
		"newline_text": `this is a text
with a new line in it`,
		"long_text": `This is a simple text.

This too, as a paragraph.
Right?

Yep!`,
		"escape_js_test":     `escape sequences \r\n\'\" special chars "?!=$<>`,
		"one_item_list":      []int{99},
		"multiple_item_list": []int{1, 1, 2, 3, 5, 8, 13, 21, 34, 55},
		"unsorted_int_list":  []int{192, 581, 22, 1, 249, 9999, 1828591, 8271},
		"fixed_item_list":    [...]int{1, 2, 3, 4},
		"misc_list":          []any{"Hello", 99, 3.14, "good"},
		"escape_text":        "This is \\a Test. \"Yep\". 'Yep'.",
		"xss":                "<script>alert(\"uh oh\");</script>",
		"intmap": map[int]string{
			1: "one",
			5: "five",
			2: "two",
		},
		"strmap": map[string]string{
			"abc": "def",
			"bcd": "efg",
			"zab": "cde",
			"gh":  "kqm",
			"ukq": "qqa",
			"aab": "aba",
		},
		"casedStrmap": map[string]string{
			"a": "a",
			"B": "B",
			"c": "c",
			"D": "D",
			"e": "e",
			"F": "F",
		},
		"func_add": func(a, b int) int {
			return a + b
		},
		"func_add_iface": func(a, b any) any {
			return a.(int) + b.(int)
		},
		"func_variadic": func(msg string, args ...any) string {
			return fmt.Sprintf(msg, args...)
		},
		"func_variadic_sum_int": func(args ...int) int {

			s := 0
			for _, i := range args {
				s += i
			}
			return s
		},
		"func_variadic_sum_int2": func(args ...exec.Value) exec.Value {

			s := 0
			for _, i := range args {
				s += i.Integer()
			}
			return ValueVactory.Value(s)
		},
		"func_with_varargs": func(params *exec.VarArgs) exec.Value {

			argsAsStr := []string{}
			for _, arg := range params.Args {
				argsAsStr = append(argsAsStr, arg.String())
			}
			kwargsAsStr := []string{}
			for _, kv := range params.Kwargs {
				v := kv.Value.String()
				if kv.Value.IsString() {
					v = "\"" + v + "\""
				}
				pair := []string{kv.Key, v}
				kwargsAsStr = append(kwargsAsStr, strings.Join(pair, "="))
			}
			sort.Strings(kwargsAsStr)
			args := strings.Join(argsAsStr, ", ")
			kwargs := strings.Join(kwargsAsStr, ", ")

			str := fmt.Sprintf("VarArgs(args=[%s], kwargs={%s})", args, kwargs)
			return ValueVactory.SafeValue(str)
		},
	},
	"complex": map[string]any{
		"user": &user{
			Name:      "john doe",
			Validated: true,
		},
		"is_admin": isAdmin,
		"post": post{
			Text:    "<h2>Hello!</h2><p>Welcome to my new blog page. I'm using gonja which supports {{ variables }} and {% tags %}.</p>",
			Created: time2,
		},
		"comments": []*comment{
			{
				Author: &user{
					Name:      "user1",
					Validated: true,
				},
				Date: time1,
				Text: "\"gonja is nice!\"",
			},
			{
				Author: &user{
					Name:      "user2",
					Validated: true,
				},
				Date: time2,
				Text: "comment2 with <script>unsafe</script> tags in it",
			},
			{
				Author: &user{
					Name:      "user3",
					Validated: false,
				},
				Date: time1,
				Text: "<b>hello!</b> there",
			},
		},
		"comments2": []*comment{
			{
				Author: &user{
					Name:      "user1",
					Validated: true,
				},
				Date: time2,
				Text: "\"gonja is nice!\"",
			},
			{
				Author: &user{
					Name:      "user1",
					Validated: true,
				},
				Date: time1,
				Text: "comment2 with <script>unsafe</script> tags in it",
			},
			{
				Author: &user{
					Name:      "user3",
					Validated: false,
				},
				Date: time1,
				Text: "<b>hello!</b> there",
			},
		},
	},
	"persons": []*person{
		{"John", "Doe", "male"},
		{"Jane", "Doe", "female"},
		{"Akira", "Toriyama", "male"},
		{"Selina", "Kyle", "female"},
		{"Axel", "Haustant", "male"},
	},
	"groupable": []map[string]string{
		{"grouper": "group 1", "value": "value 1-1"},
		{"grouper": "group 2", "value": "value 2-1"},
		{"grouper": "group 3", "value": "value 3-1"},
		{"grouper": "group 1", "value": "value 1-2"},
		{"grouper": "group 2", "value": "value 2-2"},
		{"grouper": "group 3", "value": "value 3-2"},
		{"grouper": "group 1", "value": "value 1-3"},
		{"grouper": "group 2", "value": "value 2-3"},
		{"grouper": "group 3", "value": "value 3-3"},
	},
}

Functions

func GlobErrorTests

func GlobErrorTests(t *testing.T, root string)

func GlobTemplateTests

func GlobTemplateTests(t *testing.T, root string, env *gonja.Environment)

func TestEnv

func TestEnv(root string) *gonja.Environment

Types

type Assertions

type Assertions struct {
	// contains filtered or unexported fields
}

Assertions is a collection of assertion methods that can be used to test conditions in your tests.

func NewAssert

func NewAssert(t TestingT) Assertions

NewAssert returns a new Assertions object that will log assertion failures.

func NewRequire

func NewRequire(t TestingT) Assertions

NewRequire returns a new Assertions object that will log assertion failures and stop test execution.

func (Assertions) Equal

func (a Assertions) Equal(exp, act any, msgAndArgs ...any) bool

Equal asserts that two objects are equal.

func (Assertions) EqualError

func (a Assertions) EqualError(expErr, actErr error, msgAndArgs ...any) bool

EqualError asserts that a function returned an error (i.e. not `nil`) and that it is equal to the provided error.

func (Assertions) Error

func (a Assertions) Error(err error, msgAndArgs ...any) bool

Error asserts that a function returned an error (i.e. not `nil`).

func (Assertions) False

func (a Assertions) False(exp bool, msgAndArgs ...any) bool

False asserts that the specified value is false.

func (Assertions) IsType

func (a Assertions) IsType(expType, obj any, msgAndArgs ...any) bool

IsType asserts that the specified object is of the specified type.

func (Assertions) Len

func (a Assertions) Len(obj any, length int, msgAndArgs ...any) bool

Len asserts that the specified object has specific length.

func (Assertions) Nil

func (a Assertions) Nil(obj any, msgAndArgs ...any) bool

Nil asserts that the specified object is nil.

func (Assertions) NoError

func (a Assertions) NoError(err error, msgAndArgs ...any) bool

NoError asserts that a function returned no error (i.e. `nil`).

func (Assertions) NotEqual

func (a Assertions) NotEqual(exp, act any, msgAndArgs ...any) bool

NotEqual asserts that two objects are not equal.

func (Assertions) NotNil

func (a Assertions) NotNil(obj any, msgAndArgs ...any) bool

NotNil asserts that the specified object is not nil.

func (Assertions) NotPanic

func (a Assertions) NotPanic(f func(), msgAndArgs ...any) bool

NotPanic asserts that the code inside the specified PanicTestFunc does not panic.

func (Assertions) Panic

func (a Assertions) Panic(f func(), msgAndArgs ...any) bool

Panic asserts that the code inside the specified PanicTestFunc panics.

func (Assertions) True

func (a Assertions) True(exp bool, msgAndArgs ...any) bool

True asserts that the specified value is true.

type TestingT

type TestingT interface {
	Name() string
	Errorf(format string, args ...any)
	Log(...any)
	FailNow()
}

TestingT is an interface wrapper around *testing.T.

Jump to

Keyboard shortcuts

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