$(document).ready(function() {

  module("Chaining");

  test("map/flatten/reduce", function() {
    var lyrics = [
      "I'm a lumberjack and I'm okay",
      "I sleep all night and I work all day",
      "He's a lumberjack and he's okay",
      "He sleeps all night and he works all day"
    ];
    var counts = _(lyrics).chain()
      .map(function(line) { return line.split(''); })
      .flatten()
      .reduce(function(hash, l) {
        hash[l] = hash[l] || 0;
        hash[l]++;
        return hash;
    }, {}).value();
    ok(counts['a'] == 16 && counts['e'] == 10, 'counted all the letters in the song');
  });

  test("select/reject/sortBy", function() {
    var numbers = [1,2,3,4,5,6,7,8,9,10];
    numbers = _(numbers).chain().select(function(n) {
      return n % 2 == 0;
    }).reject(function(n) {
      return n % 4 == 0;
    }).sortBy(function(n) {
      return -n;
    }).value();
    equal(numbers.join(', '), "10, 6, 2", "filtered and reversed the numbers");
  });

  test("select/reject/sortBy in functional style", function() {
    var numbers = [1,2,3,4,5,6,7,8,9,10];
    numbers = _.chain(numbers).select(function(n) {
      return n % 2 == 0;
    }).reject(function(n) {
      return n % 4 == 0;
    }).sortBy(function(n) {
      return -n;
    }).value();
    equal(numbers.join(', '), "10, 6, 2", "filtered and reversed the numbers");
  });

  test("reverse/concat/unshift/pop/map", function() {
    var numbers = [1,2,3,4,5];
    numbers = _(numbers).chain()
      .reverse()
      .concat([5, 5, 5])
      .unshift(17)
      .pop()
      .map(function(n){ return n * 2; })
      .value();
    equal(numbers.join(', '), "34, 10, 8, 6, 4, 2, 10, 10", 'can chain together array functions.');
  });

});

@@ -540,13 +540,10 @@
 unts
-%5B'a'%5D
+.a
  == 
@@ -558,13 +558,10 @@
 unts
-%5B'e'%5D
+.e
  == 
@@ -764,32 +764,33 @@
  return n %25 2 ==
+=
  0;%0A    %7D).rejec
@@ -818,32 +818,33 @@
  return n %25 4 ==
+=
  0;%0A    %7D).sortB
@@ -1158,16 +1158,17 @@
 n %25 2 ==
+=
  0;%0A    
@@ -1212,16 +1212,17 @@
 n %25 4 ==
+=
  0;%0A    
@@ -1606,32 +1606,32 @@
       .value();%0A
-
     equal(number
@@ -1721,12 +1721,254 @@
 %0A  %7D);%0A%0A
+  test(%22chaining works in small stages%22, function() %7B%0A    var o = _(%5B1, 2, 3, 4%5D).chain();%0A    deepEqual(o.filter(function(i) %7B return i %3C 3; %7D).value(), %5B1, 2%5D);%0A    deepEqual(o.filter(function(i) %7B return i %3E 2; %7D).value(), %5B3, 4%5D);%0A  %7D);%0A%0A
 %7D);%0A

Add a two-part tests for chaining, plus lint.