<|start_filename|>node_modules/express-hbs/test/apps/async/index.js<|end_filename|>
'use strict';

var express = require('express');
var cookieParser = require('cookie-parser');
var fs = require('fs');
var path = require('path');
var url = require('url');

var pages = [
  {
    id: 1,
    title: 'Title 1'
  },
  {
    id: 2,
    title: 'Title 2'
  },
  {
    id: 3,
    title: 'Title 3'
  }
]

var comments = [
  {
    id: 1,
    page: 1,
    subject: 'Title 1 Comment 1',
    auther: 'JT'
  },
  {
    id: 2,
    page: 1,
    subject: 'Title 1 Comment 2',
    auther: 'Anna'
  },
  {
    id: 3,
    page: 1,
    subject: 'Title 1 Comment 3',
    auther: 'Jane'
  },
  {
    id: 4,
    page: 1,
    subject: 'Title 1 Comment 4',
    auther: 'Bob'
  },
  {
    id: 5,
    page: 4,
    subject: 'This should not show!',
    auther: 'Jill'
  }
]

function getRandomNumber(min, max) {
  return Math.random() * (max - min) + min;
}

function create(hbs, env) {
  if (env) process.env.NODE_ENV = env;
  var app = express();
  var viewsDir = path.join(__dirname, 'views');

  // Hook in express-hbs and tell it where known directories reside
  app.engine('hbs', hbs.express4({
    defaultLayout: path.join(viewsDir, "layout.hbs")
  }));
  app.set('view engine', 'hbs');
  app.set('views', viewsDir);

  app.use(cookieParser());

  app.get('/', function (req, res) {
    res.render('index', {
      message: 'Hello,',
      username: req.cookies.user
    });
  });

  app.get('/fail', function (req, res) {
    res.render('failer');
  });

  hbs.registerAsyncHelper('user', function(username, resultcb) {
    setTimeout(function() {
      resultcb(username);
    }, getRandomNumber(100, 900))
  });

  hbs.registerAsyncHelper('pages', function(options, resultcb) {
    var self = this;
    setTimeout(function() {
      var result = [];
      for(var i = 0; i < pages.length; i++) {
        options.data.page = pages[i];
        result.push(options.fn.call(self, pages[i], options));
      }
      resultcb(result.join(''));
    }, getRandomNumber(100, 900))
  });

  hbs.registerAsyncHelper('comments', function(options, resultcb) {
    var self = this;
    setTimeout(function() {
      var result = [];
      for(var i = 0; i < comments.length; i++) {
        if (options.hash.page === comments[i].page) {
          result.push(options.fn(comments[i]));
        }
      }
      resultcb(result.join(''));
    }, getRandomNumber(100, 300))
  });

  hbs.registerAsyncHelper('failer', function(_, resultcb) {
    setTimeout(function() {
      resultcb(options.fn());
    }, 100);
  })

  return app;
}

exports.create = create;


<|start_filename|>node_modules/express-hbs/test/asyncSpecs.js<|end_filename|>
'use stirct';

var request = require('supertest');
var assert = require('assert');
var hbs = require('..');
var asyncApp = require('./apps/async');
var resolver = require('../lib/resolver');

function makeUserRequest(app, user, cb) {
  request(app)
    .get('/')
    .set('Cookie', 'user=' + user)
    .end(function(err, res) {
      if (err) cb(err);
      if (res.text.search('Hello, ' + user) <= 0) {
        return cb(new Error('Wrong template send for user ' + user + ': ' + res.text), user);
      }
      return cb(null, user, res.text);
    });
}

function requestAll(app, users, cb) {
  var status = {};
  for(var i = 0; i < users.length; i++) {
    status[users[i]] = 'Pending';
    makeUserRequest(app, users[i], function(err, user) {
      if (err) {
        status[user] = 'Error: ' + err.message;
      } else {
        status[user] = 'Completed'
      }
    });
  }
  var checkTimer = setInterval(function() {
    for(var i = 0; i < users.length; i++) {
      if (status[users[i]] === 'Pending') {
        return;
      }
    }
    clearInterval(checkTimer);
    cb(status);
  }, 100);
}

describe('async', function() {
  it('should render all async helpers', function(done) {
    var app = asyncApp.create(hbs.create(), 'production');
    requestAll(app, ['jt','anna','joe','jeff','jane'], function(results) {
      assert.equal(results.jt, 'Completed');
      assert.equal(results.anna, 'Completed');
      assert.equal(results.joe, 'Completed');
      assert.equal(results.jeff, 'Completed');
      assert.equal(results.jane, 'Completed');
      done();
    });
  });

  it('should render nested async helpers', function(done) {
    var app = asyncApp.create(hbs.create(), 'production');
    makeUserRequest(app, 'jt', function(err, user, results) {
      if (err) {
        return done(err);
      }
      assert.equal(false, resolver.hasResolvers(results));
      assert.equal(-1, results.search('This should not show!'))
      done();
    });
  });
});


