Created
April 9, 2013 15:03
-
-
Save klamping/5346440 to your computer and use it in GitHub Desktop.
YUI3 Trim() Unit Tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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