using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Client.Shared.Helpers;
namespace ZB.MOM.WW.OtOpcUa.Client.Shared.Tests.Helpers;
public class ValueConverterTests
{
/// Verifies that a boolean string "True" is converted to true.
[Fact]
public void ConvertValue_Bool_True()
{
ValueConverter.ConvertValue("True", true).ShouldBe(true);
}
/// Verifies that a boolean string "False" is converted to false.
[Fact]
public void ConvertValue_Bool_False()
{
ValueConverter.ConvertValue("False", false).ShouldBe(false);
}
/// Verifies that a byte value is converted correctly.
[Fact]
public void ConvertValue_Byte()
{
ValueConverter.ConvertValue("255", (byte)0).ShouldBe((byte)255);
}
/// Verifies that a short value is converted correctly.
[Fact]
public void ConvertValue_Short()
{
ValueConverter.ConvertValue("-100", (short)0).ShouldBe((short)-100);
}
/// Verifies that an unsigned short value is converted correctly.
[Fact]
public void ConvertValue_UShort()
{
ValueConverter.ConvertValue("65535", (ushort)0).ShouldBe((ushort)65535);
}
/// Verifies that an integer value is converted correctly.
[Fact]
public void ConvertValue_Int()
{
ValueConverter.ConvertValue("42", 0).ShouldBe(42);
}
/// Verifies that an unsigned integer value is converted correctly.
[Fact]
public void ConvertValue_UInt()
{
ValueConverter.ConvertValue("42", 0u).ShouldBe(42u);
}
/// Verifies that a long value is converted correctly.
[Fact]
public void ConvertValue_Long()
{
ValueConverter.ConvertValue("9999999999", 0L).ShouldBe(9999999999L);
}
/// Verifies that an unsigned long value is converted correctly.
[Fact]
public void ConvertValue_ULong()
{
ValueConverter.ConvertValue("18446744073709551615", 0UL).ShouldBe(ulong.MaxValue);
}
/// Verifies that a float value is converted correctly.
[Fact]
public void ConvertValue_Float()
{
ValueConverter.ConvertValue("3.14", 0f).ShouldBe(3.14f);
}
/// Verifies that a double value is converted correctly.
[Fact]
public void ConvertValue_Double()
{
ValueConverter.ConvertValue("3.14159", 0.0).ShouldBe(3.14159);
}
/// Verifies that a string value is converted correctly when the current value is a string.
[Fact]
public void ConvertValue_String_WhenCurrentIsString()
{
ValueConverter.ConvertValue("hello", "").ShouldBe("hello");
}
/// Verifies that a string value is converted correctly when the current value is null.
[Fact]
public void ConvertValue_String_WhenCurrentIsNull()
{
ValueConverter.ConvertValue("hello", null).ShouldBe("hello");
}
/// Verifies that a string value is converted correctly when the current value is an unknown type.
[Fact]
public void ConvertValue_String_WhenCurrentIsUnknownType()
{
ValueConverter.ConvertValue("hello", new object()).ShouldBe("hello");
}
/// Verifies that converting an invalid boolean value throws a FormatException.
[Fact]
public void ConvertValue_InvalidBool_Throws()
{
Should.Throw(() => ValueConverter.ConvertValue("notabool", true));
}
/// Verifies that converting an invalid integer value throws a FormatException with a descriptive message.
[Fact]
public void ConvertValue_InvalidInt_ThrowsWithDescription()
{
var ex = Should.Throw(() => ValueConverter.ConvertValue("notanint", 0));
ex.Message.ShouldContain("Int32");
ex.Message.ShouldContain("notanint");
}
/// Verifies that an overflow during conversion throws a FormatException.
[Fact]
public void ConvertValue_Overflow_ThrowsFormatException()
{
// OverflowException is now wrapped in a descriptive FormatException
var ex = Should.Throw(() => ValueConverter.ConvertValue("256", (byte)0));
ex.InnerException.ShouldBeOfType();
}
// --- Client.Shared-008: Boolean aliases ---
/// Verifies that boolean values accept numeric and word aliases.
/// The input string value.
/// The expected boolean value.
[Theory]
[InlineData("1", true)]
[InlineData("0", false)]
[InlineData("yes", true)]
[InlineData("no", false)]
[InlineData("YES", true)]
[InlineData("NO", false)]
[InlineData("true", true)]
[InlineData("false", false)]
[InlineData("True", true)]
[InlineData("False", false)]
public void ConvertValue_Bool_AcceptsNumericAndWordAliases(string input, bool expected)
{
ValueConverter.ConvertValue(input, true).ShouldBe(expected);
}
/// Verifies that converting an invalid boolean value throws a descriptive FormatException.
[Fact]
public void ConvertValue_InvalidBool_ThrowsDescriptiveFormatException()
{
var ex = Should.Throw(() => ValueConverter.ConvertValue("maybe", false));
ex.Message.ShouldContain("Boolean");
}
}