/*
 Copyright 2016 - 2017 by Jan Dockx

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

(function() {
  "use strict";

  var expect = require("chai").expect;
  var common = require("./PostconditionViolationCommon");
  var PostconditionViolation = require("../../src/I/PostconditionViolation");
  var Contract = require("../../src/I/Contract");
  var util = require("../../src/_private/util");
  var testUtil = require("../_testUtil");

  var argsCases = common.argsCases.filter(function(a) {return util.typeOf(a) === "array"});

  // describe("I", function() {
    describe("I/PostconditionViolation", function() {

      describe("#PostconditionViolation()", function() {
        common.selfCases.forEach(function(self) {
          argsCases.forEach(function(args) {
            common.resultCases.forEach(function(result) {
              it("creates an instance with all toppings for " + self + " - " + args + " - " + result, function() {
                var contractFunction = common.createCandidateContractFunction();
                var doctoredArgs = args.slice();
                doctoredArgs.push(result);
                doctoredArgs.push(contractFunction.bind(self));
                var creationResult = new PostconditionViolation(
                  contractFunction,
                  common.conditionCase,
                  self,
                  doctoredArgs
                );
                common.expectConstructorPost(
                  creationResult,
                  contractFunction,
                  common.conditionCase,
                  self,
                  args,
                  result
                );
                common.expectInvariants(creationResult);
                testUtil.log("result.stack:\n%s", creationResult.stack);
              });
            });
          });
        });
      });

      common.generatePrototypeMethodsDescriptions(
        function() {
          //noinspection MagicNumberJS
          return new PostconditionViolation(
            common.createCandidateContractFunction(),
            common.conditionCase,
            null,
            common.argsCases[0],
            42
          );
        },
        testUtil
          .x(common.conditionCases, common.selfCases, argsCases, common.resultCases)
          .map(function(parameters) {
            return function() {
              return {
                subject: new PostconditionViolation(
                  common.createCandidateContractFunction(),
                  parameters[0],
                  parameters[1],
                  parameters[2],
                  parameters[3]
                ),
                description: parameters.join(" - ")
              };
            };
          }),
        common.expectInvariants,
        function doctorArgs(args, boundContractFunction) {
          var result = Array.prototype.slice.call(args);
          //noinspection MagicNumberJS
          result.push(42); // a result
          result.push(boundContractFunction);
          return result;
        }
      );

    });
  // });

})();

@@ -2331,165 +2331,305 @@
     
-common.generatePrototypeMethodsDescriptions(%0A        function() %7B%0A          //noinspection MagicNumberJS%0A          return new PostconditionViola
+function doctorArgs(args, boundContractFunction, result) %7B%0A        var doctored = Array.prototype.slice.call(args);%0A        //noinspection MagicNumberJS%0A        var r = arguments.length %3E= 3 ? result : 42;%0A        doctored.push(r); // a result%0A        doctored.push(boundContractFunc
 tion
-(
+);
 %0A       
     
@@ -2628,44 +2628,95 @@
     
+ return doctored;%0A      %7D%0A%0A
      
+ 
 common.
-createCandidateContractF
+generatePrototypeMethodsDescriptions(%0A        f
 unct
@@ -2712,33 +2712,34 @@
       function()
-,
+ %7B
 %0A            com
@@ -2733,39 +2733,80 @@
         
-  common.conditionCase,
+var contractFunction = common.createCandidateContractFunction();
 %0A       
@@ -2812,15 +2812,24 @@
     
- 
+var self =
  null
-,
+;
 %0A   
@@ -2839,56 +2839,191 @@
     
-  common.argsCases%5B0%5D,%0A            42%0A          
+var doctoredArgs = doctorArgs(common.argsCases%5B0%5D, contractFunction.bind(self));%0A          return new PostconditionViolation(contractFunction, common.conditionCase, self, doctoredArgs
 );%0A 
@@ -3222,87 +3222,30 @@
     
-return %7B%0A                subject: new PostconditionViolation(%0A                 
+var contractFunction =
  com
@@ -3281,17 +3281,17 @@
 nction()
-,
+;
 %0A       
@@ -3289,35 +3289,42 @@
 ;%0A              
-   
+var self =
  parameters%5B0%5D,%0A
@@ -3319,19 +3319,19 @@
 ameters%5B
-0%5D,
+1%5D;
 %0A       
@@ -3329,36 +3329,62 @@
 ;%0A              
-    
+var doctoredArgs = doctorArgs(
 parameters%5B1%5D,%0A 
@@ -3382,93 +3382,183 @@
 ers%5B
-1%5D,%0A                  parameters%5B2%5D,%0A                  parameters%5B3%5D%0A                
+2%5D, contractFunction.bind(self), parameters%5B3%5D);%0A              return %7B%0A                subject: new PostconditionViolation(contractFunction, parameters%5B0%5D, self, doctoredArgs
 ),%0A 
@@ -3691,33 +3691,24 @@
 ts,%0A        
-function 
 doctorArgs(a
@@ -3709,255 +3709,8 @@
 Args
-(args, boundContractFunction) %7B%0A          var result = Array.prototype.slice.call(args);%0A          //noinspection MagicNumberJS%0A          result.push(42); // a result%0A          result.push(boundContractFunction);%0A          return result;%0A        %7D
 %0A   

Fix PostconditionViolation cases for common.generatePrototypeMethodsDescriptions