// Copyright 2025 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// V8 assertion compatibility shim for Ladybird's test-js harness

function assertEquals(expected, actual, msg) {
    if (expected instanceof RegExp && actual instanceof RegExp) {
        expect(actual.source).toBe(expected.source);
        expect(actual.flags).toBe(expected.flags);
    } else if (Array.isArray(expected) && Array.isArray(actual)) {
        expect(actual).toEqual(expected);
    } else if (expected !== null && typeof expected === "object" && actual !== null && typeof actual === "object") {
        expect(actual).toEqual(expected);
    } else {
        expect(actual).toBe(expected);
    }
}

function assertTrue(val, msg) {
    expect(val).toBeTrue();
}

function assertFalse(val, msg) {
    expect(val).toBeFalse();
}

function assertNull(val, msg) {
    expect(val).toBeNull();
}

function assertNotNull(val, msg) {
    expect(val).not.toBeNull();
}

function assertThrows(fn, type_opt, msg_opt) {
    if (typeof fn === "string") {
        try {
            try {
                fn = new Function(fn);
            } catch (e) {
                return;
            }
        } catch (e) {
            return;
        }
    }
    if (typeof fn === "string") {
        try {
            try {
                fn = new Function(fn);
            } catch (e) {
                return;
            }
        } catch (e) {
            return;
        }
    }
    expect(fn).toThrow();
}

function assertDoesNotThrow(fn, msg) {
    fn();
}

function assertInstanceof(val, type, msg) {
    expect(val instanceof type).toBeTrue();
}

function assertUnreachable(msg) {
    expect().fail("unreachable" + (msg ? ": " + msg : ""));
}

function assertEarlyError(code) {
    assertThrows(() => new Function(code));
}

function assertThrowsAtRuntime(code, type_opt) {
    const f = new Function(code);
    assertThrows(f, type_opt);
}

function assertArrayEquals(expected, actual) {
    expect(actual).toEqual(expected);
}

test("regexp-capture-4", () => {
    assertEquals(["xxy", "x"], /^(x)?\1y$/.exec("xxy"));
    assertEquals(null, /^(x)?\1y$/.exec("xy"));
    assertEquals(["y", undefined], /^(x)?\1y$/.exec("y"));
    assertEquals(null, /^(x)?y$/.exec("xxy"));
    assertEquals(["xy", "x"], /^(x)?y$/.exec("xy"));
    assertEquals(["y", undefined], /^(x)?y$/.exec("y"));
    assertEquals(["xyzxyz", "xyz", "y"], /^(x(y)?z){1,2}$/.exec("xyzxyz"));
    assertEquals(["xyzxz", "xz", undefined], /^(x(y)?z){1,2}$/.exec("xyzxz"));
    assertEquals(["xyz", "xyz", "y"], /^(x(y)?z){1,2}$/.exec("xyz"));
    assertEquals(["xX", "x"], /(?:(.)\1)?/i.exec("xX"));
    assertEquals(["xzxyz", "xyz", "y"], /^(\2x(y)?z){1,2}$/.exec("xzxyz"));
    assertEquals(["xyzxz", "xz", undefined], /^(\2x(y)?z){1,2}$/.exec("xyzxz"));
    assertEquals(["xyzxyz", "xyz", "y"], /^(\2x(y)?z){1,2}$/.exec("xyzxyz"));
});
