using ResourceString.Net.Logic.DomainObjects.Resx;
using ResourceString.Net.Logic.Factories;
using ResourceString.Net.Logic.Parsers.Resx;
namespace ResourceString.Net.Logic.Tests;
[TestClass]
public class ResxFileTests
{
private readonly string validXml = @$"
Value1
This is a greeting message.
{{2}}Value2{{0}}{{{{1}}}}
2 = prefix
0xDEADBEEF
";
private const string expectedClass = @"
using ResourceString.Net.Contract;
using System;
using System.Globalization;
using System.Resources;
using System.Threading;
namespace TestNameSpace
{
internal static class TestResourceClass
{
#region ResourceManager
private static readonly Type _Type = typeof(ResourceManager);
private static readonly Lazy _ResourceManager = new Lazy(
() => new ResourceManager(_Type.FullName ?? string.Empty, _Type.Assembly),
LazyThreadSafetyMode.PublicationOnly
);
public static ResourceManager ResourceManager => _ResourceManager.Value;
#endregion // ResourceManager
#region Test1
private static readonly Lazy LazyTest1 = new Lazy(
() => new ResourceManagerString(""Test1"", ResourceManager, CultureInfo.CurrentCulture),
LazyThreadSafetyMode.PublicationOnly
);
public static IResourceString Test1 => LazyTest1.Value;
#endregion // Test1
internal static class Test2
{
private static readonly Lazy LazyFormat = new Lazy(
() => new ResourceManagerString(""Test2"", ResourceManager, CultureInfo.CurrentCulture),
LazyThreadSafetyMode.PublicationOnly
);
public static IResourceString Format => LazyFormat.Value;
public static IResourceString From(IResourceString prefix, IResourceString p1) => new FormattedResourceString(
Format,
prefix,
p1
);
}
}
}";
private readonly string emptyXml = @"";
private readonly string invalidXml = @"";
[TestMethod]
public void Test_TryParse_ValidXml()
{
// Arrange
var expectedData = new[] {
("Test1", "Value1", string.Empty, "This is a greeting message."),
("Test2", "{2}Value2{0}{{1}}", typeof(string).FullName, "2 = prefix"),
("Test3", "0xDEADBEEF", typeof(byte[]).FullName, string.Empty)
};
// Act
var result = Parser.TryParse(validXml).Match(
Some: v => v.Resources,
None: () => throw new InvalidOperationException()
);
// Assert
CollectionAssert.AreEqual(
expectedData,
result.Select(i => (
i.Name,
i.Value,
i.Type.IfNone(string.Empty),
i.Comment.IfNone(string.Empty)
)).ToList()
);
}
[TestMethod]
public void Test_TryParse_EmptyXml()
{
// Arrange
var expectedData = Enumerable.Empty();
// Act
var isSome = Parser.TryParse(emptyXml).Match(
Some: _ => true,
None: () => false
);
// Assert
Assert.IsFalse(isSome);
}
[TestMethod]
public void Test_TryParse_InValidXml()
{
// Arrange
var expectedData = Enumerable.Empty();
// Act
var isSome = Parser.TryParse(invalidXml).Match(
Some: _ => true,
None: () => false
);
// Assert
Assert.IsFalse(isSome);
}
[TestMethod]
public void Test_ToMemberString()
{
// Act
var result = Parser.TryParse(validXml).Match(
Some: v => CodeSnippetFactory.CreateMemberCodeSnippets(v.Resources),
None: () => throw new InvalidOperationException()
);
// Assert
Assert.AreEqual(2, result.Count());
Assert.AreEqual(
"#region Test1",
result.Select(i => i.Value.Substring(0, 25).Trim()).ToList()[0]
);
Assert.AreEqual(
"internal static class Test2",
result.Select(i => i.Value.Substring(0, 45).Trim()).ToList()[1]
);
}
[TestMethod]
public void Test_ToClassString()
{
// Act
var result = Parser.TryParse(validXml).Match(
Some: v => CodeSnippetFactory.CreateResourceClassCodeSnippet(
"TestNameSpace",
"TestResourceClass",
CodeSnippetFactory.CreateResourceMangerMemberCodeSnippet("ResourceManager"),
v.Resources
),
None: () => throw new InvalidOperationException()
);
// Assert
Assert.AreEqual(
expectedClass.Trim(),
result.Value.Trim()
);
}
}