2025-01-21 20:14:00 +01:00
|
|
|
// plist-cil - An open source library to Parse and generate property lists for .NET
|
|
|
|
|
// Copyright (C) 2015 Natalia Portillo
|
|
|
|
|
//
|
|
|
|
|
// This code is based on:
|
|
|
|
|
// plist - An open source library to Parse and generate property lists
|
|
|
|
|
// Copyright (C) 2014 Daniel Dreibrodt
|
|
|
|
|
//
|
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using Claunia.PropertyList;
|
|
|
|
|
using Xunit;
|
|
|
|
|
|
|
|
|
|
namespace plistcil.test
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
public static class ValuePreprocessorTests
|
|
|
|
|
{
|
|
|
|
|
[Fact]
|
|
|
|
|
public static void TestPassiveDefaultPreprocessorsRegistered()
|
|
|
|
|
{
|
2025-01-23 17:04:01 +01:00
|
|
|
Assert.Equal(ValuePreprocessor.Preprocess(true, ValuePreprocessor.Type.BOOL), true);
|
|
|
|
|
Assert.Equal(ValuePreprocessor.Preprocess(false, ValuePreprocessor.Type.BOOL), false);
|
|
|
|
|
Assert.Equal(ValuePreprocessor.Preprocess("true", ValuePreprocessor.Type.BOOL), "true");
|
|
|
|
|
Assert.Equal(ValuePreprocessor.Preprocess("42",ValuePreprocessor.Type.INTEGER), "42");
|
|
|
|
|
Assert.Equal(ValuePreprocessor.Preprocess("3.14159",ValuePreprocessor.Type.FLOATING_POINT), "3.14159");
|
|
|
|
|
Assert.Equal(ValuePreprocessor.Preprocess("2.71828", ValuePreprocessor.Type.UNDEFINED_NUMBER), "2.71828");
|
|
|
|
|
Assert.Equal(ValuePreprocessor.Preprocess("TestString",ValuePreprocessor.Type.STRING), "TestString");
|
|
|
|
|
Assert.Equal(ValuePreprocessor.Preprocess("TestData",ValuePreprocessor.Type.DATA), "TestData");
|
2025-01-21 20:14:00 +01:00
|
|
|
byte[] value = { 0x1, 0x2, 0x4, 0x8 };
|
2025-01-23 17:04:01 +01:00
|
|
|
Assert.Equal(ValuePreprocessor.Preprocess(value,ValuePreprocessor.Type.DATA), value);
|
|
|
|
|
Assert.Equal(ValuePreprocessor.Preprocess("01.02.1903",ValuePreprocessor.Type.DATE), "01.02.1903");
|
|
|
|
|
Assert.Equal(ValuePreprocessor.Preprocess(23.0, ValuePreprocessor.Type.DATE), 23.0);
|
2025-01-21 20:14:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public static void TestRegisterPreprocessor()
|
|
|
|
|
{
|
|
|
|
|
Func<string, string> examplePreprocessor = value => new string(value.Reverse().ToArray());
|
|
|
|
|
string testString = "TestString";
|
|
|
|
|
string expected = "gnirtStseT";
|
|
|
|
|
|
2025-01-23 17:04:01 +01:00
|
|
|
ValuePreprocessor.Register(examplePreprocessor, ValuePreprocessor.Type.STRING);
|
|
|
|
|
string actual = ValuePreprocessor.Preprocess(testString, ValuePreprocessor.Type.STRING);
|
2025-01-21 20:14:00 +01:00
|
|
|
|
|
|
|
|
Assert.Equal(actual, expected);
|
|
|
|
|
|
2025-01-23 17:04:01 +01:00
|
|
|
ValuePreprocessor.Unregister<string>(ValuePreprocessor.Type.STRING);
|
2025-01-21 20:14:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public static void TestRegisteredPreprocessorSelection()
|
|
|
|
|
{
|
|
|
|
|
Func<string, string> examplePreprocessor = value => new string(value.Reverse().ToArray());
|
|
|
|
|
string testString = "TestString";
|
|
|
|
|
|
2025-01-23 17:04:01 +01:00
|
|
|
ValuePreprocessor.Register(examplePreprocessor, ValuePreprocessor.Type.STRING);
|
|
|
|
|
string actual = ValuePreprocessor.Preprocess(testString, ValuePreprocessor.Type.DATA);
|
2025-01-21 20:14:00 +01:00
|
|
|
|
|
|
|
|
// assert unchanged, since the selected preprocessor != registered preprocessor
|
|
|
|
|
Assert.Equal(actual, testString);
|
|
|
|
|
|
2025-01-23 17:04:01 +01:00
|
|
|
ValuePreprocessor.Unregister<string>(ValuePreprocessor.Type.STRING);
|
2025-01-21 20:14:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public static void TestUnregisterdPreprocessorThrows()
|
|
|
|
|
{
|
|
|
|
|
byte[] testArray = { 0x1, 0x2, 0x4, 0x8 };
|
|
|
|
|
|
|
|
|
|
// there's no registered preprocessor for byte array arguments for STRING
|
2025-01-23 17:04:01 +01:00
|
|
|
Assert.Throws<ArgumentException>(() => ValuePreprocessor.Preprocess(testArray, ValuePreprocessor.Type.STRING));
|
2025-01-21 20:14:00 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|