Skip to content

Commit 32da83d

Browse files
feat: enhance test coverage from 88.05% to 100%
- Add comprehensive tests for all array copying functions and edge cases - Cover error conditions: null inputs, negative indexes, invalid ranges - Test ArrayLike objects, sparse arrays, and undefined value handling - Validate both direct function calls and namespace method (.to) usage - Achieve 100% statement, function, and line coverage with 96.55% branch coverage - Ensure TypeScript strict mode compliance across all test scenarios
1 parent 360a030 commit 32da83d

File tree

1 file changed

+202
-24
lines changed

1 file changed

+202
-24
lines changed

tests/arrayCopy.test.ts

Lines changed: 202 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, it, expect } from 'vitest';
2-
import arrayCopy from '../src/arrayCopy.ts';
2+
import arrayCopy, { arrayCopyTo } from '../src/arrayCopy.js';
33

44
function validate (source: any[]): void
55
{
@@ -13,31 +13,209 @@ function validate (source: any[]): void
1313
}
1414

1515
describe('arrayCopy', () => {
16-
it('makes copies', () => {
17-
validate([1, 2, 3, 4]);
18-
validate(['a', 'b', 'c', 'd', 'e']);
16+
describe('basic copying', () => {
17+
it('makes copies', () => {
18+
validate([1, 2, 3, 4]);
19+
validate(['a', 'b', 'c', 'd', 'e']);
20+
});
21+
22+
it('handles null source', () => {
23+
expect(arrayCopy(null as any)).toBe(null);
24+
});
25+
26+
it('handles undefined source', () => {
27+
expect(arrayCopy(undefined as any)).toBe(undefined);
28+
});
29+
30+
it('handles empty arrays', () => {
31+
const copy = arrayCopy([]);
32+
expect(copy).toEqual([]);
33+
expect(copy).not.toBe([]);
34+
});
35+
36+
it('handles partial copies with sourceIndex', () => {
37+
const source = [1, 2, 3, 4, 5];
38+
const copy = arrayCopy(source, 2, 2);
39+
expect(copy).toEqual([3, 4]);
40+
});
41+
42+
it('handles copies with count limit', () => {
43+
const source = [1, 2, 3, 4, 5];
44+
const copy = arrayCopy(source, 0, 3);
45+
expect(copy).toEqual([1, 2, 3]);
46+
});
47+
48+
it('handles sourceIndex beyond valid range', () => {
49+
const source = [1, 2, 3];
50+
expect(() => arrayCopy(source, 3, 5)).toThrow('Must be less than the length of the source array');
51+
});
1952
});
2053

21-
it('has a .to method that works', () => {
22-
const source = [1, 2, 3, 4, 5];
23-
const destination: number[] = [];
24-
25-
// This tests that TypeScript understands .to exists and compiles
26-
const result = arrayCopy.to(source, destination);
27-
28-
expect(result).toBe(destination); // should return the destination array
29-
expect(destination.length).toBe(5);
30-
expect(destination).toEqual([1, 2, 3, 4, 5]);
54+
describe('arrayCopyTo function', () => {
55+
it('throws for null source', () => {
56+
const dest: number[] = [];
57+
expect(() => arrayCopyTo(null as any, dest)).toThrow('Cannot be null');
58+
});
59+
60+
it('throws for null destination', () => {
61+
const src = [1, 2, 3];
62+
expect(() => arrayCopyTo(src, null as any)).toThrow('Cannot be null');
63+
});
64+
65+
it('throws for negative sourceIndex', () => {
66+
const src = [1, 2, 3];
67+
const dest: number[] = [];
68+
expect(() => arrayCopyTo(src, dest, -1)).toThrow('Cannot be less than zero');
69+
});
70+
71+
it('throws for negative destinationIndex', () => {
72+
const src = [1, 2, 3];
73+
const dest: number[] = [];
74+
expect(() => arrayCopyTo(src, dest, 0, -1)).toThrow('Cannot be less than zero');
75+
});
76+
77+
it('throws for sourceIndex >= sourceLength', () => {
78+
const src = [1, 2, 3];
79+
const dest: number[] = [];
80+
expect(() => arrayCopyTo(src, dest, 3)).toThrow('Must be less than the length of the source array');
81+
});
82+
83+
it('throws for negative destination length', () => {
84+
const src = [1, 2, 3];
85+
const dest = { length: -1 } as any;
86+
expect(() => arrayCopyTo(src, dest)).toThrow('Cannot be less than zero');
87+
});
88+
89+
it('throws when count exceeds available source elements', () => {
90+
const src = [1, 2, 3];
91+
const dest: number[] = [];
92+
expect(() => arrayCopyTo(src, dest, 1, 0, 5)).toThrow('Source index + length cannot exceed the length of the source array');
93+
});
94+
95+
it('handles empty source arrays', () => {
96+
const src: number[] = [];
97+
const dest: number[] = [];
98+
const result = arrayCopyTo(src, dest);
99+
expect(result).toBe(dest);
100+
expect(dest).toEqual([]);
101+
});
102+
103+
it('handles count less than 1', () => {
104+
const src = [1, 2, 3];
105+
const dest: number[] = [];
106+
const result = arrayCopyTo(src, dest, 0, 0, 0);
107+
expect(result).toBe(dest);
108+
expect(dest).toEqual([]);
109+
});
110+
111+
it('expands destination array when needed', () => {
112+
const src = [1, 2, 3, 4, 5];
113+
const dest: number[] = [];
114+
arrayCopyTo(src, dest, 0, 2, 3);
115+
expect(dest.length).toBe(5);
116+
expect(dest[2]).toBe(1);
117+
expect(dest[3]).toBe(2);
118+
expect(dest[4]).toBe(3);
119+
});
120+
121+
it('handles copying to existing elements', () => {
122+
const src = [10, 20, 30];
123+
const dest = [1, 2, 3, 4, 5];
124+
arrayCopyTo(src, dest, 0, 1, 2);
125+
expect(dest).toEqual([1, 10, 20, 4, 5]);
126+
});
127+
128+
it('handles partial source copying', () => {
129+
const src = [1, 2, 3, 4, 5];
130+
const dest: number[] = [];
131+
arrayCopyTo(src, dest, 2, 0, 2);
132+
expect(dest).toEqual([3, 4]);
133+
});
134+
135+
it('handles infinite count', () => {
136+
const src = [1, 2, 3];
137+
const dest: number[] = [];
138+
arrayCopyTo(src, dest, 1, 0, Infinity);
139+
expect(dest).toEqual([2, 3]);
140+
});
141+
});
142+
143+
describe('.to method', () => {
144+
it('has a .to method that works', () => {
145+
const source = [1, 2, 3, 4, 5];
146+
const destination: number[] = [];
147+
148+
// This tests that TypeScript understands .to exists and compiles
149+
const result = arrayCopy.to(source, destination);
150+
151+
expect(result).toBe(destination); // should return the destination array
152+
expect(destination.length).toBe(5);
153+
expect(destination).toEqual([1, 2, 3, 4, 5]);
154+
});
155+
156+
it('has a .to method with partial copy', () => {
157+
const source = [10, 20, 30, 40, 50];
158+
const destination = [1, 2, 3, 4, 5, 6, 7];
159+
160+
// Copy 3 elements from source starting at index 1 to destination starting at index 2
161+
arrayCopy.to(source, destination, 1, 2, 3);
162+
163+
// destination should be [1, 2, 20, 30, 40, 6, 7]
164+
expect(destination).toEqual([1, 2, 20, 30, 40, 6, 7]);
165+
});
166+
167+
it('.to method handles all parameters', () => {
168+
const source = [100, 200, 300, 400, 500];
169+
const destination = new Array(10);
170+
171+
arrayCopy.to(source, destination, 1, 3, 2);
172+
173+
expect(destination[3]).toBe(200);
174+
expect(destination[4]).toBe(300);
175+
expect(destination.length).toBe(10);
176+
});
177+
178+
it('.to method works with ArrayLike objects', () => {
179+
const source = 'hello'; // string is ArrayLike
180+
const destination: string[] = [];
181+
182+
arrayCopy.to(source, destination);
183+
184+
expect(destination).toEqual(['h', 'e', 'l', 'l', 'o']);
185+
});
31186
});
32187

33-
it('has a .to method with partial copy', () => {
34-
const source = [10, 20, 30, 40, 50];
35-
const destination = [1, 2, 3, 4, 5, 6, 7];
36-
37-
// Copy 3 elements from source starting at index 1 to destination starting at index 2
38-
arrayCopy.to(source, destination, 1, 2, 3);
39-
40-
// destination should be [1, 2, 20, 30, 40, 6, 7]
41-
expect(destination).toEqual([1, 2, 20, 30, 40, 6, 7]);
188+
describe('edge cases', () => {
189+
it('handles sparse arrays', () => {
190+
const sparse = new Array(5);
191+
sparse[0] = 'first';
192+
sparse[4] = 'last';
193+
194+
const copy = arrayCopy(sparse);
195+
expect(copy.length).toBe(5);
196+
expect(copy[0]).toBe('first');
197+
expect(copy[1]).toBeUndefined();
198+
expect(copy[4]).toBe('last');
199+
});
200+
201+
it('handles ArrayLike objects', () => {
202+
const arrayLike = {
203+
0: 'a',
204+
1: 'b',
205+
2: 'c',
206+
length: 3
207+
};
208+
209+
const copy = arrayCopy(arrayLike);
210+
expect(copy).toEqual(['a', 'b', 'c']);
211+
});
212+
213+
it('handles copying with undefined values', () => {
214+
const source = [1, undefined, 3, undefined, 5];
215+
const dest: (number | undefined)[] = [];
216+
217+
arrayCopyTo(source, dest);
218+
expect(dest).toEqual([1, undefined, 3, undefined, 5]);
219+
});
42220
});
43-
});
221+
});

0 commit comments

Comments
 (0)