Code Monkey home page Code Monkey logo

ftests's Introduction

Overview

This package provides a simple and systematic way to test functions: we merely check that on given inputs, functions indeed compute the expected outputs; tests are still managed via the standard testing package.

Tests can then be wrapped in a simple struct{}:

type Test struct {
	Name string // Test name
	Fun any // Function to test
	Args []any // Inputs
	Expected []any // Expected outputs
}

Arrays of tests may then be tested using ftests.Run():

import (
	"testing"
	"github.com/mbivert/ftests"
)

func TestSomething(t *testing.T) {
	ftests.Run(t, []ftests.Test{
		{
			"(x -> x)(42) = 42",
			func(x any) any { return x },
			[]interface{}{42},
			[]interface{}{42},
		},
		{
    		...
		},
		...
	})
}

"Weird" cases (e.g. environment alteration) can be managed by wrapping the code with a few extra functions. See this blog post for some examples.

Note: Because of the regularity of the tests format, it's easy to move towards or away from this pattern, to programatically alter the tests, to run the same tests in different settings, etc. In particular, it's a great way to progressively add tests to an existing codebase with almost no overhead.

Other languages (Nix, JavaScript, Perl, etc.)

Implementations for other languages are also provided. The main requirements are rather standard nowadays:

  • tuples;
  • deep-comparison mechanism.

Usage examples

ftests's People

Contributors

mbivert 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.