Skip to content

Commit b6b74da

Browse files
[Add] unit tests
1 parent 6b1eb17 commit b6b74da

16 files changed

Lines changed: 541 additions & 69 deletions

ReqIFSharp.Tests/AttributeDefinitionTests/AttributeDefinitionEnumerationTestFixture.cs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@
1818
// </copyright>
1919
// ------------------------------------------------------------------------------------------------
2020

21+
using Microsoft.Extensions.Logging.Abstractions;
22+
2123
namespace ReqIFSharp.Tests
2224
{
25+
using NUnit.Framework;
26+
using ReqIFSharp;
2327
using System;
2428
using System.IO;
2529
using System.Runtime.Serialization;
2630
using System.Threading;
2731
using System.Xml;
2832

29-
using NUnit.Framework;
30-
31-
using ReqIFSharp;
32-
3333
/// <summary>
3434
/// Suite of tests for the <see cref="AttributeDefinitionEnumeration"/>
3535
/// </summary>
@@ -86,5 +86,22 @@ public void Verify_That_WriteXmlAsync_Throws_Exception_When_Type_I_sNull()
8686
Assert.That(async () => await attributeDefinitionEnumeration.WriteXmlAsync(writer, cancellationTokenSource.Token),
8787
Throws.Exception.TypeOf<SerializationException>());
8888
}
89+
90+
[Test]
91+
public void Verify_that_when_invalid_IsMultiValued_Exception_is_raised()
92+
{
93+
var xml = """
94+
<ATTRIBUTE-DEFINITION-ENUMERATION IDENTIFIER="AD1" MULTI-VALUED="not-a-bool" />
95+
""";
96+
97+
var xmlReader = XmlReader.Create(new StringReader(xml));
98+
xmlReader.MoveToContent();
99+
100+
var specType = new SpecificationType { ReqIFContent = new ReqIFContent() };
101+
var attributeDefinitionEnumeration = new AttributeDefinitionEnumeration(specType, NullLoggerFactory.Instance);
102+
103+
Assert.That(() => attributeDefinitionEnumeration.ReadXml(xmlReader),
104+
Throws.InstanceOf<SerializationException>());
105+
}
89106
}
90107
}

ReqIFSharp.Tests/AttributeValueTests/AttributeValueBooleanTestFixture.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ namespace ReqIFSharp.Tests
2626
using System.Threading;
2727
using System.Xml;
2828

29+
using Microsoft.Extensions.Logging.Abstractions;
30+
2931
using NUnit.Framework;
3032

3133
using ReqIFSharp;
@@ -159,5 +161,20 @@ public void Verify_that_ReadXmlAsync_throws_exception_when_cancelled()
159161
Assert.That(async () => await attributeValueBoolean.ReadXmlAsync(xmlReader, cts.Token),
160162
Throws.Exception.TypeOf<OperationCanceledException>());
161163
}
164+
165+
[Test]
166+
public void Verify_that_when_invalid_IsMultiValued_Exception_is_raised()
167+
{
168+
var xml = """
169+
<ATTRIBUTE-VALUE-BOOLEAN THE-VALUE="not-a-boolean" />
170+
""";
171+
172+
var xmlReader = XmlReader.Create(new StringReader(xml));
173+
xmlReader.MoveToContent();
174+
175+
var attributeValueBoolean = new AttributeValueBoolean(NullLoggerFactory.Instance);
176+
177+
Assert.That(() => attributeValueBoolean.ReadXml(xmlReader), Throws.InstanceOf<SerializationException>());
178+
}
162179
}
163180
}

ReqIFSharp.Tests/AttributeValueTests/AttributeValueDateTestFixture.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ namespace ReqIFSharp.Tests
2626
using System.Threading;
2727
using System.Xml;
2828

29+
using Microsoft.Extensions.Logging.Abstractions;
30+
2931
using NUnit.Framework;
3032

3133
using ReqIFSharp;
@@ -160,5 +162,41 @@ public void Verify_that_ReadXmlAsync_throws_exception_when_cancelled()
160162
Assert.That(async () => await attributeValueDate.ReadXmlAsync(xmlReader, cts.Token),
161163
Throws.Exception.TypeOf<OperationCanceledException>());
162164
}
165+
166+
[Test]
167+
public void Verify_that_when_invalid_Value_Exception_is_raised()
168+
{
169+
var xml = """
170+
<ATTRIBUTE-VALUE-DATE THE-VALUE="not-a-date" />
171+
""";
172+
173+
var xmlReader = XmlReader.Create(new StringReader(xml));
174+
xmlReader.MoveToContent();
175+
176+
var specType = new SpecificationType { ReqIFContent = new ReqIFContent() };
177+
var attributeDefinition = new AttributeDefinitionDate { SpecType = specType };
178+
179+
var attributeValueDate = new AttributeValueDate(attributeDefinition, NullLoggerFactory.Instance);
180+
181+
Assert.That(() => attributeValueDate.ReadXml(xmlReader), Throws.InstanceOf<SerializationException>());
182+
}
183+
184+
[Test]
185+
public void Verify_that_when_too_large_small_Value_Exception_is_raised()
186+
{
187+
var xml = """
188+
<ATTRIBUTE-VALUE-DATE THE-VALUE="999-11-12" />
189+
""";
190+
191+
var xmlReader = XmlReader.Create(new StringReader(xml));
192+
xmlReader.MoveToContent();
193+
194+
var specType = new SpecificationType { ReqIFContent = new ReqIFContent() };
195+
var attributeDefinition = new AttributeDefinitionDate { SpecType = specType };
196+
197+
var attributeValueDate = new AttributeValueDate(attributeDefinition, NullLoggerFactory.Instance);
198+
199+
Assert.That(() => attributeValueDate.ReadXml(xmlReader), Throws.InstanceOf<SerializationException>());
200+
}
163201
}
164202
}

ReqIFSharp.Tests/AttributeValueTests/AttributeValueIntegerTestFixture.cs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@ namespace ReqIFSharp.Tests
2222
{
2323
using System;
2424
using System.IO;
25-
using System.Threading;
2625
using System.Runtime.Serialization;
26+
using System.Threading;
2727
using System.Xml;
2828

29+
using Microsoft.Extensions.Logging.Abstractions;
30+
2931
using NUnit.Framework;
3032

3133
using ReqIFSharp;
@@ -158,5 +160,43 @@ public void Verify_that_ReadXmlAsync_throws_exception_when_cancelled()
158160
Assert.That(async () => await attributeValueInteger.ReadXmlAsync(xmlReader, cts.Token),
159161
Throws.Exception.TypeOf<OperationCanceledException>());
160162
}
163+
164+
[Test]
165+
public void Verify_that_when_too_large_Value_Exception_is_not_raised()
166+
{
167+
var xml = """
168+
<ATTRIBUTE-VALUE-INTEGER THE-VALUE="9223372036854775808" />
169+
""";
170+
171+
var xmlReader = XmlReader.Create(new StringReader(xml));
172+
xmlReader.MoveToContent();
173+
174+
var specType = new SpecificationType { ReqIFContent = new ReqIFContent() };
175+
var attributeDefinition = new AttributeDefinitionInteger() { SpecType = specType };
176+
177+
var attributeValueInteger = new AttributeValueInteger(attributeDefinition, NullLoggerFactory.Instance);
178+
179+
Assert.That(() => attributeValueInteger.ReadXml(xmlReader), Throws.Nothing);
180+
181+
Assert.That(attributeValueInteger.TheValue, Is.EqualTo(0));
182+
}
183+
184+
[Test]
185+
public void Verify_that_when_too_invalid_Value_Exception_is_not_raised()
186+
{
187+
var xml = """
188+
<ATTRIBUTE-VALUE-INTEGER THE-VALUE="not-an-integer" />
189+
""";
190+
191+
var xmlReader = XmlReader.Create(new StringReader(xml));
192+
xmlReader.MoveToContent();
193+
194+
var specType = new SpecificationType { ReqIFContent = new ReqIFContent() };
195+
var attributeDefinition = new AttributeDefinitionInteger() { SpecType = specType };
196+
197+
var attributeValueInteger = new AttributeValueInteger(attributeDefinition, NullLoggerFactory.Instance);
198+
199+
Assert.That(() => attributeValueInteger.ReadXml(xmlReader), Throws.InstanceOf<SerializationException>());
200+
}
161201
}
162202
}

ReqIFSharp.Tests/AttributeValueTests/AttributeValueRealTestFixture.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ namespace ReqIFSharp.Tests
2727
using System.Xml;
2828

2929
using Microsoft.Extensions.Logging;
30+
using Microsoft.Extensions.Logging.Abstractions;
3031

3132
using NUnit.Framework;
3233

@@ -181,5 +182,43 @@ public void Verify_that_ReadXmlAsync_throws_exception_when_cancelled()
181182
Assert.That(async () => await attributeValueReal.ReadXmlAsync(xmlReader, cts.Token),
182183
Throws.Exception.TypeOf<OperationCanceledException>());
183184
}
185+
186+
[Test]
187+
public void Verify_that_when_too_large_Value_Exception_is_not_raised()
188+
{
189+
var xml = """
190+
<ATTRIBUTE-VALUE-REAL THE-VALUE="1E+309" />
191+
""";
192+
193+
var xmlReader = XmlReader.Create(new StringReader(xml));
194+
xmlReader.MoveToContent();
195+
196+
var specType = new SpecificationType { ReqIFContent = new ReqIFContent() };
197+
var attributeDefinition = new AttributeDefinitionReal() { SpecType = specType };
198+
199+
var attributeValueReal = new AttributeValueReal(attributeDefinition, NullLoggerFactory.Instance);
200+
201+
Assert.That(() => attributeValueReal.ReadXml(xmlReader), Throws.Nothing);
202+
203+
Assert.That(attributeValueReal.TheValue, Is.EqualTo(double.PositiveInfinity));
204+
}
205+
206+
[Test]
207+
public void Verify_that_when_too_invalid_Value_Exception_is_not_raised()
208+
{
209+
var xml = """
210+
<ATTRIBUTE-VALUE-REAL THE-VALUE="not-a-real" />
211+
""";
212+
213+
var xmlReader = XmlReader.Create(new StringReader(xml));
214+
xmlReader.MoveToContent();
215+
216+
var specType = new SpecificationType { ReqIFContent = new ReqIFContent() };
217+
var attributeDefinition = new AttributeDefinitionReal() { SpecType = specType };
218+
219+
var attributeValueReal = new AttributeValueReal(attributeDefinition, NullLoggerFactory.Instance);
220+
221+
Assert.That(() => attributeValueReal.ReadXml(xmlReader), Throws.InstanceOf<SerializationException>());
222+
}
184223
}
185224
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
// -------------------------------------------------------------------------------------------------
2+
// <copyright file="DatatypeDefinitionIntegerTestFixture.cs" company="Starion Group S.A.">
3+
//
4+
// Copyright 2017-2025 Starion Group S.A.
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// </copyright>
19+
// -------------------------------------------------------------------------------------------------
20+
21+
namespace ReqIFSharp.Tests
22+
{
23+
using System;
24+
using System.IO;
25+
using System.Runtime.Serialization;
26+
using System.Xml;
27+
28+
using Microsoft.Extensions.Logging.Abstractions;
29+
30+
using NUnit.Framework;
31+
32+
using ReqIFSharp;
33+
34+
[TestFixture]
35+
public class DatatypeDefinitionIntegerTestFixture
36+
{
37+
[Test]
38+
public void Verify_that_when_MAX_is_too_large_exception_is_thrown()
39+
{
40+
var xml = """
41+
<DATATYPE-DEFINITION-INTEGER IDENTIFIER="_IrT00AfhEeelU71CdMk83g" LAST-CHANGE="2017-03-13T12:35:17.083+01:00" LONG-NAME="T_Int" MAX="9223372036854775808" MIN="-100">
42+
<ALTERNATIVE-ID>
43+
<ALTERNATIVE-ID IDENTIFIER="_IrT00AfhEeelU71CdMk83g"/>
44+
</ALTERNATIVE-ID>
45+
</DATATYPE-DEFINITION-INTEGER>
46+
""";
47+
48+
var xmlReader = XmlReader.Create(new StringReader(xml));
49+
xmlReader.MoveToContent();
50+
51+
var datatypeDefinitionInteger = new DatatypeDefinitionInteger(NullLoggerFactory.Instance);
52+
53+
Assert.That(() => datatypeDefinitionInteger.ReadXml(xmlReader), Throws.Nothing);
54+
55+
Assert.That(datatypeDefinitionInteger.Max, Is.EqualTo(Int64.MaxValue));
56+
}
57+
58+
[Test]
59+
public void Verify_that_when_MAX_is_invalid_exception_is_thrown()
60+
{
61+
var xml = """
62+
<DATATYPE-DEFINITION-INTEGER IDENTIFIER="_IrT00AfhEeelU71CdMk83g" LAST-CHANGE="2017-03-13T12:35:17.083+01:00" LONG-NAME="T_Int" MAX="not-an-integer" MIN="-100">
63+
<ALTERNATIVE-ID>
64+
<ALTERNATIVE-ID IDENTIFIER="_IrT00AfhEeelU71CdMk83g"/>
65+
</ALTERNATIVE-ID>
66+
</DATATYPE-DEFINITION-INTEGER>
67+
""";
68+
69+
var xmlReader = XmlReader.Create(new StringReader(xml));
70+
xmlReader.MoveToContent();
71+
72+
var datatypeDefinitionInteger = new DatatypeDefinitionInteger(NullLoggerFactory.Instance);
73+
74+
Assert.That(() => datatypeDefinitionInteger.ReadXml(xmlReader), Throws.InstanceOf<SerializationException>());
75+
}
76+
77+
[Test]
78+
public void Verify_that_when_MIN_is_too_large_exception_is_thrown()
79+
{
80+
var xml = """
81+
<DATATYPE-DEFINITION-INTEGER IDENTIFIER="_IrT00AfhEeelU71CdMk83g" LAST-CHANGE="2017-03-13T12:35:17.083+01:00" LONG-NAME="T_Int" MAX="100" MIN="-9223372036854775809">
82+
<ALTERNATIVE-ID>
83+
<ALTERNATIVE-ID IDENTIFIER="_IrT00AfhEeelU71CdMk83g"/>
84+
</ALTERNATIVE-ID>
85+
</DATATYPE-DEFINITION-INTEGER>
86+
""";
87+
88+
var xmlReader = XmlReader.Create(new StringReader(xml));
89+
xmlReader.MoveToContent();
90+
91+
var datatypeDefinitionInteger = new DatatypeDefinitionInteger(NullLoggerFactory.Instance);
92+
93+
Assert.That(() => datatypeDefinitionInteger.ReadXml(xmlReader), Throws.Nothing);
94+
95+
Assert.That(datatypeDefinitionInteger.Min, Is.EqualTo(Int64.MinValue));
96+
}
97+
98+
[Test]
99+
public void Verify_that_when_MIN_is_invalid_exception_is_thrown()
100+
{
101+
var xml = """
102+
<DATATYPE-DEFINITION-INTEGER IDENTIFIER="_IrT00AfhEeelU71CdMk83g" LAST-CHANGE="2017-03-13T12:35:17.083+01:00" LONG-NAME="T_Int" MAX="100" MIN="not-an-integer">
103+
<ALTERNATIVE-ID>
104+
<ALTERNATIVE-ID IDENTIFIER="_IrT00AfhEeelU71CdMk83g"/>
105+
</ALTERNATIVE-ID>
106+
</DATATYPE-DEFINITION-INTEGER>
107+
""";
108+
109+
var xmlReader = XmlReader.Create(new StringReader(xml));
110+
xmlReader.MoveToContent();
111+
112+
var datatypeDefinitionInteger = new DatatypeDefinitionInteger(NullLoggerFactory.Instance);
113+
114+
Assert.That(() => datatypeDefinitionInteger.ReadXml(xmlReader), Throws.InstanceOf<SerializationException>());
115+
}
116+
}
117+
}

0 commit comments

Comments
 (0)