11import type { Host , Port } from 'polykey/dist/network/types' ;
2+ import path from 'path' ;
23import ErrorPolykey from 'polykey/dist/ErrorPolykey' ;
34import { test } from '@fast-check/jest' ;
45import * as ids from 'polykey/dist/ids' ;
@@ -7,7 +8,6 @@ import * as polykeyErrors from 'polykey/dist/errors';
78import * as fc from 'fast-check' ;
89import * as binUtils from '@/utils/utils' ;
910import * as binParsers from '@/utils/parsers' ;
10- import path from 'path' ;
1111
1212describe ( 'outputFormatters' , ( ) => {
1313 const nonPrintableCharArb = fc
@@ -167,33 +167,26 @@ describe('outputFormatters', () => {
167167 ' key9\tvalue\n' ,
168168 ) ;
169169 } ) ;
170- test ( 'should encode non-printable characters within a dict' , ( ) => {
171- fc . assert (
172- fc . property (
173- stringWithNonPrintableCharsArb ,
174- stringWithNonPrintableCharsArb ,
175- ( key , value ) => {
176- const formattedOutput = binUtils . outputFormatter ( {
177- type : 'dict' ,
178- data : { [ key ] : value } ,
179- } ) ;
180- const expectedKey = binUtils . encodeEscapedWrapped ( key ) ;
181- // Construct the expected output
182- let expectedValue = value ;
183- expectedValue = binUtils . encodeEscapedWrapped ( expectedValue ) ;
184- expectedValue = expectedValue . replace ( / (?: \r \n | \n ) $ / , '' ) ;
185- expectedValue = expectedValue . replace ( / ( \r \n | \n ) / g, '$1\t' ) ;
186- const maxKeyLength = Math . max (
187- ...Object . keys ( { [ key ] : value } ) . map ( ( k ) => k . length ) ,
188- ) ;
189- const padding = ' ' . repeat ( maxKeyLength - key . length ) ;
190- const expectedOutput = `${ expectedKey } ${ padding } \t${ expectedValue } \n` ;
191- // Assert that the formatted output matches the expected output
192- expect ( formattedOutput ) . toBe ( expectedOutput ) ;
193- } ,
194- ) ,
195- { numRuns : 100 } , // Number of times to run the test
170+ test . prop ( [ stringWithNonPrintableCharsArb , stringWithNonPrintableCharsArb ] , {
171+ numRuns : 100 ,
172+ } ) ( 'should encode non-printable characters within a dict' , ( key , value ) => {
173+ const formattedOutput = binUtils . outputFormatter ( {
174+ type : 'dict' ,
175+ data : { [ key ] : value } ,
176+ } ) ;
177+ const expectedKey = binUtils . encodeEscapedWrapped ( key ) ;
178+ // Construct the expected output
179+ let expectedValue = value ;
180+ expectedValue = binUtils . encodeEscapedWrapped ( expectedValue ) ;
181+ expectedValue = expectedValue . replace ( / (?: \r \n | \n ) $ / , '' ) ;
182+ expectedValue = expectedValue . replace ( / ( \r \n | \n ) / g, '$1\t' ) ;
183+ const maxKeyLength = Math . max (
184+ ...Object . keys ( { [ key ] : value } ) . map ( ( k ) => k . length ) ,
196185 ) ;
186+ const padding = ' ' . repeat ( maxKeyLength - key . length ) ;
187+ const expectedOutput = `${ expectedKey } ${ padding } \t${ expectedValue } \n` ;
188+ // Assert that the formatted output matches the expected output
189+ expect ( formattedOutput ) . toBe ( expectedOutput ) ;
197190 } ) ;
198191 test ( 'errors in human and json format' , ( ) => {
199192 const nodeIdGenerator = ids . createNodeIdGenerator ( ) ;
@@ -302,45 +295,37 @@ describe('outputFormatters', () => {
302295 '\n' ,
303296 ) ;
304297 } ) ;
305- test ( 'encodeEscaped should encode all escapable characters' , ( ) => {
306- fc . assert (
307- fc . property ( stringWithNonPrintableCharsArb , ( value ) => {
308- expect ( binUtils . decodeEscaped ( binUtils . encodeEscaped ( value ) ) ) . toBe (
309- value ,
310- ) ;
311- } ) ,
312- { numRuns : 100 } , // Number of times to run the test
313- ) ;
314- } ) ;
315- test ( 'encodeEscapedReplacer should encode all escapable characters' , ( ) => {
316- fc . assert (
317- fc . property (
318- stringWithNonPrintableCharsArb ,
319- stringWithNonPrintableCharsArb ,
320- ( key , value ) => {
321- const encodedKey = binUtils . encodeEscaped ( key ) ;
322- const encodedValue = binUtils . encodeEscaped ( value ) ;
323- const object = {
324- [ key ] : value ,
325- [ key ] : {
326- [ key ] : value ,
327- } ,
328- [ key ] : [ value ] ,
329- } ;
330- const encodedObject = {
331- [ encodedKey ] : encodedValue ,
332- [ encodedKey ] : {
333- [ encodedKey ] : encodedValue ,
334- } ,
335- [ encodedKey ] : [ encodedValue ] ,
336- } ;
337- const output = JSON . stringify ( object , binUtils . encodeEscapedReplacer ) ;
338- expect ( JSON . parse ( output ) ) . toEqual ( encodedObject ) ;
298+ test . prop ( [ stringWithNonPrintableCharsArb ] , { numRuns : 100 } ) (
299+ 'encodeEscaped should encode all escapable characters' ,
300+ ( value ) => {
301+ expect ( binUtils . decodeEscaped ( binUtils . encodeEscaped ( value ) ) ) . toBe ( value ) ;
302+ } ,
303+ ) ;
304+ test . prop ( [ stringWithNonPrintableCharsArb , stringWithNonPrintableCharsArb ] , {
305+ numRuns : 100 ,
306+ } ) (
307+ 'encodeEscapedReplacer should encode all escapable characters' ,
308+ ( key , value ) => {
309+ const encodedKey = binUtils . encodeEscaped ( key ) ;
310+ const encodedValue = binUtils . encodeEscaped ( value ) ;
311+ const object = {
312+ [ key ] : value ,
313+ [ key ] : {
314+ [ key ] : value ,
339315 } ,
340- ) ,
341- { numRuns : 100 } , // Number of times to run the test
342- ) ;
343- } ) ;
316+ [ key ] : [ value ] ,
317+ } ;
318+ const encodedObject = {
319+ [ encodedKey ] : encodedValue ,
320+ [ encodedKey ] : {
321+ [ encodedKey ] : encodedValue ,
322+ } ,
323+ [ encodedKey ] : [ encodedValue ] ,
324+ } ;
325+ const output = JSON . stringify ( object , binUtils . encodeEscapedReplacer ) ;
326+ expect ( JSON . parse ( output ) ) . toEqual ( encodedObject ) ;
327+ } ,
328+ ) ;
344329} ) ;
345330
346331describe ( 'parsers' , ( ) => {
0 commit comments