mirror of
https://github.com/claude-code-best/claude-code.git
synced 2026-06-17 13:55:50 +00:00
feat: 搭建单元测试基础设施 — Bun test runner + 示例测试
添加 bunfig.toml 配置、test script,以及三组示例测试: - src/utils/array.ts (intersperse, count, uniq) - src/utils/set.ts (difference, intersects, every, union) - packages/color-diff-napi (ansi256FromRgb, colorToEscape, detectLanguage 等) 41 tests, 0 failures. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
58
src/utils/__tests__/array.test.ts
Normal file
58
src/utils/__tests__/array.test.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { count, intersperse, uniq } from "../array";
|
||||
|
||||
describe("intersperse", () => {
|
||||
test("inserts separator between elements", () => {
|
||||
const result = intersperse([1, 2, 3], () => 0);
|
||||
expect(result).toEqual([1, 0, 2, 0, 3]);
|
||||
});
|
||||
|
||||
test("returns empty array for empty input", () => {
|
||||
expect(intersperse([], () => 0)).toEqual([]);
|
||||
});
|
||||
|
||||
test("returns single element without separator", () => {
|
||||
expect(intersperse([1], () => 0)).toEqual([1]);
|
||||
});
|
||||
|
||||
test("passes index to separator function", () => {
|
||||
const result = intersperse(["a", "b", "c"], (i) => `sep-${i}`);
|
||||
expect(result).toEqual(["a", "sep-1", "b", "sep-2", "c"]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("count", () => {
|
||||
test("counts matching elements", () => {
|
||||
expect(count([1, 2, 3, 4, 5], (x) => x > 3)).toBe(2);
|
||||
});
|
||||
|
||||
test("returns 0 for empty array", () => {
|
||||
expect(count([], () => true)).toBe(0);
|
||||
});
|
||||
|
||||
test("returns 0 when nothing matches", () => {
|
||||
expect(count([1, 2, 3], (x) => x > 10)).toBe(0);
|
||||
});
|
||||
|
||||
test("counts all when everything matches", () => {
|
||||
expect(count([1, 2, 3], () => true)).toBe(3);
|
||||
});
|
||||
});
|
||||
|
||||
describe("uniq", () => {
|
||||
test("removes duplicates", () => {
|
||||
expect(uniq([1, 2, 2, 3, 3, 3])).toEqual([1, 2, 3]);
|
||||
});
|
||||
|
||||
test("preserves order of first occurrence", () => {
|
||||
expect(uniq([3, 1, 2, 1, 3])).toEqual([3, 1, 2]);
|
||||
});
|
||||
|
||||
test("handles empty array", () => {
|
||||
expect(uniq([])).toEqual([]);
|
||||
});
|
||||
|
||||
test("works with strings", () => {
|
||||
expect(uniq(["a", "b", "a"])).toEqual(["a", "b"]);
|
||||
});
|
||||
});
|
||||
63
src/utils/__tests__/set.test.ts
Normal file
63
src/utils/__tests__/set.test.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { difference, every, intersects, union } from "../set";
|
||||
|
||||
describe("difference", () => {
|
||||
test("returns elements in a but not in b", () => {
|
||||
const result = difference(new Set([1, 2, 3]), new Set([2, 3, 4]));
|
||||
expect(result).toEqual(new Set([1]));
|
||||
});
|
||||
|
||||
test("returns empty set when a is subset of b", () => {
|
||||
expect(difference(new Set([1, 2]), new Set([1, 2, 3]))).toEqual(new Set());
|
||||
});
|
||||
|
||||
test("returns a when b is empty", () => {
|
||||
expect(difference(new Set([1, 2]), new Set())).toEqual(new Set([1, 2]));
|
||||
});
|
||||
});
|
||||
|
||||
describe("intersects", () => {
|
||||
test("returns true when sets share elements", () => {
|
||||
expect(intersects(new Set([1, 2]), new Set([2, 3]))).toBe(true);
|
||||
});
|
||||
|
||||
test("returns false when sets are disjoint", () => {
|
||||
expect(intersects(new Set([1, 2]), new Set([3, 4]))).toBe(false);
|
||||
});
|
||||
|
||||
test("returns false for empty sets", () => {
|
||||
expect(intersects(new Set(), new Set([1]))).toBe(false);
|
||||
expect(intersects(new Set([1]), new Set())).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("every", () => {
|
||||
test("returns true when a is subset of b", () => {
|
||||
expect(every(new Set([1, 2]), new Set([1, 2, 3]))).toBe(true);
|
||||
});
|
||||
|
||||
test("returns false when a has elements not in b", () => {
|
||||
expect(every(new Set([1, 4]), new Set([1, 2, 3]))).toBe(false);
|
||||
});
|
||||
|
||||
test("returns true for empty a", () => {
|
||||
expect(every(new Set(), new Set([1, 2]))).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("union", () => {
|
||||
test("combines both sets", () => {
|
||||
const result = union(new Set([1, 2]), new Set([3, 4]));
|
||||
expect(result).toEqual(new Set([1, 2, 3, 4]));
|
||||
});
|
||||
|
||||
test("deduplicates shared elements", () => {
|
||||
const result = union(new Set([1, 2]), new Set([2, 3]));
|
||||
expect(result).toEqual(new Set([1, 2, 3]));
|
||||
});
|
||||
|
||||
test("handles empty sets", () => {
|
||||
expect(union(new Set(), new Set([1]))).toEqual(new Set([1]));
|
||||
expect(union(new Set([1]), new Set())).toEqual(new Set([1]));
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user