mirror of
https://github.com/morluto/rea.git
synced 2026-12-05 14:31:22 +00:00
test(apple): share single-object Nib archive fixtures (#16685)
This commit is contained in:
1 parent
b56f5d32e6
commit
5fbe7ca210
1 file changed
+27
-53
@@ -5,6 +5,12 @@ import { InterfaceBuilderDecodeBudget } from "./InterfaceBuilderDecodeBudget.js"
|
||||
import { encodeNibArchiveFixture as encodeArchive } from "./NibArchive.fixture.js";
|
||||
import { ParseFailureError } from "../../domain/parseFailure.js";
|
||||
|
||||
const singleObjectArchive = (
|
||||
className: string,
|
||||
values: Parameters<typeof encodeArchive>[0]["objects"][number]["values"],
|
||||
): Buffer =>
|
||||
encodeArchive({ classes: [className], objects: [{ classIndex: 0, values }] });
|
||||
|
||||
const archiveWithOverlappingObjectRanges = (objectCount: number): Buffer => {
|
||||
const objectRecords = Buffer.alloc(objectCount * 3);
|
||||
for (let index = 0; index < objectCount; index += 1)
|
||||
@@ -39,21 +45,13 @@ const archiveWithOverlappingObjectRanges = (objectCount: number): Buffer => {
|
||||
describe("NIBArchive decoder", () => {
|
||||
it("preserves non-finite and negative-zero real values for typed projection", () => {
|
||||
const values = decodeNibArchive(
|
||||
encodeArchive({
|
||||
classes: ["NSObject"],
|
||||
objects: [
|
||||
{
|
||||
classIndex: 0,
|
||||
values: {
|
||||
nan: { float32: Number.NaN },
|
||||
positive: { float32: Number.POSITIVE_INFINITY },
|
||||
negative: { float64: Number.NEGATIVE_INFINITY },
|
||||
negativeZero32: { float32: -0 },
|
||||
negativeZero64: { float64: -0 },
|
||||
nil: null,
|
||||
},
|
||||
},
|
||||
],
|
||||
singleObjectArchive("NSObject", {
|
||||
nan: { float32: Number.NaN },
|
||||
positive: { float32: Number.POSITIVE_INFINITY },
|
||||
negative: { float64: Number.NEGATIVE_INFINITY },
|
||||
negativeZero32: { float32: -0 },
|
||||
negativeZero64: { float64: -0 },
|
||||
nil: null,
|
||||
}),
|
||||
).objects[0]?.values;
|
||||
expect(Number.isNaN(values?.nan)).toBe(true);
|
||||
@@ -65,9 +63,9 @@ describe("NIBArchive decoder", () => {
|
||||
});
|
||||
|
||||
it("keeps leading U+FEFF in length-prefixed keys and class names", () => {
|
||||
const archive = encodeArchive({
|
||||
classes: ["\uFEFFWidget"],
|
||||
objects: [{ classIndex: 0, values: { count: 1, "\uFEFFcount": 2 } }],
|
||||
const archive = singleObjectArchive("\uFEFFWidget", {
|
||||
count: 1,
|
||||
"\uFEFFcount": 2,
|
||||
});
|
||||
expect(decodeNibArchive(archive).objects).toEqual([
|
||||
{
|
||||
@@ -108,18 +106,10 @@ describe("NIBArchive decoder", () => {
|
||||
it("reads boolean value tags with AppKit's polarity", () => {
|
||||
// Raw tags, independent of the fixture encoder's boolean encoding: AppKit's
|
||||
// decodeBoolForKey: returns NO for tag 4 and YES for tag 5 in compiled nibs.
|
||||
const archive = encodeArchive({
|
||||
classes: ["NSButton"],
|
||||
objects: [
|
||||
{
|
||||
classIndex: 0,
|
||||
values: {
|
||||
NSControlContinuous: null,
|
||||
NSControlRefusesFirstResponder: null,
|
||||
NSEnabled: 0,
|
||||
},
|
||||
},
|
||||
],
|
||||
const archive = singleObjectArchive("NSButton", {
|
||||
NSControlContinuous: null,
|
||||
NSControlRefusesFirstResponder: null,
|
||||
NSEnabled: 0,
|
||||
});
|
||||
const values = archive.readUInt32LE(38);
|
||||
// Value records: key varint, then type tag; both fields start as nil (9).
|
||||
@@ -154,16 +144,12 @@ describe("NIBArchive decoder", () => {
|
||||
});
|
||||
|
||||
it("rejects malformed references and unsupported format versions", () => {
|
||||
const valid = encodeArchive({
|
||||
classes: ["NSObject"],
|
||||
objects: [{ classIndex: 0, values: {} }],
|
||||
});
|
||||
const valid = singleObjectArchive("NSObject", {});
|
||||
const invalidVersion = Buffer.from(valid);
|
||||
invalidVersion.writeUInt32LE(2, 10);
|
||||
expect(() => decodeNibArchive(invalidVersion)).toThrow(/Unsupported/u);
|
||||
const invalidReference = encodeArchive({
|
||||
classes: ["NSObject"],
|
||||
objects: [{ classIndex: 0, values: { child: { ref: 4 } } }],
|
||||
const invalidReference = singleObjectArchive("NSObject", {
|
||||
child: { ref: 4 },
|
||||
});
|
||||
expect(() => decodeNibArchive(invalidReference)).toThrow(
|
||||
/object reference/u,
|
||||
@@ -200,10 +186,7 @@ it.each([
|
||||
"refuses an impossible $table table before charging the decode budget",
|
||||
({ countOffset }) => {
|
||||
const budget = new InterfaceBuilderDecodeBudget(95);
|
||||
const valid = encodeArchive({
|
||||
classes: ["NSObject"],
|
||||
objects: [{ classIndex: 0, values: { x: true } }],
|
||||
});
|
||||
const valid = singleObjectArchive("NSObject", { x: true });
|
||||
const truncated = Buffer.from(valid);
|
||||
truncated.writeUInt32LE(32, countOffset);
|
||||
expect(() => decodeNibArchive(truncated, budget)).toThrow(
|
||||
@@ -229,10 +212,7 @@ it.each([
|
||||
])(
|
||||
"classifies an invalid NIBArchive $label before charging for the value",
|
||||
({ label, value, editAt, byte }) => {
|
||||
const valid = encodeArchive({
|
||||
classes: ["NSObject"],
|
||||
objects: [{ classIndex: 0, values: { x: value } }],
|
||||
});
|
||||
const valid = singleObjectArchive("NSObject", { x: value });
|
||||
const invalid = Buffer.from(valid);
|
||||
invalid[invalid.readUInt32LE(38) + editAt] = byte;
|
||||
expect(() =>
|
||||
@@ -270,10 +250,7 @@ it.each([
|
||||
);
|
||||
|
||||
it("checks that a NIBArchive value leaves room for the next row", () => {
|
||||
const valid = encodeArchive({
|
||||
classes: ["NSObject"],
|
||||
objects: [{ classIndex: 0, values: { x: null, y: null } }],
|
||||
});
|
||||
const valid = singleObjectArchive("NSObject", { x: null, y: null });
|
||||
const invalid = Buffer.from(valid);
|
||||
invalid[invalid.readUInt32LE(38) + 1] = 1;
|
||||
expect(() =>
|
||||
@@ -308,10 +285,7 @@ it("retains repeated serialized collection fields in source order", () => {
|
||||
});
|
||||
|
||||
it("classifies NIBArchive input faults through ParseFailure", () => {
|
||||
const valid = encodeArchive({
|
||||
classes: ["NSObject"],
|
||||
objects: [{ classIndex: 0, values: { child: { ref: 4 } } }],
|
||||
});
|
||||
const valid = singleObjectArchive("NSObject", { child: { ref: 4 } });
|
||||
const failureOf = (bytes: Buffer): unknown => {
|
||||
try {
|
||||
decodeNibArchive(bytes);
|
||||
|
||||
Reference in new issue
Block a user