Skip to content

Instantly share code, notes, and snippets.

@klamping
Created April 9, 2013 15:03
Show Gist options
  • Save klamping/5346440 to your computer and use it in GitHub Desktop.
Save klamping/5346440 to your computer and use it in GitHub Desktop.
YUI3 Trim() Unit Tests
var YUITest = require('yuitest');
function trim(str) {
return str.replace(/^\s+|\s+$/g, '');
}
var testCase = new YUITest.TestCase({
name: "Trim Unit Tests",
"Trim should remove any trailing spaces": function() {
YUITest.Assert.areEqual("String", trim("String "), "Trailing space should be removed");
YUITest.Assert.areEqual("Another", trim("Another "), "Trailing space should be removed");
YUITest.Assert.areEqual("String", trim("String "), "All trailing spaces should be removed");
},
"Trim should remove any leading spaces": function() {
YUITest.Assert.areEqual("String", trim(" String"), "Leading space should be removed");
YUITest.Assert.areEqual("String", trim(" String"), "All leading spaces should be removed");
},
"Trim should remove leading and trailing spaces if both exist": function() {
YUITest.Assert.areEqual("String", trim(" String "),
"Both leading and trailing space should be removed");
YUITest.Assert.areEqual("String", trim(" String "),
"All leading and trailing spaces should be removed");
},
"Trim should remove any type of whitespace": function() {
YUITest.Assert.areEqual("String", trim(" \n\t String"),
"Any kind of whitespace should be trimmed");
YUITest.Assert.areEqual("String", trim(" String\n"),
"Trailing newlines should be trimmed");
},
"Trim should not remove whitespace from inside the string": function() {
var sentence = "A complete sentence with lots of whitespace.";
YUITest.Assert.areEqual(sentence, trim(sentence), "Inner space should not be removed");
}
});
//add the test cases and suites
YUITest.TestRunner.add(testCase);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment