sphere-group / pegasus

The Pegasus API for Sphere 2.0
BSD 2-Clause "Simplified" License
1 stars 0 forks source link

CommonJS standard stuff #13

Open joskuijpers opened 10 years ago

joskuijpers commented 10 years ago

After I implemented my modules, I also added assert.js and test.js, according to CommonJS/Unit Testing/1.0. It provides a number of assert functions, such as assert(), assert.equal(), assert.deepEqual() and assert.throws(). These can be used to prevent some errors.

Then there is test.js that contains only one function: run(). You provide an object with functions starting with the letters 'test' and it will execute all these. Very nice for making unit tests. In a module, you run the code:


exports['test 1'] = function() { assert.notEqual(5,1, "idiocy"); };

if(require.main == module)
    require("test").run(exports);

This means that you can include unit tests in your module, and run those tests if your module become the main module. This is how CommonJS describes it.

I think it is a neat little addition that does not cause trouble when added to pegasus but it will advertize good programming style.

joskuijpers commented 10 years ago

I forgot: I took some stuff from node.js util.js, that is: is* functions and an inherit() to make two constructors inherit each other. https://github.com/joskuijpers/Andromeda/blob/master/Andromeda/Resources/JavaScript/util.js It is much cleaner to use than when writing it yourself (and making mistakes with null and undefined).

joskuijpers commented 10 years ago

Because the implementation is bound to javascript, I also put the implementation in util.js.

fatcerberus commented 8 years ago

minisphere includes an assert module based on the CommonJS specification since v4.1.0. No test module though.