passsy / file-namingr2
Name a File Like the First Class
PublicName a Dart file after the main class it contains, and keep that class at the very top, so the file name tells the reader what is inside.
tests, run by the registry7 passed · slowest 1.1 ms
✓fires1.1 ms
fires when no class earns the file its name
write lib/image_api.dartfired: line 1
class Photo {}
class Gallery {
List<Photo> photos = [];
}✓quiet1.0 ms
stays quiet when a class matches the file name
write lib/gallery.dartstayed quiet
class Photo {}
class Gallery {
List<Photo> photos = [];
}✓quiet1.1 ms
accepts a class that ends with the file name
write lib/report/fetcher.dartstayed quiet
class ReportFetcher {}
✓quiet1.1 ms
ignores underscores and case
write lib/http_client.dartstayed quiet
class HTTPClient {}
✓quiet1.0 ms
stays quiet on a file of functions
write lib/format.dartstayed quiet
String formatPrice(int cents) => '$cents';
✓quiet1.0 ms
stays quiet when the edit writes no class
write lib/image_api.dart · wrote line 4stayed quiet
class Photo {}
class Gallery {
List<Photo> photos = [];
}✓skips0.0 ms
stays quiet on a read, a test and main.dart
read lib/image_api.dartno handler of the detector wants this event
class Photo {}
class Gallery {
List<Photo> photos = [];
}write test/image_api_test.dartno handler of the detector wants this event
class Photo {}
class Gallery {
List<Photo> photos = [];
}write lib/main.dartno handler of the detector wants this event
class Photo {}
class Gallery {
List<Photo> photos = [];
}detector.test.tseach detect() call runs the real detectorEdit
import { expect, test } from "vitest";
import { detect, edit, read, source, write } from "@wellactually/sdk/v3/test";
const photos = source`
class Photo {}
class Gallery {
List<Photo> photos = [];
}
`;
test("fires when no class earns the file its name", () => {
expect(detect(write("lib/image_api.dart", photos))).toEqual([{ line: 1 }]);
});
test("stays quiet when a class matches the file name", () => {
expect(detect(write("lib/gallery.dart", photos))).toEqual([]);
});
test("accepts a class that ends with the file name", () => {
expect(detect(write("lib/report/fetcher.dart", "class ReportFetcher {}\n"))).toEqual([]);
});
test("ignores underscores and case", () => {
expect(detect(write("lib/http_client.dart", "class HTTPClient {}\n"))).toEqual([]);
});
test("stays quiet on a file of functions", () => {
expect(detect(write("lib/format.dart", "String formatPrice(int cents) => '$cents';\n"))).toEqual([]);
});
test("stays quiet when the edit writes no class", () => {
expect(detect(edit("lib/image_api.dart", photos, { written: " List<Photo> photos = [];" }))).toEqual([]);
});
test("stays quiet on a read, a test and main.dart", () => {
expect(detect(read("lib/image_api.dart", photos))).toEqual([]);
expect(detect(write("test/image_api_test.dart", photos))).toEqual([]);
expect(detect(write("lib/main.dart", photos))).toEqual([]);
});