You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@annotator.apache.org by ge...@apache.org on 2020/05/27 18:44:32 UTC

[incubator-annotator] 01/06: Test if describe inverts test cases for match

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 34ab9c74aa02c9618bbdcbfa7405a5c82cd2177f
Author: Gerben <ge...@treora.com>
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..019b5b3
--- /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('describeTextQuote', () => {
+  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;
+}