176 lines
4.6 KiB
C#
176 lines
4.6 KiB
C#
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 = @$"<?xml version='1.0' encoding='utf-8'?>
|
|
<root>
|
|
<data name='Test1'>
|
|
<value>Value1</value>
|
|
</data>
|
|
<data name='Test2' type='{typeof(int).FullName}'>
|
|
<value>Value2</value>
|
|
</data>
|
|
</root>";
|
|
|
|
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> _ResourceManager = new Lazy<ResourceManager>(
|
|
() => new ResourceManager(_Type.FullName, _Type.Assembly),
|
|
LazyThreadSafetyMode.PublicationOnly
|
|
);
|
|
|
|
public static ResourceManager ResourceManager => _ResourceManager.Value;
|
|
|
|
#endregion // ResourceManager
|
|
|
|
|
|
#region Test1
|
|
|
|
private static readonly Lazy<IResourceString> LazyTest1 = new Lazy<IResourceString>(
|
|
() => new ResourceManagerString(""Test1"", ResourceManager, CultureInfo.CurrentCulture),
|
|
LazyThreadSafetyMode.PublicationOnly
|
|
);
|
|
|
|
public static IResourceString Test1 => LazyTest1.Value;
|
|
|
|
#endregion // Test1
|
|
|
|
#region Test2
|
|
|
|
private static readonly Lazy<IResourceString> LazyTest2 = new Lazy<IResourceString>(
|
|
() => new ResourceManagerString(""Test2"", ResourceManager, CultureInfo.CurrentCulture),
|
|
LazyThreadSafetyMode.PublicationOnly
|
|
);
|
|
|
|
public static IResourceString Test2 => LazyTest2.Value;
|
|
|
|
#endregion // Test2
|
|
|
|
}
|
|
}";
|
|
|
|
private readonly string emptyXml = @"<?xml version='1.0' encoding='utf-8'?><root></root>";
|
|
|
|
private readonly string invalidXml = @"<?xml version='1.0' encoding='utf-8'?><root></data>";
|
|
|
|
[TestMethod]
|
|
public void Test_TryParse_ValidXml()
|
|
{
|
|
// Arrange
|
|
var expectedData = new[] {
|
|
("Test1", "Value1", string.Empty),
|
|
("Test2", "Value2", typeof(int).FullName)
|
|
};
|
|
|
|
// 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))).ToList()
|
|
);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void Test_TryParse_EmptyXml()
|
|
{
|
|
// Arrange
|
|
var expectedData = Enumerable.Empty<Resource>();
|
|
|
|
// 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<Resource>();
|
|
|
|
// 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(
|
|
"#region Test2",
|
|
result.Select(i => i.Value.Substring(0, 25).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()
|
|
);
|
|
}
|
|
}
|
|
|
|
|