|
| 1 | +import { |
| 2 | + formatPrintR, |
| 3 | + formatVarDump, |
| 4 | + isValidSerialized, |
| 5 | + unserialize, |
| 6 | +} from "./unserializer.utils"; |
| 7 | + |
| 8 | +describe("unserializer.utils", () => { |
| 9 | + describe("unserialize", () => { |
| 10 | + test("parses primitive values", () => { |
| 11 | + expect(unserialize('s:5:"hello";')).toEqual({ |
| 12 | + type: "string", |
| 13 | + value: "hello", |
| 14 | + }); |
| 15 | + expect(unserialize("i:42;")).toEqual({ type: "int", value: 42 }); |
| 16 | + expect(unserialize("d:3.14;")).toEqual({ type: "float", value: 3.14 }); |
| 17 | + expect(unserialize("b:1;")).toEqual({ type: "bool", value: true }); |
| 18 | + expect(unserialize("b:0;")).toEqual({ type: "bool", value: false }); |
| 19 | + expect(unserialize("N;")).toEqual({ type: "null" }); |
| 20 | + }); |
| 21 | + |
| 22 | + test("parses multi-byte UTF-8 strings", () => { |
| 23 | + expect(unserialize('s:4:"😊";')).toEqual({ |
| 24 | + type: "string", |
| 25 | + value: "😊", |
| 26 | + }); |
| 27 | + }); |
| 28 | + |
| 29 | + test("parses nested arrays from sample payload", () => { |
| 30 | + const input = |
| 31 | + 'a:2:{i:0;s:12:"Sample array";i:1;a:2:{i:0;s:5:"Apple";i:1;s:6:"Orange";}}'; |
| 32 | + |
| 33 | + expect(unserialize(input)).toEqual({ |
| 34 | + type: "array", |
| 35 | + entries: [ |
| 36 | + { |
| 37 | + key: { type: "int", value: 0 }, |
| 38 | + value: { type: "string", value: "Sample array" }, |
| 39 | + }, |
| 40 | + { |
| 41 | + key: { type: "int", value: 1 }, |
| 42 | + value: { |
| 43 | + type: "array", |
| 44 | + entries: [ |
| 45 | + { |
| 46 | + key: { type: "int", value: 0 }, |
| 47 | + value: { type: "string", value: "Apple" }, |
| 48 | + }, |
| 49 | + { |
| 50 | + key: { type: "int", value: 1 }, |
| 51 | + value: { type: "string", value: "Orange" }, |
| 52 | + }, |
| 53 | + ], |
| 54 | + }, |
| 55 | + }, |
| 56 | + ], |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + test("resolves references", () => { |
| 61 | + const result = unserialize('a:2:{s:1:"x";s:3:"foo";s:1:"y";R:2;}'); |
| 62 | + expect(result).toEqual({ |
| 63 | + type: "array", |
| 64 | + entries: [ |
| 65 | + { |
| 66 | + key: { type: "string", value: "x" }, |
| 67 | + value: { type: "string", value: "foo" }, |
| 68 | + }, |
| 69 | + { |
| 70 | + key: { type: "string", value: "y" }, |
| 71 | + value: { type: "string", value: "foo" }, |
| 72 | + }, |
| 73 | + ], |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + test("throws clear errors for invalid input", () => { |
| 78 | + expect(() => unserialize("")).toThrow( |
| 79 | + "Please enter a serialized string." |
| 80 | + ); |
| 81 | + expect(() => unserialize("xyz")).toThrow(); |
| 82 | + expect(() => unserialize('a:1:{s:1:"x";R:99;}')).toThrow( |
| 83 | + "Reference index 99 does not exist" |
| 84 | + ); |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + describe("isValidSerialized", () => { |
| 89 | + test("returns true for valid payloads and false for invalid payloads", () => { |
| 90 | + expect(isValidSerialized('s:5:"hello";')).toBe(true); |
| 91 | + expect(isValidSerialized("i:42;")).toBe(true); |
| 92 | + expect(isValidSerialized("")).toBe(false); |
| 93 | + expect(isValidSerialized("not serialized")).toBe(false); |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + describe("formatPrintR", () => { |
| 98 | + test("matches expected output for sample payload", () => { |
| 99 | + const input = |
| 100 | + 'a:2:{i:0;s:12:"Sample array";i:1;a:2:{i:0;s:5:"Apple";i:1;s:6:"Orange";}}'; |
| 101 | + const parsed = unserialize(input); |
| 102 | + |
| 103 | + const expected = [ |
| 104 | + "Array", |
| 105 | + "(", |
| 106 | + " [0] => Sample array", |
| 107 | + " [1] => Array", |
| 108 | + " (", |
| 109 | + " [0] => Apple", |
| 110 | + " [1] => Orange", |
| 111 | + " )", |
| 112 | + "", |
| 113 | + ")", |
| 114 | + ].join("\n"); |
| 115 | + |
| 116 | + expect(formatPrintR(parsed)).toBe(expected); |
| 117 | + }); |
| 118 | + }); |
| 119 | + |
| 120 | + describe("formatVarDump", () => { |
| 121 | + test("matches expected output for sample payload", () => { |
| 122 | + const input = |
| 123 | + 'a:2:{i:0;s:12:"Sample array";i:1;a:2:{i:0;s:5:"Apple";i:1;s:6:"Orange";}}'; |
| 124 | + const parsed = unserialize(input); |
| 125 | + |
| 126 | + const expected = [ |
| 127 | + "array(2) {", |
| 128 | + " [0]=>", |
| 129 | + ' string(12) "Sample array"', |
| 130 | + " [1]=>", |
| 131 | + " array(2) {", |
| 132 | + " [0]=>", |
| 133 | + ' string(5) "Apple"', |
| 134 | + " [1]=>", |
| 135 | + ' string(6) "Orange"', |
| 136 | + " }", |
| 137 | + "}", |
| 138 | + ].join("\n"); |
| 139 | + |
| 140 | + expect(formatVarDump(parsed)).toBe(expected); |
| 141 | + }); |
| 142 | + |
| 143 | + test("uses UTF-8 byte length for strings", () => { |
| 144 | + const parsed = unserialize('s:4:"😊";'); |
| 145 | + expect(formatVarDump(parsed)).toBe('string(4) "😊"'); |
| 146 | + }); |
| 147 | + }); |
| 148 | +}); |
0 commit comments