const test = require('test'),
      asserts = test.asserts,
      App = require('juice').Application,
      qmock = require('./lib/qmock');

// TEST:
//   Check that buildAction behaves as expected for various different cases

function setup(test) {
  var app = qmock.Mocks();
  App.call(app);
  app.buildAction = App.prototype.buildAction;

  return function() {
    return test(app) ;
  }
}

exports.test_inline_action = setup(function(app) {
  var cb = function() { },
      res = app.buildAction( "/foo", cb );

  // TODO: rename this to res.controller
  asserts.same(res.action, cb, "action correct for literal function '/foo'")
  asserts.same(res.__matcher, /^\/foo$/, "action's matcher correct");
  // TODO: rename this to template or default_template
  asserts.same("render" in res, false, "action template not inferred for function actions");

  res = app.buildAction( "/foo/bar", { action: cb } );
  asserts.same(res.action, cb, "action correct for action hash '/foo/bar'")
  asserts.same(res.__matcher, /^\/foo\/bar$/, "action's matcher correct");
  asserts.same("render" in res, false, "action template not inferred for function actions");
})

exports.test_named_action = setup(function(app) {
  var foo_cb = function() { },
      foo_bar_cb = function() { };

  // The qmock API is a little bit batty. Or lacking docks. one or the other
  // You cant throw an error from a mocked method it seems.

  // TODO: Rename getAction (in application.js) to just 'controller()' and
  //       remove the first argument to it
  var getController = app.expects().method("getAction");
  getController.expectedArgs = [
    { accepts: [{}, "foo"], returns: foo_cb },
    { accepts: [{}, "foo.bar"], returns: foo_bar_cb }
  ];

  var res = app.buildAction("/foo", "foo");
  asserts.same(res.action, foo_cb, "action correct '/foo'")
  asserts.same(res.__matcher, /^\/foo$/, "action's matcher correct");
  asserts.same(res.render, "foo", "action template inferred");

  var res = app.buildAction("/foo/bar/\\d", "foo.bar");
  asserts.same(res.action, foo_bar_cb, "action correct")
  asserts.same(res.__matcher, /^\/foo\/bar\/\d$/, "action's matcher correct");
  asserts.same(res.render, "foo/bar", "action template inferred");
})

exports.test_custom_matcher = setup(function(app) {
  var cb = function() { },
      m = function() { return false };

  var res = app.buildAction( "/foo", { action: cb, __matcher: m } );

  asserts.same(res.__matcher, m, "action's custom matcher preserved");

  asserts.throwsOk(
    function() { app.buildAction( "/foo", { action: cb, __matcher: "not a func" } ) },
    "TypeError: action.__matcher is not a function",
    "__matcher must be a function");
})

if (require.main == module)
  test.runner(exports);

@@ -220,16 +220,24 @@
  cases%0A%0A
+var app%0A
 function
@@ -253,19 +253,39 @@
 st) %7B%0A  
-var
+return function() %7B%0A   
  app = q
@@ -298,16 +298,128 @@
 ocks();%0A
+    var App = require('juice').Application;%0A    // Call the constructor on the mock to setup member variables%0A  
   App.ca
@@ -427,16 +427,18 @@
 l(app);%0A
+  
   app.bu
@@ -479,31 +479,8 @@
 ion;
-%0A%0A  return function() %7B
 %0A   
@@ -496,17 +496,16 @@
 est(app)
- 
 ;%0A  %7D%0A%7D%0A
@@ -2800,16 +2800,928 @@
 %22);%0A%7D)%0A%0A
+exports.test_static_action = setup(function test( app ) %7B%0A  var url = %22/foo%22,%0A      act = %7B static: %22bar%22 %7D,%0A      ret = %7B return: %22value%22 %7D;%0A%0A  // Who named that method %3C_%3C TODO: rename it to something not shit.%0A  app.expects(1).method(%22buildServeStaticAction%22)%0A                .interface( %7B accepts: %5Bact, url%5D, returns: ret %7D );%0A%0A  var res = app.buildAction( %22/foo%22, %7B static: %22bar%22 %7D );%0A%0A  asserts.ok(app.verify(), %22static action built ok%22);%0A  asserts.same(res, ret, %22... and returned%22);%0A%0A%7D)%0A%0Aexports.test_subapp_action = setup(function test( app ) %7B%0A  var url = %22/foo%22,%0A      act = %7B app: %22bar%22 %7D,%0A      ret = %7B return: %22value%22 %7D;%0A%0A  app.expects(1).method(%22buildSubappAction%22)%0A                .interface( %7B accepts: %5Bact, url%5D, returns: ret %7D );%0A%0A  var res = app.buildAction( %22/foo%22, %7B app: %22bar%22 %7D );%0A%0A  asserts.ok(app.verify(), %22subapp action built ok%22);%0A  asserts.same(res, ret, %22... and returned%22);%0A%7D)%0A%0A
 if (requ

Test (mocked) static and subapp action building