Coder Social home page Coder Social logo

gospec's Introduction

gospec

An implementation of the specification pattern in Go.

The module includes a regular implementation and a traceable one, which can 'explain' its execution.

Quick Start

Embed BaseSpecification, instantiated to the type that will be tested, in your own specification. Ensure the method that checks for specification satisfaction has the following signature: IsSatisfiedBy(t T) bool.

type User struct {
  id      string
  name    string
}

type IsBob struct {
  BaseSpecification[User]
}

func NewIsBob() *IsBob {
  s := IsBob{}
  s.Init(&s)
  return &s
}

func (s *IsBob) IsSatisfiedBy(u User) bool {
  return u.name == "Bob"
}

Once you have defined and instantiated several specifications you can compose a more complex one by using Boolean operators.

adultSpec := NewIsLegalAdult()
flaggedSpec := NewIsFlagged()
bobSpec := NewIsBob()

compositeSpec := adultSpec.Not().Or(flaggedSpec).Or(bobSpec),

satisfied := compositeSpec.IsSatisfiedBy(userBob)

Use BaseTSpecification instead if you want to be able to trace the execution of Boolean operations in a composite specification. This type requires the addition of a GetName() string method to your specification and also the inclusion of a 'Tracer', an interface that can process operation trace instances (OpTrace).

type IsFlagged struct {
  gospec.BaseTSpecification[User]
  flaggedIDs  []string
  specName    string
}

func NewIsFlagged(t gospec.Tracer) *IsFlagged {
  s := IsFlagged{
    flaggedIDs: []string{"1"},
    specName:   "IsFlaggedSpec",
  }
  s.Init(&s, t)
  return &s
}

func (s *IsFlagged) IsSatisfiedBy(u User) bool {
  for _, id := range s.flaggedIDs {
    if id == u.id {
      return true
    }
  }
return false
}

func (s *IsFlagged) GetName() string {
  return s.specName
}

Operation trace instances can produce a verbose explanation of their contents.

Run the logged_specification example for a basic demonstration of its functionality.

go test -v examples/logged_specification_test.go

Development Status: Beta

Core features have been tested, trace features have not been tested.

gospec's People

Contributors

jrlangford avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.