using Amphora; using System; using System.Collections.Generic; using System.Linq; using UnitTestSharp; namespace Susurrus.UnitTests { public class SoundPackTests : TestFixture { public class Unmarked { public static readonly SoundSpec Hum = new SoundSpec("Effects", "hum.ogg"); } [SoundPack("Test/Sounds/")] public static class TwoSounds { public static readonly SoundSpec Hum = new SoundSpec("Effects", "hum.ogg"); public static readonly SoundSpec Click = new SoundSpec("Effects", new[] { "click.ogg", "tap.ogg" }); public static readonly string NotASpec = "ignored"; } [SoundPack("Test")] public static class SharedFiles { public static readonly SoundSpec Quiet = new SoundSpec("Effects", "hum.ogg", baseVolume: 0.2f); public static readonly SoundSpec Loud = new SoundSpec("Effects", "hum.ogg"); } [SoundPack("Test")] public static class HiddenField { private static readonly SoundSpec Secret = new SoundSpec("Effects", "secret.ogg"); public static SoundSpec Reveal() { return Secret; } } [SoundPack("Test")] public static class NoSpecs { public static readonly int Count = 3; } [SoundPack("Test")] public static class NullSpec { public static readonly SoundSpec Missing = null; } public class FolderOfTests : TestFixture { public void NullType_Throws() { CheckThrow(typeof(ArgumentNullException)); SoundPack.FolderOf(null); } public void ATypeWithoutTheAttribute_Throws() { CheckThrow(typeof(ArgumentException)); SoundPack.FolderOf(typeof(Unmarked)); } public void GivesTheFolderWithoutItsTrailingSlash() { CheckEqual("Test/Sounds", SoundPack.FolderOf(typeof(TwoSounds))); } } public class SpecsOfTests : TestFixture { public void GivesEverySpecFieldWithItsName() { IReadOnlyList> specs = SoundPack.SpecsOf(typeof(TwoSounds)); CheckEqual(2, specs.Count); Check(TwoSounds.Hum == specs.Single(pair => pair.Key == "Hum").Value); Check(TwoSounds.Click == specs.Single(pair => pair.Key == "Click").Value); } public void FindsAFieldThatIsNotPublic() { IReadOnlyList> specs = SoundPack.SpecsOf(typeof(HiddenField)); CheckEqual(1, specs.Count); CheckEqual("Secret", specs[0].Key); Check(HiddenField.Reveal() == specs[0].Value); } public void ATypeWithoutTheAttribute_Throws() { CheckThrow(typeof(ArgumentException)); SoundPack.SpecsOf(typeof(Unmarked)); } public void ATypeWithNoSpecFields_Throws() { CheckThrow(typeof(ArgumentException)); SoundPack.SpecsOf(typeof(NoSpecs)); } public void AFieldWithoutASpec_Throws() { CheckThrow(typeof(ArgumentException)); SoundPack.SpecsOf(typeof(NullSpec)); } } public class FilesOfTests : TestFixture { public void PutsThePacksFolderInFrontOfEachFile() { IReadOnlyList files = SoundPack.FilesOf(typeof(TwoSounds)); CheckEqual(3, files.Count); CheckTrue(files.Contains("Test/Sounds/hum.ogg")); CheckTrue(files.Contains("Test/Sounds/click.ogg")); CheckTrue(files.Contains("Test/Sounds/tap.ogg")); } public void ListsAFileTwoSpecsShareOnce() { IReadOnlyList files = SoundPack.FilesOf(typeof(SharedFiles)); CheckEqual(1, files.Count); CheckEqual("Test/hum.ogg", files[0]); } public void ATypeWithoutTheAttribute_Throws() { CheckThrow(typeof(ArgumentException)); SoundPack.FilesOf(typeof(Unmarked)); } } public class CtorTests : TestFixture { public void NullIds_Throws() { CheckThrow(typeof(ArgumentNullException)); new SoundPack(null, new Sound[0]); } public void NullClips_Throws() { CheckThrow(typeof(ArgumentNullException)); new SoundPack(new Dictionary(), null); } public void ChangingTheIdsAfterwards_ChangesNothing() { var ids = new Dictionary { { TwoSounds.Hum, new SoundId(4) } }; SoundPack pack = new SoundPack(ids, new Sound[0]); ids.Clear(); CheckEqual(new SoundId(4), pack.IdOf(TwoSounds.Hum)); } public void ChangingTheClipsAfterwards_ChangesNothing() { Sound clip = SoundDefinitionTests.Clip(); var clips = new List { clip }; SoundPack pack = new SoundPack(new Dictionary(), clips); clips.Clear(); pack.Dispose(); CheckTrue(clip.IsDisposed); } } public class IdOfTests : TestFixture { public void NullSpec_Throws() { CheckThrow(typeof(ArgumentNullException)); new SoundPack(new Dictionary(), new Sound[0]).IdOf(null); } public void ASpecThatIsNotInThePack_Throws() { SoundPack pack = new SoundPack( new Dictionary { { TwoSounds.Hum, new SoundId(0) } }, new Sound[0]); CheckThrow(typeof(ArgumentException)); pack.IdOf(TwoSounds.Click); } public void GivesTheIdEachSpecWasDefinedAs() { SoundPack pack = new SoundPack( new Dictionary { { TwoSounds.Hum, new SoundId(3) }, { TwoSounds.Click, new SoundId(5) }, }, new Sound[0]); CheckEqual(new SoundId(3), pack.IdOf(TwoSounds.Hum)); CheckEqual(new SoundId(5), pack.IdOf(TwoSounds.Click)); } } public class DisposeTests : TestFixture { public void DisposesEveryClip() { Sound first = SoundDefinitionTests.Clip(); Sound second = SoundDefinitionTests.Clip(); SoundPack pack = new SoundPack(new Dictionary(), new[] { first, second }); pack.Dispose(); CheckTrue(first.IsDisposed); CheckTrue(second.IsDisposed); } public void CalledTwice_DoesNothingTheSecondTime() { SoundPack pack = new SoundPack( new Dictionary(), new[] { SoundDefinitionTests.Clip() }); pack.Dispose(); pack.Dispose(); } } } }