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 2021/06/25 08:55:50 UTC

[incubator-annotator] 02/03: Simplify tests

This is an automated email from the ASF dual-hosted git repository.

gerben pushed a commit to branch simpler-matcher-creation
in repository https://gitbox.apache.org/repos/asf/incubator-annotator.git

commit cc999477f12eadd802f30b94bab861a6e6e6a820
Author: Gerben <ge...@treora.com>
AuthorDate: Sun Jun 20 15:29:04 2021 +0200

    Simplify tests
    
    Possible since a Node is now accepted as the scope
---
 packages/dom/test/text-position/describe.test.ts |  4 +---
 packages/dom/test/text-position/match.test.ts    | 19 ++++---------------
 packages/dom/test/text-quote/describe.test.ts    |  8 ++------
 packages/dom/test/text-quote/match.test.ts       | 20 ++++----------------
 4 files changed, 11 insertions(+), 40 deletions(-)

diff --git a/packages/dom/test/text-position/describe.test.ts b/packages/dom/test/text-position/describe.test.ts
index 3c4f2c9..34a58c8 100644
--- a/packages/dom/test/text-position/describe.test.ts
+++ b/packages/dom/test/text-position/describe.test.ts
@@ -33,11 +33,9 @@ describe('createTextPositionSelectorMatcher', () => {
       const range = expected[0];
       it(`case: '${name}'`, async () => {
         const doc = domParser.parseFromString(html, 'text/html');
-        const scope = doc.createRange();
-        scope.selectNodeContents(doc);
         const result = await describeTextPosition(
           hydrateRange(range, doc),
-          scope,
+          doc,
         );
         assert.deepEqual(result, selector);
       });
diff --git a/packages/dom/test/text-position/match.test.ts b/packages/dom/test/text-position/match.test.ts
index 6d51c95..f59f490 100644
--- a/packages/dom/test/text-position/match.test.ts
+++ b/packages/dom/test/text-position/match.test.ts
@@ -33,28 +33,20 @@ describe('createTextPositionSelectorMatcher', () => {
   )) {
     it(`works for case: '${name}'`, async () => {
       const doc = domParser.parseFromString(html, 'text/html');
-
-      const scope = doc.createRange();
-      scope.selectNodeContents(doc);
-
-      await testMatcher(doc, scope, selector, expected);
+      await testMatcher(doc, doc, selector, expected);
     });
   }
 
   it('handles adjacent text nodes', async () => {
     const { html, selector } = testCases['simple'];
     const doc = domParser.parseFromString(html, 'text/html');
-
-    const scope = doc.createRange();
-    scope.selectNodeContents(doc);
-
     const textNode = evaluateXPath(doc, '//b/text()') as Text;
 
     textNode.splitText(16);
     // console.log([...textNode.parentNode.childNodes].map(node => node.textContent))
     // โ†’ [ 'l๐Ÿ˜ƒrem ipsum dol', 'or amet yada yada' ]
 
-    await testMatcher(doc, scope, selector, [
+    await testMatcher(doc, doc, selector, [
       {
         startContainerXPath: '//b/text()[1]',
         startOffset: 13,
@@ -68,9 +60,6 @@ describe('createTextPositionSelectorMatcher', () => {
     const { html, selector } = testCases['simple'];
     const doc = domParser.parseFromString(html, 'text/html');
 
-    const scope = doc.createRange();
-    scope.selectNodeContents(doc);
-
     const textNode = evaluateXPath(doc, '//b/text()') as Text;
     textNode.splitText(textNode.length);
     textNode.splitText(21);
@@ -83,7 +72,7 @@ describe('createTextPositionSelectorMatcher', () => {
     // console.log([...textNode.parentNode.childNodes].map(node => node.textContent))
     // โ†’ [ '', 'l๐Ÿ˜ƒrem ipsum ', '', 'dolor', '', ' am', '', 'et yada yada', '' ]
 
-    await testMatcher(doc, scope, selector, [
+    await testMatcher(doc, doc, selector, [
       {
         startContainerXPath: '//b/text()[4]', // "dolor"
         startOffset: 0,
@@ -178,7 +167,7 @@ describe('createTextPositionSelectorMatcher', () => {
 
 async function testMatcher(
   doc: Document,
-  scope: Range,
+  scope: Node | Range,
   selector: TextPositionSelector,
   expected: RangeInfo[],
 ) {
diff --git a/packages/dom/test/text-quote/describe.test.ts b/packages/dom/test/text-quote/describe.test.ts
index ceed528..ac48ed2 100644
--- a/packages/dom/test/text-quote/describe.test.ts
+++ b/packages/dom/test/text-quote/describe.test.ts
@@ -38,11 +38,9 @@ function runTestCases(testCases: DescribeTextQuoteTestCases) {
   )) {
     it(`works for case: ${name}`, async () => {
       const doc = domParser.parseFromString(html, 'text/html');
-      const scope = doc.createRange();
-      scope.selectNodeContents(doc);
       const result = await describeTextQuote(
         hydrateRange(range, doc),
-        scope,
+        doc,
         options,
       );
       assert.deepEqual(result, expected);
@@ -142,11 +140,9 @@ describe('describeTextQuote', () => {
     for (const [name, { html, selector, expected }] of applicableTestCases) {
       it(`case: '${name}'`, async () => {
         const doc = domParser.parseFromString(html, 'text/html');
-        const scope = doc.createRange();
-        scope.selectNodeContents(doc);
         for (const rangeInfo of expected) {
           const range = hydrateRange(rangeInfo, doc);
-          const result = await describeTextQuote(range, scope);
+          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)
diff --git a/packages/dom/test/text-quote/match.test.ts b/packages/dom/test/text-quote/match.test.ts
index d2aefdb..f023215 100644
--- a/packages/dom/test/text-quote/match.test.ts
+++ b/packages/dom/test/text-quote/match.test.ts
@@ -33,21 +33,13 @@ describe('createTextQuoteSelectorMatcher', () => {
   )) {
     it(`works for case: '${name}'`, async () => {
       const doc = domParser.parseFromString(html, 'text/html');
-
-      const scope = doc.createRange();
-      scope.selectNodeContents(doc);
-
-      await testMatcher(doc, scope, selector, expected);
+      await testMatcher(doc, doc, selector, expected);
     });
   }
 
   it('handles adjacent text nodes', async () => {
     const { html, selector } = testCases['simple'];
     const doc = domParser.parseFromString(html, 'text/html');
-
-    const scope = doc.createRange();
-    scope.selectNodeContents(doc);
-
     const textNode = evaluateXPath(doc, '//b/text()') as Text;
 
     for (let index = textNode.length - 1; index > 0; index--)
@@ -55,7 +47,7 @@ describe('createTextQuoteSelectorMatcher', () => {
     // console.log([...textNode.parentNode.childNodes].map(node => node.textContent))
     // โ†’ 'l',  'o', 'r', 'e', 'm', โ€ฆ
 
-    await testMatcher(doc, scope, selector, [
+    await testMatcher(doc, doc, selector, [
       {
         startContainerXPath: '//b/text()[13]',
         startOffset: 0,
@@ -68,10 +60,6 @@ describe('createTextQuoteSelectorMatcher', () => {
   it('handles empty text nodes', async () => {
     const { html, selector } = testCases['simple'];
     const doc = domParser.parseFromString(html, 'text/html');
-
-    const scope = doc.createRange();
-    scope.selectNodeContents(doc);
-
     const textNode = evaluateXPath(doc, '//b/text()') as Text;
     textNode.splitText(textNode.length);
     textNode.splitText(20);
@@ -84,7 +72,7 @@ describe('createTextQuoteSelectorMatcher', () => {
     // console.log([...textNode.parentNode.childNodes].map(node => node.textContent))
     // โ†’ '', 'lorem ipsum ', '', 'dolor', '', ' am', '', 'et yada yada', ''
 
-    await testMatcher(doc, scope, selector, [
+    await testMatcher(doc, doc, selector, [
       {
         startContainerXPath: '//b/text()[4]', // "dolor"
         startOffset: 0,
@@ -187,7 +175,7 @@ describe('createTextQuoteSelectorMatcher', () => {
 
 async function testMatcher(
   doc: Document,
-  scope: Range,
+  scope: Node | Range,
   selector: TextQuoteSelector,
   expected: RangeInfo[],
 ) {