<|start_filename|>node_modules/express-hbs/lib/generate-id.js<|end_filename|>
'use strict';

var alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_';

function generateId(length) {
  if (!length) {
    length = 8;
  }
  var res = '';
  for (var i = 0; i < length; ++i) {
    res += alphabet[Math.floor(Math.random() * alphabet.length)];
  }
  return res;
}

module.exports = generateId;


<|start_filename|>node_modules/express-hbs/test/localTemplateOptions.js<|end_filename|>
'use strict';
var assert = require('assert');
var path = require('path');
var hbs = require('..');
var H = require('./helpers');

describe('local template options', function () {
  var dirname = path.join(__dirname, 'localTemplateOptions');

  describe('express3', function () {

    it('merges res.locals._templateOptions with the self._templateOptions', function (done) {
      var instance = hbs.create();
      var render = instance.express3({});
      instance.updateTemplateOptions({
          data: {
            greeting: 'Hello,',
            firstName: 'Freddy',
            lastName: 'Krueger'
          } 
      });

      var locals = H.createLocals('express3', dirname, {
        _templateOptions: {
          data: {
            lastName: 'Mercury'
          } 
        } 
      });

      render(path.join(dirname, 'template.hbs'), locals, function (err, html) {
        assert.ifError(err);
        assert.strictEqual(H.stripWs(html), H.stripWs('Hello, <NAME>')); 
        done(err);
      });
    });

    it('removes _templateOptions from the locals data', function (done) {
      var instance = hbs.create();
      var render = instance.express3({});
      instance.updateTemplateOptions({
          data: {
            greeting: 'Hello,',
            firstName: 'Freddy',
            lastName: 'Krueger'
          } 
      });

      var locals = H.createLocals('express3', dirname, {
        _templateOptions: {
          data: {
            lastName: 'Mercury'
          } 
        } 
      });

      render(path.join(dirname, 'data-access-template.hbs'), locals, function (err, html) {
        assert.ifError(err);
        assert.strictEqual(H.stripWs(html), H.stripWs('')); 
        done(err);
      });
    });
  });

  describe('express4', function () {

    it('merges res.locals._templateOptions with the self._templateOptions', function (done) {
      var instance = hbs.create();
      var render = instance.express4({});
      instance.updateTemplateOptions({
          data: {
            greeting: 'Hello,',
            firstName: 'Freddy',
            lastName: 'Krueger'
          } 
      });

      var locals = H.createLocals('express4', dirname, {
        _templateOptions: {
          data: {
            lastName: 'Mercury'
          } 
        } 
      });

      render(path.join(dirname, 'template.hbs'), locals, function (err, html) {
        assert.ifError(err);
        assert.strictEqual(H.stripWs(html), H.stripWs('Hello, <NAME>')); 
        done(err);
      });
    });

    it('removes _templateOptions from the locals data', function (done) {
      var instance = hbs.create();
      var render = instance.express3({});
      instance.updateTemplateOptions({
          data: {
            greeting: 'Hello,',
            firstName: 'Freddy',
            lastName: 'Krueger'
          } 
      });

      var locals = H.createLocals('express3', dirname, {
        _templateOptions: {
          data: {
            lastName: 'Mercury'
          } 
        } 
      });

      render(path.join(dirname, 'data-access-template.hbs'), locals, function (err, html) {
        assert.ifError(err);
        assert.strictEqual(H.stripWs(html), H.stripWs('')); 
        done(err);
      });
    });
  });
});


<|start_filename|>node_modules/express-hbs/test/customExtension.js<|end_filename|>
'use strict';
var assert = require('assert');
var hbs = require('..');
var path = require('path');
var H = require('./helpers');


// MEANJS is using custom extension .server.view.html instead of .hbs 
// https://github.com/meanjs/mean
describe('custom extension for partials view', function() {
  var dirname = path.join(__dirname, 'views/customExtension');
  var render = hbs.create().express4({
      extname: '.server.view.html',
      partialsDir: dirname + '/partialsDir'
  });

  it('should allow rendering multiple partials with custom extension', function(done) {
    function check(err, html) {
      assert.ifError(err);
      assert.equal(
        '<html>' + 
          '<subpartial>1</subpartial>' + 
          '<partial>1</partial>' + 
          '<subpartial>2</subpartial>' + 
          '<partial>2</partial>' + 
        '</html>', 
      H.stripWs(html));
      done();
    }

    var options = {cache: true, settings: {views: dirname }};
    render(dirname + '/template.server.view.html', options, check);
  });

});
