public void MultipleFile_PropertyHasInvalidType_Throws() { Assert.Throws <InvalidOperationException>( () => ContentLoader .For <MultipleFileInvalidTypeContent>() .LoadFromDirectory(tempDirectory)); }
public void MultipleRequiredFile_Throws() { Assert.Throws <FileNotFoundException>( () => ContentLoader .For <MultipleRequiredFileContent>() .LoadFromDirectory(tempDirectory)); }
public void OptionalFile_DoesNotOverwriteInitialValue() { using var content = ContentLoader .For <OptionalFileWithInitialValue>() .LoadFromDirectory(tempDirectory); Assert.Equal("abc", content.File); }
public void MultipleOptionalFiles_DoesNotOverwriteInitialValue() { using var content = ContentLoader .For <MultipleOptionalFilesWithInitialValue>() .LoadFromDirectory(tempDirectory); Assert.Equal(new [] { "abc" }, content.Files); }
public void DeserializerNotExists_ThrowsAndFreeResources() { File.WriteAllText(Path.Combine(tempDirectory, "file.xml"), "<xml />", Encoding.UTF8); Assert.Throws <InvalidOperationException>(() => ContentLoader .For <DeserializerNotExistsContent>() .LoadFromDirectory(tempDirectory)); }
public void LoadRequiredString_FileNotExists_Throws() { var content = ContentLoader .For <LoadRequiredStringContent>() .WithDeserializer(s => new StreamReader(s, Encoding.UTF8).ReadToEnd()); Assert.Throws <FileNotFoundException>(() => content.LoadFromDirectory(tempDirectory)); }
public void MultipleOptionalFile_CreateEmptyList() { using var content = ContentLoader .For <MultipleOptionalFiles>() .LoadFromDirectory(tempDirectory); Assert.NotNull(content.Files); Assert.Empty(content.Files); }
public void LoadOptionalString_FileNotExists_Success() { using var content = ContentLoader .For <LoadOptionalStringContent>() .WithDeserializer(s => new StreamReader(s, Encoding.UTF8).ReadToEnd()) .LoadFromDirectory(tempDirectory); Assert.Null(content.StringProperty); }
public void FileNotFound_ThrowsAndFreeResources() { File.WriteAllText(Path.Combine(tempDirectory, "a.txt"), "AAA", Encoding.UTF8); File.WriteAllText(Path.Combine(tempDirectory, "c.txt"), "CCC", Encoding.UTF8); Assert.Throws <FileNotFoundException>(() => ContentLoader .For <FileNotFoundContent>() .WithDeserializer(s => new StreamReader(s, Encoding.UTF8).ReadToEnd()) .LoadFromDirectory(tempDirectory)); }
public void LoadOptionalString_FileExists_Success() { File.WriteAllText(Path.Combine(tempDirectory, "file.txt"), "Hello!", Encoding.UTF8); using var content = ContentLoader .For <LoadOptionalStringContent>() .WithDeserializer(s => new StreamReader(s, Encoding.UTF8).ReadToEnd()) .LoadFromDirectory(tempDirectory); Assert.Equal("Hello!", content.StringProperty); }
public void MultipleFiles() { File.WriteAllText(Path.Combine(tempDirectory, "1file.txt"), "1", Encoding.UTF8); File.WriteAllText(Path.Combine(tempDirectory, "2file.txt"), "2", Encoding.UTF8); File.WriteAllText(Path.Combine(tempDirectory, "3file.txt"), "3", Encoding.UTF8); using var content = ContentLoader .For <MultipleFilesContent>() .WithDeserializer(s => s.ReadString(Encoding.UTF8)) .LoadFromDirectory(tempDirectory); Assert.Equal(new[] { "1", "2", "3" }, content.Files); }
public void InjectPath() { var filePath = Path.GetFullPath(Path.Combine(tempDirectory, "file.txt")); File.WriteAllText(filePath, "Hello!", Encoding.UTF8); using var content = ContentLoader .For <InjectPathContent>() .WithDeserializer(s => new StreamReader(s, Encoding.UTF8).ReadToEnd()) .LoadFromDirectory(tempDirectory); Assert.Equal(filePath, content.FilePath); }
public void LoadGlobalString_FileInParentDirectory_Success() { var parentDirectory = tempDirectory; var contentDirectory = Path.Combine(parentDirectory, "content_directory"); Directory.CreateDirectory(contentDirectory); File.WriteAllText(Path.Combine(parentDirectory, "file.txt"), "Hello!", Encoding.UTF8); using var content = ContentLoader .For <LoadGlobalStringContent>() .WithDeserializer(s => new StreamReader(s, Encoding.UTF8).ReadToEnd()) .LoadFromDirectory(contentDirectory); Assert.Equal("Hello!", content.StringProperty); }
public void LoadGlobalString_FileInAncestor20Directory_Success() { var ancestorDirectory = tempDirectory; var currentContentDirectory = ancestorDirectory; for (var i = 0; i < 20; i++) { currentContentDirectory = Path.Combine(currentContentDirectory, $"dir_{i}"); Directory.CreateDirectory(currentContentDirectory); } File.WriteAllText(Path.Combine(tempDirectory, "file.txt"), "Hello!", Encoding.UTF8); using var content = ContentLoader .For <LoadGlobalStringContent>() .WithDeserializer(s => new StreamReader(s, Encoding.UTF8).ReadToEnd()) .LoadFromDirectory(currentContentDirectory); Assert.Equal("Hello!", content.StringProperty); }
public void TestApplyRulesToNumbers() { PrepareDirectory(); // arrange using var content = ContentLoader .For <MyContent>() .WithDeserializer(s => s.ReadString().Split(",").Select(int.Parse).ToArray()) // for numbers .WithDeserializer(s => s.ReadString().Split("\n")) // for rules .LoadFromDirectory(testDirectory); // act var actualNumbers = ApplyRulesToNumbers(content.Numbers, content.Rules); // assert ContentVerifier .UseDirectory(testDirectory) .SaveActualAs("actual.num", s => s.WriteString(string.Join(",", actualNumbers))) .ReadExpectedAs("expected.num", s => s.ReadString().Split(",").Select(int.Parse).ToArray()) .Verify(expectedNumbers => Assert.Equal(expectedNumbers, actualNumbers)); }
public void InjectMultiplePaths() { var paths = new[] { "1file.txt", "2file.txt", "3file.txt" } .Select(fileName => Path.GetFullPath(Path.Combine(tempDirectory, fileName))) .ToList(); foreach (var path in paths) { File.WriteAllText(path, "x", Encoding.UTF8); } using var content = ContentLoader .For <InjectMultiplePathsContent>() .LoadFromDirectory(tempDirectory); Assert.Equal(paths, content.Files); }
public void DifferentDeserializers_Success() { File.WriteAllText(Path.Combine(tempDirectory, "file.xml"), "<xml />", Encoding.UTF8); File.WriteAllText(Path.Combine(tempDirectory, "array.txt"), "1,2,3", Encoding.UTF8); File.WriteAllBytes(Path.Combine(tempDirectory, "bytes.bin"), new byte[] { 1, 2, 3 }); using var content = ContentLoader .For <DifferentDeserializersContent>() .WithDeserializer(XDocument.Load) .WithDeserializer(s => new StreamReader(s).ReadToEnd().Split(",").Select(int.Parse).ToArray()) .WithDeserializer(s => { var ms = new MemoryStream(); s.CopyTo(ms); return(ms.ToArray()); }) .LoadFromDirectory(tempDirectory); Assert.Equal("xml", content.Xml.Root.Name.LocalName); Assert.Equal(new[] { 1, 2, 3 }, content.Array); Assert.Equal(new byte[] { 1, 2, 3 }, content.Bytes); }