Skip to content

Commit 637e140

Browse files
committed
Handle range merging if left or right is union
1 parent 1b9e9f1 commit 637e140

File tree

2 files changed

+131
-0
lines changed

2 files changed

+131
-0
lines changed

lib/preprocess/GenericsContext.ts

+25
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,31 @@ export class GenericsContext {
250250

251251
return rangeA;
252252
}
253+
254+
// Handle left or right being a union
255+
if (rangeA.isA('ParameterRangeUnion')) {
256+
return this.mergeUnion(rangeA, rangeB);
257+
}
258+
if (rangeB.isA('ParameterRangeUnion')) {
259+
return this.mergeUnion(rangeB, rangeA);
260+
}
261+
}
262+
263+
protected mergeUnion(rangeUnion: Resource, rangeOther: Resource): Resource | undefined {
264+
const elements = rangeUnion.properties.parameterRangeElements;
265+
const mergedValues = elements
266+
.map(element => this.mergeRanges(rangeOther, element))
267+
.filter(Boolean);
268+
if (mergedValues.length === 0) {
269+
return;
270+
}
271+
if (mergedValues.length === 1) {
272+
return mergedValues[0];
273+
}
274+
return this.objectLoader.createCompactedResource({
275+
'@type': 'ParameterRangeUnion',
276+
parameterRangeElements: mergedValues,
277+
});
253278
}
254279

255280
/**

test/unit/preprocess/GenericsContexts-test.ts

+106
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,112 @@ describe('GenericsContext', () => {
751751
),
752752
).toBeUndefined();
753753
});
754+
755+
it('should return union of matches if left is a union type and right is not', () => {
756+
expectOutputProperties(
757+
genericsContext.mergeRanges(
758+
objectLoader.createCompactedResource({
759+
'@type': 'ParameterRangeUnion',
760+
parameterRangeElements: [
761+
'xsd:integer',
762+
'xsd:boolean',
763+
'xsd:integer',
764+
],
765+
}),
766+
objectLoader.createCompactedResource('xsd:integer'),
767+
),
768+
objectLoader.createCompactedResource({
769+
'@type': 'ParameterRangeUnion',
770+
parameterRangeElements: [
771+
'xsd:integer',
772+
'xsd:integer',
773+
],
774+
}),
775+
);
776+
});
777+
778+
it('should return union of matches for one match if left is a union type and right is not', () => {
779+
expect(
780+
genericsContext.mergeRanges(
781+
objectLoader.createCompactedResource({
782+
'@type': 'ParameterRangeUnion',
783+
parameterRangeElements: [
784+
'xsd:integer',
785+
'xsd:boolean',
786+
'xsd:boolean',
787+
],
788+
}),
789+
objectLoader.createCompactedResource('xsd:integer'),
790+
)!.term,
791+
).toEqualRdfTerm(objectLoader.createCompactedResource('xsd:integer')!.term);
792+
});
793+
794+
it('should not merge if union of matches of left does not match right', () => {
795+
expect(genericsContext.mergeRanges(
796+
objectLoader.createCompactedResource({
797+
'@type': 'ParameterRangeUnion',
798+
parameterRangeElements: [
799+
'xsd:boolean',
800+
'xsd:boolean',
801+
'xsd:boolean',
802+
],
803+
}),
804+
objectLoader.createCompactedResource('xsd:integer'),
805+
)).toBeUndefined();
806+
});
807+
808+
it('should return union of matches if right is a union type and left is not', () => {
809+
expectOutputProperties(
810+
genericsContext.mergeRanges(
811+
objectLoader.createCompactedResource('xsd:integer'),
812+
objectLoader.createCompactedResource({
813+
'@type': 'ParameterRangeUnion',
814+
parameterRangeElements: [
815+
'xsd:integer',
816+
'xsd:boolean',
817+
'xsd:integer',
818+
],
819+
}),
820+
),
821+
objectLoader.createCompactedResource({
822+
'@type': 'ParameterRangeUnion',
823+
parameterRangeElements: [
824+
'xsd:integer',
825+
'xsd:integer',
826+
],
827+
}),
828+
);
829+
});
830+
831+
it('should return union of matches for one match if right is a union type and left is not', () => {
832+
expect(
833+
genericsContext.mergeRanges(
834+
objectLoader.createCompactedResource('xsd:integer'),
835+
objectLoader.createCompactedResource({
836+
'@type': 'ParameterRangeUnion',
837+
parameterRangeElements: [
838+
'xsd:integer',
839+
'xsd:boolean',
840+
'xsd:boolean',
841+
],
842+
}),
843+
)!.term,
844+
).toEqualRdfTerm(objectLoader.createCompactedResource('xsd:integer')!.term);
845+
});
846+
847+
it('should not merge if union of matches of right does not match left', () => {
848+
expect(genericsContext.mergeRanges(
849+
objectLoader.createCompactedResource('xsd:integer'),
850+
objectLoader.createCompactedResource({
851+
'@type': 'ParameterRangeUnion',
852+
parameterRangeElements: [
853+
'xsd:boolean',
854+
'xsd:boolean',
855+
'xsd:boolean',
856+
],
857+
}),
858+
)).toBeUndefined();
859+
});
754860
});
755861

756862
describe('isXsdSubType', () => {

0 commit comments

Comments
 (0)