/* 
Copyright (c) 2008 Laurynas Liutkus

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

*/

//RSpec

function expect_create(klassname) {
  debug('expect create...');
  //record expectation
  var expectationId = RSpec.expectations.size();
  var expectation = new Expectation(null, klassname, expectationId);
  expectation.klassname = klassname;
  expectation.backup = eval(klassname);
  RSpec.expectations[expectationId] = expectation;
  // stub it
  eval(klassname+' = function() { var expectation = RSpec.expectations[' + expectationId + ']; expectation.called(arguments); return expectation.returned_object; }');
  return expectation;
}

var RSpecMethods = {
  should_receive: function(name) {
    //ensure method is defined
    if (!eval('this.' + name + ';')) {
      throw new JsUnitException(null, "method #{name} for #{mock} is not even defined!".interpolate({name: name, mock: this.rspecInspect()}));
    }
    //record expectation
    var expectationId = RSpec.expectations.size();
    var expectation = new Expectation(name, this, expectationId);
    RSpec.expectations[expectationId] = expectation;
    // stub it
    eval('this.' + name + ' = function() { RSpec.expectations[' + expectationId + '].called(arguments); }');
    return expectation;
  },

  rspecInspect: function(id) {
    if (this.inspect) {
      return this.inspect();
    }
    else {
      return id;
    }
  }
};

var RSpec = Class.create();
RSpec.setUp = function() {
  this.expectations = [];
  this.lastMock = 0;

}
RSpec.setUp();
RSpec.tearDown = function() {
  this.expectations.each(function(expectation) {
    // for constructor mocks:
    if(expectation.name == null) {
      //expectation.target = expectation.backup;
      eval(expectation.klassname + ' = expectation.backup;');
    }
    if (expectation.calls != expectation.count) {
      throw new JsUnitException(null, "Mock '#{mock}' expected '#{name}' #{count} times but got it #{calls} times".interpolate(expectation));
    }
    if (expectation.params) {
      var expected = Object.toJSON(expectation.params);
      var got = Object.toJSON(expectation.actualParams);
      if (expected != got) {
        throw new JsUnitException('komentaras', "expected:\n" + expected + ",\n but got:\n" + got);
      }
    }
  });
}

Expectation = Class.create({
  initialize: function(name, target, id) {
    this.name = name;
    this.count = 1;
    this.calls = 0;
    this.target = target;
    this.id = id;
    if (target.rspecInspect) this.mock = target.rspecInspect(id);
  },
  with_params: function() {
    this.params = arguments;
    //var c = [];
    //if (params.length > 0) {
      //for(i=0; i<params.length; i++) {
        //c.push(i);
      //}
    //}
    //var paramsGot = c.collect(function(i) { return 'a' + i;}).join(', ');
    //eval('this.target.' + this.name + ' = function(' + paramsGot +') { RSpec.expectations[' + this.id + '].called([' + paramsGot + ']); }');
    //eval('this.target.' + this.name + ' = function() { RSpec.expectations[' + this.id + '].called(arguments); }');
    return this;
  },
  and_return: function(returned_object) {
    this.returned_object = returned_object;
    return this;
  },
  exactly: function(count) {
    this.count = count;
    return this;
  },
  called: function(actualParams) {
    debug("called " + this.name);
    this.calls += 1;
    this.actualParams = actualParams;
    return this.returned_object;
  }
});

//END RSpec

