Function overloading

by Arnaud Buchholz

JavaScript

function overload () {

}

var result = overload(function () {
	return "no result";
}, function (a) {
	return a;
}, function (a, b) {
	return a + b;
});

// Minimalist testing framework

function assert(condition) {
	if (!condition) {
  	throw "Assertion failed";
  }
}

function it(label, callback) {
	var line = document.createElement("div"),
  	  result;
  line.appendChild(document.createTextNode(label + ":"));
  result = line.appendChild(document.createElement("span"));
  try {
  	callback();
    span.style.color = "green";
    span.innerHTML = "OK";
  } catch (e) {
    span.style.color = "red";
    span.innerHTML = "KO";
  }
  document.body.appendChild(line);
}

// Test cases

it("returns no result with no parameter", function () {
	assert(result() === "no result");
});

it("returns the parameter with only one parameter", function () {
	assert(result(1) === 1);
});

it("returns the sum of parameters with two parameters", function () {
	assert(result(1) === 1);
});

it("returns the sum of parameters with two parameters", function () {
	assert(result(1) === 1);
});