Пример #1
0
        public void GeneratesDynamicObject()
        {
            // Given
            IDocument document = Substitute.For<IDocument>();
            IEnumerable<KeyValuePair<string, object>> items = null;
            document
                .When(x => x.Clone(Arg.Any<IEnumerable<KeyValuePair<string, object>>>()))
                .Do(x => items = x.Arg<IEnumerable<KeyValuePair<string, object>>>());
            document.Content.Returns(@"
A: 1
B: true
C: Yes
");
            Yaml yaml = new Yaml("MyYaml");

            // When
            yaml.Execute(new[] { document }, null).ToList();  // Make sure to materialize the result list

            // Then
            document.Received(1).Clone(Arg.Any<IEnumerable<KeyValuePair<string, object>>>());
            Assert.AreEqual(1, items.Count());
            Assert.IsInstanceOf<DynamicYaml>(items.First().Value);
            Assert.AreEqual(1, (int)((dynamic)items.First().Value).A);
            Assert.AreEqual(true, (bool)((dynamic)items.First().Value).B);
            Assert.AreEqual("Yes", (string)((dynamic)items.First().Value).C);
        }
Пример #2
0
        public void FlattensTopLevelScalarNodes()
        {
            // Given
            IDocument document = Substitute.For<IDocument>();
            IEnumerable<KeyValuePair<string, object>> items = null;
            document
                .When(x => x.Clone(Arg.Any<IEnumerable<KeyValuePair<string, object>>>()))
                .Do(x => items = x.Arg<IEnumerable<KeyValuePair<string, object>>>());
            document.Content.Returns(@"
A: 1
B: true
C: Yes
");
            Yaml yaml = new Yaml();

            // When
            yaml.Execute(new[] { document }, null).ToList();  // Make sure to materialize the result list

            // Then
            document.Received(1).Clone(Arg.Any<IEnumerable<KeyValuePair<string, object>>>());
            Assert.AreEqual(3, items.Count());
            Assert.AreEqual("1", items.First(x => x.Key == "A").Value);
            Assert.AreEqual("true", items.First(x => x.Key == "B").Value);
            Assert.AreEqual("Yes", items.First(x => x.Key == "C").Value);
        }
Пример #3
0
        public void FlattensTopLevelScalarNodes()
        {
            // Given
            IDocument document = Substitute.For <IDocument>();
            IEnumerable <KeyValuePair <string, object> > items = null;

            document
            .When(x => x.Clone(Arg.Any <IEnumerable <KeyValuePair <string, object> > >()))
            .Do(x => items = x.Arg <IEnumerable <KeyValuePair <string, object> > >());
            document.Content.Returns(@"
A: 1
B: true
C: Yes
");
            Yaml yaml = new Yaml();

            // When
            yaml.Execute(new[] { document }, null).ToList();  // Make sure to materialize the result list

            // Then
            document.Received(1).Clone(Arg.Any <IEnumerable <KeyValuePair <string, object> > >());
            Assert.AreEqual(3, items.Count());
            Assert.AreEqual("1", items.First(x => x.Key == "A").Value);
            Assert.AreEqual("true", items.First(x => x.Key == "B").Value);
            Assert.AreEqual("Yes", items.First(x => x.Key == "C").Value);
        }
Пример #4
0
            public void UsesDocumentNestingForComplexChildren()
            {
                // Given
                IExecutionContext context  = new TestExecutionContext();
                IDocument         document = new TestDocument(@"
C:
  - X: 1
    Y: 2
  - X: 4
    Z: 5
");
                Yaml yaml = new Yaml();

                // When
                IList <IDocument> documents = yaml.Execute(new[] { document }, context).ToList();  // Make sure to materialize the result list

                // Then
                documents.Count.ShouldBe(1);
                documents[0].Keys.ShouldBe(new[] { "C" }, true);
                documents[0]["C"].ShouldBeOfType <IDocument[]>();
                IDocument[] subDocuments = (IDocument[])documents[0]["C"];
                subDocuments.Length.ShouldBe(2);
                subDocuments[0].Keys.ShouldBe(new[] { "X", "Y" }, true);
                subDocuments[0]["X"].ShouldBe("1");
                subDocuments[0]["Y"].ShouldBe("2");
                subDocuments[1].Keys.ShouldBe(new[] { "X", "Z" }, true);
                subDocuments[1]["X"].ShouldBe("4");
                subDocuments[1]["Z"].ShouldBe("5");
            }
Пример #5
0
        public void GeneratesDynamicObject()
        {
            // Given
            IDocument document = Substitute.For <IDocument>();
            IEnumerable <KeyValuePair <string, object> > items = null;

            document
            .When(x => x.Clone(Arg.Any <IEnumerable <KeyValuePair <string, object> > >()))
            .Do(x => items = x.Arg <IEnumerable <KeyValuePair <string, object> > >());
            document.Content.Returns(@"
A: 1
B: true
C: Yes
");
            Yaml yaml = new Yaml("MyYaml");

            // When
            yaml.Execute(new[] { document }, null).ToList();  // Make sure to materialize the result list

            // Then
            document.Received(1).Clone(Arg.Any <IEnumerable <KeyValuePair <string, object> > >());
            Assert.AreEqual(1, items.Count());
            Assert.IsInstanceOf <DynamicYaml>(items.First().Value);
            Assert.AreEqual(1, (int)((dynamic)items.First().Value).A);
            Assert.AreEqual(true, (bool)((dynamic)items.First().Value).B);
            Assert.AreEqual("Yes", (string)((dynamic)items.First().Value).C);
        }
Пример #6
0
            public void GeneratesDynamicObjectAndFlattens()
            {
                // Given
                IExecutionContext context  = new TestExecutionContext();
                IDocument         document = new TestDocument(@"
A: 1
B: true
C: Yes
");
                Yaml yaml = new Yaml("MyYaml", true);

                // When
                IList <IDocument> documents = yaml.Execute(new[] { document }, context).ToList();  // Make sure to materialize the result list

                // Then
                documents.Count.ShouldBe(1);
                documents[0].Keys.ShouldBe(new[] { "MyYaml", "A", "B", "C" }, true);
                documents[0]["MyYaml"].ShouldBeOfType <DynamicYaml>();
                ((int)((dynamic)documents[0]["MyYaml"]).A).ShouldBe(1);
                ((bool)((dynamic)documents[0]["MyYaml"]).B).ShouldBe(true);
                ((string)((dynamic)documents[0]["MyYaml"]).C).ShouldBe("Yes");
                documents[0]["A"].ShouldBe("1");
                documents[0]["B"].ShouldBe("true");
                documents[0]["C"].ShouldBe("Yes");
            }
Пример #7
0
        public void SetsMetadataKey()
        {
            // Given
            IDocument document = Substitute.For<IDocument>();
            document.Content.Returns(@"A: 1");
            Yaml yaml = new Yaml("MyYaml");

            // When
            yaml.Execute(new [] { document }, null).ToList();  // Make sure to materialize the result list

            // Then
            document.Received(1).Clone(Arg.Any<IEnumerable<KeyValuePair<string, object>>>());
            document.Received().Clone(Arg.Is<IEnumerable<KeyValuePair<string, object>>>(x => x.First().Key == "MyYaml"));
        }
Пример #8
0
            public void SetsMetadataKey()
            {
                // Given
                IExecutionContext context  = new TestExecutionContext();
                IDocument         document = new TestDocument(@"A: 1");
                Yaml yaml = new Yaml("MyYaml");

                // When
                IList <IDocument> documents = yaml.Execute(new[] { document }, context).ToList();  // Make sure to materialize the result list

                // Then
                documents.Count.ShouldBe(1);
                documents[0].Keys.ShouldBe(new[] { "MyYaml" }, true);
            }
Пример #9
0
            public void EmptyReturnIfEmptyInputAndNotFlatten()
            {
                // Given
                IExecutionContext context  = new TestExecutionContext();
                IDocument         document = new TestDocument(@"
");
                Yaml yaml = new Yaml("Foo");

                // When
                IList <IDocument> documents = yaml.Execute(new[] { document }, context).ToList();  // Make sure to materialize the result list

                // Then
                documents.Count.ShouldBe(0);
            }
Пример #10
0
        public void SetsMetadataKey()
        {
            // Given
            IDocument document = Substitute.For <IDocument>();

            document.Content.Returns(@"A: 1");
            Yaml yaml = new Yaml("MyYaml");

            // When
            yaml.Execute(new [] { document }, null).ToList();  // Make sure to materialize the result list

            // Then
            document.Received(1).Clone(Arg.Any <IEnumerable <KeyValuePair <string, object> > >());
            document.Received().Clone(Arg.Is <IEnumerable <KeyValuePair <string, object> > >(x => x.First().Key == "MyYaml"));
        }
Пример #11
0
            public void UsesDocumentNestingForComplexChildren()
            {
                // Given
                IDocument document = Substitute.For <IDocument>();
                IList <KeyValuePair <string, object> >         items    = null;
                List <IList <KeyValuePair <string, object> > > subItems = new List <IList <KeyValuePair <string, object> > >();
                IExecutionContext context = Substitute.For <IExecutionContext>();

                context
                .When(x => x.GetDocument(Arg.Any <IDocument>(), Arg.Any <IEnumerable <KeyValuePair <string, object> > >()))
                .Do(x => items = x.Arg <IEnumerable <KeyValuePair <string, object> > >().ToList());
                context
                .When(x => x.GetDocument(Arg.Any <IEnumerable <KeyValuePair <string, object> > >()))
                .Do(x => subItems.Add(x.Arg <IEnumerable <KeyValuePair <string, object> > >().ToList()));
                document.Content.Returns(@"
C:
  - X: 1
    Y: 2
  - X: 4
    Z: 5
");
                Yaml yaml = new Yaml();

                // When
                yaml.Execute(new[] { document }, context).ToList();  // Make sure to materialize the result list

                // Then
                context.Received(1).GetDocument(Arg.Any <IDocument>(), Arg.Any <IEnumerable <KeyValuePair <string, object> > >());
                Assert.AreEqual(1, items.Count);
                IDocument[] documents = items.First(x => x.Key == "C").Value as IDocument[];
                Assert.IsNotNull(documents);

                context.Received(2).GetDocument(Arg.Any <IEnumerable <KeyValuePair <string, object> > >());
                Assert.AreEqual(2, subItems.Count);
                Assert.AreEqual("X", subItems[0][0].Key);
                Assert.AreEqual("1", subItems[0][0].Value);
                Assert.AreEqual("Y", subItems[0][1].Key);
                Assert.AreEqual("2", subItems[0][1].Value);
                Assert.AreEqual("X", subItems[1][0].Key);
                Assert.AreEqual("4", subItems[1][0].Value);
                Assert.AreEqual("Z", subItems[1][1].Key);
                Assert.AreEqual("5", subItems[1][1].Value);
            }
Пример #12
0
            public void FlattensTopLevelScalarNodes()
            {
                // Given
                IExecutionContext context  = new TestExecutionContext();
                IDocument         document = new TestDocument(@"
A: 1
B: true
C: Yes
");
                Yaml yaml = new Yaml();

                // When
                IList <IDocument> documents = yaml.Execute(new[] { document }, context).ToList();  // Make sure to materialize the result list

                // Then
                documents.Count.ShouldBe(1);
                documents[0].Keys.ShouldBe(new[] { "A", "B", "C" }, true);
                documents[0]["A"].ShouldBe("1");
                documents[0]["B"].ShouldBe("true");
                documents[0]["C"].ShouldBe("Yes");
            }
Пример #13
0
        public void SetsMetadataKey()
        {
            // Given
            IDocument document = Substitute.For<IDocument>();
            IEnumerable<KeyValuePair<string, object>> items = null;
            document
                .When(x => x.Clone(Arg.Any<IEnumerable<KeyValuePair<string, object>>>()))
                .Do(x => items = x.Arg<IEnumerable<KeyValuePair<string, object>>>());
            document.Content.Returns(@"A: 1");
            Yaml yaml = new Yaml("MyYaml");

            // When
            yaml.Execute(new [] { document }, null).ToList();  // Make sure to materialize the result list

            // Then
            document.Received().Clone(Arg.Any<IEnumerable<KeyValuePair<string, object>>>());
            Assert.AreEqual(1, items.Count());
            Assert.AreEqual("MyYaml", items.First().Key);
        }