Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(184)

Side by Side Diff: third_party/WebKit/LayoutTests/editing/assert_selection.js

Issue 2138653002: [Editing][CodeHealth] Improve result comparing in assert_selection (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/WebKit/LayoutTests/editing/assert_selection.html ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 'use strict'; 5 'use strict';
6 6
7 // This file provides |assert_selection(sample, tester, expectedText)| assertion 7 // This file provides |assert_selection(sample, tester, expectedText)| assertion
8 // to W3C test harness to write editing test cases easier. 8 // to W3C test harness to write editing test cases easier.
9 // 9 //
10 // |sample| is an HTML fragment text which is inserted as |innerHTML|. It should 10 // |sample| is an HTML fragment text which is inserted as |innerHTML|. It should
(...skipping 562 matching lines...) Expand 10 before | Expand all | Expand 10 after
573 serialize() { 573 serialize() {
574 /** @type {!SampleSelection} */ 574 /** @type {!SampleSelection} */
575 const selection = SampleSelection.fromDOMSelection(this.selection_); 575 const selection = SampleSelection.fromDOMSelection(this.selection_);
576 /** @type {!Serializer} */ 576 /** @type {!Serializer} */
577 const serializer = new Serializer(selection); 577 const serializer = new Serializer(selection);
578 return serializer.serialize(this.document_); 578 return serializer.serialize(this.document_);
579 } 579 }
580 } 580 }
581 581
582 function assembleDescription() { 582 function assembleDescription() {
583 const RE_TEST_FUNCTION =
584 new RegExp('at Object.test \\(.*?([^/]+?):(\\d+):(\\d+)\\)');
585 function getStack() { 583 function getStack() {
586 let stack; 584 let stack;
587 try { 585 try {
588 throw new Error('get line number'); 586 throw new Error('get line number');
589 } catch (error) { 587 } catch (error) {
590 stack = error.stack.split('\n').slice(1); 588 stack = error.stack.split('\n').slice(1);
591 } 589 }
592 return stack 590 return stack
593 } 591 }
592
593 const RE_IN_ASSERT_SELECTION = new RegExp('assert_selection\\.js');
594 for (const line of getStack()) { 594 for (const line of getStack()) {
595 const match = RE_TEST_FUNCTION.exec(line); 595 const match = RE_IN_ASSERT_SELECTION.exec(line);
596 if (!match) 596 if (!match) {
597 continue; 597 const RE_LAYOUTTESTS = new RegExp('LayoutTests.*');
598 return `${match[1]}(${match[2]})`; 598 return RE_LAYOUTTESTS.exec(line);
599 }
599 } 600 }
600 return ''; 601 return '';
601 } 602 }
602 603
603 /** 604 /**
604 * @param {string} expectedText 605 * @param {string} expectedText
605 */ 606 */
606 function checkExpectedText(expectedText) { 607 function checkExpectedText(expectedText) {
607 /** @type {number} */ 608 /** @type {number} */
608 const anchorOffset = expectedText.indexOf('^'); 609 const anchorOffset = expectedText.indexOf('^');
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
645 } else if (typeof(tester) === 'string') { 646 } else if (typeof(tester) === 'string') {
646 const strings = tester.split(' '); 647 const strings = tester.split(' ');
647 sample.document.execCommand(strings[0], false, strings[1]); 648 sample.document.execCommand(strings[0], false, strings[1]);
648 } else { 649 } else {
649 throw new Error(`Invalid tester: ${tester}`); 650 throw new Error(`Invalid tester: ${tester}`);
650 } 651 }
651 /** @type {string} */ 652 /** @type {string} */
652 const actualText = sample.serialize(); 653 const actualText = sample.serialize();
653 // We keep sample HTML when assertion is false for ease of debugging test 654 // We keep sample HTML when assertion is false for ease of debugging test
654 // case. 655 // case.
655 if (actualText === expectedText) 656 if (actualText === expectedText) {
656 sample.remove(); 657 sample.remove();
657 assert_equals(actualText, expectedText, description); 658 } else {
659 var comparedLength = Math.min(actualText.length, expectedText.length);
yosin_UTC9 2016/07/11 06:48:18 Could you introduce mismatch(str1, str2) and misma
yoichio 2016/07/11 09:04:14 Done.
660 var i = 0;
661 for (; i < comparedLength; ++i) {
662 if (actualText[i] !== expectedText[i])
663 break;
664 }
665
666 throw new Error(`${description}\n` +
667 `\t expected ${expectedText},\n` +
668 `\t but got ${actualText},\n` +
669 `\t sameupto ${expectedText.substr(0,i)}`);
670 }
658 } 671 }
659 672
660 // Export symbols 673 // Export symbols
661 window.Sample = Sample; 674 window.Sample = Sample;
662 window.assert_selection = assertSelection; 675 window.assert_selection = assertSelection;
663 })(); 676 })();
OLDNEW
« no previous file with comments | « third_party/WebKit/LayoutTests/editing/assert_selection.html ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698