|
| 1 | +import { Comparable } from '../comparable'; |
1 | 2 | import { DeepMap } from '../map';
|
| 3 | +import { Options } from '../options'; |
2 | 4 | import { TestObjectField, TestObject } from './common/test.utils';
|
3 | 5 |
|
4 | 6 | describe('DeepMap', () => {
|
@@ -358,5 +360,67 @@ describe('DeepMap', () => {
|
358 | 360 | expect([...differenceMap.entries()]).toStrictEqual([[new K('1'), new V('1')]]);
|
359 | 361 | });
|
360 | 362 | });
|
| 363 | + |
| 364 | + describe('Options checksum validation', () => { |
| 365 | + type TestOptions = Options<number, number, number, number>; |
| 366 | + type ComparableOperation = keyof Comparable<number>; |
| 367 | + |
| 368 | + const operationTypes: ComparableOperation[] = ['equals', 'contains', 'union', 'intersection', 'difference']; |
| 369 | + const errorMsg = 'Structures must use same options for Comparable interface operations'; |
| 370 | + |
| 371 | + const optionsA1: TestOptions = {}; |
| 372 | + const optionsA2: TestOptions = { useToJsonTransform: true }; |
| 373 | + const optionsA3: TestOptions = { transformer: (n) => n + 1 }; |
| 374 | + const optionsA4: TestOptions = { transformer: (k) => k + 1 }; |
| 375 | + const optionsA5: TestOptions = { transformer: (k) => k + 1, unorderedSets: true }; |
| 376 | + const optionsA6: TestOptions = { unorderedSets: true }; |
| 377 | + const optionsA7: TestOptions = undefined as unknown as TestOptions; |
| 378 | + |
| 379 | + const optionsB1: TestOptions = {}; |
| 380 | + const optionsB2: TestOptions = { useToJsonTransform: true }; |
| 381 | + const optionsB3: TestOptions = { transformer: (n) => n + 1 }; |
| 382 | + const optionsB4: TestOptions = { transformer: (k) => k + 1 }; |
| 383 | + const optionsB5: TestOptions = { transformer: (k) => k + 1, unorderedSets: true }; |
| 384 | + const optionsB6: TestOptions = { unorderedSets: true }; |
| 385 | + const optionsB7: TestOptions = undefined as unknown as TestOptions; |
| 386 | + |
| 387 | + function getMapWithOptions(options: TestOptions): DeepMap<number, number> { |
| 388 | + return new DeepMap([[1, 1]], options); |
| 389 | + } |
| 390 | + |
| 391 | + function expectOptionsError(opts1: TestOptions, opts2: TestOptions, operation: ComparableOperation): void { |
| 392 | + expect(() => getMapWithOptions(opts1)[operation](getMapWithOptions(opts2))).toThrow(errorMsg); |
| 393 | + } |
| 394 | + |
| 395 | + function expectNoOptionsError( |
| 396 | + opts1: TestOptions, |
| 397 | + opts2: TestOptions, |
| 398 | + operation: ComparableOperation |
| 399 | + ): void { |
| 400 | + expect(() => getMapWithOptions(opts1)[operation](getMapWithOptions(opts2))).not.toThrow(); |
| 401 | + } |
| 402 | + |
| 403 | + it.each(operationTypes)('error when attempting %s operation with different options', async (operation) => { |
| 404 | + expectOptionsError(optionsA1, optionsA2, operation); |
| 405 | + expectOptionsError(optionsA2, optionsA3, operation); |
| 406 | + expectOptionsError(optionsA3, optionsA4, operation); |
| 407 | + expectOptionsError(optionsA4, optionsA5, operation); |
| 408 | + expectOptionsError(optionsA5, optionsA6, operation); |
| 409 | + expectOptionsError(optionsA6, optionsA7, operation); |
| 410 | + }); |
| 411 | + |
| 412 | + it.each(operationTypes)( |
| 413 | + 'no error when attempting %s operation with identical options', |
| 414 | + async (operation) => { |
| 415 | + expectNoOptionsError(optionsA1, optionsB1, operation); |
| 416 | + expectNoOptionsError(optionsA2, optionsB2, operation); |
| 417 | + expectNoOptionsError(optionsA3, optionsB3, operation); |
| 418 | + expectNoOptionsError(optionsA4, optionsB4, operation); |
| 419 | + expectNoOptionsError(optionsA5, optionsB5, operation); |
| 420 | + expectNoOptionsError(optionsA6, optionsB6, operation); |
| 421 | + expectNoOptionsError(optionsA7, optionsB7, operation); |
| 422 | + } |
| 423 | + ); |
| 424 | + }); |
361 | 425 | });
|
362 | 426 | });
|
0 commit comments