-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Core.Models.ShotTypeAttribute<T>
- Loading branch information
Showing
12 changed files
with
717 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
ThScoreFileConverter.Core.Tests/Extensions/TypeExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System; | ||
using System.Linq; | ||
using ThScoreFileConverter.Core.Extensions; | ||
|
||
internal static class Global; | ||
|
||
namespace ThScoreFileConverter.Core.Tests.Extensions | ||
{ | ||
[TestClass] | ||
public class TypeExtensionsTests | ||
{ | ||
[TestMethod] | ||
public void GetLeafNamespaceTest() | ||
{ | ||
Assert.AreEqual("System", typeof(Math).GetLeafNamespace()); | ||
Assert.AreEqual("Linq", typeof(Enumerable).GetLeafNamespace()); | ||
Assert.AreEqual("Extensions", typeof(TypeExtensionsTests).GetLeafNamespace()); | ||
} | ||
|
||
[TestMethod] | ||
public void GetLeafNamespaceTestNull() | ||
{ | ||
Type type = null!; | ||
_ = Assert.ThrowsException<ArgumentNullException>(type.GetLeafNamespace); | ||
} | ||
|
||
[TestMethod] | ||
public void GetLeafNamespaceTestTwice() | ||
{ | ||
Assert.AreEqual("System", typeof(Math).GetLeafNamespace()); | ||
Assert.AreEqual("System", typeof(Math).GetLeafNamespace()); // cached | ||
} | ||
|
||
[TestMethod] | ||
public void GetLeafNamespaceTestGlobal() | ||
{ | ||
Assert.AreEqual(string.Empty, typeof(Global).GetLeafNamespace()); | ||
} | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
ThScoreFileConverter.Core.Tests/Models/ShotTypeAttributeTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using ThScoreFileConverter.Core.Models; | ||
using ThScoreFileConverter.Core.Models.Th06; | ||
using ThScoreFileConverter.Core.Resources; | ||
|
||
namespace ThScoreFileConverter.Core.Tests.Models; | ||
|
||
[TestClass] | ||
public class ShotTypeAttributeTests | ||
{ | ||
[TestMethod] | ||
public void ShotTypeAttributeTest() | ||
{ | ||
var attribute = new ShotTypeAttribute<Chara>(Chara.ReimuA); | ||
|
||
Assert.IsNotNull(attribute); | ||
Assert.AreEqual("Th06.ReimuA", attribute.Name); | ||
Assert.AreEqual("Th06.ReimuAFullName", attribute.FullName); | ||
Assert.AreEqual(typeof(ShotTypeNames), attribute.ResourceType); | ||
Assert.AreEqual(Chara.ReimuA, attribute.Value); | ||
} | ||
|
||
public static IEnumerable<object[]> InvalidCharacters => TestHelper.GetInvalidEnumerators<Chara>(); | ||
|
||
[DataTestMethod] | ||
[DynamicData(nameof(InvalidCharacters))] | ||
public void ShotTypeAttributeTestInvalid(int chara) | ||
{ | ||
_ = Assert.ThrowsException<ArgumentException>(() => new ShotTypeAttribute<Chara>((Chara)chara)); | ||
} | ||
|
||
[TestMethod] | ||
public void GetLocalizedNameTest() | ||
{ | ||
using var backup = TestHelper.BackupCultureInfo(); | ||
|
||
var attribute = new ShotTypeAttribute<Chara>(Chara.MarisaA); | ||
|
||
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("ja-JP"); | ||
Assert.AreEqual("魔", attribute.GetLocalizedName()); | ||
|
||
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("en-US"); | ||
Assert.AreEqual("Magic", attribute.GetLocalizedName()); | ||
} | ||
|
||
[TestMethod] | ||
public void GetLocalizedNameTestUnregistered() | ||
{ | ||
using var backup = TestHelper.BackupCultureInfo(); | ||
|
||
var attribute = new ShotTypeAttribute<DayOfWeek>(DayOfWeek.Sunday); | ||
|
||
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("ja-JP"); | ||
Assert.AreEqual("System.Sunday", attribute.GetLocalizedName()); | ||
|
||
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("en-US"); | ||
Assert.AreEqual("System.Sunday", attribute.GetLocalizedName()); | ||
} | ||
|
||
[TestMethod] | ||
public void GetLocalizedFullNameTest() | ||
{ | ||
using var backup = TestHelper.BackupCultureInfo(); | ||
|
||
var attribute = new ShotTypeAttribute<Chara>(Chara.MarisaA); | ||
|
||
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("ja-JP"); | ||
Assert.AreEqual("魔符", attribute.GetLocalizedFullName()); | ||
|
||
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("en-US"); | ||
Assert.AreEqual("Magic Sign", attribute.GetLocalizedFullName()); | ||
} | ||
|
||
[TestMethod] | ||
public void GetLocalizedFullNameTestUnregistered() | ||
{ | ||
using var backup = TestHelper.BackupCultureInfo(); | ||
|
||
var attribute = new ShotTypeAttribute<DayOfWeek>(DayOfWeek.Sunday); | ||
|
||
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("ja-JP"); | ||
Assert.AreEqual("System.SundayFullName", attribute.GetLocalizedFullName()); | ||
|
||
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("en-US"); | ||
Assert.AreEqual("System.SundayFullName", attribute.GetLocalizedFullName()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
//----------------------------------------------------------------------- | ||
// <copyright file="TypeExtensions.cs" company="None"> | ||
// Copyright (c) IIHOSHI Yoshinori. | ||
// Licensed under the BSD-2-Clause license. See LICENSE.txt file in the project root for full license information. | ||
// </copyright> | ||
//----------------------------------------------------------------------- | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using CommunityToolkit.Diagnostics; | ||
|
||
namespace ThScoreFileConverter.Core.Extensions; | ||
|
||
/// <summary> | ||
/// Provides some extension methods for <see cref="Type"/>. | ||
/// </summary> | ||
public static class TypeExtensions | ||
{ | ||
private static readonly Dictionary<Type, string> LeafNamespaceCache = []; | ||
|
||
/// <summary> | ||
/// Gets the name of the "leaf" namespace of <paramref name="type"/>. | ||
/// </summary> | ||
/// <param name="type">The type.</param> | ||
/// <returns>The name of the "leaf" namespace of <paramref name="type"/>.</returns> | ||
public static string GetLeafNamespace(this Type type) | ||
{ | ||
Guard.IsNotNull(type); | ||
|
||
if (LeafNamespaceCache.TryGetValue(type, out var value)) | ||
{ | ||
return value; | ||
} | ||
|
||
if (string.IsNullOrEmpty(type.Namespace)) | ||
{ | ||
value = string.Empty; | ||
} | ||
else | ||
{ | ||
var index = type.Namespace.LastIndexOf(Type.Delimiter); | ||
value = (index >= 0) ? type.Namespace[(index + 1)..] : type.Namespace; | ||
} | ||
|
||
LeafNamespaceCache.Add(type, value); | ||
return value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
//----------------------------------------------------------------------- | ||
// <copyright file="ShotTypeAttribute{T}.cs" company="None"> | ||
// Copyright (c) IIHOSHI Yoshinori. | ||
// Licensed under the BSD-2-Clause license. See LICENSE.txt file in the project root for full license information. | ||
// </copyright> | ||
//----------------------------------------------------------------------- | ||
|
||
using System; | ||
using CommunityToolkit.Diagnostics; | ||
using ThScoreFileConverter.Core.Extensions; | ||
using ThScoreFileConverter.Core.Resources; | ||
|
||
namespace ThScoreFileConverter.Core.Models; | ||
|
||
/// <summary> | ||
/// Provides names of the playable character's shot type represented as an enumeration field. | ||
/// </summary> | ||
/// <typeparam name="T">The enumeration type representing playable characters' shot types.</typeparam> | ||
[CLSCompliant(false)] | ||
public sealed class ShotTypeAttribute<T> : EnumDisplayAttribute | ||
where T : struct, Enum | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ShotTypeAttribute{T}"/> class. | ||
/// </summary> | ||
/// <param name="value">An enumeration field representing a playable character's shot type.</param> | ||
public ShotTypeAttribute(T value) | ||
: base($"{typeof(T).GetLeafNamespace()}.{value}", typeof(ShotTypeNames)) | ||
{ | ||
Guard.IsTrue(Enum.IsDefined(value)); | ||
this.Value = value; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the value of the playable character's shot type. | ||
/// </summary> | ||
public T Value { get; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.