utils.spec.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright 2021-2024 Avaiga Private Limited
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  5. * the License. You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  10. * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  11. * specific language governing permissions and limitations under the License.
  12. */
  13. import { extractPrefix, extractSuffix, precisionFormat, sprintfParse, sprintfToD3Converter } from "./formatConversion";
  14. function extractSuffixWrapper(formatString: string): string {
  15. let result = extractSuffix(formatString);
  16. result = result.replace("[object Object]", "%d");
  17. return result;
  18. }
  19. describe("format conversion", () => {
  20. it("returns formatted precision with provided precision and specifier", () => {
  21. expect(precisionFormat(".5", "f")).toBe(".5f");
  22. });
  23. it("returns formatted precision with default precision when none is provided", () => {
  24. expect(precisionFormat(undefined, "f")).toBe(".2f");
  25. });
  26. it("returns empty string when no format string is provided", () => {
  27. expect(sprintfToD3Converter()).toBe("");
  28. });
  29. it("should parse non-placeholder text", () => {
  30. const result = sprintfParse("Hello, World!");
  31. expect(result).toEqual(["Hello, World!"]);
  32. });
  33. it('should parse the "%%" escape sequence', () => {
  34. const result = sprintfParse("%%");
  35. expect(result).toEqual(["%"]);
  36. });
  37. it("should parse placeholders", () => {
  38. const result = sprintfParse("%d");
  39. expect(result).toEqual([{ placeholder: "%d" }]);
  40. });
  41. it("should parse complex format strings", () => {
  42. const result = sprintfParse("Hello, %s. You have %d new messages.");
  43. expect(result).toEqual([
  44. "Hello, ",
  45. { placeholder: "%s" },
  46. ". You have ",
  47. { placeholder: "%d" },
  48. " new messages.",
  49. ]);
  50. });
  51. it("should extract placeholder value", () => {
  52. const result = sprintfToD3Converter("%d");
  53. expect(result).toBe("d");
  54. });
  55. it("should extract prefix from format string", () => {
  56. const result = extractPrefix("Hello, %s. You have %d new messages.");
  57. expect(result).toBe("Hello, ");
  58. });
  59. it("should extract suffix from format string", () => {
  60. const result = extractSuffixWrapper("Hello, %s. You have %d new messages.");
  61. expect(result).toBe(". You have %d new messages.");
  62. });
  63. it("should return empty string when no format string is provided to extractPrefix", () => {
  64. const result = extractPrefix();
  65. expect(result).toBe("");
  66. });
  67. it("should return empty string when no format string is provided to extractSuffix", () => {
  68. const result = extractSuffix();
  69. expect(result).toBe("");
  70. });
  71. it("should break the loop for invalid placeholder", () => {
  72. const result = sprintfParse("Hello, %z");
  73. expect(result).toEqual(["Hello, "]);
  74. });
  75. it("should return 'b' for '%b'", () => {
  76. expect(sprintfToD3Converter("%b")).toBe("b");
  77. });
  78. it("should return 'e' for '%e'", () => {
  79. expect(sprintfToD3Converter("%e")).toBe("e");
  80. });
  81. it("should return 'o' for '%o'", () => {
  82. expect(sprintfToD3Converter("%o")).toBe("o");
  83. });
  84. it("should return 'x' for '%x'", () => {
  85. expect(sprintfToD3Converter("%x")).toBe("x");
  86. });
  87. it("should return 'X' for '%X'", () => {
  88. expect(sprintfToD3Converter("%X")).toBe("X");
  89. });
  90. it("should return 'd' for '%i'", () => {
  91. expect(sprintfToD3Converter("%i")).toBe("d");
  92. });
  93. it("should return '.2f' for '%f'", () => {
  94. expect(sprintfToD3Converter("%f")).toBe(".2f");
  95. });
  96. it("should return '.2g' for '%g'", () => {
  97. expect(sprintfToD3Converter("%g")).toBe(".2g");
  98. });
  99. it("should return '(' for '%u'", () => {
  100. expect(sprintfToD3Converter("%u")).toBe("(");
  101. });
  102. it("should return '' for unsupported converter", () => {
  103. expect(sprintfToD3Converter("hi")).toBe("");
  104. });
  105. });