Skip to content

Commit

Permalink
tests: Verify range-constraint and lenght-constraint work with only o…
Browse files Browse the repository at this point in the history
…ne bound (#548)

* test(constraints): add tests

* fix(constraints): add language server hint for boundless constraint

* test(constraints): add tests for language server hint

closes #533
  • Loading branch information
TungstnBallon authored May 3, 2024
1 parent 2326441 commit 5e89f86
Show file tree
Hide file tree
Showing 10 changed files with 210 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,24 @@ describe('Validation of LengthConstraintExecutor', () => {

expect(valid).toBe(false);
});

it('should work with only a lower bound specified', async () => {
const text = readJvTestAsset(
'length-constraint-executor/only-lower-bound.jv',
);

const valid = await parseAndValidateConstraint(text, 'morethan2chars');

expect(valid).toBe(true);
});

it('should work with only an uppper bound specified', async () => {
const text = readJvTestAsset(
'length-constraint-executor/only-upper-bound.jv',
);

const valid = await parseAndValidateConstraint(text, '');

expect(valid).toBe(true);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,24 @@ describe('Validation of RangeConstraintExecutor', () => {

expect(valid).toBe(false);
});

it('should work with only an upper bound specified', async () => {
const text = readJvTestAsset(
'range-constraint-executor/only-upper-bound.jv',
);

const valid = await parseAndValidateConstraint(text, 0);

expect(valid).toBe(true);
});

it('should work with only a lower bound specified', async () => {
const text = readJvTestAsset(
'range-constraint-executor/only-lower-bound.jv',
);

const valid = await parseAndValidateConstraint(text, 999999999);

expect(valid).toBe(true);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// SPDX-FileCopyrightText: 2024 Friedrich-Alexander-Universitat Erlangen-Nurnberg
//
// SPDX-License-Identifier: AGPL-3.0-only

constraint TestConstraint oftype LengthConstraint {
minLength: 2;
}

valuetype TestValueType oftype text {
constraints: [
TestConstraint,
];
}

pipeline TestPipeline {

block TestExtractor oftype TestFileExtractor {
}

block TestLoader oftype TestTableLoader {
}

block TestProperty oftype TestProperty {
valuetypeAssignmentProperty: "test" oftype TestValueType;
}

TestExtractor -> TestProperty -> TestLoader;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// SPDX-FileCopyrightText: 2024 Friedrich-Alexander-Universitat Erlangen-Nurnberg
//
// SPDX-License-Identifier: AGPL-3.0-only

constraint TestConstraint oftype LengthConstraint {
maxLength: 3;
}

valuetype TestValueType oftype text {
constraints: [
TestConstraint,
];
}

pipeline TestPipeline {

block TestExtractor oftype TestFileExtractor {
}

block TestLoader oftype TestTableLoader {
}

block TestProperty oftype TestProperty {
valuetypeAssignmentProperty: "test" oftype TestValueType;
}

TestExtractor -> TestProperty -> TestLoader;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// SPDX-FileCopyrightText: 2024 Friedrich-Alexander-Universitat Erlangen-Nurnberg
//
// SPDX-License-Identifier: AGPL-3.0-only

constraint TestConstraint oftype RangeConstraint {
lowerBound: 1;
}

valuetype TestValueType oftype integer {
constraints: [
TestConstraint,
];
}

pipeline TestPipeline {

block TestExtractor oftype TestFileExtractor {
}

block TestLoader oftype TestTableLoader {
}

block TestProperty oftype TestProperty {
valuetypeAssignmentProperty: "test" oftype TestValueType;
}

TestExtractor -> TestProperty -> TestLoader;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// SPDX-FileCopyrightText: 2024 Friedrich-Alexander-Universitat Erlangen-Nurnberg
//
// SPDX-License-Identifier: AGPL-3.0-only

constraint TestConstraint oftype RangeConstraint {
upperBound: 10;
}

valuetype TestValueType oftype integer {
constraints: [
TestConstraint,
];
}

pipeline TestPipeline {

block TestExtractor oftype TestFileExtractor {
}

block TestLoader oftype TestTableLoader {
}

block TestProperty oftype TestProperty {
valuetypeAssignmentProperty: "test" oftype TestValueType;
}

TestExtractor -> TestProperty -> TestLoader;
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,21 @@ describe('Validation of constraint type specific property bodies', () => {
expect.any(Object),
);
});

it('should hint if no bounds were specified', async () => {
const text = readJvTestAsset(
'property-body/constrainttype-specific/length-constraint/no-bound.jv',
);

await parseAndValidatePropertyAssignment(text);

expect(validationAcceptorMock).toHaveBeenCalledTimes(1);
expect(validationAcceptorMock).toHaveBeenCalledWith(
'hint',
'This constraint should either specify an upper or lower bound, otherwise it has no effect.',
expect.any(Object),
);
});
});

describe('RangeConstraint constraint type', () => {
Expand Down Expand Up @@ -122,5 +137,20 @@ describe('Validation of constraint type specific property bodies', () => {
expect.any(Object),
);
});

it('should hint if no bounds were specified', async () => {
const text = readJvTestAsset(
'property-body/constrainttype-specific/range-constraint/no-bound.jv',
);

await parseAndValidatePropertyAssignment(text);

expect(validationAcceptorMock).toHaveBeenCalledTimes(1);
expect(validationAcceptorMock).toHaveBeenCalledWith(
'hint',
'This constraint should either specify an upper or lower bound, otherwise it has no effect.',
expect.any(Object),
);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ function checkLengthConstraintPropertyBody(
(p) => p.name === 'maxLength',
);

if (minLengthProperty === undefined && maxLengthProperty === undefined) {
props.validationContext.accept(
'hint',
'This constraint should either specify an upper or lower bound, otherwise it has no effect.',
{ node: propertyBody },
);
}

if (minLengthProperty === undefined || maxLengthProperty === undefined) {
return;
}
Expand Down Expand Up @@ -71,6 +79,14 @@ function checkRangeConstraintPropertyBody(
(p) => p.name === 'upperBound',
);

if (lowerBoundProperty === undefined && upperBoundProperty === undefined) {
props.validationContext.accept(
'hint',
'This constraint should either specify an upper or lower bound, otherwise it has no effect.',
{ node: propertyBody },
);
}

if (lowerBoundProperty === undefined || upperBoundProperty === undefined) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// SPDX-FileCopyrightText: 2023 Friedrich-Alexander-Universitat Erlangen-Nurnberg
//
// SPDX-License-Identifier: AGPL-3.0-only

constraint Test oftype LengthConstraint {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// SPDX-FileCopyrightText: 2023 Friedrich-Alexander-Universitat Erlangen-Nurnberg
//
// SPDX-License-Identifier: AGPL-3.0-only

constraint Test oftype RangeConstraint {
}

0 comments on commit 5e89f86

Please sign in to comment.