123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
-
- var thunkify = require('..');
- var assert = require('assert');
- var fs = require('fs');
-
- describe('thunkify(fn)', function(){
- it('should work when sync', function(done){
- function read(file, fn) {
- fn(null, 'file: ' + file);
- }
-
- read = thunkify(read);
-
- read('foo.txt')(function(err, res){
- assert(!err);
- assert('file: foo.txt' == res);
- done();
- });
- })
-
- it('should work when async', function(done){
- function read(file, fn) {
- setTimeout(function(){
- fn(null, 'file: ' + file);
- }, 5);
- }
-
- read = thunkify(read);
-
- read('foo.txt')(function(err, res){
- assert(!err);
- assert('file: foo.txt' == res);
- done();
- });
- })
-
- it('should maintain the receiver', function(done){
- function load(fn) {
- fn(null, this.name);
- }
-
- var user = {
- name: 'tobi',
- load: thunkify(load)
- };
-
- user.load()(function(err, name){
- if (err) return done(err);
- assert('tobi' == name);
- done();
- });
- })
-
- it('should catch errors', function(done){
- function load(fn) {
- throw new Error('boom');
- }
-
- load = thunkify(load);
-
- load()(function(err){
- assert(err);
- assert('boom' == err.message);
- done();
- });
- })
-
- it('should ignore multiple callbacks', function(done){
- function load(fn) {
- fn(null, 1);
- fn(null, 2);
- fn(null, 3);
- }
-
- load = thunkify(load);
-
- load()(done);
- })
-
- it('should pass all results', function(done){
- function read(file, fn) {
- setTimeout(function(){
- fn(null, file[0], file[1]);
- }, 5);
- }
-
- read = thunkify(read);
-
- read('foo.txt')(function(err, a, b){
- assert(!err);
- assert('f' == a);
- assert('o' == b);
- done();
- });
- })
-
- it('should work with node methods', function(done){
- fs.readFile = thunkify(fs.readFile);
-
- fs.readFile('package.json')(function(err, buf){
- assert(!err);
- assert(Buffer.isBuffer(buf));
-
- fs.readFile('package.json', 'utf8')(function(err, str){
- assert(!err);
- assert('string' == typeof str);
- done();
- });
- });
- })
- })
|