Files
plist-cil/plist-cil.test/ValuePreprocessorTests.cs

124 lines
5.2 KiB
C#
Raw Permalink Normal View History

2025-01-21 20:14:00 +01:00
using System;
using System.Linq;
using Claunia.PropertyList;
using Xunit;
2025-08-07 22:31:12 +01:00
namespace plistcil.test;
public static class ValuePreprocessorTests
2025-01-21 20:14:00 +01:00
{
2025-08-07 22:31:12 +01:00
// lock tests to make sure temporarily added / replaced preprocessors don't interfere with the other tests in this suite
private static readonly object _testLock = new();
2025-08-07 22:31:12 +01:00
[Fact]
public static void TestPassiveDefaultPreprocessorsRegistered()
{
byte[] testByteArray = [0x1, 0x2, 0x4, 0x8];
2025-01-27 18:21:22 +01:00
2025-08-07 22:31:12 +01:00
Assert.Equal(true, ValuePreprocessor.Preprocess(true, ValuePreprocessor.Type.BOOL));
Assert.Equal(false, ValuePreprocessor.Preprocess(false, ValuePreprocessor.Type.BOOL));
Assert.Equal("true", ValuePreprocessor.Preprocess("true", ValuePreprocessor.Type.BOOL));
2025-01-27 18:21:22 +01:00
2025-08-07 22:31:12 +01:00
Assert.Equal("42", ValuePreprocessor.Preprocess("42", ValuePreprocessor.Type.INTEGER));
Assert.Equal(testByteArray, ValuePreprocessor.Preprocess(testByteArray, ValuePreprocessor.Type.INTEGER));
2025-01-27 18:21:22 +01:00
2025-08-07 22:31:12 +01:00
Assert.Equal("3.14159", ValuePreprocessor.Preprocess("3.14159", ValuePreprocessor.Type.FLOATING_POINT));
Assert.Equal(testByteArray, ValuePreprocessor.Preprocess(testByteArray, ValuePreprocessor.Type.FLOATING_POINT));
2025-01-27 18:21:22 +01:00
2025-08-07 22:31:12 +01:00
Assert.Equal("2.71828", ValuePreprocessor.Preprocess("2.71828", ValuePreprocessor.Type.UNDEFINED_NUMBER));
2025-01-27 18:21:22 +01:00
2025-08-07 22:31:12 +01:00
Assert.Equal("TestString", ValuePreprocessor.Preprocess("TestString", ValuePreprocessor.Type.STRING));
Assert.Equal(testByteArray, ValuePreprocessor.Preprocess(testByteArray, ValuePreprocessor.Type.STRING));
2025-01-27 18:21:22 +01:00
2025-08-07 22:31:12 +01:00
Assert.Equal("TestData", ValuePreprocessor.Preprocess("TestData", ValuePreprocessor.Type.DATA));
Assert.Equal(testByteArray, ValuePreprocessor.Preprocess(testByteArray, ValuePreprocessor.Type.DATA));
2025-01-27 18:21:22 +01:00
2025-08-07 22:31:12 +01:00
Assert.Equal(testByteArray, ValuePreprocessor.Preprocess(testByteArray, ValuePreprocessor.Type.DATE));
Assert.Equal("01.02.1903", ValuePreprocessor.Preprocess("01.02.1903", ValuePreprocessor.Type.DATE));
Assert.Equal(23.0, ValuePreprocessor.Preprocess(23.0, ValuePreprocessor.Type.DATE));
}
2025-01-21 20:14:00 +01:00
2025-08-07 22:31:12 +01:00
[Fact]
public static void TestRegisterPreprocessor()
{
lock(_testLock)
2025-01-21 20:14:00 +01:00
{
2025-08-07 22:31:12 +01:00
Func<string, string> examplePreprocessor = value => new string(value.Reverse().ToArray());
string testString = "TestString";
string expected = "gnirtStseT";
2025-08-07 22:31:12 +01:00
var testType = (ValuePreprocessor.Type)42;
2025-08-07 22:31:12 +01:00
ValuePreprocessor.Set(examplePreprocessor, testType);
string actual = ValuePreprocessor.Preprocess(testString, testType);
2025-08-07 22:31:12 +01:00
Assert.Equal(actual, expected);
2025-08-07 22:31:12 +01:00
ValuePreprocessor.Unset<string>(testType);
}
2025-08-07 22:31:12 +01:00
}
2025-08-07 22:31:12 +01:00
[Fact]
public static void TestRegisteredPreprocessorSelection1()
{
lock(_testLock)
{
2025-08-07 22:31:12 +01:00
Func<short, short> examplePreprocessor = value => (short)(value - 1);
short testShort = 42;
string testString = "TestString";
2025-08-07 22:31:12 +01:00
var testType = (ValuePreprocessor.Type)42;
2025-01-21 20:14:00 +01:00
2025-08-07 22:31:12 +01:00
// correct value type, differing data type
ValuePreprocessor.Set(examplePreprocessor, testType);
ValuePreprocessor.Set(ValuePreprocessor.GetDefault<string>(), testType);
2025-01-21 20:14:00 +01:00
2025-08-07 22:31:12 +01:00
string actual1 = ValuePreprocessor.Preprocess(testString, testType);
short actual2 = ValuePreprocessor.Preprocess(testShort, testType);
2025-01-21 20:14:00 +01:00
2025-08-07 22:31:12 +01:00
// assert unchanged, since the selected preprocessor != tested preprocessor
Assert.Equal(actual1, testString);
Assert.NotEqual(actual2, testShort);
2025-08-07 22:31:12 +01:00
ValuePreprocessor.Remove<short>(testType);
ValuePreprocessor.Remove<string>(testType);
2025-01-21 20:14:00 +01:00
}
2025-08-07 22:31:12 +01:00
}
2025-01-21 20:14:00 +01:00
2025-08-07 22:31:12 +01:00
[Fact]
public static void TestRegisteredPreprocessorSelection2()
{
lock(_testLock)
2025-01-21 20:14:00 +01:00
{
2025-08-07 22:31:12 +01:00
Func<string, string> examplePreprocessor = value => new string(value.Reverse().ToArray());
byte[] testByteArray = [0x42];
string testString = "TestString";
2025-08-07 22:31:12 +01:00
var testType = (ValuePreprocessor.Type)42;
2025-08-07 22:31:12 +01:00
// correct value type, differing data type
ValuePreprocessor.Set(examplePreprocessor, testType);
ValuePreprocessor.Set(ValuePreprocessor.GetDefault<byte[]>(), testType);
2025-08-07 22:31:12 +01:00
string actual1 = ValuePreprocessor.Preprocess(testString, testType);
byte[] actual2 = ValuePreprocessor.Preprocess(testByteArray, testType);
2025-01-21 20:14:00 +01:00
2025-08-07 22:31:12 +01:00
Assert.NotEqual(actual1, testString);
2025-01-21 20:14:00 +01:00
2025-08-07 22:31:12 +01:00
// assert unchanged, since the selected preprocessor != tested preprocessor
Assert.Equal(actual2, testByteArray);
2025-01-21 20:14:00 +01:00
2025-08-07 22:31:12 +01:00
ValuePreprocessor.Unset<string>(testType);
ValuePreprocessor.Remove<byte[]>(testType);
2025-01-21 20:14:00 +01:00
}
2025-08-07 22:31:12 +01:00
}
2025-01-21 20:14:00 +01:00
2025-08-07 22:31:12 +01:00
[Fact]
public static void TestUnregisteredPreprocessorThrows()
{
int[] testArray = [1, 2, 4, 8];
2025-01-21 20:14:00 +01:00
2025-08-07 22:31:12 +01:00
// there's no registered preprocessor for byte array arguments for STRING
Assert.Throws<ArgumentException>(() => ValuePreprocessor.Preprocess(testArray, ValuePreprocessor.Type.STRING));
2025-01-21 20:14:00 +01:00
}
}