Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions Schema.Test/MemberMetadataTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,36 @@
AssertMentions(Errors(schema), "outside the 32-bit range");
}

/// <summary>
/// 9223372036854775807 is long.MaxValue as written, but as a double it rounds to 2^63, which
/// no Long can hold.
/// </summary>
[TestMethod]
[DataRow(1e19)]
[DataRow(-1e19)]
[DataRow(9223372036854775807.0)]
public void ADefaultOutsideInt64RangeOnALongMemberIsAnError(double value)
{
SchemaMember member = MemberWith(new SchemaTypes.Long(), out Schema schema);
member.DefaultValue = new NumberDefault { Value = value };

AssertMentions(Errors(schema), "outside the 64-bit range");
}

/// <summary>
/// The largest double below 2^63, and the smallest Long, are both representable.
/// </summary>
[TestMethod]
[DataRow(9223372036854774784.0)]
[DataRow(-9223372036854775808.0)]
public void ADefaultInsideInt64RangeOnALongMemberIsAccepted(double value)
{
SchemaMember member = MemberWith(new SchemaTypes.Long(), out Schema schema);
member.DefaultValue = new NumberDefault { Value = value };

Assert.AreEqual(0, Errors(schema).Count);

Check warning on line 278 in Schema.Test/MemberMetadataTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use 'Assert.IsEmpty' instead of 'Assert.AreEqual'

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_Schema&issues=AaDr4t6SHmxAf3IaHX6k&open=AaDr4t6SHmxAf3IaHX6k&pullRequest=225
}

[TestMethod]
public void ADefaultOfTheWrongKindIsAnError()
{
Expand Down
11 changes: 11 additions & 0 deletions Schema/Models/Schema.Validation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@
/// <summary>
/// A default has to be of the member's own kind, and inside its range.
/// </summary>
private static void ValidateMemberDefault(Collection<SchemaValidationIssue> issues, ISchemaMetadataCarrier carrier, BaseType type, string path, ISchemaElement element, string kind)

Check warning on line 343 in Schema/Models/Schema.Validation.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Refactor this method to reduce its Cognitive Complexity from 19 to the 15 allowed.

Check warning on line 343 in Schema/Models/Schema.Validation.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Refactor this method to reduce its Cognitive Complexity from 19 to the 15 allowed.

Check warning on line 343 in Schema/Models/Schema.Validation.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Refactor this method to reduce its Cognitive Complexity from 19 to the 15 allowed.

Check warning on line 343 in Schema/Models/Schema.Validation.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Refactor this method to reduce its Cognitive Complexity from 19 to the 15 allowed.

Check warning on line 343 in Schema/Models/Schema.Validation.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Refactor this method to reduce its Cognitive Complexity from 19 to the 15 allowed.

Check warning on line 343 in Schema/Models/Schema.Validation.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Refactor this method to reduce its Cognitive Complexity from 19 to the 15 allowed.

Check warning on line 343 in Schema/Models/Schema.Validation.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Refactor this method to reduce its Cognitive Complexity from 19 to the 15 allowed.

Check warning on line 343 in Schema/Models/Schema.Validation.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Refactor this method to reduce its Cognitive Complexity from 19 to the 15 allowed.

Check warning on line 343 in Schema/Models/Schema.Validation.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Refactor this method to reduce its Cognitive Complexity from 19 to the 15 allowed.

Check warning on line 343 in Schema/Models/Schema.Validation.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Refactor this method to reduce its Cognitive Complexity from 19 to the 15 allowed.
{
switch (carrier.DefaultValue)
{
Expand Down Expand Up @@ -369,6 +369,11 @@
Report(issues, path, element, $"The default {Number(number.Value)} is outside the 32-bit range of Int ({int.MinValue}..{int.MaxValue}).");
}

if (type is Long && isWholeNumber && !FitsInt64(number.Value))
{
Report(issues, path, element, $"The default {Number(number.Value)} is outside the 64-bit range of Long ({long.MinValue}..{long.MaxValue}).");
}

// A wrapping range is a period rather than a bound, so a value outside it is
// un-normalised rather than wrong -- the same reading a validator must take of
// live data, applied here to the default.
Expand Down Expand Up @@ -489,6 +494,12 @@

private static bool FitsInt32(double value) => value is >= int.MinValue and <= int.MaxValue;

/// <summary>
/// The upper bound is exclusive because (double)long.MaxValue rounds up to 2^63, which is
/// already one past the range: an inclusive check would pass the one value it most needs to stop.
/// </summary>
private static bool FitsInt64(double value) => value is >= -9223372036854775808.0 and < 9223372036854775808.0;

/// <summary>
/// Formats a number for a message, culture-invariantly: a validation message that says
/// "0,5" on one machine and "0.5" on another is a support problem.
Expand Down
Loading