Beispiel #1
0
  it('should call the matching pattern function with value when a pattern matches', () => {
    const whenMock = createMockFunction().returns('pattern')

    expect(pattern().when(String, whenMock)('value')).to.equal('pattern')
    expect(whenMock.calls.length).to.equal(1)
    expect(whenMock.calls[0].args).to.deep.equal(['value'])
  })
Beispiel #2
0
  it('should call the default function with value when no pattern matches', () => {
    const defaultMock = createMockFunction().returns('default')

    expect(pattern().default(defaultMock)('value')).to.equal('default')
    expect(defaultMock.calls.length).to.equal(1)
    expect(defaultMock.calls[0].args).to.deep.equal(['value'])
  })
Beispiel #3
0
    it('should support Array', () => {
      const match = pattern()
        .when(Array, true)
        .default(false)

      expect(match([])).to.be.true
      expect(match({})).to.be.false
    })
Beispiel #4
0
    it('should not match non-objects', () => {
      const match = pattern()
        .when({}, true)
        .default(false)

      expect(match(1)).to.be.false
      expect(match(false)).to.be.false
      expect(match(null)).to.be.false
    })
Beispiel #5
0
    it('should not match non-arrays', () => {
      const match = pattern()
        .when([], true)
        .default(false)

      expect(match({})).to.be.false
      expect(match(false)).to.be.false
      expect(match(null)).to.be.false
    })
Beispiel #6
0
    it('should support catching all arrays', () => {
      const match = pattern()
        .when([], true)
        .default(false)

      expect(match([])).to.be.true
      expect(match([1])).to.be.true
      expect(match([[], {}])).to.be.true
    })
Beispiel #7
0
    it('should support catching all objects', () => {
      const match = pattern()
        .when({}, true)
        .default(false)

      expect(match({})).to.be.true
      expect(match([])).to.be.true
      expect(match({foo: 'bar'})).to.be.true
    })
Beispiel #8
0
  it('fibonacci', () => {
    const fib = pattern()
      .when(0, 0)
      .when(1, 1)
      .default(n => fib(n - 1) + fib(n - 2))

    expect(fib(5)).to.equal(5)
    expect(fib(8)).to.equal(21)
  })
Beispiel #9
0
    it('should support Number', () => {
      const match = pattern()
        .when(Number, true)
        .default(false)

      expect(match(1)).to.be.true
      expect(match(0)).to.be.true
      expect(match(true)).to.be.false
      expect(match(null)).to.be.false
    })
Beispiel #10
0
  it('should pass extra parameters to the default function', () => {
    const defaultMock = createMockFunction()

    pattern()
      .default(defaultMock)
      ('value', 'extra', 'parameters')

    expect(defaultMock.calls.length).to.equal(1)
    expect(defaultMock.calls[0].args).to.deep.equal(['value', 'extra', 'parameters'])
  })