Skip to content

Commit

Permalink
diff
Browse files Browse the repository at this point in the history
  • Loading branch information
hitesh-1997 committed Nov 23, 2024
1 parent b31229c commit 9ab18aa
Show file tree
Hide file tree
Showing 3 changed files with 224 additions and 173 deletions.
Original file line number Diff line number Diff line change
@@ -1,83 +1,75 @@
import { describe, expect, it } from 'vitest'
import { parseTextAndGenerateChangeEvents, getPositionAt } from './helper';
import {applyTextDocumentChanges} from './utils';
import { processContinousChangesForText } from './helper';
import dedent from 'dedent'
import { describe, expect, it } from 'vitest'
import { getPositionAt, parseTextAndGenerateChangeEvents } from './helper'
import { processContinousChangesForText } from './helper'
import { applyTextDocumentChanges } from './utils'

describe('parseTextAndGenerateChangeEvents', () => {

const testChanges = (
params: {
text: string,
expectedOriginalString: string,
expectedChanges: string[],
}
) => {
const { text, expectedOriginalString, expectedChanges } = params;
const { originalText, changeEvents } = parseTextAndGenerateChangeEvents(text);
expect(originalText).to.equal(expectedOriginalString);
expect(changeEvents.length).to.equal(expectedChanges.length);
for(let i=0; i<changeEvents.length; i++) {
const changes = changeEvents.slice(0, i+1);
const testChanges = (params: {
text: string
expectedOriginalString: string
expectedChanges: string[]
}) => {
const { text, expectedOriginalString, expectedChanges } = params
const { originalText, changeEvents } = parseTextAndGenerateChangeEvents(text)
expect(originalText).to.equal(expectedOriginalString)
expect(changeEvents.length).to.equal(expectedChanges.length)
for (let i = 0; i < changeEvents.length; i++) {
const changes = changeEvents.slice(0, i + 1)
const newContent = applyTextDocumentChanges(originalText, changes)
expect(newContent).to.equal(expectedChanges[i], `Failed at index ${i}. Expected "${expectedChanges[i]}" but got "${newContent}"`);
expect(newContent).to.equal(
expectedChanges[i],
`Failed at index ${i}. Expected "${expectedChanges[i]}" but got "${newContent}"`
)
}
}

it('should handle insert markers correctly', () => {
const text = 'This is a <I>test </I>string.'
const expectedOriginalString = 'This is a string.'
const expectedChanges = [
'This is a test string.'
]
testChanges({ text, expectedOriginalString, expectedChanges });
});
const expectedChanges = ['This is a test string.']
testChanges({ text, expectedOriginalString, expectedChanges })
})

it('should handle delete markers correctly', () => {
const text = 'This is a <D>sample </D>string.'
const expectedOriginalString = 'This is a sample string.'
const expectedChanges = [
'This is a string.'
]
testChanges({ text, expectedOriginalString, expectedChanges });
});

const expectedChanges = ['This is a string.']
testChanges({ text, expectedOriginalString, expectedChanges })
})

it('should handle replace markers correctly', () => {
const text = 'Please <R>replace<RM>swap</R> this word.'
const expectedOriginalString = 'Please replace this word.'
const expectedChanges = [
'Please swap this word.'
]
testChanges({ text, expectedOriginalString, expectedChanges });
});
const expectedChanges = ['Please swap this word.']
testChanges({ text, expectedOriginalString, expectedChanges })
})

it('should handle multiple markers correctly', () => {
const text = 'start<I> and</I> middle<D> unnecessary</D> text<R> swap<RM> change</R> end.';
const expectedOriginalString = 'start middle unnecessary text swap end.';
const text = 'start<I> and</I> middle<D> unnecessary</D> text<R> swap<RM> change</R> end.'
const expectedOriginalString = 'start middle unnecessary text swap end.'
const expectedChanges = [
'start and middle unnecessary text swap end.',
'start and middle text swap end.',
'start and middle text change end.'
'start and middle text change end.',
]
testChanges({ text, expectedOriginalString, expectedChanges });
});
testChanges({ text, expectedOriginalString, expectedChanges })
})

it('should handle text without markers correctly', () => {
const text = 'This is plain text.';
const expectedOriginalString = 'This is plain text.';
const text = 'This is plain text.'
const expectedOriginalString = 'This is plain text.'
const expectedChanges: string[] = []
testChanges({ text, expectedOriginalString, expectedChanges });
});
testChanges({ text, expectedOriginalString, expectedChanges })
})

it('should ignore unmatched markers', () => {
const inputText = 'Unmatched markers <I>insert text without closing.';
const { originalText, changeEvents } = parseTextAndGenerateChangeEvents(inputText);

expect(originalText).to.equal('Unmatched markers <I>insert text without closing.');
expect(changeEvents).to.have.lengthOf(0);
});
const inputText = 'Unmatched markers <I>insert text without closing.'
const { originalText, changeEvents } = parseTextAndGenerateChangeEvents(inputText)

expect(originalText).to.equal('Unmatched markers <I>insert text without closing.')
expect(changeEvents).to.have.lengthOf(0)
})

