passsy / fakes-overridesr3
Override Pattern for Fakes
PublicGive each method of a fake a nullable override function field placed right above it, so a test can change one behavior without a new fake.
tests, run by the registry6 passed · slowest 2.8 ms
✓fires2.8 ms
fires on a fake class without override fields
write test/fakes/fake_api.dartfired: line 1
class FakeApi implements Api {
Future<void> addPhoto(String uuid, double lat, double lon) async {}
}✓quiet2.4 ms
stays quiet on a fake that already uses the override pattern
write test/fakes/fake_api.dartstayed quiet
class FakeApi implements Api {
Future<void> Function(String uuid, double lat, double lon)? overrideAddPhoto;
Future<void> addPhoto(String uuid, double lat, double lon) async {
if (overrideAddPhoto != null) {
return overrideAddPhoto!(uuid, lat, lon);
}
}
}✓quiet2.1 ms
stays quiet on a class that is not a fake
write lib/api.dartstayed quiet
class Faker {}
class RealApi implements Api {}
✓quiet2.0 ms
stays quiet on a legacy fake when the agent edits elsewhere or reads
write test/fakes/fake_api.dart · wrote line 2stayed quiet
class FakeApi implements Api {
Future<void> addPhoto(String uuid, double lat, double lon) async {}
}read test/fakes/fake_api.dartno handler of the detector wants this event
class FakeApi implements Api {
Future<void> addPhoto(String uuid, double lat, double lon) async {}
}✓fires2.6 ms
points at the principle when the user asks for a fake
prompt · in fixtures/dart_appfired: "fake"
Write a fake for the photo api✓quiet2.2 ms
stays quiet on the same words outside a Dart project
promptstayed quiet
Write a fake for the photo apidetector.test.tseach detect() call runs the real detectorEdit
import { expect, test } from "vitest";
import { detect, edit, prompt, read, source, write } from "@wellactually/sdk/v3/test";
const fake = source`
class FakeApi implements Api {
@override
Future<void> addPhoto(String uuid, double lat, double lon) async {}
}
`;
test("fires on a fake class without override fields", () => {
expect(detect(write("test/fakes/fake_api.dart", fake))).toEqual([{ line: 1 }]);
});
test("stays quiet on a fake that already uses the override pattern", () => {
const code = source`
class FakeApi implements Api {
Future<void> Function(String uuid, double lat, double lon)? overrideAddPhoto;
@override
Future<void> addPhoto(String uuid, double lat, double lon) async {
if (overrideAddPhoto != null) {
return overrideAddPhoto!(uuid, lat, lon);
}
}
}
`;
expect(detect(write("test/fakes/fake_api.dart", code))).toEqual([]);
});
test("stays quiet on a class that is not a fake", () => {
expect(detect(write("lib/api.dart", "class Faker {}\nclass RealApi implements Api {}\n"))).toEqual([]);
});
test("stays quiet on a legacy fake when the agent edits elsewhere or reads", () => {
expect(detect(edit("test/fakes/fake_api.dart", fake, { written: " @override" }))).toEqual([]);
expect(detect(read("test/fakes/fake_api.dart", fake))).toEqual([]);
});
const dartApp = { project: "fixtures/dart_app" };
test("points at the principle when the user asks for a fake", () => {
expect(detect(prompt("Write a fake for the photo api"), dartApp)).toEqual([{ match: "fake", depth: "pointer" }]);
});
test("stays quiet on the same words outside a Dart project", () => {
expect(detect(prompt("Write a fake for the photo api"))).toEqual([]);
});