toLowerCase.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. //String.prototype.toLowerCase()
  6. //TO DO : Need to add Unicode and Upper Ascii Characters test cases and also Test cases that would throw exception(NaN and undefined Objects)
  7. var id=0;
  8. function verify(get_actual,get_expected,testid,testdesc)
  9. {
  10. if(get_actual!=get_expected)
  11. WScript.Echo(testid+":"+testdesc+"\t"+"failed"+"\n"+"got"+get_actual+"\t for\t"+get_expected)
  12. }
  13. //test 1
  14. verify("\tMICROSOFT".toLowerCase(), "\tmicrosoft", id++, "\"Testing Escape character tab\"")
  15. //test 2
  16. verify("\nMICROSOFT".toLowerCase(), "\nmicrosoft", id++, "\"Testing Escape character new line\"")
  17. //test3
  18. verify("\rMICROSOFT".toLowerCase(), "\rmicrosoft", id++, "\"Testing Escape character return \"")
  19. //test 4
  20. verify("\'MICROSOFT\'".toLowerCase(), "\'microsoft\'", id++, "\"Testing Escape character single quote\"")
  21. //test 5
  22. verify("MICROO\bSOFT".toLowerCase(), "microo\bsoft", id++, "\"Testing Escape character backspace\"")
  23. //test 6
  24. verify("\"MICROSOFT\"".toLowerCase(), "\"microsoft\"", id++, "\"Testing Escape character double quote\"")
  25. //test 7
  26. verify("microsoft".toLowerCase(), "microsoft", id++, "\"Testing passing lower case characters\"")
  27. //test 8
  28. verify("ABCDEFGHIJKLMNOPQRSTUVWXYZ".toLowerCase(), "abcdefghijklmnopqrstuvwxyz", id++, "\"Testing passing uppercase case characters\"")
  29. //test 9
  30. verify("(!@#$%^&*<,()+;:>?/)".toLowerCase(), "(!@#$%^&*<,()+;:>?/)", id++, "\" Testing passing Special Characters \"")
  31. //test 10
  32. verify("[email protected]".toLowerCase(), "[email protected]", id++, "\"Testing mix of characters eg email id\"");
  33. //test 11
  34. verify("ONEMICROSOFTWAY,156THNE31STPL,WA98054".toLowerCase(), "onemicrosoftway,156thne31stpl,wa98054", id++, "\"Testing mix of characters eg address\"");
  35. //test 12
  36. verify("1-800-CALL-HSBC".toLowerCase(), "1-800-call-hsbc", id++, id++, "\"Testing mix of characters eg phone number\" ");
  37. //test 13: Coercing Other Object types : Arrays
  38. var arr=new Array(3);
  39. arr[0]="JSCRIPT";
  40. arr[1]=12345;
  41. arr[2]="[email protected]";
  42. Array.prototype.toLowerCase=String.prototype.toLowerCase; //the prototype method of string can now be called from the array object
  43. verify(arr.toLowerCase(), "jscript,12345,[email protected]", id++, "\"Testing Coercible Objects eg Array\" ");
  44. //test 14 Coercing Other Object types : Number
  45. var num=new Number();
  46. num=12345
  47. Number.prototype.toLowerCase=String.prototype.toLowerCase;
  48. verify(num.toLowerCase(), "12345", id++, "\"Testing Coercible Objects eg Number\" ");
  49. //test 15 Coercing Other Object types : Boolean
  50. var mybool=new Boolean(false);
  51. Boolean.prototype.toLowerCase=String.prototype.toLowerCase;
  52. verify(mybool.toLowerCase(), "false", id++, "\"Testing Coercible Objects eg Boolean\" ");
  53. //test 16 Coercing Other Object types : Object
  54. var obj=new Object()
  55. Object.prototype.toLowerCase=String.prototype.toLowerCase;
  56. verify(obj.toLowerCase(), "[object object]", id++, "\"Testing Coercible Objects eg Object\" ");
  57. //Need to test for null and undefined but have to know the error message
  58. //test 17 Concatenated String
  59. verify(("CONCATENATED"+"STRING").toLowerCase(), "concatenatedstring", id++, "\" Testing Concatenated String\"");
  60. //test 18 Indirect Call through Function
  61. var Foo=function(){}
  62. Foo.prototype.test=function(){return "MYSTRING";}
  63. var fun=new Foo()
  64. verify(fun.test().toLowerCase(), "mystring", id++, "\"Testing indirect calling eg function\"")
  65. //test 19 Indirect call through property
  66. var myobj=new Object();
  67. myobj.prop="STRING";
  68. verify(myobj.prop.toLowerCase(), "string", id++, "\"Testing indirect calling eg property\"");
  69. WScript.Echo("done");
  70. //test 20 implicit calls
  71. var a = 1;
  72. var b = 2;
  73. var obj = {toString: function(){ a=3; return "Hello World";}};
  74. a = b;
  75. Object.prototype.toLowerCase = String.prototype.toLowerCase;
  76. var f = obj.toLowerCase();
  77. WScript.Echo (a);