Moar fixups to strings

This commit is contained in:
Bertie690 2025-08-03 17:12:59 -04:00
parent c89accc673
commit b98ff5ae90
4 changed files with 24 additions and 28 deletions

View File

@ -1,10 +1,10 @@
import type { ArenaTag, ArenaTagTypeMap } from "#data/arena-tag"; import type { ArenaTag, ArenaTagTypeMap } from "#data/arena-tag";
import type { ArenaTagSide } from "#enums/arena-tag-side"; import type { ArenaTagSide } from "#enums/arena-tag-side";
import { ArenaTagType } from "#enums/arena-tag-type"; import type { ArenaTagType } from "#enums/arena-tag-type";
import type { OneOther } from "#test/@types/test-helpers"; import type { OneOther } from "#test/@types/test-helpers";
// biome-ignore lint/correctness/noUnusedImports: TSDoc // biome-ignore lint/correctness/noUnusedImports: TSDoc
import type { GameManager } from "#test/test-utils/game-manager"; import type { GameManager } from "#test/test-utils/game-manager";
import { getEnumStr, getOnelineDiffStr, stringifyEnumArray } from "#test/test-utils/string-utils"; import { getOnelineDiffStr } from "#test/test-utils/string-utils";
import { isGameManagerInstance, receivedStr } from "#test/test-utils/test-utils"; import { isGameManagerInstance, receivedStr } from "#test/test-utils/test-utils";
import type { NonFunctionPropertiesRecursive } from "#types/type-helpers"; import type { NonFunctionPropertiesRecursive } from "#types/type-helpers";
import type { MatcherState, SyncExpectationResult } from "@vitest/expect"; import type { MatcherState, SyncExpectationResult } from "@vitest/expect";
@ -17,7 +17,7 @@ export type toHaveArenaTagOptions<T extends ArenaTagType> = OneOther<ArenaTagTyp
/** /**
* Matcher to check if the {@linkcode Arena} has a given {@linkcode ArenaTag} active. * Matcher to check if the {@linkcode Arena} has a given {@linkcode ArenaTag} active.
* @param received - The object to check. Should be the current {@linkcode GameManager}. * @param received - The object to check. Should be the current {@linkcode GameManager}.
* @param expectedType - The {@linkcode ArenaTagType} of the desired tag, or a partially-filled object * @param expectedTag - The {@linkcode ArenaTagType} of the desired tag, or a partially-filled object
* containing the desired properties * containing the desired properties
* @param side - The {@linkcode ArenaTagSide | side of the field} the tag should affect, or * @param side - The {@linkcode ArenaTagSide | side of the field} the tag should affect, or
* {@linkcode ArenaTagSide.BOTH} to check both sides * {@linkcode ArenaTagSide.BOTH} to check both sides
@ -27,7 +27,7 @@ export function toHaveArenaTag<T extends ArenaTagType>(
this: MatcherState, this: MatcherState,
received: unknown, received: unknown,
// simplified types used for brevity; full overloads are in `vitest.d.ts` // simplified types used for brevity; full overloads are in `vitest.d.ts`
expectedType: T | (Partial<NonFunctionPropertiesRecursive<ArenaTag>> & { tagType: T; side: ArenaTagSide }), expectedTag: T | (Partial<NonFunctionPropertiesRecursive<ArenaTag>> & { tagType: T; side: ArenaTagSide }),
side?: ArenaTagSide, side?: ArenaTagSide,
): SyncExpectationResult { ): SyncExpectationResult {
if (!isGameManagerInstance(received)) { if (!isGameManagerInstance(received)) {
@ -44,40 +44,36 @@ export function toHaveArenaTag<T extends ArenaTagType>(
}; };
} }
if (typeof expectedType === "string") { if (typeof expectedTag === "string") {
// Coerce lone `tagType`s into objects // Coerce lone `tagType`s into objects
// Bangs are ok as we enforce safety via overloads // Bangs are ok as we enforce safety via overloads
expectedType = { tagType: expectedType, side: side! }; expectedTag = { tagType: expectedTag, side: side! };
} }
// We need to get all tags for the case of checking properties of a tag present on both sides of the arena // We need to get all tags for the case of checking properties of a tag present on both sides of the arena
const tags = received.scene.arena.findTagsOnSide(t => t.tagType === expectedType.tagType, expectedType.side); const tags = received.scene.arena.findTagsOnSide(t => t.tagType === expectedTag.tagType, expectedTag.side);
if (!tags.length) { if (tags.length === 0) {
const expectedStr = getEnumStr(ArenaTagType, expectedType.tagType);
return { return {
pass: false, pass: false,
message: () => `Expected the arena to have a tag matching ${expectedStr}, but it didn't!`, message: () => `Expected the Arena to have a tag of type ${expectedTag.tagType}, but it didn't!`,
expected: getEnumStr(ArenaTagType, expectedType.tagType), expected: expectedTag.tagType,
actual: stringifyEnumArray( actual: received.scene.arena.tags.map(t => t.tagType),
ArenaTagType,
received.scene.arena.tags.map(t => t.tagType),
),
}; };
} }
// Pass if any of the matching tags meet our criteria // Pass if any of the matching tags meet our criteria
const pass = tags.some(tag => const pass = tags.some(tag =>
this.equals(tag, expectedType, [...this.customTesters, this.utils.subsetEquality, this.utils.iterableEquality]), this.equals(tag, expectedTag, [...this.customTesters, this.utils.subsetEquality, this.utils.iterableEquality]),
); );
const expectedStr = getOnelineDiffStr.call(this, expectedType); const expectedStr = getOnelineDiffStr.call(this, expectedTag);
return { return {
pass, pass,
message: () => message: () =>
pass pass
? `Expected the arena to NOT have a tag matching ${expectedStr}, but it did!` ? `Expected the Arena to NOT have a tag matching ${expectedStr}, but it did!`
: `Expected the arena to have a tag matching ${expectedStr}, but it didn't!`, : `Expected the Arena to have a tag matching ${expectedStr}, but it didn't!`,
expected: expectedType, expected: expectedTag,
actual: tags, actual: tags,
}; };
} }

View File

@ -41,8 +41,8 @@ export function toHaveTerrain(
pass, pass,
message: () => message: () =>
pass pass
? `Expected Arena to NOT have ${expectedStr} active, but it did!` ? `Expected the Arena to NOT have ${expectedStr} active, but it did!`
: `Expected Arena to have ${expectedStr} active, but got ${actualStr} instead!`, : `Expected the Arena to have ${expectedStr} active, but got ${actualStr} instead!`,
expected: expectedTerrainType, expected: expectedTerrainType,
actual, actual,
}; };

View File

@ -41,8 +41,8 @@ export function toHaveWeather(
pass, pass,
message: () => message: () =>
pass pass
? `Expected Arena to NOT have ${expectedStr} weather active, but it did!` ? `Expected the Arena to NOT have ${expectedStr} weather active, but it did!`
: `Expected Arena to have ${expectedStr} weather active, but got ${actualStr} instead!`, : `Expected the Arena to have ${expectedStr} weather active, but got ${actualStr} instead!`,
expected: expectedWeatherType, expected: expectedWeatherType,
actual, actual,
}; };

View File

@ -34,10 +34,10 @@ interface getEnumStrOptions {
* @returns The stringified representation of `val` as dictated by the options. * @returns The stringified representation of `val` as dictated by the options.
* @example * @example
* ```ts * ```ts
* enum fakeEnum { * enum testEnum {
* ONE: 1, * ONE = 1,
* TWO: 2, * TWO = 2,
* THREE: 3, * THREE = 3,
* } * }
* getEnumStr(fakeEnum, fakeEnum.ONE); // Output: "ONE (=1)" * getEnumStr(fakeEnum, fakeEnum.ONE); // Output: "ONE (=1)"
* getEnumStr(fakeEnum, fakeEnum.TWO, {casing: "Title", prefix: "fakeEnum.", suffix: "!!!"}); // Output: "fakeEnum.TWO!!! (=2)" * getEnumStr(fakeEnum, fakeEnum.TWO, {casing: "Title", prefix: "fakeEnum.", suffix: "!!!"}); // Output: "fakeEnum.TWO!!! (=2)"