PageRenderTime 56ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/v1.0/test/modules/string.js

https://bitbucket.org/esatterwhite/braveheart
JavaScript | 277 lines | 220 code | 56 blank | 1 comment | 1 complexity | 386477b30cf8e0da7b660eeaf2413b91 MD5 | raw file
  1. /*jshint laxcomma:true, smarttabs: true */
  2. define(['require','exports', 'module', 'string'], function( require, exports, module, string ){
  3. exports.cleaning ={
  4. setUp: function( callback ){
  5. this.str = "This is a pretty decent sentence. it probably has some Problems ";
  6. callback();
  7. }
  8. ,clean: function( test){
  9. test.equal( this.str.length, 69 );
  10. test.equal( string.clean(this.str).length , 63, "clean method should remove leading, trailing and all extraneous whitespace" );
  11. test.done();
  12. }
  13. ,trim: function( test){
  14. test.equal( this.str.length, 69 );
  15. test.equal( string.trim(this.str ).length , 66, "trim method should remove only the leading and trailing whitespace" );
  16. test.notEqual( string.trim(this.str ).length , this.str.length, "trim method should remove only the leading and trailing whitespace" );
  17. test.equal( string.trim(" Hello world, I have a lot of space " ).length , 40, "trim method should remove only the leading and trailing whitespace" );
  18. test.done();
  19. }
  20. };
  21. exports.stripping = {
  22. setUp: function( callback ){
  23. this.str =
  24. "<div>"
  25. +"<script>var x = \"hello world\";console.log( x );</script>"
  26. +"<span> this is some text & the number 5</span>"
  27. +"</div>";
  28. callback();
  29. }
  30. ,scripts: function( test ){
  31. var text = string.stripScripts( this.str );
  32. test.expect( 3 );
  33. test.strictEqual( text.indexOf( 'script' ), -1, "stripScripts method should remove all script tags");
  34. test.strictEqual( text.indexOf( 'console' ), -1, "stripScripts method should remove all contents within the script tag");
  35. try{
  36. string.stripScripts( this.str, eval );
  37. test.ok(1, "executing of scripts should not throw an error");
  38. } catch ( e ){
  39. test.done();
  40. }
  41. test.done();
  42. }
  43. ,tags: function( test ){
  44. var str = string.stripTags( this.str, "span");
  45. test.equal( str.indexOf( 'span'), -1, "stripTags should remove the passed in tag ");
  46. test.notEqual(str.indexOf( 'world'), -1, "stripTags, by default should not remove the contents of the tag that was removed ");
  47. str = string.stripTags( this.str, "span", true );
  48. test.done();
  49. }
  50. ,nonAlpha: function( test ){
  51. var str = string.stripNonAlpha( this.str );
  52. test.strictEqual( str.indexOf( "5" ), -1, "stripNonAlpha should strip out numbers");
  53. test.strictEqual( str.indexOf( ">" ), -1, "stripNonAlpha should strip special characters ( <,> )");
  54. test.strictEqual( str.indexOf( "&" ), -1, "stripNonAlpha should strip special characters ( &, # )");
  55. test.done();
  56. }
  57. };
  58. exports.munging ={
  59. setUp: function( callback ){
  60. this.string = "abc123";
  61. callback();
  62. }
  63. ,reverse: function( test ){
  64. var s = string.reverse( this.string );
  65. test.equal(typeof s, "string", "reverse should return a string");
  66. test.equal( this.string, 'abc123', "revese should not alter the original string");
  67. test.strictEqual( s, "321cba", "reverse should return the characters in the opposing order the started");
  68. test.done();
  69. }
  70. ,"String: Hyphenate": function( test ){
  71. var str;
  72. str = "helloWorld";
  73. test.strictEqual( string.hyphenate( str ), "hello-world", "Expected \"hello-world\" got :" + string.hyphenate( str ) );
  74. str = "thisIsIt";
  75. test.strictEqual( string.hyphenate( str ), "this-is-it", "Expected \"this-is-it\" got :" + string.hyphenate( str ) );
  76. str = "thisIsNotit";
  77. test.strictEqual( string.hyphenate( str ), "this-is-notit", "Expected \"this-is-notit\" got :" + string.hyphenate( str ) );
  78. str = "powerToTheP3ople";
  79. test.strictEqual( string.hyphenate( str ), "power-to-the-p3ople", "Expected \"power-to-the-p3ople\" got :" + string.hyphenate( str ) );
  80. test.done();
  81. }
  82. ,"String: camelCase": function( test ){
  83. var str;
  84. str = "hello-world";
  85. test.strictEqual( string.camelCase( str ), "helloWorld", "Expected \"helloWorld\" got :" + string.camelCase( str ) );
  86. str = "this-is-it";
  87. test.strictEqual( string.camelCase( str ), "thisIsIt", "Expected \"thisIsIt\" got :" + string.camelCase( str ) );
  88. str = "this-is-notit";
  89. test.strictEqual( string.camelCase( str ), "thisIsNotit", "Expected \"thisIsNotit\" got :" + string.camelCase( str ) );
  90. str = "power-to-the-p3ople";
  91. test.strictEqual( string.camelCase( str ), "powerToTheP3ople", "Expected \"powerToTheP3ople\" got :" + string.camelCase( str ) );
  92. test.done();
  93. }
  94. ,"String: slugify": function( test ){
  95. var str;
  96. str = "hello world";
  97. test.strictEqual( string.slugify( str ), "hello-world", "Expected \"hello-world\" got :" + string.slugify( str ) );
  98. str = "this is it";
  99. test.strictEqual( string.slugify( str ), "this-is-it", "Expected \"this-is-it\" got :" + string.slugify( str ) );
  100. str = "this is notit";
  101. test.strictEqual( string.slugify( str ), "this-is-notit", "Expected \"this-is-notit\" got :" + string.slugify( str ) );
  102. str = "power to the p3ople";
  103. test.strictEqual( string.slugify( str ), "power-to-the-p3ople", "Expected \"power-to-the-p3ople\" got :" + string.slugify( str ) );
  104. test.done();
  105. }
  106. ,"String: truncate": function( test ){
  107. var str
  108. ,tail = "...";
  109. str = "abc123";
  110. test.strictEqual( string.truncate(str, 1, tail), tail, "Passing a maxlength smaller than the tails size should return the tail");
  111. test.strictEqual( string.truncate(str, 4, tail), "a" + tail, "Max length should include the length of the tail - A length of 4, got a length of " + string.truncate(str, 4, tail).length );
  112. test.strictEqual( string.truncate(str, 10, tail), str, "Passing a maxlength larger than the string size should return the string");
  113. test.done();
  114. }
  115. , "String: padRight" : function( test ){
  116. test.strictEqual( string.padRight( this.string, 10, "x"),"abc123xxxx" )
  117. test.done()
  118. }
  119. ,"String: padLeft": function( test ){
  120. test.strictEqual( string.padLeft( this.string, 10, "x"),"xxxxabc123" )
  121. test.done()
  122. }
  123. };
  124. exports.evaluating = {
  125. setUp: function( callback ){
  126. this.str = "hello_world-This is $(#)33";
  127. callback();
  128. }
  129. ,contains: function( test ){
  130. test.ok(string.contains( this.str, "hello"), "string should return true" );
  131. test.ok(string.contains( this.str, "hello_world"), "string should return true" );
  132. test.ok(string.contains( this.str, "hello"), "string should return true" );
  133. test.strictEqual(false, string.contains( this.str, "hello world"), "string should return false" );
  134. test.done();
  135. }
  136. };
  137. exports.toInt = {
  138. setUp: function( callback ){
  139. this.str = "100.84903px";
  140. callback();
  141. }
  142. ,"String: toint": function( test ){
  143. test.equal( string.toInt( this.str ), "100", "toInt should remove decimals");
  144. test.strictEqual( string.toInt( this.str ), 100, "toInt should return a Number object");
  145. test.strictEqual( string.toInt( this.str, 2), 4, "toInt should accept a second praram as a numeric base. Expected 4, got: " + string.toInt( this.str, 2));
  146. test.equal( typeof string.toInt( "hello"), "number", "toInt should always return a number");
  147. test.strictEqual( string.toInt( "hello"), 0, "expected 0, got" + string.toInt( "hello"));
  148. test.done();
  149. }
  150. ,"String: px to Int": function( test ){
  151. test.equal( string.pxToInt( this.str), "100", "pxToInt should remove trailing \"px\" ");
  152. test.equal( typeof string.pxToInt( this.str), "number", "pxToInt should always return a number");
  153. test.equal( typeof string.pxToInt( this.str), "number", "pxToInt should always return a number");
  154. test.done();
  155. }
  156. };
  157. exports.toFloat = {
  158. setUp: function( callback ){
  159. this.str = "100.191";
  160. callback();
  161. }
  162. ,"String: toFloat": function( test ){
  163. test.equal( string.toFloat( this.str ), "100.191", "toFloat should remove decimals");
  164. test.strictEqual( string.toFloat( this.str ), 100.191, "toFloat should return a Number object");
  165. test.equal( typeof string.toFloat( "hello"), "number", "toFloat should always return a number");
  166. test.strictEqual( string.toFloat( "hello"), 0, "expected 0, got" + string.toFloat( "hello"));
  167. test.done();
  168. }
  169. };
  170. exports.ID = {
  171. "string unique id": function( test ){
  172. var id = string.id()
  173. ,count = 0
  174. ,interval
  175. ,next;
  176. interval = setInterval( function(){
  177. if( count > 11 ){
  178. next = string.id();
  179. test.notEqual( id, next, "string.id should return a unique ID");
  180. id = next;
  181. count++;
  182. }else{
  183. clearInterval( interval );
  184. test.done();
  185. }
  186. }, 1);
  187. }
  188. };
  189. exports.pluralize = {
  190. setUp: function( callback ){
  191. this.easyWord = "Power"
  192. this.mildWord = "fish"
  193. this.moderateWord = "Index"
  194. this.hardword = "knife";
  195. callback();
  196. }
  197. ,"pluralize w/ Count": function( test ){
  198. test.expect( 3 );
  199. test.equal( string.pluralize(1, this.easyWord, this.easyWord+"s" ), "Power", "Expected Power, but got " + string.pluralize(1, this.easyWord, this.easyWord+"s" ) )
  200. test.equal( string.pluralize(0, this.easyWord, this.easyWord+"s" ), "Powers" , "Expected Powers, but got " + string.pluralize(1, this.easyWord, this.easyWord+"s" ) )
  201. test.equal( string.pluralize(2, this.easyWord, this.easyWord+"s" ), "Powers" , "Expected Powers, but got " + string.pluralize(1, this.easyWord, this.easyWord+"s" ) )
  202. test.done();
  203. }
  204. ,"Smart pluralize Power": function( test ){
  205. test.equal( string.smartPlural(this.easyWord ), "Powers")
  206. test.done();
  207. }
  208. ,"Smart pluralize fish": function( test ){
  209. test.equal( string.smartPlural(this.mildWord ), "fishes")
  210. test.done();
  211. }
  212. ,"Smart pluralize Index": function( test ){
  213. test.equal( string.smartPlural(this.moderateWord ), "Indcies")
  214. test.done();
  215. }
  216. ,"Smart pluralize knife": function( test ){
  217. test.equal( string.smartPlural(this.hardword ), "knives")
  218. test.done();
  219. }
  220. }
  221. });