'use strict';
const bufferEqual = require('buffer-equal');

module.exports = methodBuilder => {

  bodyMethod('haveBodyObject',
    function predicate(textResponse, text) {
      return textResponse == JSON.stringify(text);
    },
    function actual(text) {
      return JSON.parse(text);
    });
  bodyMethod('haveBodyText',
    function predicate(textResponse, text) {
      return textResponse == text;
    },
    function actual(text) {
      return text;
    });
  bodyMethod('haveBodyRegexpMatch',
    function predicate(textResponse, regexp) {
      return textResponse.match(regexp);
    },
    function actual(text) {
      return text
    });

  bufferMethod('haveBodyBuffer');

  bodyMethod('haveBodyThat',
    function predicate(textResponse, predicate) {
      return predicate(textResponse);
    },
    function actual(text) {
      return text;
    });

  function bodyMethod(name, predicate, actual) {
    methodBuilder({
      name: name,
      predicate: function (res, args) {
        return predicate(res.text, args[0])
      },
      msgSuccess: function(){
        return 'expected #{exp} to match body with #{act}';
      },
      msgFail: function(){
        return 'expected #{exp} not to match body with #{act}';
      },
      expected: function (args) {
        return args[0]
      },
      actual: function (res) {
        return actual(res.text);
      }
    });
  }

  function bufferMethod(name) {
    methodBuilder({
      name: name,
      predicate: function (res, args) {
        return bufferEqual(res.buffer, args[0]);
      },
      msgSuccess: function () {
        return 'expected #{exp} to match body with #{act}';
      },
      msgFail: function () {
        return 'expected #{exp} not to match body with #{act}';
      },
      expected: function (args) {
        return args[0]
      },
      actual: function (res) {
        return res.buffer;
      }
    });
  }

}
@@ -90,16 +90,17 @@
 r =%3E %7B%0A%0A
+%0A
   bodyMe
@@ -141,36 +141,27 @@
 n predicate(
-textResponse
+res
 , text) %7B%0A  
@@ -167,36 +167,32 @@
      return 
+res.
 text
-Response
  == JSON.str
@@ -224,36 +224,35 @@
 function actual(
-text
+res
 ) %7B%0A      return
@@ -263,16 +263,20 @@
 N.parse(
+res.
 text);%0A 
@@ -274,32 +274,33 @@
 .text);%0A    %7D);%0A
+%0A
   bodyMethod('ha
@@ -335,28 +335,19 @@
 edicate(
-textResponse
+res
 , text) 
@@ -361,28 +361,24 @@
  return 
+res.
 text
-Response
  == text
@@ -398,36 +398,35 @@
 function actual(
-text
+res
 ) %7B%0A      return
@@ -418,32 +418,36 @@
  %7B%0A      return 
+res.
 text;%0A    %7D);%0A  
@@ -444,16 +444,17 @@
     %7D);%0A
+%0A
   bodyMe
@@ -504,28 +504,19 @@
 edicate(
-textResponse
+res
 , regexp
@@ -532,28 +532,24 @@
  return 
+res.
 text
-Response
 .match(r
@@ -575,36 +575,35 @@
 function actual(
-text
+res
 ) %7B%0A      return
@@ -599,24 +599,28 @@
      return 
+res.
 text%0A    %7D);
@@ -624,21 +624,19 @@
 %7D);%0A%0A  b
-uffer
+ody
 Method('
@@ -650,18 +650,169 @@
 yBuffer'
-);
+,%0A    function predicate(res, buffer) %7B%0A      return bufferEqual(res.buffer, buffer);%0A    %7D,%0A    function actual(res) %7B%0A      return res.buffer;%0A    %7D);%0A
 %0A%0A  body
@@ -857,28 +857,19 @@
 edicate(
-textResponse
+res
 , predic
@@ -902,20 +902,16 @@
 ate(
+res.
 text
-Response
 );%0A 
@@ -936,20 +936,19 @@
  actual(
-text
+res
 ) %7B%0A    
@@ -956,16 +956,20 @@
  return 
+res.
 text;%0A  
@@ -1130,21 +1130,16 @@
 cate(res
-.text
 , args%5B0
@@ -1467,29 +1467,24 @@
 n actual(res
-.text
 );%0A      %7D%0A 
@@ -1499,521 +1499,7 @@
  %7D%0A%0A
-  function bufferMethod(name) %7B%0A    methodBuilder(%7B%0A      name: name,%0A      predicate: function (res, args) %7B%0A        return bufferEqual(res.buffer, args%5B0%5D);%0A      %7D,%0A      msgSuccess: function () %7B%0A        return 'expected #%7Bexp%7D to match body with #%7Bact%7D';%0A      %7D,%0A      msgFail: function () %7B%0A        return 'expected #%7Bexp%7D not to match body with #%7Bact%7D';%0A      %7D,%0A      expected: function (args) %7B%0A        return args%5B0%5D%0A      %7D,%0A      actual: function (res) %7B%0A        return res.buffer;%0A      %7D%0A    %7D);%0A  %7D%0A
 %0A%7D
+;

refactor some duplication