@@ -7,6 +7,8 @@ const path = require("path");
77const { Readable } = require ( "stream" ) ;
88const { pathToFileURL } = require ( "url" ) ;
99const {
10+ TreeEvents,
11+ agreement,
1012 compress,
1113 cutsAt,
1214 exists,
@@ -28,6 +30,7 @@ const {
2830 run,
2931 sweepExitCode,
3032 sweepMode,
33+ treeDifference,
3134 wantedExpectations,
3235 wantedRespelling
3336} = require ( "../../../tooling/compare-tools-harness" ) ;
@@ -1037,3 +1040,124 @@ describe("profileShares", () => {
10371040 ) . toEqual ( [ [ "terser minify.js" , 0 ] ] ) ;
10381041 } ) ;
10391042} ) ;
1043+
1044+ describe ( "parser agreement" , ( ) => {
1045+ /**
1046+ * @param {string[] } texts what each `<p>` holds
1047+ * @returns {TreeEvents } a body of paragraphs
1048+ */
1049+ const paragraphs = ( texts ) => {
1050+ const events = new TreeEvents ( ) ;
1051+ events . open ( "body" , "<body>" ) ;
1052+ for ( const text of texts ) {
1053+ events . open ( "p" , "<p>" ) ;
1054+ events . text ( text ) ;
1055+ events . close ( "</p>" ) ;
1056+ }
1057+ events . leaf ( "comment" ) ;
1058+ events . close ( "</body>" ) ;
1059+ return events ;
1060+ } ;
1061+
1062+ it ( "numbers each node among its same-named siblings and folds text runs" , ( ) => {
1063+ const events = new TreeEvents ( ) ;
1064+ events . open ( "a" , "<a>" ) ;
1065+ expect ( events . depth ) . toBe ( 1 ) ;
1066+ events . text ( "x" ) ;
1067+ events . text ( "y" ) ;
1068+ events . open ( "b" , "<b>" ) ;
1069+ events . close ( "</b>" ) ;
1070+ events . text ( "z" ) ;
1071+ events . open ( "b" , "<b>" ) ;
1072+ events . close ( "</b>" ) ;
1073+ events . close ( "</a>" ) ;
1074+ events . open ( "a" , "<a>" ) ;
1075+ events . close ( "</a>" ) ;
1076+ expect ( events . depth ) . toBe ( 0 ) ;
1077+ expect ( events . list ) . toEqual ( [
1078+ [ "/a[1]" , "open" , "<a>" ] ,
1079+ [ "/a[1]" , "text" , "xy" ] ,
1080+ [ "/a[1]/b[1]" , "open" , "<b>" ] ,
1081+ [ "/a[1]/b[1]" , "close" , "</b>" ] ,
1082+ [ "/a[1]" , "text" , "z" ] ,
1083+ [ "/a[1]/b[2]" , "open" , "<b>" ] ,
1084+ [ "/a[1]/b[2]" , "close" , "</b>" ] ,
1085+ [ "/a[1]" , "close" , "</a>" ] ,
1086+ [ "/a[2]" , "open" , "<a>" ] ,
1087+ [ "/a[2]" , "close" , "</a>" ]
1088+ ] ) ;
1089+ } ) ;
1090+
1091+ it ( "names the first event two trees differ at, and what each side says there" , ( ) => {
1092+ expect (
1093+ treeDifference ( paragraphs ( [ "a" , "b" ] ) , paragraphs ( [ "a" , "b" ] ) , "ref" )
1094+ ) . toBeUndefined ( ) ;
1095+ expect (
1096+ treeDifference ( paragraphs ( [ "a" , "b" ] ) , paragraphs ( [ "a" , "c" ] ) , "ref" )
1097+ ) . toBe ( '/body[1]/p[2]: ref text "b", tool text "c"' ) ;
1098+ expect (
1099+ treeDifference ( paragraphs ( [ "a" ] ) , paragraphs ( [ "a" , "b" ] ) , "ref" )
1100+ ) . toBe ( "/body[1]: ref comment, tool <p>" ) ;
1101+ const empty = new TreeEvents ( ) ;
1102+ expect ( treeDifference ( empty , paragraphs ( [ ] ) , "ref" ) ) . toBe (
1103+ "/body[1]: ref nothing, tool <body>"
1104+ ) ;
1105+ const loose = new TreeEvents ( ) ;
1106+ loose . leaf ( "comment" ) ;
1107+ expect ( treeDifference ( loose , empty , "ref" ) ) . toBe (
1108+ "/: ref comment, tool nothing"
1109+ ) ;
1110+ expect (
1111+ treeDifference ( paragraphs ( [ "x" . repeat ( 80 ) ] ) , paragraphs ( [ "y" ] ) , "ref" )
1112+ ) . toBe ( `/body[1]/p[1]: ref text "${ "x" . repeat ( 41 ) } …, tool text "y"` ) ;
1113+ /**
1114+ * @param {string } said what the innermost node says
1115+ * @returns {TreeEvents } it under twenty nested sections
1116+ */
1117+ const deep = ( said ) => {
1118+ const events = new TreeEvents ( ) ;
1119+ for ( let i = 0 ; i < 20 ; i ++ ) events . open ( "section" , "<section>" ) ;
1120+ events . leaf ( said ) ;
1121+ return events ;
1122+ } ;
1123+ expect ( treeDifference ( deep ( "a" ) , deep ( "b" ) , "ref" ) ) . toBe (
1124+ `…${ "/section[1]" . repeat ( 20 ) . slice ( - 59 ) } : ref a, tool b`
1125+ ) ;
1126+ } ) ;
1127+
1128+ it ( "answers each parser's cell, reading the reference once" , async ( ) => {
1129+ const reference = jest . fn ( ( /** @type {string } */ source ) =>
1130+ paragraphs ( source . split ( " " ) )
1131+ ) ;
1132+ /** @typedef {import("../../../tooling/compare-tools-harness").TreeReader } TreeReader */
1133+ const readers = new Map (
1134+ /** @type {[string, TreeReader][] } */ ( [
1135+ [ "ref" , reference ] ,
1136+ [ "same" , ( source ) => Promise . resolve ( paragraphs ( source . split ( " " ) ) ) ] ,
1137+ [ "other" , ( ) => paragraphs ( [ "no" ] ) ] ,
1138+ [
1139+ "throws" ,
1140+ ( ) => {
1141+ throw new Error ( "bad input\nmore" ) ;
1142+ }
1143+ ]
1144+ ] )
1145+ ) ;
1146+ const cell = agreement ( "ref" , readers , "a b" ) ;
1147+ expect ( await cell ( "ref" ) ) . toBe ( "reference" ) ;
1148+ expect ( await cell ( "unread" ) ) . toBe ( "own dialect" ) ;
1149+ expect ( await cell ( "same" ) ) . toBe ( "yes" ) ;
1150+ expect ( await cell ( "other" ) ) . toBe (
1151+ 'no — /body[1]/p[1]: ref text "a", tool text "no"'
1152+ ) ;
1153+ expect ( await cell ( "throws" ) ) . toBe ( "could not compare: bad input" ) ;
1154+ expect ( reference ) . toHaveBeenCalledTimes ( 1 ) ;
1155+
1156+ readers . set ( "ref" , ( ) => {
1157+ throw new Error ( "not well-formed" ) ;
1158+ } ) ;
1159+ expect ( await agreement ( "ref" , readers , "a" ) ( "same" ) ) . toBe (
1160+ "could not compare: ref rejects it: not well-formed"
1161+ ) ;
1162+ } ) ;
1163+ } ) ;
0 commit comments