This is an automated email from the ASF dual-hosted git repository. gerben pushed a commit to branch dom-tests in repository https://gitbox.apache.org/repos/asf/incubator-annotator.git
commit e1ac57a96fb828056de0332ab2cf6fcb83d67af4 Author: Gerben <[email protected]> AuthorDate: Mon May 25 14:05:03 2020 +0200 Test if describe inverts test cases for match --- packages/dom/test/text-quote-describe.ts | 49 ++++++++++++++++++++++++++++++++ packages/dom/test/utils.ts | 7 +++++ 2 files changed, 56 insertions(+) diff --git a/packages/dom/test/text-quote-describe.ts b/packages/dom/test/text-quote-describe.ts new file mode 100644 index 0000000..8d9f74c --- /dev/null +++ b/packages/dom/test/text-quote-describe.ts @@ -0,0 +1,49 @@ +/** + * @license + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { assert } from 'chai'; +import { describeTextQuote } from '../src/text-quote/describe'; +import testMatchCases from './text-quote-match-cases'; +import { hydrateRange } from './utils'; + +const domParser = new window.DOMParser(); + +describe('createTextQuoteSelectorMatcher', () => { + describe('inverts test cases of text quote matcher', () => { + const applicableTestCases = Object.entries(testMatchCases) + .filter(([_, { expected }]) => expected.length > 0); + + for (const [name, { html, selector, expected }] of applicableTestCases) { + it(`case: '${name}'`, async () => { + const doc = domParser.parseFromString(html, 'text/html'); + for (const rangeInfo of expected) { + const range = hydrateRange(rangeInfo, doc); + const result = await describeTextQuote(range, doc); + assert.equal(result.exact, selector.exact); + // Our result may have a different combination of prefix/suffix; only check for obvious inconsistency. + if (selector.prefix && result.prefix) + assert(selector.prefix.endsWith(result.prefix.substring(result.prefix.length - selector.prefix.length)), 'Inconsistent prefixes'); + if (selector.suffix && result.suffix) + assert(selector.suffix.startsWith(result.suffix.substring(0, selector.suffix.length)), 'Inconsistent suffixes'); + } + }); + } + }); +}); diff --git a/packages/dom/test/utils.ts b/packages/dom/test/utils.ts index 7aaa9c9..889059c 100644 --- a/packages/dom/test/utils.ts +++ b/packages/dom/test/utils.ts @@ -16,3 +16,10 @@ export function evaluateXPath(doc: Document, xpath: string): Node { ); return nodes[0]; } + +export function hydrateRange(rangeInfo: RangeInfo, doc: Document): Range { + const range = doc.createRange(); + range.setStart(evaluateXPath(doc, rangeInfo.startContainerXPath), rangeInfo.startOffset); + range.setEnd(evaluateXPath(doc, rangeInfo.endContainerXPath), rangeInfo.endOffset); + return range; +}
