simple-squiggle

A restricted subset of Squiggle
Log | Files | Refs | README

onMismatch.test.js (1164B)


      1 var assert = require('assert');
      2 var typed = require('../typed-function');
      3 
      4 describe('onMismatch handler', () => {
      5   const square = typed('square', {
      6     number: x => x*x,
      7     string: s => s + s
      8   })
      9 
     10   it('should replace the return value of a mismatched call', () => {
     11     typed.onMismatch = () => 42
     12     assert.strictEqual(square(5), 25)
     13     assert.strictEqual(square('yo'), 'yoyo')
     14     assert.strictEqual(square([13]), 42)
     15   })
     16 
     17   const myErrorLog = []
     18   it('should allow error logging', () => {
     19     typed.onMismatch = (name, args, signatures) => {
     20       myErrorLog.push(typed.createError(name, args, signatures))
     21       return null
     22     }
     23     square({the: 'circle'})
     24     square(7)
     25     square('me')
     26     square(1,2)
     27     assert.strictEqual(myErrorLog.length, 2)
     28     assert('data' in myErrorLog[0])
     29   })
     30 
     31   it('should allow changing the error', () => {
     32     typed.onMismatch = name => { throw Error('Problem with ' + name) }
     33     assert.throws(() => square(['one']), /Problem with square/)
     34   })
     35 
     36   it('should allow a return to standard behavior', () => {
     37     typed.onMismatch = typed.throwMismatchError
     38     assert.throws(() => square('be', 'there'), TypeError)
     39   })
     40 })