spectest

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2024 License: MIT Imports: 4 Imported by: 17

README

spectest

Idea

define testable specs of an interface.

the expected behavior of the interface is defined by the tests and the resulting testsuite can be used, to make sure that the implementation behaves according to the specs.

additionally the specs can be printed in markdown format.

Usage

1. define your specs
package mylibrary

import (
  "gitlab.com/golang-utils/spectest"
  "fmt"
)

type MyInterface1 interface {
  DoStuff1() string
  DoStuff2() string
  MyInterface2
}

type MyInterface2 interface {
  DoOtherStuff() string
}

func MySpecSuite(gen func() MyInterface1) *spectest.Suite {
  name := "MyLibSpecTests"

  if gen != nil {
    name += fmt.Sprintf(" for %T", gen())
  }

  st := spectest.NewSuite(name, "the testsuite for the library")
  sp1 := st.NewSpec("MyInterface1", "spec of the MyInterface1 interface")
  sp1.AddTest("DoStuff1", func(tt *testing.T) {
    impl1 := gen()
    got := impl1.DoStuff1()
    expected := "something"
    if got != expected {
      tt.Errorf("MyInterface1#DoStuff1() == %q // expected %q", got, expected)
    }
  })
  sp1.AddTest("DoStuff2", func(tt *testing.T) {
    impl1 := gen()
    got := impl1.DoStuff2()
    expected := "something2"
    if got != expected {
      tt.Errorf("MyInterface1#DoStuff2() == %q // expected %q", got, expected)
    }
  })
  

  sp2 := st.NewSpec("MyInterface2", "spec of the MyInterface2 interface")
  sp2.AddTest("DoOtherStuff", func(tt *testing.T) {
    impl1 := gen()
    got := impl1.DoOtherStuff()
    expected := "something else"
    if got != expected {
      tt.Errorf("MyInterface2#DoOtherStuff() == %q // expected %q", got, expected)
    }
  })

  sp1.AddSubSpec(sp2)
  st.AddSpec(sp1)

  return st
2. document the specs by printing it as markdown
package main

import (
  "fmt"
  "os"
  "mylibrary"
)

func main() {
  mylibrary.MySpecSuite(nil).WriteTo(os.Stdout)
}

3. the specs can be used to verify that an implementation is behaving correctly
package myimpl_test

import (
  "testing"
  "mylibrary"
)

func TestBehaviorAccodingToSpecs(t *testing.T) {
  mylibrary.MySpecSuite(func () mylibrary.MyInterface1 {
    return NewImplementation()
  }).Run(t)
}

Documentation

see https://pkg.go.dev/gitlab.com/golang-utils/spectest

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Spec

type Spec struct {
	Context     string
	Description string

	SubSpecs []*Spec

	// BeforeRun is a callback that is called before running the spec
	BeforeRun func(*Spec)

	// AfterRun is a callback that is called after running the spec
	AfterRun func(*Spec)

	// BeforeTest is a callback that is called before running each test of the spec
	BeforeTest func(*SpecTest)

	// AfterTest is a callback that is called after running each test of the spec
	AfterTest func(*SpecTest)
	// contains filtered or unexported fields
}

func NewSpec

func NewSpec(context, description string) *Spec

func (*Spec) AddSubSpec

func (s *Spec) AddSubSpec(sp *Spec)

func (*Spec) AddTest

func (s *Spec) AddTest(name string, fn func(t *testing.T))

func (*Spec) Run

func (s *Spec) Run(outername string, t *testing.T) (success bool)

func (*Spec) WriteMarkdown

func (s *Spec) WriteMarkdown(prefix string, wr io.Writer)

type SpecTest

type SpecTest struct {
	Name   string
	TestFn func(t *testing.T)
}

func (*SpecTest) Call

func (s *SpecTest) Call(t *testing.T)

type Suite

type Suite struct {
	Name        string
	Description string
	Specs       []*Spec
}

func NewSuite

func NewSuite(name, description string) *Suite

func (*Suite) AddSpec

func (s *Suite) AddSpec(sp *Spec)

func (*Suite) Run

func (s *Suite) Run(name string, t *testing.T) (success bool)

func (*Suite) WriteTo

func (s *Suite) WriteTo(wr io.Writer) (int64, error)

Jump to

Keyboard shortcuts

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