1

I'm new to coffeescript and I try to create a library for some added syntactic sugar for both cofffeescript and javascript. It uses a lot of decorators, so I'm surprised that this test fails:

it 'sandbox', () ->

  id = (x) -> x
  fn = (y) -> y == 1
  f = id fn
  should(f).be.equal(fn)
  should(f 3).be.false()

What I think I'm doing:

  • create function id that returns its first argument.
  • create function fn that returns true iff its first argument is 1
  • apply id on fn. I expect the result f to be exactly the same (reference wise!) as fn.

should.js says that my result f isn't even a function:

1) Function guard predicate #bakeFunctionPredicate sandbox:
   TypeError: object is not a function
   at Context.<anonymous> (/Users/luftzug/private/jspatterns/test/patterns.test.coffee:31:7)
   at Test.Runnable.run (/Users/luftzug/private/jspatterns/node_modules/grunt-mocha-cli/node_modules/mocha/lib/runnable.js:221:32)
   at Runner.runTest (/Users/luftzug/private/jspatterns/node_modules/grunt-mocha-cli/node_modules/mocha/lib/runner.js:378:10)
   at /Users/luftzug/private/jspatterns/node_modules/grunt-mocha-cli/node_modules/mocha/lib/runner.js:456:12
   at next (/Users/luftzug/private/jspatterns/node_modules/grunt-mocha-cli/node_modules/mocha/lib/runner.js:303:14)
   at /Users/luftzug/private/jspatterns/node_modules/grunt-mocha-cli/node_modules/mocha/lib/runner.js:313:7
   at next (/Users/luftzug/private/jspatterns/node_modules/grunt-mocha-cli/node_modules/mocha/lib/runner.js:251:23)
   at Immediate._onImmediate (/Users/luftzug/private/jspatterns/node_modules/grunt-mocha-cli/node_modules/mocha/lib/runner.js:280:5)
   at processImmediate [as _immediateCallback] (timers.js:367:17)

I am very confused. Is it shouldjs that does something unexpected, or coffeescript is not being translated to the code I expect it to translate to?

Luftzig
  • 523
  • 4
  • 14
  • Are you sure the problem is with `f`? The [should.js documentation](https://github.com/shouldjs/should.js/wiki/Breaking-changes) notes that `be.false` changed to `be.false()` in version 7. Maybe you are still using an older version? – andersschuller Oct 20 '15 at 18:57
  • @andersschuller It fails on the line before that, so no, that's not the problem (but could be a problem, who knows). – Luftzig Oct 20 '15 at 20:29

2 Answers2

0

Error in stack is not AssertionError and that means problem not in should.js itself. I converted code from coffee to js and it looks correct. I think you do not have should function in your test: var should = require('should')

den bardadym
  • 2,747
  • 3
  • 25
  • 27
  • that's interesting. I have a `should = require 'should'` which is the coffeescript equivalent of your code elsewhere in the file, and I have other tests that do pass and check simple comparisons with should.js. I'll see if I might be shadowing it some how. – Luftzig Oct 20 '15 at 20:32
0

I took a look at this and could not reproduce the problem.

Questions:

  • What statement is at line 31? (line numbers and console.log can clarify what is going on here).
  • Why the TypeError (it is trying to invoke object as a function)
  • What does the file compile to (does the javascript show the problem more clearly)

There must be something going on outside what you have shown (old package version?, grunt-mocha-cli configuration?, ???)

Here is what I did to validate. NOTE: all npm packages are fresh installs.

I started with your base functionality and proved 'What I think I'm doing' true.

id = (x) -> x
fn = (y) -> y == 1
console.log "typeof fn: #{typeof fn}" # function
f = id fn
console.log "typeof f: #{typeof f}"   # function
console.log "fn is f: #{fn is f}"     # true
console.log "fn == f: #{fn == f}"     # true
# CoffeeScript thinks they are equal

Then I tested that 'should' assertions passed

should(f).be.equal(fn)
should(f 3).be.false()
# No assertions here

Then tried to recreate your test

describe 'function comparison', ->
  it 'should provide identity for functions', ->
    id = (x) -> x
    fn = (y) -> y == 1
    f = id fn
    should(f).be.equal(fn)
    should(f 3).be.false()

Using this Gruntfile.coffee

module.exports = (grunt) ->

  require('load-grunt-tasks')(grunt)

  grunt.initConfig
    pkg: grunt.file.readJSON 'package.json'

    mochacli:
      options:
        require: ['should', 'coffee-script/register']
        reporter: 'spec'
        compilers: ['coffee:coffee-script']
      all: ['coffeescript/functionComparison.coffee']

  # run test for functionComparison.coffee
  grunt.registerTask 'default', ['mochacli']

which yields

Running "mochacli:all" (mochacli) task

  function comparison
    ✓ should provide identity for functions

  1 passing (3ms)

Done, without errors.
Steve Tarver
  • 3,030
  • 2
  • 25
  • 33