add version 0.0.1
This commit is contained in:
106
ResourceString.Net.Logic.Tests/LogicResourcesTests.cs
Normal file
106
ResourceString.Net.Logic.Tests/LogicResourcesTests.cs
Normal file
@@ -0,0 +1,106 @@
|
||||
using ResourceString.Net.Logic.Factories;
|
||||
|
||||
namespace ResourceString.Net.Logic.Tests;
|
||||
|
||||
[TestClass]
|
||||
public class LogicResourcesTests
|
||||
{
|
||||
|
||||
[TestMethod]
|
||||
public void Test_Get_ResourceStringMembers()
|
||||
{
|
||||
var rm = Properties.Resources.ResourceManager;
|
||||
Assert.AreEqual(
|
||||
new FormattedResourceString(
|
||||
Properties.Resources.ResourceStringMembers.Format,
|
||||
LiteralString.Factory("ResourceStringMembers"),
|
||||
LiteralString.Factory("ResourceManager")
|
||||
).Value,
|
||||
string.Format(
|
||||
rm.GetString("ResourceStringMembers") ?? string.Empty,
|
||||
"ResourceStringMembers",
|
||||
"ResourceManager"
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Test_Get_ResourceFormatClassMembers()
|
||||
{
|
||||
var rm = Properties.Resources.ResourceManager;
|
||||
Assert.AreEqual(
|
||||
new FormattedResourceString(
|
||||
Properties.Resources.ResourceFormatClassMembers.Format,
|
||||
LiteralString.Factory("ResourceFormatClassMembers"),
|
||||
LiteralString.Factory("ResourceManager"),
|
||||
LiteralString.Factory("Format")
|
||||
).Value,
|
||||
string.Format(
|
||||
rm.GetString("ResourceFormatClassMembers") ?? string.Empty,
|
||||
"ResourceFormatClassMembers",
|
||||
"ResourceManager",
|
||||
"Format"
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Test_Get_ResourceFormatClassFromMethod()
|
||||
{
|
||||
var rm = Properties.Resources.ResourceManager;
|
||||
Assert.AreEqual(
|
||||
new FormattedResourceString(
|
||||
Properties.Resources.ResourceFormatClassFromMethod.Format,
|
||||
LiteralString.Factory("IResourceString name"),
|
||||
LiteralString.Factory("name")
|
||||
).Value,
|
||||
string.Format(
|
||||
rm.GetString("ResourceFormatClassFromMethod") ?? string.Empty,
|
||||
"IResourceString name",
|
||||
"name"
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Test_Get_ResourceManagerMemberTemplate()
|
||||
{
|
||||
var rm = Properties.Resources.ResourceManager;
|
||||
Assert.AreEqual(
|
||||
CodeSnippetFactory.CreateResourceMangerMemberCodeSnippet(
|
||||
"Resources"
|
||||
).Value,
|
||||
string.Format(
|
||||
rm.GetString("ResourceManagerMemberTemplate") ?? string.Empty,
|
||||
"Resources"
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Test_Get_ResourcesClassTemplate()
|
||||
{
|
||||
var rm = Properties.Resources.ResourceManager;
|
||||
|
||||
var rmSnippet = CodeSnippetFactory.CreateResourceMangerMemberCodeSnippet(
|
||||
"Resources"
|
||||
);
|
||||
|
||||
Assert.AreEqual(
|
||||
CodeSnippetFactory.CreateResourceClassCodeSnippet(
|
||||
"ResourceString.Net.Logic.Properties",
|
||||
"Resources",
|
||||
rmSnippet,
|
||||
Enumerable.Empty<IResourceString>()
|
||||
).Value,
|
||||
string.Format(
|
||||
rm.GetString("ResourcesClassTemplate") ?? string.Empty,
|
||||
"ResourceString.Net.Logic.Properties",
|
||||
"Resources",
|
||||
rmSnippet.Value,
|
||||
string.Empty
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<LangVersion>latest</LangVersion>
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
|
||||
<PackageReference Include="MSTest.TestAdapter" Version="3.0.2" />
|
||||
<PackageReference Include="MSTest.TestFramework" Version="3.0.2" />
|
||||
<PackageReference Include="coverlet.collector" Version="3.2.0">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\ResourceString.Net.Logic\ResourceString.Net.Logic.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
192
ResourceString.Net.Logic.Tests/ResxFileTests.cs
Normal file
192
ResourceString.Net.Logic.Tests/ResxFileTests.cs
Normal file
@@ -0,0 +1,192 @@
|
||||
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>
|
||||
<comment>This is a greeting message.</comment>
|
||||
</data>
|
||||
<data name='Test2' type='{typeof(string).FullName}'>
|
||||
<value>{{2}}Value2{{0}}{{{{1}}}}</value>
|
||||
<comment>2 = prefix</comment>
|
||||
</data>
|
||||
<data name='Test3' type='{typeof(byte[]).FullName}'>
|
||||
<value>0xDEADBEEF</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 ?? string.Empty, _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
|
||||
|
||||
internal static class Test2
|
||||
{
|
||||
private static readonly Lazy<IResourceString> LazyFormat = new Lazy<IResourceString>(
|
||||
() => 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 = @"<?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, "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<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(
|
||||
"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()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
4
ResourceString.Net.Logic.Tests/Usings.cs
Normal file
4
ResourceString.Net.Logic.Tests/Usings.cs
Normal file
@@ -0,0 +1,4 @@
|
||||
global using LanguageExt;
|
||||
global using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
global using ResourceString.Net.Contract;
|
||||
global using ResourceString.Net.Logic;
|
||||
Reference in New Issue
Block a user