conveyz

package module
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2024 License: MIT Imports: 17 Imported by: 0

README

gopherz

ezpkg.io/conveyz

PkgGoDev GitHub License version

Package conveyz extends the package convey with additional functionality and make it work with gomega. See the original blog post.

Installation

go get -u ezpkg.io/[email protected]

Features

  • Convey functions to group tests. SConvey to skip and FConvey to focus on a test.
  • Tests can be nested, and will be executed in nested order. In the example below, it will print:
[0] → [1] → [1.1]
[0] → [1] → [1.2]
[0] → [2] → [2.1]
[0] → [2] → [2.2]
Differences to the original package
  • This package ezpkg.io/conveyz uses gomega for assertions. To me, gomega is more powerful and easier to use.
  • With this package, we only need to set FConvey at a single block level. The original package requires to set FocusConvey at every nested level.
  • FConvey and SConvey will make go test fail. This is to avoid accidentally skipping tests. To skip tests but not make go test fail, use SkipConveyAsTODO.
  • Output looks better with more colors. Stacktrace is nicer.

Examples

See conveyz/examples/conveyz_test.go or stringz/stringz_test.go for more examples.

import (
	"fmt"
	"testing"

	. "ezpkg.io/conveyz"
)

func Test(t *testing.T) {
	Convey("Start", t, func() {
		s := "[0]"
		defer func() { fmt.Printf("\n%s\n", s) }()

		add := func(part string) {
			s = AppendStr(s, part)
		}

		Convey("Test 1:", func() {
			add(" → [1]")
			Ω(s).To(Equal("[0] → [1]"))

			Convey("Test 1.1:", func() {
				add(" → [1.1]")
				Ω(s).To(Equal("[0] → [1] → [1.1]"))
			})
			Convey("Test 1.2:", func() {
				add(" → [1.2]")
				Ω(s).To(Equal("[0] → [1] → [1.2]"))
			})
		})
        // 👇change to FConvey to focus on this block and all children
        // 👇change to SConvey to skip the block
		// 👇change to SkipConveyAsTODO to mark as TODO
		Convey("Test 2:", func() {
			add(" → [2]")
			Ω(s).To(Equal("[0] → [2]"))
			
			Convey("Test 2.1:", func() {
				add(" → [2.1]")
				Ω(s).To(Equal("[0] → [2] → [2.1]"))
			})
			Convey("Test 2.2:", func() {
				add(" → [2.2]")
				Ω(s).To(Equal("[0] → [2] → [2.2]"))
			})
		})
		SkipConvey("failure message", func() {
			// 👆change SkipConvey to Convey to see failure messages
			// 👆change SkipConvey to SkipConveyAsTODO to mark as TODO

			Convey("fail", func() {
				//  Expected
				//      <string>: [0] → [2]
				//  to equal
				//      <string>: this test will fail
				Ω(s).To(Equal("this test will fail"))
			})
			Convey("UNEXPECTED", func() {
				// UNEXPECTED: Refusing to compare <nil> to <nil>.
				//  Be explicit and use BeNil() instead.  This is to avoid mistakes where both sides of an assertion are erroneously uninitialized.
				Ω(nil).To(Equal(nil))
			})
		})
	})
}

func AppendStr(s, part string) string {	return s + part }
func WillPanic() { panic("let's panic! 💥") }
func CallFunc(fn func()) { fn() }
The output will look like this:

About ezpkg.io

As I work on various Go projects, I often find myself creating utility functions, extending existing packages, or developing packages to solve specific problems. Moving from one project to another, I usually have to copy or rewrite these solutions. So I created this repository to have all these utilities and packages in one place. Hopefully, you'll find them useful as well.

For more information, see the main repository.

Author

Oliver Nguyen  github

Documentation

Overview

Package conveyz extends the package convey with additional functionality and make it work with gomega.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func And added in v0.2.0

func BeADirectory added in v0.2.0

func BeADirectory() types.GomegaMatcher

func BeARegularFile added in v0.2.0

func BeARegularFile() types.GomegaMatcher

func BeAnExistingFile added in v0.2.0

func BeAnExistingFile() types.GomegaMatcher

func BeAssignableToTypeOf added in v0.2.0

func BeAssignableToTypeOf(expected any) types.GomegaMatcher

func BeClosed added in v0.2.0

func BeClosed() types.GomegaMatcher

func BeComparableTo added in v0.2.0

func BeComparableTo(expected any, opts ...cmp.Option) types.GomegaMatcher

func BeElementOf added in v0.2.0

func BeElementOf(elements ...any) types.GomegaMatcher

func BeEmpty added in v0.2.0

func BeEmpty() types.GomegaMatcher

func BeEquivalentTo added in v0.2.0

func BeEquivalentTo(expected any) types.GomegaMatcher

func BeFalse added in v0.2.0

func BeFalse() types.GomegaMatcher

func BeFalseBecause added in v0.2.0

func BeFalseBecause(format string, args ...any) types.GomegaMatcher

func BeIdenticalTo added in v0.2.0

func BeIdenticalTo(expected any) types.GomegaMatcher

func BeKeyOf added in v0.2.0

func BeKeyOf(element any) types.GomegaMatcher

func BeNil added in v0.2.0

func BeNil() types.GomegaMatcher

func BeNumerically added in v0.2.0

func BeNumerically(comparator string, compareTo ...any) types.GomegaMatcher