it('should handle complex multi-line text with mixed markers', () => {
const text = dedent`
Expand Down Expand Up @@ -135,20 +127,14 @@ describe('parseTextAndGenerateChangeEvents', () => {
Fifth line addition
End of text.
`,
];
testChanges({ text, expectedOriginalString, expectedChanges });
});
]
testChanges({ text, expectedOriginalString, expectedChanges })
})

it('should handle continuous insert markers correctly', () => {
const text = 'Hello <IC>World</IC>!'
const expectedOriginalString = 'Hello !'
const expectedChanges = [
'Hello W!',
'Hello Wo!',
'Hello Wor!',
'Hello Worl!',
'Hello World!',
]
const expectedChanges = ['Hello W!', 'Hello Wo!', 'Hello Wor!', 'Hello Worl!', 'Hello World!']
testChanges({ text, expectedOriginalString, expectedChanges })
})

Expand Down Expand Up @@ -180,55 +166,52 @@ describe('parseTextAndGenerateChangeEvents', () => {
]
testChanges({ text, expectedOriginalString, expectedChanges })
})

});


})

describe('processContinousChangesForText', () => {

it('should convert <IC> tags into individual <I> tags for each character', () => {
const input = 'Hello <IC>World</IC>!';
const expectedOutput = 'Hello <I>W</I><I>o</I><I>r</I><I>l</I><I>d</I>!';
expect(processContinousChangesForText(input)).toBe(expectedOutput);
});
const input = 'Hello <IC>World</IC>!'
const expectedOutput = 'Hello <I>W</I><I>o</I><I>r</I><I>l</I><I>d</I>!'
expect(processContinousChangesForText(input)).toBe(expectedOutput)
})

it('should convert <DC> tags into individual <D> tags for each character', () => {
const input = 'Delete <DC>this</DC> text.';
const expectedOutput = 'Delete <D>t</D><D>h</D><D>i</D><D>s</D> text.';
expect(processContinousChangesForText(input)).toBe(expectedOutput);
});
const input = 'Delete <DC>this</DC> text.'
const expectedOutput = 'Delete <D>t</D><D>h</D><D>i</D><D>s</D> text.'
expect(processContinousChangesForText(input)).toBe(expectedOutput)
})

it('should handle multiple <IC> and <DC> tags in the input', () => {
const input = '<IC>Hello</IC> and <DC>Goodbye</DC>!';
const expectedOutput = '<I>H</I><I>e</I><I>l</I><I>l</I><I>o</I> and <D>G</D><D>o</D><D>o</D><D>d</D><D>b</D><D>y</D><D>e</D>!';
expect(processContinousChangesForText(input)).toBe(expectedOutput);
});
const input = '<IC>Hello</IC> and <DC>Goodbye</DC>!'
const expectedOutput =
'<I>H</I><I>e</I><I>l</I><I>l</I><I>o</I> and <D>G</D><D>o</D><D>o</D><D>d</D><D>b</D><D>y</D><D>e</D>!'
expect(processContinousChangesForText(input)).toBe(expectedOutput)
})

it('should return the same text if there are no <IC> or <DC> tags', () => {
const input = 'No changes here.';
expect(processContinousChangesForText(input)).toBe(input);
});
const input = 'No changes here.'
expect(processContinousChangesForText(input)).toBe(input)
})

it('should handle empty <IC> and <DC> tags gracefully', () => {
const input = 'Empty <IC></IC> and <DC></DC> tags.';
const expectedOutput = 'Empty and tags.';
expect(processContinousChangesForText(input)).toBe(expectedOutput);
});
const input = 'Empty <IC></IC> and <DC></DC> tags.'
const expectedOutput = 'Empty and tags.'
expect(processContinousChangesForText(input)).toBe(expectedOutput)
})

it('should handle special characters within <IC> and <DC> tags', () => {
const input = 'Special <IC>chars: !@#</IC> and <DC>123</DC>.';
const expectedOutput = 'Special <I>c</I><I>h</I><I>a</I><I>r</I><I>s</I><I>:</I><I> </I><I>!</I><I>@</I><I>#</I> and <D>1</D><D>2</D><D>3</D>.';
expect(processContinousChangesForText(input)).toBe(expectedOutput);
});
const input = 'Special <IC>chars: !@#</IC> and <DC>123</DC>.'
const expectedOutput =
'Special <I>c</I><I>h</I><I>a</I><I>r</I><I>s</I><I>:</I><I> </I><I>!</I><I>@</I><I>#</I> and <D>1</D><D>2</D><D>3</D>.'
expect(processContinousChangesForText(input)).toBe(expectedOutput)
})

it('should handle consecutive <IC> and <DC> tags without text in between', () => {
const input = '<IC>ABC</IC><DC>123</DC>';
const expectedOutput = '<I>A</I><I>B</I><I>C</I><D>1</D><D>2</D><D>3</D>';
expect(processContinousChangesForText(input)).toBe(expectedOutput);
});
});

const input = '<IC>ABC</IC><DC>123</DC>'
const expectedOutput = '<I>A</I><I>B</I><I>C</I><D>1</D><D>2</D><D>3</D>'
expect(processContinousChangesForText(input)).toBe(expectedOutput)
})
})

describe('getPositionAt', () => {
it('should return position at offset 0', () => {
Expand Down
Loading

0 comments on commit 9ab18aa

Please sign in to comment.