-
Notifications
You must be signed in to change notification settings - Fork 555
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add validation to CellType for number types
- Loading branch information
1 parent
d3175aa
commit 5be169a
Showing
11 changed files
with
313 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using DocumentFormat.OpenXml.Framework; | ||
using DocumentFormat.OpenXml.Validation; | ||
|
||
namespace DocumentFormat.OpenXml.Spreadsheet | ||
{ | ||
public partial class CellType : IValidator | ||
{ | ||
void IValidator.Validate(ValidationContext context) | ||
{ | ||
if (DataType is null || !DataType.HasValue) | ||
{ | ||
return; | ||
} | ||
|
||
if (CellValue is CellValue value) | ||
{ | ||
var success = DataType.Value switch | ||
{ | ||
CellValues.Boolean => value.TryGetBoolean(out _), | ||
CellValues.Date => value.TryGetDateTimeOffset(out _) || value.TryGetDateTime(out _), | ||
CellValues.Number => value.TryGetInt(out _) || value.TryGetDouble(out _) || value.TryGetDecimal(out _), | ||
_ => true, | ||
}; | ||
|
||
if (success) | ||
{ | ||
context.CreateError( | ||
id: "Sem_CellValue", | ||
errorType: ValidationErrorType.Semantic, | ||
description: string.Format(ValidationResources.Sem_CellValue, value.InnerText, DataType.Value) | ||
); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/DocumentFormat.OpenXml/Validation/ValidationResources.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
test/DocumentFormat.OpenXml.Tests/Spreadsheet/CellTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using DocumentFormat.OpenXml.Spreadsheet; | ||
using DocumentFormat.OpenXml.Validation; | ||
using Xunit; | ||
|
||
namespace DocumentFormat.OpenXml.Tests | ||
{ | ||
public class CellTests | ||
{ | ||
[InlineData("StringValue", CellValues.Number, false)] | ||
[InlineData("1", CellValues.Number, true)] | ||
[InlineData("1.0", CellValues.Number, true)] | ||
[InlineData("-1.0", CellValues.Number, true)] | ||
[InlineData("StringValue", CellValues.String, true)] | ||
[Theory] | ||
public void CellValidationTest(string value, CellValues type, bool success) | ||
{ | ||
var cell = new Cell | ||
{ | ||
CellValue = new CellValue(value), | ||
DataType = type, | ||
}; | ||
|
||
var validator = new OpenXmlValidator(); | ||
var results = validator.Validate(cell); | ||
|
||
if (success) | ||
{ | ||
Assert.Empty(results); | ||
} | ||
else | ||
{ | ||
Assert.Single(results); | ||
} | ||
} | ||
} | ||
} |
128 changes: 128 additions & 0 deletions
128
test/DocumentFormat.OpenXml.Tests/Spreadsheet/SpreadsheetCellTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using DocumentFormat.OpenXml.Spreadsheet; | ||
using System; | ||
using System.Collections.Generic; | ||
using Xunit; | ||
|
||
namespace DocumentFormat.OpenXml.Tests | ||
{ | ||
public class SpreadsheetCellTests | ||
{ | ||
[Fact] | ||
public void CellDateTimeTest() | ||
{ | ||
var dt = new DateTime(2017, 11, 28, 12, 25, 2); | ||
var value = new CellValue(dt); | ||
|
||
Assert.Equal("2017-11-28T12:25:02.000", value.Text); | ||
|
||
Assert.True(value.TryGetDateTime(out var result)); | ||
Assert.Equal(dt, result); | ||
} | ||
|
||
[Fact] | ||
public void CellDateTimeOffsetTest() | ||
{ | ||
var dt = new DateTimeOffset(2017, 11, 28, 12, 25, 2, TimeSpan.Zero); | ||
var value = new CellValue(dt); | ||
|
||
Assert.Equal("2017-11-28T12:25:02.000+00:00", value.Text); | ||
|
||
Assert.True(value.TryGetDateTimeOffset(out var result)); | ||
Assert.Equal(dt, result); | ||
} | ||
|
||
[Fact] | ||
public void CellDateTimeWithMillisecondsTest() | ||
{ | ||
var dt = new DateTime(2017, 11, 28, 12, 25, 2).AddMilliseconds(123); | ||
var value = new CellValue(dt); | ||
|
||
Assert.Equal("2017-11-28T12:25:02.123", value.Text); | ||
|
||
Assert.True(value.TryGetDateTime(out var result)); | ||
Assert.Equal(dt, result); | ||
} | ||
|
||
[Fact] | ||
public void CellDateTimeOffsetWithMillisecondsTest() | ||
{ | ||
var dt = new DateTimeOffset(2017, 11, 28, 12, 25, 2, TimeSpan.Zero).AddMilliseconds(123); | ||
var value = new CellValue(dt); | ||
|
||
Assert.Equal("2017-11-28T12:25:02.123+00:00", value.Text); | ||
Assert.True(value.TryGetDateTimeOffset(out var result)); | ||
Assert.Equal(dt, result); | ||
} | ||
|
||
[InlineData(-1.5)] | ||
[InlineData(-1.0)] | ||
[InlineData(0.0)] | ||
[InlineData(1.0)] | ||
[InlineData(1.5)] | ||
[Theory] | ||
public void CellDoubleTest(double num) | ||
{ | ||
var value = new CellValue(num); | ||
|
||
Assert.Equal(num.ToString(), value.Text); | ||
Assert.Equal(num.ToString(), value.InnerText); | ||
Assert.Equal(@$"<x:v xmlns:x=""http://schemas.openxmlformats.org/spreadsheetml/2006/main"">{num}</x:v>", value.OuterXml); | ||
Assert.True(value.TryGetDouble(out var result)); | ||
Assert.Equal(num, result); | ||
} | ||
|
||
[InlineData(int.MinValue)] | ||
[InlineData(int.MinValue + 1)] | ||
[InlineData(-1)] | ||
[InlineData(0)] | ||
[InlineData(1)] | ||
[InlineData(int.MaxValue - 1)] | ||
[InlineData(int.MaxValue)] | ||
[Theory] | ||
public void CellIntTest(int num) | ||
{ | ||
var value = new CellValue(num); | ||
|
||
Assert.Equal(num.ToString(), value.Text); | ||
Assert.Equal(num.ToString(), value.InnerText); | ||
Assert.Equal(@$"<x:v xmlns:x=""http://schemas.openxmlformats.org/spreadsheetml/2006/main"">{num}</x:v>", value.OuterXml); | ||
Assert.True(value.TryGetInt(out var result)); | ||
Assert.Equal(num, result); | ||
} | ||
|
||
[MemberData(nameof(DecimalTests))] | ||
[Theory] | ||
public void CellDecimalTest(decimal num) | ||
{ | ||
var value = new CellValue(num); | ||
|
||
Assert.Equal(num.ToString(), value.Text); | ||
Assert.Equal(num.ToString(), value.InnerText); | ||
Assert.Equal(@$"<x:v xmlns:x=""http://schemas.openxmlformats.org/spreadsheetml/2006/main"">{num}</x:v>", value.OuterXml); | ||
Assert.True(value.TryGetDecimal(out var result)); | ||
Assert.Equal(num, result); | ||
} | ||
|
||
private static readonly decimal[] _decimalValues = new decimal[] | ||
{ | ||
decimal.MinValue, | ||
decimal.MinValue + 1, | ||
-1M, | ||
0M, | ||
1M, | ||
decimal.MaxValue - 1, | ||
decimal.MaxValue, | ||
}; | ||
|
||
public static IEnumerable<object[]> DecimalTests() | ||
{ | ||
foreach (var v in _decimalValues) | ||
{ | ||
yield return new object[] { v }; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.