Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

fantasy-laws

Package Overview
Dependencies
Maintainers
13
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fantasy-laws

Property-based tests for Fantasy Land -compliant algebraic data types

  • 1.1.0
  • Source
  • npm
  • Socket score

Version published
Maintainers
13
Created
Source

fantasy-laws

Property-based tests to verify the lawfulness of Fantasy Land -compliant algebraic data types.

Installation

Add fantasy-laws, jsverify, sanctuary-show, and sanctuary-type-classes to "devDependencies" in package.json, then run npm install.

Usage

Usage is best explained by example. The following code defines a Sum type which is intended to satisfy Setoid, Semigroup, Monoid, and Group:

function Sum(value) {
  if (!(this instanceof Sum)) return new Sum (value);
  this.value = value;
}

//  Sum.fantasy-land/empty :: () -> Sum
Sum['fantasy-land/empty'] = function() { return Sum (0); };

//  Sum#fantasy-land/equals :: Sum ~> Sum -> Boolean
Sum.prototype['fantasy-land/equals'] = function(other) {
  return Z.equals (this.value, other.value);
};

//  Sum#fantasy-land/concat :: Sum ~> Sum -> Sum
Sum.prototype['fantasy-land/concat'] = function(other) {
  return Sum (this.value + other.value);
};

//  Sum#fantasy-land/invert :: Sum ~> () -> Sum
Sum.prototype['fantasy-land/invert'] = function() {
  return Sum (-this.value);
};

The following steps demonstrate how to test the Group laws:

  1. Require fantasy-laws, jsverify, sanctuary-show, and sanctuary-type-classes:

    const laws = require ('fantasy-laws');
    const jsc = require ('jsverify');
    const show = require ('sanctuary-show');
    const Z = require ('sanctuary-type-classes');
    
  2. Require the type to be tested:

    const Sum = require ('../Sum');
    
  3. Define an "arbitrary" for the type:

    //    SumArb :: Arbitrary Sum
    const SumArb = jsc.number.smap (Sum, sum => sum.value, show);
    
  4. Provide the fixed parameters to laws.Group:

    const {leftInverse, rightInverse} = laws.Group (Z.equals, Sum);
    
  5. Provide the appropriate number of arbitraries to the function associated with a particular law to produce a thunk:

    //    testLeftInverse :: () -> Undefined !
    const testLeftInverse = leftInverse (SumArb);
    
    //    testRightInverse :: () -> Undefined !
    const testRightInverse = rightInverse (SumArb);
    
  6. To run the tests, invoke the thunk or use a test runner such as Mocha:

    suite ('Group laws', () => {
      test ('left inverse', testLeftInverse);
      test ('right inverse', testRightInverse);
    });
    

FAQs

Package last updated on 28 May 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc