<commit_before>"use strict";
/*jshint -W030 */

var chai = require('chai');
var testUtil = require('../test-util');

var junkie = require('../../lib/junkie');
var ResolutionError = require('../../lib/ResolutionError');

chai.should();

var A, B, C, D;
var AFactory, BFactory;

describe("factory method resolver integration", function() {

  beforeEach(function() {
    A = testUtil.createType();
    B = testUtil.createType();
    C = testUtil.createType();
    D = testUtil.createType();

    AFactory = testUtil.createFactory(A);
    BFactory = testUtil.createFactory(B);
  });

  describe("with no deps", function() {

    it("should fail missing factory method", function(done) {
      var c = junkie.newContainer();

      var F = {};

      c.register("A", F)
        .with.factoryMethod("gimme");

      c.resolve("A").catch(function(err) {
        err.should.be.an.instanceof(ResolutionError);
        err.message.should.equal("FactoryMethod resolver: Method not found: gimme");
        done();
      });
    });

    it("should call factory method on type", function() {
      var c = junkie.newContainer();

      var F = {
        gimme: function() {
          return new A();
        }
      };

      c.register("A", F)
        .with.factoryMethod("gimme");

      return c.resolve("A").then(function(a) {
        a.should.be.instanceof(A);
      });
    });

    it("should call factory method on instance", function() {
      var c = junkie.newContainer();

      var F = function() {
        this.gimme = function() {
          return new A();
        };
      };

      c.register("A", F)
        .with.constructor()
        .with.factoryMethod("gimme");

      return c.resolve("A").then(function(a) {
        a.should.be.instanceof(A);
      });
    });

    it('should chain factory-method-created promise resolve', function() {
      var c = junkie.newContainer();

      var F = {
        gimme: function() {
          return Promise.resolve(new A());
        }
      };

      c.register("A", F)
        .with.factoryMethod("gimme");

      return c.resolve("A").then(function(a) {
        a.should.be.instanceof(A);
      });
    });

    it('should chain factory-method-created promise reject', function(done) {
      var c = junkie.newContainer();

      var F = {
        gimme: function() {
          return Promise.reject(new Error("oh noo"));
        }
      };

      c.register("A", F)
        .with.factoryMethod("gimme");

      c.resolve("A").catch(function(err) {
        err.should.be.an.instanceof(Error);
        err.message.should.equal("oh noo");
        done();
      });
    });

    it("should fail a missing dep", function(done) {
      var c = junkie.newContainer();

      var F = {
        gimme: function(arg) {
          return new A(arg);
        }
      };

      c.register("A", F)
        .with.factoryMethod("gimme", "B");

      c.resolve("A").catch(function(err) {
        err.should.be.an.instanceof(ResolutionError);
        err.message.should.equal("Not found: B");
        done();
      });
    });
  });


  describe("with deps", function() {

    it("should call factory method on type", function() {
      var c = junkie.newContainer();

      var F = {
        gimme: function(arg) {
          return new A(arg);
        }
      };

      c.register("A", F)
        .with.factoryMethod("gimme", "B");
      c.register("B", B);

      return c.resolve("A").then(function(a) {
        a.should.be.instanceof(A);
        a._args.should.deep.equal([ B ]);
      });
    });

    it("should call factory method on instance", function() {
      var c = junkie.newContainer();

      var F = function() {
        this.gimme = function(arg) {
          return new A(arg);
        };
      };

      c.register("A", F)
        .with.constructor()
        .with.factoryMethod("gimme", "B");
      c.register("B", B);

      return c.resolve("A")
        .then(function(a) {
          a.should.be.instanceof(A);
        });
    });



    it("should fail when factory method throws an error", function(done) {
      var c = junkie.newContainer();

      var F = {
        gimme: function() {
          throw new Error("Uh oh")
        }
      };

      c.register("A", F)
        .with.factoryMethod("gimme", "B");
      c.register("B", B);

      c.resolve("A").catch(function(err) {
        err.should.be.an.instanceof(Error);<commit_msg>Fix jshint error :P<commit_after>"use strict";
/*jshint -W030 */

var chai = require('chai');
var testUtil = require('../test-util');

var junkie = require('../../lib/junkie');
var ResolutionError = require('../../lib/ResolutionError');

chai.should();

var A, B, C, D;
var AFactory, BFactory;

describe("factory method resolver integration", function() {

  beforeEach(function() {
    A = testUtil.createType();
    B = testUtil.createType();
    C = testUtil.createType();
    D = testUtil.createType();

    AFactory = testUtil.createFactory(A);
    BFactory = testUtil.createFactory(B);
  });

  describe("with no deps", function() {

    it("should fail missing factory method", function(done) {
      var c = junkie.newContainer();

      var F = {};

      c.register("A", F)
        .with.factoryMethod("gimme");

      c.resolve("A").catch(function(err) {
        err.should.be.an.instanceof(ResolutionError);
        err.message.should.equal("FactoryMethod resolver: Method not found: gimme");
        done();
      });
    });

    it("should call factory method on type", function() {
      var c = junkie.newContainer();

      var F = {
        gimme: function() {
          return new A();
        }
      };

      c.register("A", F)
        .with.factoryMethod("gimme");

      return c.resolve("A").then(function(a) {
        a.should.be.instanceof(A);
      });
    });

    it("should call factory method on instance", function() {
      var c = junkie.newContainer();

      var F = function() {
        this.gimme = function() {
          return new A();
        };
      };

      c.register("A", F)
        .with.constructor()
        .with.factoryMethod("gimme");

      return c.resolve("A").then(function(a) {
        a.should.be.instanceof(A);
      });
    });

    it('should chain factory-method-created promise resolve', function() {
      var c = junkie.newContainer();

      var F = {
        gimme: function() {
          return Promise.resolve(new A());
        }
      };

      c.register("A", F)
        .with.factoryMethod("gimme");

      return c.resolve("A").then(function(a) {
        a.should.be.instanceof(A);
      });
    });

    it('should chain factory-method-created promise reject', function(done) {
      var c = junkie.newContainer();

      var F = {
        gimme: function() {
          return Promise.reject(new Error("oh noo"));
        }
      };

      c.register("A", F)
        .with.factoryMethod("gimme");

      c.resolve("A").catch(function(err) {
        err.should.be.an.instanceof(Error);
        err.message.should.equal("oh noo");
        done();
      });
    });

    it("should fail a missing dep", function(done) {
      var c = junkie.newContainer();

      var F = {
        gimme: function(arg) {
          return new A(arg);
        }
      };

      c.register("A", F)
        .with.factoryMethod("gimme", "B");

      c.resolve("A").catch(function(err) {
        err.should.be.an.instanceof(ResolutionError);
        err.message.should.equal("Not found: B");
        done();
      });
    });
  });


  describe("with deps", function() {

    it("should call factory method on type", function() {
      var c = junkie.newContainer();

      var F = {
        gimme: function(arg) {
          return new A(arg);
        }
      };

      c.register("A", F)
        .with.factoryMethod("gimme", "B");
      c.register("B", B);

      return c.resolve("A").then(function(a) {
        a.should.be.instanceof(A);
        a._args.should.deep.equal([ B ]);
      });
    });

    it("should call factory method on instance", function() {
      var c = junkie.newContainer();

      var F = function() {
        this.gimme = function(arg) {
          return new A(arg);
        };
      };

      c.register("A", F)
        .with.constructor()
        .with.factoryMethod("gimme", "B");
      c.register("B", B);

      return c.resolve("A")
        .then(function(a) {
          a.should.be.instanceof(A);
        });
    });



    it("should fail when factory method throws an error", function(done) {
      var c = junkie.newContainer();

      var F = {
        gimme: function() {
          throw new Error("Uh oh");
        }
      };

      c.register("A", F)
        .with.factoryMethod("gimme", "B");
      c.register("B", B);

      c.resolve("A").catch(function(err) {
        err.should.be.an.instanceof(Error);
{'fraction_non_alphanumeric': 0.17123365548607164, 'fraction_numerical': 0.0006822057987492893, 'mean_word_length': 2.0499306518723994, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5931443', 'n_tokens_mistral': 2829, 'n_tokens_neox': 2696, 'n_words': 574}