using Amphora; using System; using System.IO; using UnitTestSharp; namespace Susurrus.UnitTests { public class DirectorySoundSourceTests : TestFixture { public class Folder : IDisposable { public readonly string Path; public Folder() { Path = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "SusurrusTests-" + Guid.NewGuid()); Directory.CreateDirectory(Path); } public void Put(string relativePath, params byte[] bytes) { string full = System.IO.Path.Combine(Path, relativePath); Directory.CreateDirectory(System.IO.Path.GetDirectoryName(full)); File.WriteAllBytes(full, bytes); } public void Dispose() { Directory.Delete(Path, recursive: true); } } public class CtorTests : TestFixture { public void NullRoot_Throws() { CheckThrow(typeof(ArgumentException)); new DirectorySoundSource(null); } public void EmptyRoot_Throws() { CheckThrow(typeof(ArgumentException)); new DirectorySoundSource(""); } } public class ReadTests : TestFixture { public void NullPath_Throws() { CheckThrow(typeof(ArgumentNullException)); new DirectorySoundSource("anywhere").Read(null); } public void AFileInASubfolder_IsFoundByAPathWithForwardSlashes() { using (Folder folder = new Folder()) { folder.Put(System.IO.Path.Combine("Sounds", "a.ogg"), 4, 5, 6); StatusOr read = new DirectorySoundSource(folder.Path).Read("Sounds/a.ogg"); CheckTrue(read.IsOk); CheckEqual(3, read.Value.Length); CheckEqual((byte)5, read.Value[1]); } } public void AMissingFile_FailsNamingThePath() { using (Folder folder = new Folder()) { StatusOr read = new DirectorySoundSource(folder.Path).Read("Sounds/missing.ogg"); CheckFalse(read.IsOk); CheckTrue(read.Status.Describe().StartsWith("A sound file couldn't be read. Sounds/missing.ogg: ")); } } public void AFolderWhereAFileWasAsked_Fails() { using (Folder folder = new Folder()) { Directory.CreateDirectory(System.IO.Path.Combine(folder.Path, "Sounds")); CheckFalse(new DirectorySoundSource(folder.Path).Read("Sounds").IsOk); } } } } }