A game framework written with osu! in mind.
at master 89 lines 2.7 kB view raw
1// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. 2// See the LICENCE file in the repository root for full licence text. 3 4using System; 5using NUnit.Framework; 6using osu.Framework.Extensions; 7using osu.Framework.Localisation; 8 9namespace osu.Framework.Tests.Localisation 10{ 11 [TestFixture] 12 public class LocalisableDescriptionAttributeTest 13 { 14 [TestCase(EnumA.Item1, "Item1")] 15 [TestCase(EnumA.Item2, "B")] 16 public void TestNonLocalisableDescriptionReturnsDescriptionOrToString(EnumA value, string expected) 17 { 18 Assert.That(value.GetLocalisableDescription().ToString(), Is.EqualTo(expected)); 19 } 20 21 [TestCase(EnumB.Item1, "Localised A")] 22 [TestCase(EnumB.Item2, "Localised B")] 23 public void TestLocalisableDescriptionReturnsDesignatedString(EnumB value, string expected) 24 { 25 Assert.That(value.GetLocalisableDescription().ToString(), Is.EqualTo(expected)); 26 } 27 28 [Test] 29 public void TestClassLocalisableDescription() 30 { 31 Assert.That(new ClassA().GetLocalisableDescription().ToString(), Is.EqualTo("Localised A")); 32 } 33 34 [Test] 35 public void TestLocalisableDescriptionWithNonExistingMemberThrows() 36 { 37 Assert.Throws<InvalidOperationException>(() => EnumC.Item1.GetLocalisableDescription()); 38 } 39 40 [Test] 41 public void TestLocalisableDescriptionWithInstanceMemberThrows() 42 { 43 Assert.Throws<InvalidOperationException>(() => EnumD.Item1.GetLocalisableDescription()); 44 } 45 46 public enum EnumA 47 { 48 Item1, 49 50 [System.ComponentModel.Description("B")] 51 Item2 52 } 53 54 public enum EnumB 55 { 56 [LocalisableDescription(typeof(TestStrings), nameof(TestStrings.A))] 57 Item1, 58 59 [LocalisableDescription(typeof(TestStrings), nameof(TestStrings.B))] 60 Item2 61 } 62 63 public enum EnumC 64 { 65 [LocalisableDescription(typeof(LocalisableDescriptionAttributeTest), nameof(TestStrings.A))] 66 Item1, 67 } 68 69 public enum EnumD 70 { 71 [LocalisableDescription(typeof(TestStrings), nameof(TestStrings.Instance))] 72 Item1, 73 } 74 75 [LocalisableDescription(typeof(TestStrings), nameof(TestStrings.A))] 76 public class ClassA 77 { 78 } 79 80 private class TestStrings 81 { 82 public static LocalisableString A => "Localised A"; 83 84 public static readonly LocalisableString B = "Localised B"; 85 86 public LocalisableString Instance => string.Empty; 87 } 88 } 89}