1import {
2  insertContentsAtOffset,
3  replaceContentsWithOffset,
4  searchFromOffset,
5} from '../commonCodeMod';
6
7describe(insertContentsAtOffset, () => {
8  it('should insert in the middle', () => {
9    expect(insertContentsAtOffset('aabbcc', 'dd', 4)).toEqual('aabbddcc');
10  });
11
12  it('should insert at the head', () => {
13    expect(insertContentsAtOffset('aabbcc', 'dd', 0)).toEqual('ddaabbcc');
14  });
15
16  it('should insert at the tail', () => {
17    expect(insertContentsAtOffset('aabbcc', 'dd', 6)).toEqual('aabbccdd');
18  });
19
20  it('should throw for boundary errors', () => {
21    expect(() => {
22      insertContentsAtOffset('aabbcc', 'dd', -1);
23    }).toThrow();
24    expect(() => {
25      insertContentsAtOffset('aabbcc', 'dd', 999);
26    }).toThrow();
27  });
28});
29
30describe(replaceContentsWithOffset, () => {
31  it('should support replacement in the middle', () => {
32    expect(replaceContentsWithOffset('abc', 'd', 1, 1)).toEqual('adc');
33    expect(replaceContentsWithOffset('aabbcc', '', 2, 3)).toEqual('aacc');
34    expect(replaceContentsWithOffset('aabbcc', 'dd', 2, 3)).toEqual('aaddcc');
35    expect(replaceContentsWithOffset('aabbcc', 'ExtendString', 2, 3)).toEqual('aaExtendStringcc');
36  });
37
38  it('should throw for boundary errors', () => {
39    expect(() => {
40      replaceContentsWithOffset('aabbcc', 'dd', -1, -1);
41    }).toThrow();
42    expect(() => {
43      replaceContentsWithOffset('aabbcc', 'dd', 0, 999);
44    }).toThrow();
45    expect(() => {
46      replaceContentsWithOffset('aabbcc', 'dd', 2, 1);
47    }).toThrow();
48  });
49});
50
51describe(searchFromOffset, () => {
52  it('should return matched index + offset', () => {
53    expect(searchFromOffset('aabbaabb', /aabb/, 0)).toBe(0);
54    expect(searchFromOffset('aabbaabb', /aabb/, 2)).toBe(4);
55  });
56
57  it('should return -1 if not found', () => {
58    expect(searchFromOffset('aabbaabb', /cc/, 2)).toBe(-1);
59  });
60
61  it('should return -1 if offset out of bound', () => {
62    expect(searchFromOffset('aabbaabb', /cc/, 100)).toBe(-1);
63  });
64});
65