Skip to content

Commit

Permalink
test: more case
Browse files Browse the repository at this point in the history
  • Loading branch information
shaokeyibb committed Aug 23, 2024
1 parent 2bb63b3 commit b53111c
Showing 1 changed file with 110 additions and 2 deletions.
112 changes: 110 additions & 2 deletions cloudflare-workers/test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,120 @@
// test/index.spec.ts
import { env, createExecutionContext, waitOnExecutionContext, SELF } from 'cloudflare:test';
import { describe, it, expect } from 'vitest';
import { calcOffsetModification } from '../src/utils';
import { ModifiedCommentBody, Offset } from '../src/types';

describe('Commit offset patch', () => {
it('Insert inner unit test', () => {
const offsets: Offset[] = [
{ start: 0, end: 10 },
{ start: 20, end: 30 },
];

const diff: ModifiedCommentBody['diff'] = [{ tag: 'insert', i1: 5, i2: 5, j1: 5, j2: 10 }];

const res = calcOffsetModification(offsets, diff);

expect(res).toEqual([
{ from: { start: 0, end: 10 }, to: { start: 0, end: 15 } },
{ from: { start: 20, end: 30 }, to: { start: 25, end: 35 } },
]);
});

it('Insert before unit test', () => {
const offsets: Offset[] = [
{ start: 0, end: 10 },
{ start: 20, end: 30 },
];

const diff: ModifiedCommentBody['diff'] = [{ tag: 'insert', i1: 15, i2: 15, j1: 15, j2: 20 }];

const res = calcOffsetModification(offsets, diff);

expect(res).toEqual([
{ from: { start: 0, end: 10 }, to: { start: 0, end: 10 } },
{ from: { start: 20, end: 30 }, to: { start: 25, end: 35 } },
]);
});

it('Delete before unit test', () => {
const offsets: Offset[] = [
{ start: 0, end: 10 },
{ start: 20, end: 30 },
];

const diff: ModifiedCommentBody['diff'] = [{ tag: 'delete', i1: 15, i2: 20, j1: 15, j2: 15 }];

const res = calcOffsetModification(offsets, diff);

expect(res).toEqual([
{ from: { start: 0, end: 10 }, to: { start: 0, end: 10 } },
{ from: { start: 20, end: 30 }, to: { start: 15, end: 25 } },
]);
});

it('Replace inner unit test', () => {
const offsets: Offset[] = [
{ start: 0, end: 10 },
{ start: 20, end: 30 },
];

const diff: ModifiedCommentBody['diff'] = [{ tag: 'replace', i1: 5, i2: 8, j1: 5, j2: 10 }];

const res = calcOffsetModification(offsets, diff);

expect(res).toEqual([
{ from: { start: 0, end: 10 }, to: { start: 0, end: 12 } },
{ from: { start: 20, end: 30 }, to: { start: 22, end: 32 } },
]);
});

it('Replace right unit test', () => {
const offsets: Offset[] = [
{ start: 0, end: 10 },
{ start: 20, end: 30 },
];

const diff: ModifiedCommentBody['diff'] = [{ tag: 'replace', i1: 5, i2: 15, j1: 5, j2: 10 }];

const res = calcOffsetModification(offsets, diff);

expect(res).toEqual([
{ from: { start: 0, end: 10 }, to: { start: 0, end: 5 } },
{ from: { start: 20, end: 30 }, to: { start: 15, end: 25 } },
]);
});

it('Replace left unit test', () => {
const offsets: Offset[] = [
{ start: 0, end: 10 },
{ start: 20, end: 30 },
];

const diff: ModifiedCommentBody['diff'] = [{ tag: 'replace', i1: 15, i2: 25, j1: 15, j2: 20 }];

const res = calcOffsetModification(offsets, diff);

expect(res).toEqual([
{ from: { start: 0, end: 10 }, to: { start: 0, end: 10 } },
{ from: { start: 20, end: 30 }, to: { start: 15, end: 20 } },
]);
});

it('Replace union unit test', () => {
const offsets: Offset[] = [
{ start: 0, end: 10 },
{ start: 20, end: 30 },
];

const diff: ModifiedCommentBody['diff'] = [{ tag: 'replace', i1: 5, i2: 35, j1: 5, j2: 10 }];

const res = calcOffsetModification(offsets, diff);

expect(res).toEqual([{ from: { start: 0, end: 10 }, to: { start: 0, end: 5 } }, { from: { start: 20, end: 30 } }]);
});

// https://github.com/OI-wiki/OI-wiki/blob/master/docs/index.md
it('OI-Wiki index unit test', async () => {
it('OI-Wiki index unit test', () => {
const offsets: Offset[] = [
{ start: 372, end: 439 },
{ start: 441, end: 586 },
Expand Down

0 comments on commit b53111c

Please sign in to comment.