toCase.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js");
  6. function testASCII(lower, upper, message) {
  7. const toUppers = [ String.prototype.toUpperCase, String.prototype.toLocaleUpperCase ];
  8. const toLowers = [ String.prototype.toLowerCase, String.prototype.toLocaleLowerCase ];
  9. for (const func of toUppers) {
  10. assert.areEqual(upper, func.call(lower), `lower.${func.name}(): ${message}`);
  11. assert.areEqual(upper, func.call(upper), `upper.${func.name}(): ${message}`);
  12. }
  13. for (const func of toLowers) {
  14. assert.areEqual(lower, func.call(upper), `upper.${func.name}(): ${message}`);
  15. assert.areEqual(lower, func.call(lower), `lower.${func.name}(): ${message}`);
  16. }
  17. }
  18. testRunner.runTests([
  19. {
  20. name: "Visible ASCII characters",
  21. body() {
  22. testASCII("", "", "Empty string");
  23. let i = 32;
  24. let upper = "";
  25. let lower = "";
  26. for (; i < 65; i++) {
  27. upper += String.fromCharCode(i);
  28. lower += String.fromCharCode(i);
  29. }
  30. for (; i < 91; i++) {
  31. upper += String.fromCharCode(i);
  32. }
  33. for (i = 97; i < 123; i++) {
  34. lower += String.fromCharCode(i);
  35. }
  36. for (i = 91; i < 97; i++) {
  37. upper += String.fromCharCode(i);
  38. lower += String.fromCharCode(i);
  39. }
  40. for (i = 123; i < 127; i++) {
  41. upper += String.fromCharCode(i);
  42. lower += String.fromCharCode(i);
  43. }
  44. testASCII(lower, upper, "Visible ASCII");
  45. }
  46. },
  47. {
  48. name: "Special characters",
  49. body() {
  50. const specialCharacters = {
  51. "\n": "newline",
  52. "\t": "tab",
  53. "\r": "carriage return",
  54. "\0": "null",
  55. "\"": "double quote",
  56. "\'": "single quote",
  57. "\b": "backspace",
  58. };
  59. for (const c in specialCharacters) {
  60. testASCII(`${c}microsoft`, `${c}MICROSOFT`, `string with ${specialCharacters[c]} at the beginning`);
  61. testASCII(`micro${c}soft`, `MICRO${c}SOFT`, `string with ${specialCharacters[c]} in the middle`);
  62. testASCII(`microsoft${c}`, `MICROSOFT${c}`, `string with ${specialCharacters[c]} at the end`);
  63. }
  64. }
  65. },
  66. {
  67. name: "Type conversion",
  68. body() {
  69. const convertible = [
  70. [new Number(123), "123", "123"],
  71. [new Boolean(true), "true", "TRUE"],
  72. [new String("aBc"), "abc", "ABC"],
  73. [new Object(), "[object object]", "[OBJECT OBJECT]"],
  74. [["Chakra", 2018, true], "chakra,2018,true", "CHAKRA,2018,TRUE"],
  75. [{ toString: () => "Hello" }, "hello", "HELLO"]
  76. ];
  77. for (const test of convertible) {
  78. for (const func of [String.prototype.toLowerCase, String.prototype.toLocaleLowerCase]) {
  79. assert.areEqual(test[1], func.call(test[0]), `${func.name}: type conversion of ${test[0]} to ${test[1]}`);
  80. }
  81. for (const func of [String.prototype.toUpperCase, String.prototype.toLocaleUpperCase]) {
  82. assert.areEqual(test[2], func.call(test[0]), `${func.name}: type conversion of ${test[0]} to ${test[2]}`);
  83. }
  84. }
  85. }
  86. },
  87. {
  88. name: "Correct errors are thrown",
  89. body() {
  90. for (const badThis of [null, undefined]) {
  91. for (const func of [String.prototype.toUpperCase, String.prototype.toLocaleUpperCase, String.prototype.toLowerCase, String.prototype.toLocaleLowerCase]) {
  92. assert.throws(() => func.call(badThis), TypeError, `${func.name}.call(${Object.prototype.toString.call(badThis)})`);
  93. }
  94. }
  95. }
  96. },
  97. ], { verbose: false });