Browse Source

unit test for formatConversion.ts

namnguyen 10 tháng trước cách đây
mục cha
commit
df4b42a6dc

+ 19 - 20
frontend/taipy-gui/src/utils/formatConversion.ts

@@ -15,8 +15,8 @@
  * Regular expressions used for parsing sprintf format strings.
  */
 const re = {
-    text: /^[^\x25]+/,                         // Matches non-placeholder text
-    modulo: /^\x25{2}/,                        // Matches the '%%' escape sequence
+    text: /^[^\x25]+/, // Matches non-placeholder text
+    modulo: /^\x25{2}/, // Matches the '%%' escape sequence
     placeholder: /^\x25?(?:\.(\d+))?([b-giostuvxX])/, // Matches placeholders
 };
 
@@ -27,13 +27,13 @@ const re = {
 export const precisionFormat = (precision?: string, specifier?: string): string => {
     // Default to precision of 2 if not specified
     return "." + (precision?.slice(1) ?? "2") + specifier;
-}
+};
 
 /*
  * This function parses a sprintf format string and returns an array of strings and objects. Each object has a single
  * key, 'placeholder', that contains the placeholder string.
  */
-export const sprintfParse = (fmt?: string): (string | { placeholder: string; })[] => {
+export const sprintfParse = (fmt?: string): (string | { placeholder: string })[] => {
     let _fmt = fmt;
     let match;
     const parseTree = [];
@@ -44,7 +44,7 @@ export const sprintfParse = (fmt?: string): (string | { placeholder: string; })[
             parseTree.push(match[0]);
         } else if ((match = re.modulo.exec(_fmt)) !== null) {
             // '%%' escape sequence
-            parseTree.push('%');
+            parseTree.push("%");
         } else if ((match = re.placeholder.exec(_fmt)) !== null) {
             // Placeholder
             if (match && match[0]) {
@@ -52,15 +52,17 @@ export const sprintfParse = (fmt?: string): (string | { placeholder: string; })[
                     placeholder: match[0],
                 });
             }
+        } else {
+            // If none of the conditions are met, break the loop
+            break;
         }
 
         if (match) {
             _fmt = _fmt.substring(match[0].length);
         }
     }
-
     return parseTree;
-}
+};
 
 /*
  * This function converts a sprintf format string to a D3 format string. It takes an optional sprintf format string and
@@ -68,10 +70,10 @@ export const sprintfParse = (fmt?: string): (string | { placeholder: string; })[
  */
 export const sprintfToD3Converter = (fmt?: string): string => {
     const sprintfFmtArr = sprintfParse(fmt);
-    const objectIndex = sprintfFmtArr.findIndex((element) => typeof element === 'object');
+    const objectIndex = sprintfFmtArr.findIndex((element) => typeof element === "object");
     let placeholderValue;
 
-    if (typeof sprintfFmtArr[objectIndex] === 'object' && sprintfFmtArr[objectIndex] !== null) {
+    if (typeof sprintfFmtArr[objectIndex] === "object" && sprintfFmtArr[objectIndex] !== null) {
         placeholderValue = (sprintfFmtArr[objectIndex] as { placeholder: string }).placeholder;
     }
 
@@ -94,12 +96,10 @@ export const sprintfToD3Converter = (fmt?: string): string => {
             case "g":
                 return precisionFormat(precision, type);
             case "u":
-                return "("
-            default:
-                return "";
+                return "(";
         }
     });
-}
+};
 
 /*
  * This function extracts the prefix from a sprintf format string. It takes an optional sprintf format string and returns
@@ -108,9 +108,9 @@ export const sprintfToD3Converter = (fmt?: string): string => {
 export const extractPrefix = (fmt?: string): string => {
     if (!fmt) return "";
     const sprintfFmtArr = sprintfParse(fmt);
-    const objectIndex = sprintfFmtArr.findIndex((element) => typeof element === 'object');
-    return sprintfFmtArr.slice(0, objectIndex).join('');
-}
+    const objectIndex = sprintfFmtArr.findIndex((element) => typeof element === "object");
+    return sprintfFmtArr.slice(0, objectIndex).join("");
+};
 
 /*
  * This function extracts the suffix from a sprintf format string. It takes an optional sprintf format string and returns
@@ -119,7 +119,6 @@ export const extractPrefix = (fmt?: string): string => {
 export const extractSuffix = (fmt?: string): string => {
     if (!fmt) return "";
     const sprintfFmtArr = sprintfParse(fmt);
-    const objectIndex = sprintfFmtArr.findIndex((element) => typeof element === 'object');
-    return sprintfFmtArr.slice(objectIndex + 1).join('');
-}
-
+    const objectIndex = sprintfFmtArr.findIndex((element) => typeof element === "object");
+    return sprintfFmtArr.slice(objectIndex + 1).join("");
+};

+ 15 - 0
frontend/taipy-gui/src/utils/utils.spec.ts

@@ -63,6 +63,18 @@ describe("format conversion", () => {
         const result = extractSuffixWrapper("Hello, %s. You have %d new messages.");
         expect(result).toBe(". You have %d new messages.");
     });
+    it("should return empty string when no format string is provided to extractPrefix", () => {
+        const result = extractPrefix();
+        expect(result).toBe("");
+    });
+    it("should return empty string when no format string is provided to extractSuffix", () => {
+        const result = extractSuffix();
+        expect(result).toBe("");
+    });
+    it("should break the loop for invalid placeholder", () => {
+        const result = sprintfParse("Hello, %z");
+        expect(result).toEqual(["Hello, "]);
+    });
     it("should return 'b' for '%b'", () => {
         expect(sprintfToD3Converter("%b")).toBe("b");
     });
@@ -91,4 +103,7 @@ describe("format conversion", () => {
     it("should return '(' for '%u'", () => {
         expect(sprintfToD3Converter("%u")).toBe("(");
     });
+    it("should return '' for unsupported converter", () => {
+        expect(sprintfToD3Converter("hi")).toBe("");
+    });
 });