From b53111c77f97d706efba3617f5ce1681f286d573 Mon Sep 17 00:00:00 2001 From: shaokeyibb Date: Fri, 23 Aug 2024 16:04:33 +0800 Subject: [PATCH] test: more case --- cloudflare-workers/test/index.spec.ts | 112 +++++++++++++++++++++++++- 1 file changed, 110 insertions(+), 2 deletions(-) diff --git a/cloudflare-workers/test/index.spec.ts b/cloudflare-workers/test/index.spec.ts index 85e2d992..7edb13fe 100644 --- a/cloudflare-workers/test/index.spec.ts +++ b/cloudflare-workers/test/index.spec.ts @@ -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 },