示例#1
0
 public void MultipleFile_PropertyHasInvalidType_Throws()
 {
     Assert.Throws <InvalidOperationException>(
         () => ContentLoader
         .For <MultipleFileInvalidTypeContent>()
         .LoadFromDirectory(tempDirectory));
 }
示例#2
0
 public void MultipleRequiredFile_Throws()
 {
     Assert.Throws <FileNotFoundException>(
         () => ContentLoader
         .For <MultipleRequiredFileContent>()
         .LoadFromDirectory(tempDirectory));
 }
示例#3
0
        public void OptionalFile_DoesNotOverwriteInitialValue()
        {
            using var content = ContentLoader
                                .For <OptionalFileWithInitialValue>()
                                .LoadFromDirectory(tempDirectory);

            Assert.Equal("abc", content.File);
        }
示例#4
0
        public void MultipleOptionalFiles_DoesNotOverwriteInitialValue()
        {
            using var content = ContentLoader
                                .For <MultipleOptionalFilesWithInitialValue>()
                                .LoadFromDirectory(tempDirectory);

            Assert.Equal(new [] { "abc" }, content.Files);
        }
示例#5
0
        public void DeserializerNotExists_ThrowsAndFreeResources()
        {
            File.WriteAllText(Path.Combine(tempDirectory, "file.xml"), "<xml />", Encoding.UTF8);

            Assert.Throws <InvalidOperationException>(() => ContentLoader
                                                      .For <DeserializerNotExistsContent>()
                                                      .LoadFromDirectory(tempDirectory));
        }
示例#6
0
        public void LoadRequiredString_FileNotExists_Throws()
        {
            var content = ContentLoader
                          .For <LoadRequiredStringContent>()
                          .WithDeserializer(s => new StreamReader(s, Encoding.UTF8).ReadToEnd());

            Assert.Throws <FileNotFoundException>(() => content.LoadFromDirectory(tempDirectory));
        }
示例#7
0
        public void MultipleOptionalFile_CreateEmptyList()
        {
            using var content = ContentLoader
                                .For <MultipleOptionalFiles>()
                                .LoadFromDirectory(tempDirectory);

            Assert.NotNull(content.Files);
            Assert.Empty(content.Files);
        }
示例#8
0
        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);
        }
示例#9
0
        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));
        }
示例#10
0
        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);
        }
示例#11
0
        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);
        }
示例#12
0
        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);
        }
示例#13
0
        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);
        }
示例#14
0
        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);
        }
示例#15
0
        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));
        }
示例#16
0
        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);
        }
示例#17
0
        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);
        }