Skip to content

Commit

Permalink
Adding rule set tests for quotation correction
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben King committed Dec 10, 2024
1 parent d8400d4 commit aecda54
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
23 changes: 22 additions & 1 deletion packages/punctuation-checker/test/rule-set/rule-set.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { DiagnosticProvider, DocumentManager, TextDocument, TextDocumentFactory } from '@sillsdev/lynx';
import {
DiagnosticProvider,
DocumentManager,
OnTypeFormattingProvider,
TextDocument,
TextDocumentFactory,
} from '@sillsdev/lynx';
import { describe, expect, it } from 'vitest';

import { RuleSet } from '../../src';
Expand All @@ -20,6 +26,8 @@ describe('DiagnosticProviderFactory tests', () => {
openingPunctuationMark: '\u201E',
closingPunctuationMark: '\u201F',
})
.mapAmbiguousQuotationMark('+', '\u201E')
.mapAmbiguousQuotationMark('+', '\u201F')
.build();
const pairedPunctuationConfig: PairedPunctuationConfig = new PairedPunctuationConfig.Builder()
.addRule({
Expand Down Expand Up @@ -117,4 +125,17 @@ describe('DiagnosticProviderFactory tests', () => {
.getDiagnostics('<'),
).toHaveLength(1);
});

it('creates all known on-type formatters when createOnTypeFormattingProviders is called', async () => {
const ruleSet: RuleSet = new RuleSet(allowedCharacterSet, quotationConfig, pairedPunctuationConfig);
const onTypeFormatters: OnTypeFormattingProvider[] = ruleSet.createOnTypeFormattingProviders(stubDocumentManager);

expect(onTypeFormatters.length).toEqual(1);

expect(onTypeFormatters[0].id).toEqual('quote-corrector');

expect(await onTypeFormatters[0].getOnTypeEdits('A', { line: 0, character: 0 }, '')).toBe(undefined);
expect(await onTypeFormatters[0].getOnTypeEdits('+A', { line: 0, character: 0 }, '')).toHaveLength(1);
expect(await onTypeFormatters[0].getOnTypeEdits('+A+', { line: 0, character: 0 }, '')).toHaveLength(2);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
DiagnosticProvider,
DiagnosticSeverity,
DocumentManager,
OnTypeFormattingProvider,
TextDocument,
TextDocumentFactory,
} from '@sillsdev/lynx';
Expand Down Expand Up @@ -635,4 +636,60 @@ describe('Standard English rule set tests', () => {
]);
});
});

describe('Smart quotes (a.k.a. quote correction)', () => {
const standardEnglishRuleSet = StandardRuleSets.English;

it('makes no corrections for well-formed English Biblical text', async () => {
const stubDocumentManager: DocumentManager<TextDocument> = new StubDocumentManager(new TextDocumentFactory());
const quoteCorrector: OnTypeFormattingProvider =
standardEnglishRuleSet.createOnTypeFormattingProviders(stubDocumentManager)[0];
expect(
await quoteCorrector.getOnTypeEdits(
`So Pharaoh summoned Abram and said, “What is this you have done to me? Why didn't you tell me that she was your wife?
Why did you say, ‘She is my sister,’ so that I took her to be my wife? Here is your wife! Take her and go!”`,
{ line: 0, character: 0 },
'x',
),
).toBe(undefined);
});

it('corrects intentionally placed ambiguities in otherwise well-formed English Biblical text', async () => {
const stubDocumentManager: DocumentManager<TextDocument> = new StubDocumentManager(new TextDocumentFactory());
const quoteCorrector: OnTypeFormattingProvider =
standardEnglishRuleSet.createOnTypeFormattingProviders(stubDocumentManager)[0];

expect(
await quoteCorrector.getOnTypeEdits(
`The LORD who rules over all says this: “These people have said, 'The time for rebuilding the LORD's temple has not yet come.’”`,
{ line: 0, character: 0 },
't',
),
).toEqual([
{
range: {
start: { line: 0, character: 64 },
end: { line: 0, character: 65 },
},
newText: '\u2018',
},
]);

expect(
await quoteCorrector.getOnTypeEdits(
`The LORD who rules over all says this: “These people have said, ‘The time for rebuilding the LORD's temple has not yet come.’"`,
{ line: 0, character: 0 },
't',
),
).toEqual([
{
range: {
start: { line: 0, character: 125 },
end: { line: 0, character: 126 },
},
newText: '\u201D',
},
]);
});
});
});

0 comments on commit aecda54

Please sign in to comment.