ts

package module
v0.0.2-alpha Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2024 License: GPL-3.0 Imports: 7 Imported by: 0

README

Testcases (ts)

About

That is simple wrap testify's Suite to write testcases and run them.

It's some kind of experiment.

Support:

  • Check expected results without error ts.ToTestValuesExpected(expected ...interfaces{}) or ts.TTVE(expected ...interfaces{}).
  • Check expected results with nil error ts.ToTestValuesExpectedWithNilError(expected ...interfaces{}) or ts.TTVEWNE(expected ...interfaces{}).
  • Check expected errors (expected error as last returned parameters) . ts.ToTestErrorExpected(err error) or ts.TTEE(err error)
  • Check panic error ts.ToTestPanicErrorExpected(msg interface{}) or ts.TTPEE(msg interface{}).
  • Check not nil error ts.ToTestCheckErrorExpected() or ts.TTCEE().

Examples

package test

import (
	"testing"
	"github.com/ThCompiler/ts"
)

func sum(a, b int64) int64 {
	return a + b
}

type SumSuite struct {
	ts.TestCasesSuite
}

func (s *SumSuite) TestCorrectNumber() {
	s.RunTest(
		sum,
		ts.TestCase{
			Name:     "Zero With One",
			Args:     ts.TTA(int64(0), int64(1)),
			Expected: ts.TTVE(int64(1)),
		},
		ts.TestCase{
			Name:     "Two With One",
			Args:     ts.TTA(int64(2), int64(1)),
			Expected: ts.TTVE(int64(3)),
		},
		ts.TestCase{
			Name:     "Ten With One",
			Args:     ts.TTA(int64(10), int64(1)),
			Expected: ts.TTVE(int64(11)),
		},
		ts.TestCase{
			Name:     "One With One",
			Args:     ts.TTA(int64(1), int64(1)),
			Expected: ts.TTVE(int64(2)),
		},
	)
}

func TestSumSuite(t *testing.T) {
	suite.Run(t, new(SumSuite))
}

This example is located in the example folder.

Future

  • Add documentation.
  • Add Some feature to work with gmock (now, i has no idea how it will be work and be comfortable for using).
  • Improve the way to set the function under test.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RunTest

func RunTest(t *testing.T, fun any, cases ...TestCase)

RunTest is a function to run test cases for testing function out of TestCasesSuite

func TTA

func TTA(args ...interface{}) []interface{}

TTA is a wrapper over a function ToTestArgs to shorten its name

func ToTestArgs

func ToTestArgs(args ...interface{}) []interface{}

ToTestArgs is a small function to convert a list of args to an array of []interfaces{} It can be easier write ToTestArgs(a, b, c) then []interface{}{a, b, c}.

Types

type MockTestFunction

type MockTestFunction func(ctrl *gomock.Controller) []interface{}

MockTestFunction is a interface of function to initialize mocks for a test case

type TestCase

type TestCase struct {
	Name      string           // Name of test
	Args      []interface{}    // Args of testing function
	Expected  TestExpected     // Info about expected return of testing function
	InitMocks MockTestFunction // For Mock initialization !!!future
}

TestCase is a structure describing the test case

type TestCasesSuite

type TestCasesSuite struct {
	suite.Suite
}

TestCasesSuite is a wrapper over a suite.Suite with added methods for run test cases.

func (*TestCasesSuite) RunTest

func (s *TestCasesSuite) RunTest(fun any, cases ...TestCase)

RunTest is a function to run a list of test cases for the tested function

type TestErrorExpected

type TestErrorExpected struct {
	CheckError bool // indication that it is only necessary to check
	// for an error, without checking for a match with the expected one
	Error error // expected error
}

TestErrorExpected is a struct contained info about expected returns of tested function

type TestExpected

type TestExpected struct {
	PanicError      *TestPanicErrorExpected // expected panic message of tested function
	Error           *TestErrorExpected      // expected error of tested function
	ExpectedReturns []interface{}           // expected returns of tested function
}

TestExpected is a struct contained info about expected returns of tested function

func TTCEE

func TTCEE() TestExpected

TTCEE is a wrapper over a function ToTestCheckErrorExpected to shorten its name

func TTE

func TTE(checkError bool, err error, withPanic bool, panicMsg string,
	expedites ...interface{},
) TestExpected

TTE is a wrapper over a function ToTestExpected to shorten its name

func TTEE

func TTEE(err error) TestExpected

TTEE is a wrapper over a function ToTestErrorExpected to shorten its name

func TTPEE

func TTPEE(msg interface{}) TestExpected

TTPEE is a wrapper over a function ToTestPanicErrorExpected to shorten its name

func TTVE

func TTVE(expedites ...interface{}) TestExpected

TTVE is a wrapper over a function ToTestValuesExpected to shorten its name

func TTVEWNE

func TTVEWNE(expedites ...interface{}) TestExpected

TTVEWNE is a wrapper over a function ToTestValuesExpectedWithNilError to shorten its name

func ToTestCheckErrorExpected

func ToTestCheckErrorExpected() TestExpected

ToTestCheckErrorExpected is a function to construct test exception with only waiting for the not specified error as the result of the function under test

func ToTestErrorExpected

func ToTestErrorExpected(err error) TestExpected

ToTestErrorExpected is a function to construct test exception with only waiting for the specified error as the result of the function under test

func ToTestExpected

func ToTestExpected(checkError bool, err error, withPanic bool, panicMsg interface{},
	expedites ...interface{},
) TestExpected

ToTestExpected is a function to construct test exception all variant of waiting results of the function under test

func ToTestPanicErrorExpected

func ToTestPanicErrorExpected(msg interface{}) TestExpected

ToTestPanicErrorExpected is a function to construct test exception with only waiting for the panic message as the result of the function under test

func ToTestValuesExpected

func ToTestValuesExpected(expedites ...interface{}) TestExpected

ToTestValuesExpected is a function to construct test exception with only waiting for the specified arguments and no one error as the result of the function under test

func ToTestValuesExpectedWithNilError

func ToTestValuesExpectedWithNilError(expedites ...interface{}) TestExpected

ToTestValuesExpectedWithNilError is a function to construct test exception with only waiting for

the specified arguments and nil error as the result of the function under test

func (*TestExpected) HaveError

func (te *TestExpected) HaveError() bool

HaveError is a function checking if an error is expected

func (*TestExpected) HavePanicError

func (te *TestExpected) HavePanicError() bool

HavePanicError is a function checking if a panic is expected

func (*TestExpected) MustErrorExpected

func (te *TestExpected) MustErrorExpected() TestErrorExpected

MustErrorExpected is a function returning expected error. If it does not exist, function panic with error

func (*TestExpected) MustPanicErrorExpected

func (te *TestExpected) MustPanicErrorExpected() TestPanicErrorExpected

MustPanicErrorExpected is a function returning expected panic error. If it does not exist, function panic with error

type TestPanicErrorExpected

type TestPanicErrorExpected struct {
	Msg interface{} // expected message
}

TestPanicErrorExpected is a struct contained info about the expected panic message of tested function

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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