mooncake

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2022 License: MIT Imports: 4 Imported by: 0

README

Mooncake

A simple way to generate mocks for multiple purposes

Go Report Card Build Status codecov GitHub GoDoc

Table of Contents

What is Mooncake

Mooncake is a simple way to generate mocks for multiple purposes.

It was designed to be uncomplicated and simples, focused on development agility while reducing bureaucracy.

Compatible with different types of interfaces such as:

  • Default interfaces
type Simple interface{
  MyMethod()
}
  • Nested interfaces
type Nested interface{
  Simple
}
  • Generic Interfaces
type Generic[T,Z any] interface{
  MyCustomMethod(T) (T,Z)
}
  • Generic Nested Interfaces
type NestGeneric[T,Z any] interface{
  Generic[T,Z]
}

Development Status

This project is under development. Therefore, some features may contain minor instabilities, in addition to the possibility of new features being added periodically.

Getting Start

To start using mooncake you need to follow the steps below

Installation

To add mooncake to your project run:

go get github.com/GuilhermeCaruso/mooncake

To install the mooncake generator (moongen) run:

go install github.com/GuilhermeCaruso/mooncake/moongen@v0.0.1

Mooncake Configuration File

Once you have decided to use mooncake in your project you will need to create a configuration file

The file must be in the yaml extension. His name doesn't matter, however we recommend it to be mooncake

  • Create mooncake.yaml file

Once created the following template must be used

mocks:
  package: #package
  path: #path
  files:
    - #files
  output: #output
  prefix: #prefix
Field Definition Example
package package name of files created mocks
path path for the interfaces directory interfaces/
files list of interface files to be mocked -
output path to the directory of the generated files mocks/
prefix optional value to be added as prefix on generated files generated

How to generate

Once the configuration file is done, to generate the files, run:

moongen --file <path_to_config_file>

How to use

After you have generated the mocks, to use the resources you can go like this:

package example

import (
  "testing"

  "github.com/GuilhermeCaruso/mooncake"
)

func checkValue(t *testing.T, es SimpleInterface, expectedResult string) {
  v, err := es.Get()
  if v != expectedResult {
    t.Errorf("unexpected result. expected=%v got=%v", expectedResult, v)
  }
  if err != nil {
    t.Errorf("unexpected error. expected=<nil> got=%v", err.Error())
  }
}

func TestWithMock(t *testing.T) {
  // Prepare new Mooncake Agent
  a := mooncake.NewAgent()
  // Start Implementation using created agent
  ac := NewMockSimpleInterface(a)
  // Define the implementation and responses
  ac.Prepare().Get().SetReturn("mocked_value", nil)
  checkValue(t, ac, "mocked_value")
}

License

MIT licensed. See the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidNumberOfReturns = func(key string, expected, got int) error {
		return fmt.Errorf("invalid number of returns for %s. expected=%v got=%v", key, expected, got)
	}

	ErrInvalidTypeOfReturn = func(key string, expected, got interface{}) error {
		return fmt.Errorf("invalid type of return for %s. expected=%v got=%v", key, expected, got)
	}
)
View Source
var (
	ErrNoCallsRegistered = func(key string) error {
		return fmt.Errorf("no calls registered for %s", key)
	}
)

Functions

This section is empty.

Types

type AgentController

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

func NewAgentController

func NewAgentController(key string, imp reflect.Type) AgentController

func (*AgentController) AnyTime

func (ag *AgentController) AnyTime() *AgentController

func (*AgentController) Repeat

func (ag *AgentController) Repeat(count int) *AgentController

func (*AgentController) SetReturn

func (ag *AgentController) SetReturn(args ...interface{}) *AgentController

type MooncakeAgent

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

func NewAgent

func NewAgent() *MooncakeAgent

func (*MooncakeAgent) CleanQueue

func (ma *MooncakeAgent) CleanQueue() *MooncakeAgent

func (*MooncakeAgent) GetCall

func (ma *MooncakeAgent) GetCall(key string) []ReturnDetail

func (*MooncakeAgent) SetCall

func (ma *MooncakeAgent) SetCall(key string, typeImpl reflect.Type) *AgentController

type MooncakeLifetime

type MooncakeLifetime int
const (
	LT_ANY_TIME MooncakeLifetime = iota
	LT_ONE_CALL
	LT_REPEAT
)

type MooncakeTypes

type MooncakeTypes string
const (
	ANY MooncakeTypes = "any"
)

type ReturnDetail

type ReturnDetail struct {
	Value interface{}
	DType reflect.Type
}

Directories

Path Synopsis
examples
default
############################ Generated by Mooncake Date: 2022-08-28 17:30:16 Source: examples/interfaces/example.go ############################
############################ Generated by Mooncake Date: 2022-08-28 17:30:16 Source: examples/interfaces/example.go ############################
generic
############################ Generated by Mooncake Date: 2022-08-28 17:30:16 Source: examples/interfaces/example.go ############################
############################ Generated by Mooncake Date: 2022-08-28 17:30:16 Source: examples/interfaces/example.go ############################

Jump to

Keyboard shortcuts

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