Charliesoftware.files.wordpress.com



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""><html xmlns="" ><head> <title>qUnit Test</title> <link rel="stylesheet" href="" type="text/css" media="screen" /> <script type="text/javascript" src=""></script> <script type="text/javascript" src=""></script> <!-- Code under test --> <script type="text/javascript"> var customDelimiterRegex = new RegExp("//.+\\n", "gi"); function add(inputString) { var numbers = parseDelimiters(inputString); if (isNullOrEmptyString(numbers)) return 0; if (isSingleNumber(numbers)) return convertToInt(numbers); return addMultipleNumbers(numbers); } function isNullOrEmptyString(numbers) { return numbers == "" || numbers == null; } function isSingleNumber(numbers) { return convertToArray(numbers) == 1; } function addMultipleNumbers(numbers) { var numbersArray = convertToArray(numbers); var total = 0; $(numbersArray).each(function () { total += convertToInt(this); }); return total; } function convertToInt(numberAsString) { if (isNullOrEmptyString(numberAsString)) throw "Invalid input"; var number = parseInt(numberAsString); if (number < 0) throw "Negatives not allowed"; if (number > 1000) return 0; return number; } function convertToArray(numbers) { return numbers.split(","); } function parseDelimiters(inputString) { var delimiterCollection = null; if (hasCustomDelimiters(inputString)) { var delimiterString = inputString.match(customDelimiterRegex)[0]; var delimiterCollection = getDelimiterCollectionFromString(delimiterString); } return getCommaSeperatedNumbersFromInputString(inputString, delimiterCollection); } function hasCustomDelimiters(inputString) { return inputString.match(customDelimiterRegex) != null; } function getCommaSeperatedNumbersFromInputString(inputString, delimiterCollection) { var numbers = inputString.replace(customDelimiterRegex, "").replace("\n", ","); if (delimiterCollection != null) { $(delimiterCollection).each(function () { numbers = replaceDelimiterWithComma(this, numbers); }); } return numbers; } function getDelimiterCollectionFromString(delimiterString) { var delimiters = delimiterString.substring(2, delimiterString.length - 1); if (delimiters.search(/\]\[/) > -1) delimiters = delimiters.substring(1, delimiters.length - 1); var delimitersArray = delimiters.split("]["); return delimitersArray; } function replaceDelimiterWithComma(delimiter, numbers) { var escapedDelimiter = getEscapedDelimiter(delimiter); return numbers.replace(new RegExp(escapedDelimiter, "gi"), ","); } function getEscapedDelimiter(delimiter) { var specialCharacters = ["[", "\\", "^", "$", ".", "|", "?", "*", "+", "(", ")"]; var escapedDelimiter = ""; for (i = 0; i < delimiter.length; i++) { if ($.inArray(delimiter.charAt(i), specialCharacters) > -1) { escapedDelimiter += "\\"; } escapedDelimiter += delimiter.charAt(i); } return escapedDelimiter; } </script> <!-- Tests --> <script type="text/javascript"> $(document).ready(function () { module("add"); test("add_EmptyString_return0", function () { var result = add(""); equals(result, 0); }); test("add_1_return1", function () { var result = add("1"); equals(result, 1); }); test("add_1,2_return3", function () { var result = add("1,2"); equals(result, 3); }); test("add_1,2,4,6_return13", function () { var result = add("1,2,4,6"); equals(result, 13); }); test("add_2,7,90,106_return205", function () { var result = add("2,7,90,106"); equals(result, 205); }); test("add_lineBreakAsDelimiter1\\n2,3_return6", function () { var result = add("1\n2,3"); equals(result, 6); }); test("add_twoDelimitersInARow1,\\n_throwException", function () { try { var result = add("1,\n"); ok(false, "Expected exception"); } catch (ex) { equals(ex, "Invalid input"); } }); test("add_customDelimiter//;\\n1;2_return3", function () { var result = add("//;\n1;2"); equals(result, 3); }); test("add_customSpeicalCharacterDelimtier//.\\n1.2\\n4,5_return12", function () { var result = add("//.\n1.2\n4,5"); equals(result, 12); }); test("add_negativeNumber1,-1,2_throwException", function () { try { var result = add("1,-1,2"); ok(false, "Expected exception"); } catch (ex) { equals(ex, "Negatives not allowed"); } }); test("add_greaterThan1000_returnSumOfAllNumbersBelow1000", function () { var result = add("2,1001"); equals(result, 2); }); test("add_delimiterOfAnyLength//***\\n1***2***3_return6", function () { var result = add("//***\n1***2***3"); equals(result, 6); }); test("add_multipleCustomDelimiter_return6", function () { var result = add("//[a][s]\n1a2s3"); equals(result, 6); }); test("add_multipleCustomDelimiterOfAnyLength_return6", function () { var result = add("//[$efg][!**]\n1$efg2!**3"); equals(result, 6); }); }); </script></head><body><h1 id="qunit-header">qUnit Test</h1> <h2 id="qunit-banner"></h2> <h2 id="qunit-userAgent"></h2> <ol id="qunit-tests"></ol></body></html> ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download