Skip to content

Instantly share code, notes, and snippets.

@GabrielCastro
Last active December 24, 2015 22:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GabrielCastro/6873167 to your computer and use it in GitHub Desktop.
Save GabrielCastro/6873167 to your computer and use it in GitHub Desktop.
Assignment One tester
function assert(msg, expected, result) {
if (expected !== result) {
setTimeout(function () {
var errMsg = "****** ASSERT ERROR ****** " + msg + " { expected " + expected + " !=== " + result + " } ";
console.log(errMsg);
//throw new Error(errMsg);
}, 0);
} else {
console.log("\t" + msg + " OK");
}
}
function test_0_0() {
var test0 = new UPC("726412175425");
var test1 = new UPC("235262623623");
var test2 = new UPC("897654324282");
var test3 = new UPC("111111111117");
var test4 = new UPC("222222222224");
}
function test_3_2_1_1() {
console.log("Executing 3.2.1 UPC Object Test Code Example 1");
var sample_code = "111111111117",
upc_with_valid_code = new UPC(sample_code);
assert("did the code get recorded correctly?", true, (sample_code === upc_with_valid_code.code));
}
function test_3_2_1_2() {
console.log("Executing 3.2.1 UPC Object Test Code Example 2");
var sample_code = "111111111111",
upc_with_invalid_code = new UPC(sample_code);
assert("is the code empty?", true, ("" === upc_with_invalid_code.code));
upc_with_invalid_code.set_code("1234", true);
assert("is invalid upc code set?", true, ("1234" === upc_with_invalid_code.code));
}
function test_3_2_1_3() {
console.log("Executing 3.2.1 UPC Object Test Code Example 3");
var upc_with_no_code = new UPC();
assert("is the code empty?", true, (undefined === upc_with_no_code.code));
upc_with_no_code.set_code("1234", true);
assert("is invalid upc code set?", true, ("1234" === upc_with_no_code.code));
upc_with_no_code.set_code("897654324283");
assert("is invalid upc code set?", false, ("897654324283" === upc_with_no_code.code));
}
function test_3_3_1_1() {
console.log("Executing 3.3.1 Product Object Test Code Example 1");
var empty_product = new Product({});
//should cause no exceptions.
}
function test_3_3_2_2() {
console.log("Executing 3.3.2 Product Object Test Code Example 2");
var product_with_name = new Product({
"name": "Waterman FP"
});
assert("is the name correct?", false, ("Mont Blanc" === product_with_name.name));
}
function test_3_3_3_3() {
console.log("Excuting 3.3.3 Product Object Test Code Example 3");
var full_valid_product = new Product({
"name": "Waterman FP",
"price": 12000,
"upc": new UPC("222222222224"),
"image_path": "/foo/bar.png"
});
assert("is the name correct?", true, ("Waterman FP" === full_valid_product.name));
assert("is the price correct?", true, (12000 === full_valid_product.price));
assert("is the upc correct?", true, ("222222222224" === full_valid_product.upc.code));
assert("is the image_path correct?", true, ("/foo/bar.png" === full_valid_product.image_path));
}
function test_3_3_4() {
console.log("Executing 3.3.4 Product Object Test Code Example 4");
var full_product_from_invalid_options = new Product({
"price": -12000,
"upc": new UPC("44"),
"image_path": "/foo/bar.not"
});
assert("is the name correct?", true, ("TBD" === full_product_from_invalid_options.name));
assert("is the price correct?", true, (0 === full_product_from_invalid_options.price));
assert("is the upc correct?", true, ("" === full_product_from_invalid_options.upc.code));
assert("is the image_path correct?", true, (undefined === full_product_from_invalid_options.image));
}
function test_3_3_5() {
console.log("Excuting 3.3.5 Product Object Test Code Example 5");
var was_listener_called = false,
example_listener = function (new_price, product) {
was_listener_called = true
assert("is the name correct?", true, ("Waterman FP" === product.name));
assert("is the price correct?", true, (250.00 === new_price));
},
product_for_listeners = new Product({
"name": "Waterman FP",
"price": 12000,
"upc": new UPC("222222222224"),
"image_path": "/foo/bar.png"
});
assert("is the name correct?", true, ("Waterman FP" === product_for_listeners.name));
assert("is the price correct?", true, (12000 === product_for_listeners.price));
assert("is the upc correct?", true, ("222222222224" === product_for_listeners.upc.code));
assert("is the image_path correct?", true, ("/foo/bar.png" === product_for_listeners.image_path));
product_for_listeners.addPriceChangeListener(example_listener);
product_for_listeners.setPrice(250.00);
assert("was listener called?", true, was_listener_called);
assert("is the price correct?", true, (250.00 === product_for_listeners.price));
}
function testAll() {
test_0_0();
test_3_2_1_1();
test_3_2_1_2();
test_3_2_1_3();
test_3_3_1_1();
test_3_3_2_2();
test_3_3_3_3();
test_3_3_4();
test_3_3_5();
}
testAll();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment