Files
romanett 9b034f8291 Enable nullable reference types centrally; migrate all unset and annotations-only projects (#4700)
# Description

Nullable reference types are now enabled once, in `common.props`, and
every project in the repository builds warning-clean with them.

**Configuration**
- `<Nullable>enable</Nullable>` lives in `common.props`. All per-project
`<Nullable>` settings are removed: 178 project files (enable,
annotations, and the `disable` in the three Aot test projects).
- 419 per-file `#nullable enable`/`restore` directives and the 4
`#nullable disable` directives are removed.
- Kept on purpose:
- The 5 `<auto-generated>` schema files (`UANodeSet.cs`, `UA Type
Dictionary.cs`, `OPCBinarySchema.cs`, `SecuredApplication.cs`,
`PInvokeHelper.cs`) keep `#nullable enable`. The compiler ignores the
project setting in generated code, so the directive is required there.
- `tests/Opc.Ua.Interop.LegacyPeer` keeps `disable`. It builds against
the 1.5.378 packages and does not import the repository props.
- `docs/DeveloperGuide.md` and `.github/copilot-instructions.md` now
describe the central setting.

**Migration** (the 43 unset and 13 annotations-only projects, roughly
10k diagnostics)
- **Production code: annotated at the declarations.** Covers
`tools/Opc.Ua.SourceGeneration.Core`, `src/Opc.Ua.Lds.Server` and the
LDS sample.
- Members, parameters and returns are nullable where null actually
flows.
- Null-check helpers carry `[NotNullWhen]` (`IsNull`, `IsValidXmlName`,
`IsMethodTypeDesign`).
- `CoreUtils.Format`, `Utils.Format`, `ITemplateWriter.WriteLine` take
`params object?[]`. `Template.AddReplacement` takes `string?` (rendering
already skips null replacements).
- `IRegisteredServerStore.RegisterAsync` returns
`Task<RegistrationEntry?>`: it returns null when a registration is
removed.
- Dereferences backed by an invariant (for example, the validated model)
use `!`, so runtime behaviour is unchanged.
- **Behavioural changes in production code** (small, deliberate):
- `NodeSetToModelDesign` throws `InvalidOperationException` when the
file is not a NodeSet. Before, it threw a NullReferenceException on the
next line.
- `ModelDesignValidator` starts its lookup tables empty instead of null.
A redundant `if (m_nodes == null) return;` guard is gone.
- **Test code: minimal annotations.**
  - `?` on values that can be null, `!` where the test asserts non-null.
  - `= null!` for fixture members assigned in SetUp.
- `TryGetStructure<T>(out T? x)` where the inferred `T?` broke the
`IEncodeable` constraint.
- A few redundant `== null` checks on structs (`NodeId`, `ArrayOf<T>`)
folded into the adjacent `IsNull`/`Count` checks.

**Notes for reviewers**
- The diff is large (~1,500 files) but almost entirely mechanical: `?`,
`!` and directive removal. The non-annotation changes are listed above.
- Known follow-up, not done here:
`IModelDesign.Namespaces/TargetNamespace/NamespaceUris` stay
non-nullable even though the validator returns null before it loads a
dictionary. Making them nullable would ripple through every generator
call site.

## Related Issues

- Fixes #4439

## Checklist

- [ ] I have signed the
[CLA](https://opcfoundation.org/license/cla/ContributorLicenseAgreementv1.0.pdf)
and read the
[CONTRIBUTING](https://github.com/OPCFoundation/UA-.NETStandard/blob/master/CONTRIBUTING.md)
doc.
- [ ] I have added tests that prove my fix is effective or that my
feature works and increased code coverage.
- [x] I have added all necessary documentation.
- [x] I have verified that my changes do not introduce (new) build or
analyzer warnings.
- [ ] I ran **all** tests locally using the **UA.slnx** solution against
at least .net **framework** and .net **10**, and all passed.
- [ ] I fixed **all** failing and flaky tests in the CI pipelines and
**all** CodeQL warnings.
- [ ] I have addressed **all** PR feedback received.

The "no new warnings" box covers net10.0 and net48: full non-incremental
builds of `UA.slnx`, `tools/SourceGeneration.slnx`,
`tools/MigrationAnalyzer.slnx` and the Aot test projects finished with 0
warnings and 0 errors under the repository's warnings-as-errors
settings. net9.0 and net8.0 also had no nullable diagnostics, but they
were not rebuilt after the final fixes. Tests have not been run locally
yet.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 08:48:13 +02:00

1382 lines
43 KiB
C#

/* ========================================================================
* Copyright (c) 2005-2025 The OPC Foundation, Inc. All rights reserved.
*
* OPC Foundation MIT License 1.00
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* The complete license agreement can be found here:
* http://opcfoundation.org/License/MIT/1.00/
* ======================================================================*/
using NUnit.Framework;
namespace Opc.Ua.Schema.Model.Tests
{
/// <summary>
/// Unit tests for the <see cref="LocalizedText"/> class Equals method.
/// </summary>
[TestFixture]
[Category("ModelDesign")]
[SetCulture("en-us")]
[SetUICulture("en-us")]
[Parallelizable]
public class LocalizedTextTests
{
/// <summary>
/// Tests that Equals returns false when comparing with null.
/// </summary>
[Test]
public void Equals_NullOther_ReturnsFalse()
{
// Arrange
var localizedText = new LocalizedText
{
Key = "key1",
DoNotIgnore = true,
Value = "value1",
IsAutogenerated = false
};
// Act
#pragma warning disable CA1508 // Avoid dead conditional code
bool result = localizedText.Equals(null!);
#pragma warning restore CA1508 // Avoid dead conditional code
// Assert
Assert.That(result, Is.False);
}
/// <summary>
/// Tests that Equals returns true when comparing an instance with itself.
/// </summary>
[Test]
public void Equals_SameInstance_ReturnsTrue()
{
// Arrange
var localizedText = new LocalizedText
{
Key = "key1",
DoNotIgnore = true,
Value = "value1",
IsAutogenerated = false
};
// Act
bool result = localizedText.Equals(localizedText);
// Assert
Assert.That(result, Is.True);
}
/// <summary>
/// Tests that Equals returns true when comparing two instances with identical property values.
/// </summary>
[Test]
public void Equals_IdenticalValues_ReturnsTrue()
{
// Arrange
var localizedText1 = new LocalizedText
{
Key = "key1",
DoNotIgnore = true,
Value = "value1",
IsAutogenerated = false
};
var localizedText2 = new LocalizedText
{
Key = "key1",
DoNotIgnore = true,
Value = "value1",
IsAutogenerated = false
};
// Act
bool result = localizedText1.Equals(localizedText2);
// Assert
Assert.That(result, Is.True);
}
/// <summary>
/// Tests that Equals returns false when the Key property differs.
/// </summary>
[Test]
public void Equals_DifferentKey_ReturnsFalse()
{
// Arrange
var localizedText1 = new LocalizedText
{
Key = "key1",
DoNotIgnore = true,
Value = "value1",
IsAutogenerated = false
};
var localizedText2 = new LocalizedText
{
Key = "key2",
DoNotIgnore = true,
Value = "value1",
IsAutogenerated = false
};
// Act
bool result = localizedText1.Equals(localizedText2);
// Assert
Assert.That(result, Is.False);
}
/// <summary>
/// Tests that Equals returns false when the DoNotIgnore property differs.
/// </summary>
[Test]
public void Equals_DifferentDoNotIgnore_ReturnsFalse()
{
// Arrange
var localizedText1 = new LocalizedText
{
Key = "key1",
DoNotIgnore = true,
Value = "value1",
IsAutogenerated = false
};
var localizedText2 = new LocalizedText
{
Key = "key1",
DoNotIgnore = false,
Value = "value1",
IsAutogenerated = false
};
// Act
bool result = localizedText1.Equals(localizedText2);
// Assert
Assert.That(result, Is.False);
}
/// <summary>
/// Tests that Equals returns false when the Value property differs.
/// </summary>
[Test]
public void Equals_DifferentValue_ReturnsFalse()
{
// Arrange
var localizedText1 = new LocalizedText
{
Key = "key1",
DoNotIgnore = true,
Value = "value1",
IsAutogenerated = false
};
var localizedText2 = new LocalizedText
{
Key = "key1",
DoNotIgnore = true,
Value = "value2",
IsAutogenerated = false
};
// Act
bool result = localizedText1.Equals(localizedText2);
// Assert
Assert.That(result, Is.False);
}
/// <summary>
/// Tests that Equals returns false when the IsAutogenerated property differs.
/// </summary>
[Test]
public void Equals_DifferentIsAutogenerated_ReturnsFalse()
{
// Arrange
var localizedText1 = new LocalizedText
{
Key = "key1",
DoNotIgnore = true,
Value = "value1",
IsAutogenerated = false
};
var localizedText2 = new LocalizedText
{
Key = "key1",
DoNotIgnore = true,
Value = "value1",
IsAutogenerated = true
};
// Act
bool result = localizedText1.Equals(localizedText2);
// Assert
Assert.That(result, Is.False);
}
/// <summary>
/// Tests that Equals handles null Key values correctly.
/// Verifies that two instances with null Key values are equal if all other properties match.
/// </summary>
[Test]
public void Equals_NullKeyInBoth_ReturnsTrue()
{
// Arrange
var localizedText1 = new LocalizedText
{
Key = null,
DoNotIgnore = true,
Value = "value1",
IsAutogenerated = false
};
var localizedText2 = new LocalizedText
{
Key = null,
DoNotIgnore = true,
Value = "value1",
IsAutogenerated = false
};
// Act
bool result = localizedText1.Equals(localizedText2);
// Assert
Assert.That(result, Is.True);
}
/// <summary>
/// Tests that Equals returns false when one instance has null Key and the other has a non-null Key.
/// </summary>
[Test]
public void Equals_NullKeyInOneInstance_ReturnsFalse()
{
// Arrange
var localizedText1 = new LocalizedText
{
Key = null,
DoNotIgnore = true,
Value = "value1",
IsAutogenerated = false
};
var localizedText2 = new LocalizedText
{
Key = "key1",
DoNotIgnore = true,
Value = "value1",
IsAutogenerated = false
};
// Act
bool result = localizedText1.Equals(localizedText2);
// Assert
Assert.That(result, Is.False);
}
/// <summary>
/// Tests that Equals handles null Value correctly.
/// Verifies that two instances with null Value are equal if all other properties match.
/// </summary>
[Test]
public void Equals_NullValueInBoth_ReturnsTrue()
{
// Arrange
var localizedText1 = new LocalizedText
{
Key = "key1",
DoNotIgnore = true,
Value = null,
IsAutogenerated = false
};
var localizedText2 = new LocalizedText
{
Key = "key1",
DoNotIgnore = true,
Value = null,
IsAutogenerated = false
};
// Act
bool result = localizedText1.Equals(localizedText2);
// Assert
Assert.That(result, Is.True);
}
/// <summary>
/// Tests that Equals returns false when one instance has null Value and the other has a non-null Value.
/// </summary>
[Test]
public void Equals_NullValueInOneInstance_ReturnsFalse()
{
// Arrange
var localizedText1 = new LocalizedText
{
Key = "key1",
DoNotIgnore = true,
Value = null,
IsAutogenerated = false
};
var localizedText2 = new LocalizedText
{
Key = "key1",
DoNotIgnore = true,
Value = "value1",
IsAutogenerated = false
};
// Act
bool result = localizedText1.Equals(localizedText2);
// Assert
Assert.That(result, Is.False);
}
/// <summary>
/// Tests that Equals handles empty string Key values correctly.
/// Verifies that two instances with empty Key values are equal if all other properties match.
/// </summary>
[Test]
public void Equals_EmptyKeyInBoth_ReturnsTrue()
{
// Arrange
var localizedText1 = new LocalizedText
{
Key = string.Empty,
DoNotIgnore = true,
Value = "value1",
IsAutogenerated = false
};
var localizedText2 = new LocalizedText
{
Key = string.Empty,
DoNotIgnore = true,
Value = "value1",
IsAutogenerated = false
};
// Act
bool result = localizedText1.Equals(localizedText2);
// Assert
Assert.That(result, Is.True);
}
/// <summary>
/// Tests that Equals handles empty string Value correctly.
/// Verifies that two instances with empty Value are equal if all other properties match.
/// </summary>
[Test]
public void Equals_EmptyValueInBoth_ReturnsTrue()
{
// Arrange
var localizedText1 = new LocalizedText
{
Key = "key1",
DoNotIgnore = true,
Value = string.Empty,
IsAutogenerated = false
};
var localizedText2 = new LocalizedText
{
Key = "key1",
DoNotIgnore = true,
Value = string.Empty,
IsAutogenerated = false
};
// Act
bool result = localizedText1.Equals(localizedText2);
// Assert
Assert.That(result, Is.True);
}
/// <summary>
/// Tests that Equals handles whitespace string values correctly in Key property.
/// Verifies that whitespace is considered a distinct value from empty or other whitespace patterns.
/// </summary>
[Test]
public void Equals_WhitespaceKeyInBoth_ReturnsTrue()
{
// Arrange
var localizedText1 = new LocalizedText
{
Key = " ",
DoNotIgnore = true,
Value = "value1",
IsAutogenerated = false
};
var localizedText2 = new LocalizedText
{
Key = " ",
DoNotIgnore = true,
Value = "value1",
IsAutogenerated = false
};
// Act
bool result = localizedText1.Equals(localizedText2);
// Assert
Assert.That(result, Is.True);
}
/// <summary>
/// Tests that Equals handles whitespace string values correctly in Value property.
/// Verifies that whitespace is considered a distinct value from empty or other whitespace patterns.
/// </summary>
[Test]
public void Equals_WhitespaceValueInBoth_ReturnsTrue()
{
// Arrange
var localizedText1 = new LocalizedText
{
Key = "key1",
DoNotIgnore = true,
Value = " ",
IsAutogenerated = false
};
var localizedText2 = new LocalizedText
{
Key = "key1",
DoNotIgnore = true,
Value = " ",
IsAutogenerated = false
};
// Act
bool result = localizedText1.Equals(localizedText2);
// Assert
Assert.That(result, Is.True);
}
/// <summary>
/// Tests that Equals handles special characters in string properties correctly.
/// Verifies that special characters are properly compared.
/// </summary>
[Test]
public void Equals_SpecialCharactersInStrings_ReturnsTrue()
{
// Arrange
var localizedText1 = new LocalizedText
{
Key = "key\t\r\n\\!@#$%^&*()",
DoNotIgnore = true,
Value = "value\t\r\n\\!@#$%^&*()",
IsAutogenerated = false
};
var localizedText2 = new LocalizedText
{
Key = "key\t\r\n\\!@#$%^&*()",
DoNotIgnore = true,
Value = "value\t\r\n\\!@#$%^&*()",
IsAutogenerated = false
};
// Act
bool result = localizedText1.Equals(localizedText2);
// Assert
Assert.That(result, Is.True);
}
/// <summary>
/// Tests that Equals correctly compares instances with default constructor values.
/// The default constructor initializes Key to "" and DoNotIgnore to false.
/// </summary>
[Test]
public void Equals_DefaultConstructorValues_ReturnsTrue()
{
// Arrange
var localizedText1 = new LocalizedText();
var localizedText2 = new LocalizedText();
// Act
bool result = localizedText1.Equals(localizedText2);
// Assert
Assert.That(result, Is.True);
}
/// <summary>
/// Tests that Equals correctly handles instances with all boolean combinations.
/// Verifies equality with both DoNotIgnore and IsAutogenerated set to true.
/// </summary>
[Test]
public void Equals_BothBooleanPropertiesTrue_ReturnsTrue()
{
// Arrange
var localizedText1 = new LocalizedText
{
Key = "key1",
DoNotIgnore = true,
Value = "value1",
IsAutogenerated = true
};
var localizedText2 = new LocalizedText
{
Key = "key1",
DoNotIgnore = true,
Value = "value1",
IsAutogenerated = true
};
// Act
bool result = localizedText1.Equals(localizedText2);
// Assert
Assert.That(result, Is.True);
}
/// <summary>
/// Tests that Equals correctly handles instances with all properties set to false.
/// Verifies equality with both DoNotIgnore and IsAutogenerated set to false.
/// </summary>
[Test]
public void Equals_BothBooleanPropertiesFalse_ReturnsTrue()
{
// Arrange
var localizedText1 = new LocalizedText
{
Key = "key1",
DoNotIgnore = false,
Value = "value1",
IsAutogenerated = false
};
var localizedText2 = new LocalizedText
{
Key = "key1",
DoNotIgnore = false,
Value = "value1",
IsAutogenerated = false
};
// Act
bool result = localizedText1.Equals(localizedText2);
// Assert
Assert.That(result, Is.True);
}
/// <summary>
/// Tests that Equals handles very long string values correctly.
/// Verifies that string comparison works properly with large strings.
/// </summary>
[Test]
public void Equals_VeryLongStrings_ReturnsTrue()
{
// Arrange
string longString = new('a', 10000);
var localizedText1 = new LocalizedText
{
Key = longString,
DoNotIgnore = true,
Value = longString,
IsAutogenerated = false
};
var localizedText2 = new LocalizedText
{
Key = longString,
DoNotIgnore = true,
Value = longString,
IsAutogenerated = false
};
// Act
bool result = localizedText1.Equals(localizedText2);
// Assert
Assert.That(result, Is.True);
}
/// <summary>
/// Tests that Equals returns false when comparing very long strings that differ.
/// Verifies that string comparison correctly identifies differences in large strings.
/// </summary>
[Test]
public void Equals_VeryLongStringsDifferent_ReturnsFalse()
{
// Arrange
string longString1 = new('a', 10000);
string longString2 = new('b', 10000);
var localizedText1 = new LocalizedText
{
Key = longString1,
DoNotIgnore = true,
Value = "value1",
IsAutogenerated = false
};
var localizedText2 = new LocalizedText
{
Key = longString2,
DoNotIgnore = true,
Value = "value1",
IsAutogenerated = false
};
// Act
bool result = localizedText1.Equals(localizedText2);
// Assert
Assert.That(result, Is.False);
}
/// <summary>
/// Tests that Equals(object) returns false when parameter is null.
/// Verifies null handling in object overload.
/// Expected result: false.
/// </summary>
[Test]
public void Equals_NullObject_ReturnsFalse()
{
// Arrange
var text = new LocalizedText
{
Key = "testKey",
Value = "testValue"
};
// Act
#pragma warning disable CA1508 // Avoid dead conditional code
bool result = text.Equals((object)null!);
#pragma warning restore CA1508 // Avoid dead conditional code
// Assert
Assert.That(result, Is.False);
}
/// <summary>
/// Tests that Equals(object) returns false when parameter is of different type.
/// Verifies type checking via 'as' operator cast.
/// Expected result: false.
/// </summary>
[Test]
public void Equals_DifferentType_ReturnsFalse()
{
// Arrange
var text = new LocalizedText
{
Key = "testKey",
Value = "testValue"
};
object differentType = "string object";
// Act
bool result = text.Equals(differentType);
// Assert
Assert.That(result, Is.False);
}
/// <summary>
/// Tests that Equals(object) returns true when comparing two instances with identical property values.
/// Verifies value-based equality comparison.
/// Expected result: true.
/// </summary>
[Test]
public void Equals_EqualInstances_ReturnsTrue()
{
// Arrange
var text1 = new LocalizedText
{
Key = "testKey",
DoNotIgnore = true,
Value = "testValue",
IsAutogenerated = false
};
var text2 = new LocalizedText
{
Key = "testKey",
DoNotIgnore = true,
Value = "testValue",
IsAutogenerated = false
};
// Act
bool result = text1.Equals((object)text2);
// Assert
Assert.That(result, Is.True);
}
/// <summary>
/// Tests that Equals(object) returns true when both instances have null Value and other properties are equal.
/// Verifies null value handling in equality comparison.
/// Expected result: true.
/// </summary>
[Test]
public void Equals_BothNullValue_ReturnsTrue()
{
// Arrange
var text1 = new LocalizedText
{
Key = "testKey",
DoNotIgnore = true,
Value = null,
IsAutogenerated = false
};
var text2 = new LocalizedText
{
Key = "testKey",
DoNotIgnore = true,
Value = null,
IsAutogenerated = false
};
// Act
bool result = text1.Equals((object)text2);
// Assert
Assert.That(result, Is.True);
}
/// <summary>
/// Tests that Equals(object) returns false when one instance has null Value and the other does not.
/// Verifies null vs non-null value comparison.
/// Expected result: false.
/// </summary>
[Test]
public void Equals_OneNullValue_ReturnsFalse()
{
// Arrange
var text1 = new LocalizedText
{
Key = "testKey",
DoNotIgnore = true,
Value = null,
IsAutogenerated = false
};
var text2 = new LocalizedText
{
Key = "testKey",
DoNotIgnore = true,
Value = "testValue",
IsAutogenerated = false
};
// Act
bool result = text1.Equals((object)text2);
// Assert
Assert.That(result, Is.False);
}
/// <summary>
/// Tests that Equals(object) returns true when comparing instances with empty string Key and Value.
/// Verifies handling of empty strings in equality comparison.
/// Expected result: true.
/// </summary>
[Test]
public void Equals_EmptyStrings_ReturnsTrue()
{
// Arrange
var text1 = new LocalizedText
{
Key = string.Empty,
DoNotIgnore = false,
Value = string.Empty,
IsAutogenerated = false
};
var text2 = new LocalizedText
{
Key = string.Empty,
DoNotIgnore = false,
Value = string.Empty,
IsAutogenerated = false
};
// Act
bool result = text1.Equals((object)text2);
// Assert
Assert.That(result, Is.True);
}
/// <summary>
/// Tests that Equals(object) returns true when comparing default-initialized instances.
/// Verifies equality comparison with default property values.
/// Expected result: true.
/// </summary>
[Test]
public void Equals_DefaultInstances_ReturnsTrue()
{
// Arrange
var text1 = new LocalizedText();
var text2 = new LocalizedText();
// Act
bool result = text1.Equals((object)text2);
// Assert
Assert.That(result, Is.True);
}
/// <summary>
/// Tests that Equals(object) returns false when Key differs by whitespace.
/// Verifies that whitespace differences are detected.
/// Expected result: false.
/// </summary>
[Test]
public void Equals_KeyDiffersByWhitespace_ReturnsFalse()
{
// Arrange
var text1 = new LocalizedText
{
Key = "testKey",
DoNotIgnore = false,
Value = "testValue",
IsAutogenerated = false
};
var text2 = new LocalizedText
{
Key = "testKey ",
DoNotIgnore = false,
Value = "testValue",
IsAutogenerated = false
};
// Act
bool result = text1.Equals((object)text2);
// Assert
Assert.That(result, Is.False);
}
/// <summary>
/// Tests that Equals(object) returns false when Value differs by case.
/// Verifies that string comparison is case-sensitive.
/// Expected result: false.
/// </summary>
[Test]
public void Equals_ValueDiffersByCase_ReturnsFalse()
{
// Arrange
var text1 = new LocalizedText
{
Key = "testKey",
DoNotIgnore = false,
Value = "TestValue",
IsAutogenerated = false
};
var text2 = new LocalizedText
{
Key = "testKey",
DoNotIgnore = false,
Value = "testvalue",
IsAutogenerated = false
};
// Act
bool result = text1.Equals((object)text2);
// Assert
Assert.That(result, Is.False);
}
/// <summary>
/// Tests that GetHashCode returns consistent value across multiple invocations for the same object.
/// Input: LocalizedText with all properties set to default values.
/// Expected: Hash code remains consistent.
/// </summary>
[Test]
public void GetHashCode_ConsistentInvocations_ReturnsSameHashCode()
{
// Arrange
var localizedText = new LocalizedText
{
Key = "TestKey",
DoNotIgnore = true,
Value = "TestValue",
IsAutogenerated = false
};
// Act
int hashCode1 = localizedText.GetHashCode();
int hashCode2 = localizedText.GetHashCode();
// Assert
Assert.That(hashCode2, Is.EqualTo(hashCode1));
}
/// <summary>
/// Tests that equal objects produce equal hash codes.
/// Input: Two LocalizedText instances with identical property values.
/// Expected: Both objects return the same hash code.
/// </summary>
[Test]
public void GetHashCode_EqualObjects_ReturnsSameHashCode()
{
// Arrange
var localizedText1 = new LocalizedText
{
Key = "TestKey",
DoNotIgnore = true,
Value = "TestValue",
IsAutogenerated = false
};
var localizedText2 = new LocalizedText
{
Key = "TestKey",
DoNotIgnore = true,
Value = "TestValue",
IsAutogenerated = false
};
// Act
int hashCode1 = localizedText1.GetHashCode();
int hashCode2 = localizedText2.GetHashCode();
// Assert
Assert.That(hashCode2, Is.EqualTo(hashCode1));
}
/// <summary>
/// Tests that different Key values produce different hash codes.
/// Input: Two LocalizedText instances differing only in Key property.
/// Expected: Different hash codes are returned.
/// </summary>
[Test]
public void GetHashCode_DifferentKey_ReturnsDifferentHashCode()
{
// Arrange
var localizedText1 = new LocalizedText
{
Key = "Key1",
DoNotIgnore = false,
Value = "Value",
IsAutogenerated = false
};
var localizedText2 = new LocalizedText
{
Key = "Key2",
DoNotIgnore = false,
Value = "Value",
IsAutogenerated = false
};
// Act
int hashCode1 = localizedText1.GetHashCode();
int hashCode2 = localizedText2.GetHashCode();
// Assert
Assert.That(hashCode2, Is.Not.EqualTo(hashCode1));
}
/// <summary>
/// Tests that different DoNotIgnore values produce different hash codes.
/// Input: Two LocalizedText instances differing only in DoNotIgnore property.
/// Expected: Different hash codes are returned.
/// </summary>
[Test]
public void GetHashCode_DifferentDoNotIgnore_ReturnsDifferentHashCode()
{
// Arrange
var localizedText1 = new LocalizedText
{
Key = "TestKey",
DoNotIgnore = true,
Value = "TestValue",
IsAutogenerated = false
};
var localizedText2 = new LocalizedText
{
Key = "TestKey",
DoNotIgnore = false,
Value = "TestValue",
IsAutogenerated = false
};
// Act
int hashCode1 = localizedText1.GetHashCode();
int hashCode2 = localizedText2.GetHashCode();
// Assert
Assert.That(hashCode2, Is.Not.EqualTo(hashCode1));
}
/// <summary>
/// Tests that different Value values produce different hash codes.
/// Input: Two LocalizedText instances differing only in Value property.
/// Expected: Different hash codes are returned.
/// </summary>
[Test]
public void GetHashCode_DifferentValue_ReturnsDifferentHashCode()
{
// Arrange
var localizedText1 = new LocalizedText
{
Key = "TestKey",
DoNotIgnore = false,
Value = "Value1",
IsAutogenerated = false
};
var localizedText2 = new LocalizedText
{
Key = "TestKey",
DoNotIgnore = false,
Value = "Value2",
IsAutogenerated = false
};
// Act
int hashCode1 = localizedText1.GetHashCode();
int hashCode2 = localizedText2.GetHashCode();
// Assert
Assert.That(hashCode2, Is.Not.EqualTo(hashCode1));
}
/// <summary>
/// Tests that different IsAutogenerated values produce different hash codes.
/// Input: Two LocalizedText instances differing only in IsAutogenerated property.
/// Expected: Different hash codes are returned.
/// </summary>
[Test]
public void GetHashCode_DifferentIsAutogenerated_ReturnsDifferentHashCode()
{
// Arrange
var localizedText1 = new LocalizedText
{
Key = "TestKey",
DoNotIgnore = false,
Value = "TestValue",
IsAutogenerated = true
};
var localizedText2 = new LocalizedText
{
Key = "TestKey",
DoNotIgnore = false,
Value = "TestValue",
IsAutogenerated = false
};
// Act
int hashCode1 = localizedText1.GetHashCode();
int hashCode2 = localizedText2.GetHashCode();
// Assert
Assert.That(hashCode2, Is.Not.EqualTo(hashCode1));
}
/// <summary>
/// Tests GetHashCode with null Key value.
/// Input: LocalizedText with Key set to null.
/// Expected: Hash code is computed without throwing exception.
/// </summary>
[Test]
public void GetHashCode_NullKey_ReturnsHashCode()
{
// Arrange
var localizedText = new LocalizedText
{
Key = null,
DoNotIgnore = false,
Value = "TestValue",
IsAutogenerated = false
};
// Act
int hashCode = localizedText.GetHashCode();
// Assert
Assert.That(hashCode, Is.InstanceOf<int>());
}
/// <summary>
/// Tests GetHashCode with null Value.
/// Input: LocalizedText with Value set to null.
/// Expected: Hash code is computed without throwing exception.
/// </summary>
[Test]
public void GetHashCode_NullValue_ReturnsHashCode()
{
// Arrange
var localizedText = new LocalizedText
{
Key = "TestKey",
DoNotIgnore = false,
Value = null,
IsAutogenerated = false
};
// Act
int hashCode = localizedText.GetHashCode();
// Assert
Assert.That(hashCode, Is.InstanceOf<int>());
}
/// <summary>
/// Tests GetHashCode with all null string properties.
/// Input: LocalizedText with Key and Value set to null.
/// Expected: Hash code is computed without throwing exception.
/// </summary>
[Test]
public void GetHashCode_AllNullStrings_ReturnsHashCode()
{
// Arrange
var localizedText = new LocalizedText
{
Key = null,
DoNotIgnore = false,
Value = null,
IsAutogenerated = false
};
// Act
int hashCode = localizedText.GetHashCode();
// Assert
Assert.That(hashCode, Is.InstanceOf<int>());
}
/// <summary>
/// Tests GetHashCode with empty string properties.
/// Input: LocalizedText with Key and Value set to empty strings.
/// Expected: Hash code is computed correctly.
/// </summary>
[Test]
public void GetHashCode_EmptyStrings_ReturnsHashCode()
{
// Arrange
var localizedText = new LocalizedText
{
Key = string.Empty,
DoNotIgnore = false,
Value = string.Empty,
IsAutogenerated = false
};
// Act
int hashCode = localizedText.GetHashCode();
// Assert
Assert.That(hashCode, Is.InstanceOf<int>());
}
/// <summary>
/// Tests GetHashCode with whitespace string properties.
/// Input: LocalizedText with Key and Value set to whitespace strings.
/// Expected: Hash code is computed correctly.
/// </summary>
[Test]
public void GetHashCode_WhitespaceStrings_ReturnsHashCode()
{
// Arrange
var localizedText = new LocalizedText
{
Key = " ",
DoNotIgnore = false,
Value = " ",
IsAutogenerated = false
};
// Act
int hashCode = localizedText.GetHashCode();
// Assert
Assert.That(hashCode, Is.InstanceOf<int>());
}
/// <summary>
/// Tests GetHashCode with very long string properties.
/// Input: LocalizedText with Key and Value set to very long strings.
/// Expected: Hash code is computed correctly.
/// </summary>
[Test]
public void GetHashCode_VeryLongStrings_ReturnsHashCode()
{
// Arrange
string longString = new('A', 10000);
var localizedText = new LocalizedText
{
Key = longString,
DoNotIgnore = true,
Value = longString,
IsAutogenerated = true
};
// Act
int hashCode = localizedText.GetHashCode();
// Assert
Assert.That(hashCode, Is.InstanceOf<int>());
}
/// <summary>
/// Tests GetHashCode with special characters in string properties.
/// Input: LocalizedText with Key and Value containing special characters.
/// Expected: Hash code is computed correctly.
/// </summary>
[Test]
public void GetHashCode_SpecialCharacters_ReturnsHashCode()
{
// Arrange
var localizedText = new LocalizedText
{
Key = "Test\nKey\r\n\t",
DoNotIgnore = false,
Value = "Value\0\x01\x1F",
IsAutogenerated = false
};
// Act
int hashCode = localizedText.GetHashCode();
// Assert
Assert.That(hashCode, Is.InstanceOf<int>());
}
/// <summary>
/// Tests that two LocalizedText instances with different combinations of boolean properties produce different hash codes.
/// Input: Multiple LocalizedText instances with different boolean property combinations.
/// Expected: Different hash codes for different boolean combinations.
/// </summary>
[TestCase(false, false, TestName = "GetHashCode_BooleanCombination_FalseFalse")]
[TestCase(false, true, TestName = "GetHashCode_BooleanCombination_FalseTrue")]
[TestCase(true, false, TestName = "GetHashCode_BooleanCombination_TrueTrue")]
[TestCase(true, true, TestName = "GetHashCode_BooleanCombination_TrueFalse")]
public void GetHashCode_DifferentBooleanCombinations_ReturnsHashCode(bool doNotIgnore, bool isAutogenerated)
{
// Arrange
var localizedText = new LocalizedText
{
Key = "TestKey",
DoNotIgnore = doNotIgnore,
Value = "TestValue",
IsAutogenerated = isAutogenerated
};
// Act
int hashCode = localizedText.GetHashCode();
// Assert
Assert.That(hashCode, Is.InstanceOf<int>());
}
/// <summary>
/// Tests that equal LocalizedText instances with null properties produce equal hash codes.
/// Input: Two LocalizedText instances with identical null properties.
/// Expected: Same hash code is returned.
/// </summary>
[Test]
public void GetHashCode_EqualObjectsWithNullProperties_ReturnsSameHashCode()
{
// Arrange
var localizedText1 = new LocalizedText
{
Key = null,
DoNotIgnore = true,
Value = null,
IsAutogenerated = true
};
var localizedText2 = new LocalizedText
{
Key = null,
DoNotIgnore = true,
Value = null,
IsAutogenerated = true
};
// Act
int hashCode1 = localizedText1.GetHashCode();
int hashCode2 = localizedText2.GetHashCode();
// Assert
Assert.That(hashCode2, Is.EqualTo(hashCode1));
}
/// <summary>
/// Tests that LocalizedText instances where one has null Key and the other has non-null Key produce different hash codes.
/// Input: Two LocalizedText instances differing only in Key nullability.
/// Expected: Different hash codes are returned.
/// </summary>
[Test]
public void GetHashCode_NullVsNonNullKey_ReturnsDifferentHashCode()
{
// Arrange
var localizedText1 = new LocalizedText
{
Key = null,
DoNotIgnore = false,
Value = "Value",
IsAutogenerated = false
};
var localizedText2 = new LocalizedText
{
Key = "Key",
DoNotIgnore = false,
Value = "Value",
IsAutogenerated = false
};
// Act
int hashCode1 = localizedText1.GetHashCode();
int hashCode2 = localizedText2.GetHashCode();
// Assert
Assert.That(hashCode2, Is.Not.EqualTo(hashCode1));
}
/// <summary>
/// Tests that LocalizedText instances where one has null Value and the other has non-null Value produce different hash codes.
/// Input: Two LocalizedText instances differing only in Value nullability.
/// Expected: Different hash codes are returned.
/// </summary>
[Test]
public void GetHashCode_NullVsNonNullValue_ReturnsDifferentHashCode()
{
// Arrange
var localizedText1 = new LocalizedText
{
Key = "Key",
DoNotIgnore = false,
Value = null,
IsAutogenerated = false
};
var localizedText2 = new LocalizedText
{
Key = "Key",
DoNotIgnore = false,
Value = "Value",
IsAutogenerated = false
};
// Act
int hashCode1 = localizedText1.GetHashCode();
int hashCode2 = localizedText2.GetHashCode();
// Assert
Assert.That(hashCode2, Is.Not.EqualTo(hashCode1));
}
}
}