func BeSent added in v0.2.0

func BeSent(arg any) types.GomegaMatcher

func BeTemporally added in v0.2.0

func BeTemporally(comparator string, compareTo time.Time, threshold ...time.Duration) types.GomegaMatcher

func BeTrue added in v0.2.0

func BeTrue() types.GomegaMatcher

func BeTrueBecause added in v0.2.0

func BeTrueBecause(format string, args ...any) types.GomegaMatcher

func BeZero added in v0.2.0

func BeZero() types.GomegaMatcher

func ConsistOf added in v0.2.0

func ConsistOf(elements ...any) types.GomegaMatcher

func ContainElement added in v0.2.0

func ContainElement(element any, result ...any) types.GomegaMatcher

func ContainElements added in v0.2.0

func ContainElements(elements ...any) types.GomegaMatcher

func ContainSubstring added in v0.2.0

func ContainSubstring(substr string, args ...any) types.GomegaMatcher

func Convey

func Convey(items ...any)

func Equal added in v0.2.0

func Equal(expected any) types.GomegaMatcher

func FConvey

func FConvey(items ...any)

FConvey (alias of FocusConvey) runs the current scope and all child scopes, but skips all other scopes. It also makes the test fail.

func FocusConvey

func FocusConvey(items ...any)

FocusConvey runs the current scope and all child scopes, but skips all other scopes. It also makes the test fail.

func GomegaExpect

func GomegaExpect(actual any, extra ...any) gomega.Assertion

GomegaExpect is an adapter to make gomega work with goconvey.

Usage: Ω := GomegaExpect

func HaveCap added in v0.2.0

func HaveCap(count int) types.GomegaMatcher

func HaveEach added in v0.2.0

func HaveEach(element any) types.GomegaMatcher

func HaveExactElements added in v0.2.0

func HaveExactElements(elements ...any) types.GomegaMatcher

func HaveExistingField added in v0.2.0

func HaveExistingField(field string) types.GomegaMatcher

func HaveField added in v0.2.0

func HaveField(field string, expected any) types.GomegaMatcher

func HaveHTTPBody added in v0.2.0

func HaveHTTPBody(expected any) types.GomegaMatcher

func HaveHTTPHeaderWithValue added in v0.2.0

func HaveHTTPHeaderWithValue(header string, value any) types.GomegaMatcher

func HaveHTTPStatus added in v0.2.0

func HaveHTTPStatus(expected ...any) types.GomegaMatcher

func HaveKey added in v0.2.0

func HaveKey(key any) types.GomegaMatcher

func HaveKeyWithValue added in v0.2.0

func HaveKeyWithValue(key any, value any) types.GomegaMatcher

func HaveLen added in v0.2.0

func HaveLen(count int) types.GomegaMatcher

func HaveOccurred added in v0.2.0

func HaveOccurred() types.GomegaMatcher

func HavePrefix added in v0.2.0

func HavePrefix(prefix string, args ...any) types.GomegaMatcher

func HaveSuffix added in v0.2.0

func HaveSuffix(suffix string, args ...any) types.GomegaMatcher

func HaveValue added in v0.2.0

func HaveValue(matcher types.GomegaMatcher) types.GomegaMatcher

func MatchError added in v0.2.0

func MatchError(expected any, functionErrorDescription ...any) types.GomegaMatcher

func MatchJSON added in v0.2.0

func MatchJSON(json any) types.GomegaMatcher

func MatchRegexp added in v0.2.0

func MatchRegexp(regexp string, args ...any) types.GomegaMatcher

func MatchXML added in v0.2.0

func MatchXML(xml any) types.GomegaMatcher

func MatchYAML added in v0.2.0

func MatchYAML(yaml any) types.GomegaMatcher

func Not added in v0.2.0

func Or added in v0.2.0

func Panic added in v0.2.0

func Panic() types.GomegaMatcher

func PanicWith added in v0.2.0

func PanicWith(expected any) types.GomegaMatcher

func Receive added in v0.2.0

func Receive(args ...any) types.GomegaMatcher

func Reset

func Reset(action func())

Reset registers a cleanup function to run after each Convey() in the same scope.

func SConvey

func SConvey(items ...any)

SConvey (alias of SkipConvey) skips the current scope and all child scopes. It also makes the test fail.

func Satisfy added in v0.2.0

func Satisfy(predicate any) types.GomegaMatcher

func SatisfyAll added in v0.2.0

func SatisfyAll(matchers ...types.GomegaMatcher) types.GomegaMatcher

func SatisfyAny added in v0.2.0

func SatisfyAny(matchers ...types.GomegaMatcher) types.GomegaMatcher

func SkipConvey

func SkipConvey(items ...any)

SkipConvey skips the current scope and all child scopes. It also makes the test fail.

func SkipConveyAsTODO added in v0.1.0

func SkipConveyAsTODO(items ...any)

SkipConveyAsTODO is similar to SkipConvey but does not make the test fail.

func Succeed added in v0.2.0

func Succeed() types.GomegaMatcher

func WithTransform added in v0.2.0

func WithTransform(transform any, matcher types.GomegaMatcher) types.GomegaMatcher

func Ω added in v0.2.0

func Ω(actual any, extra ...any) gomega.Assertion

Ω is a shortcut for GomegaExpect, which is an adapter to make gomega work with goconvey.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