Inheritance: NodeBase
Ejemplo n.º 1
0
 public void should_get_ancestors()
 {
     var ancestor = new Node("oh");
     var node = new Node(ancestor);
     node.Parent.ShouldBeSameAs(ancestor);
     node.Parent.HasParent.ShouldBeFalse();
 }
Ejemplo n.º 2
0
 public void should_get_node(NodeType type)
 {
     var parent = new Node { NodeType = type };
     var child = new Node("hai");
     parent.Add(child, x => { });
     parent.GetNode("hai").ShouldEqual(child);
 }
Ejemplo n.º 3
0
        public void should_get_node_by_name(string name, StringComparison comparison, bool hasResult)
        {
            var node = new Node("oh");
            var result = new[] { node, new Node("hai") }.GetNode(name, comparison);

            if (!hasResult) result.ShouldBeNull();
            else result.ShouldBeSameAs(node);
        }
Ejemplo n.º 4
0
 public void should_get_inner_value()
 {
     var value = new SimpleValue("hai", typeof(string).ToCachedType());
     var node = new Node(value);
     node.Value.ShouldBeSameAs(value.Instance);
     node.SpecifiedType.Type.ShouldBe<String>();
     node.ActualType.Type.ShouldBe<String>();
 }
Ejemplo n.º 5
0
        public void should_indicate_if_node_has_null_value(string value, bool isNull)
        {
            var node = new Node { NodeType = NodeType.Value, Value = value };

            node.IsNullValue().ShouldEqual(isNull);
            ((INode)null).IsNullValue().ShouldBeFalse();

            node.IsNonNullValue().ShouldEqual(!isNull);
            ((INode)null).IsNonNullValue().ShouldBeFalse();
        }
Ejemplo n.º 6
0
        public void Setup()
        {
            _options = Options.Create();
            _source = new ValueNode(
                new Context(_options, Mode.Deserialize, "object"), null,
                new SimpleValue(typeof(int?).ToCachedType()), null, null);
            _target = new Node("yada") { NodeType = NodeType.Value, Format = "xml", Type = "element" };
            _writers = new WriterConventions(_options);

            _visitorIncrement = (s, t, o) =>
            {
                should_have_valid_parameters(s, t, o);
                t.Value = t.Value == null ? 1 : (int?)t.Value + 1;
            };

            _writerObjectIncrement = (v, s, t, o) =>
            {
                should_have_valid_parameters(s, t, o);
                t.Value = (int?) t.Value + 1 ?? 1;
            };

            _writerIncrement = (v, s, t, o) =>
            {
                should_have_valid_parameters(s, t, o);
                t.Value = (int?) t.Value + 1 ?? 1;
            };

            _writerNullableIncrement = (v, s, t, o) =>
            {
                should_have_valid_parameters(s, t, o);
                t.Value = t.Value == null ? 1 : (int?)t.Value + 1;
            };

            _writerIncrementValue = (v, s, t, o) =>
            {
                should_have_valid_parameters(s, t, o);
                return v + 1;
            };

            _writerIncrementNullableValue = (v, s, t, o) =>
            {
                should_have_valid_parameters(s, t, o);
                return v == null ? 1 : v + 1;
            };
        }
Ejemplo n.º 7
0
 public void Setup()
 {
     _source = new Node();
     _target = Substitute.ForPartsOf<Node>();
 }
Ejemplo n.º 8
0
        public void should_throw_node_type_mismatch_error_if_invalid_and_mapping_does_not_exist(
            NodeType sourceType, NodeType targetType)
        {
            var source = new Node("Source", "xml") { Type = "element", NodeType = sourceType };
            if (sourceType == NodeType.Value) source.Value = "";
            var target = new Node("Target") { NodeType = targetType };

            var exception = Assert.Throws<FriendlyMappingException>(() =>
                new NodeMapper<Node, Node>(_noMatch, null, _noMatch, null)
                    .Map(source, target, Mode.Deserialize));

            var message = "Cannot map {0} {1} node to {2} {3} node.".ToFormat(
                source.NodeType.GetArticle(), source.NodeType.ToLower(),
                target.NodeType.GetArticle(), target.NodeType.ToLower());

            var friendlyMessage = "Should be {0} {1} but was {2} {3}.".ToFormat(
                target.NodeType.GetArticle(), target.NodeType.ToLower(),
                source.NodeType.GetArticle(), source.NodeType.ToLower());

            exception.Message.ShouldEqual("Error deserializing xml element 'Source' to 'Target': " + message);
            exception.FriendlyMessage.ShouldEqual("Could not read xml element 'Source': " + friendlyMessage);
            exception.InnerException.ShouldBeType<NodeTypeMismatchException>();

            var innerException = exception.InnerException.As<NodeTypeMismatchException>();
            innerException.Message.ShouldEqual(message);
            innerException.FriendlyMessage.ShouldEqual(friendlyMessage);
        }
Ejemplo n.º 9
0
 protected override void AddNode(INode node, bool named, Action<INode> modify)
 {
     var child = new Node(named ? node.Name : null, node.NodeType, this);
     modify(child);
     _nodes.Add(child);
 }
Ejemplo n.º 10
0
 public void should_not_allow_value_to_be_read_if_node_is_an_object_or_array(NodeType type)
 {
     Assert.Throws<ValueNotSupportedException>(() =>
     { var result = new Node { NodeType = type }.Value; });
 }
Ejemplo n.º 11
0
 public void should_insert_a_node_into_a_member_of_the_same_type()
 {
     var instance = new NodeInsertion();
     var parent = CreateAccessModifierNode(value: new SimpleValue(instance, typeof(NodeInsertion).ToCachedType())); 
     var child = new Node("Node");
     parent.ShouldNotExecuteCallback<INode>((s, c) => s.Add(child, c));
     parent.GetNode("Node").ShouldBeSameAs(child);
     instance.Node.ShouldBeSameAs(child);
 }
Ejemplo n.º 12
0
 public void should_insert_a_node_matching_on_configured_case_comparison(
     StringComparison comparison, string name)
 {
     var instance = new NodeInsertion();
     var parent = CreateAccessModifierNode(Options.Create(x => x.Deserialization(y => y.WithNameComparison(comparison))),
         new SimpleValue(instance, typeof(NodeInsertion).ToCachedType()));
     var child = new Node(name);
     parent.ShouldNotExecuteCallback<INode>((s, c) => s.Add(child, c));
     parent.GetNode(name).ShouldBeSameAs(child);
     instance.NodeInterface.ShouldBeSameAs(child);
 }
Ejemplo n.º 13
0
 public void should_return_node_value_when_a_node_implementation(string member)
 {
     var instance = new NodeMembers();
     var child = new Node("yada");
     instance.SetPropertyOrFieldValue(member, child);
     var parent = CreateAccessModifierNode(Options.Create(),
         new SimpleValue(instance, typeof (NodeMembers).ToCachedType()));
     parent.GetNode("yada").ShouldBeSameAs(child);
 }
Ejemplo n.º 14
0
 protected override void AddNode(INode node, bool named, Action<INode> modify)
 {
     var child = new Node(named ? node.Name : null, Format, this, null, _encoder) { NodeType = node.NodeType };
     child.Metadata.Add(node.Metadata);
     modify(child);
     _children.AddItem(child);
 }
Ejemplo n.º 15
0
 public void should_not_allow_name_to_be_read_if_not_supported()
 {
     Assert.Throws<NameNotSupportedException>(() => 
         { var result = new Node { IsNamed = false }.Name; });
 }
Ejemplo n.º 16
0
 public void should_ignore_a_node_that_implements_inode_but_not_the_same_type_as_the_member()
 {
     var instance = new NodeInsertion();
     var parent = CreateAccessModifierNode(value: new SimpleValue(instance, typeof(NodeInsertion).ToCachedType()));
     var child = new Node("JsonNode");
     parent.ShouldNotExecuteCallback<INode>((s, c) => s.Add(child, c));
     var jsonNode = parent.GetNode("JsonNode");
     jsonNode.ShouldNotBeNull();
     jsonNode.ShouldNotBeSameAs(child);
     jsonNode.NodeType.ShouldEqual(NodeType.Value);
     jsonNode.Value.ShouldBeNull();
     instance.JsonNode.ShouldBeNull();
 }
Ejemplo n.º 17
0
 public void should_get_ancestors_when_root()
 {
     var node = new Node("hai");
     node.HasParent.ShouldBeFalse();
 }
Ejemplo n.º 18
0
        public void should_add_and_get_unamed_node_on_an_array_and_variable(NodeType type)
        {
            var metadata = new Metadata(new object());
            var node = new Node { NodeType = type };

            node.ShouldExecuteCallback<INode>(
                (x, c) => node.Add(NodeType.Value, metadata, c), x =>
                {
                    x.IsNamed.ShouldBeFalse();
                    x.NodeType.ShouldEqual(NodeType.Value);
                    x.Metadata.ShouldNotBeSameAs(metadata);
                    x.Metadata.Get<object>().ShouldBeSameAs(metadata.Get<object>());
                    node.ShouldContainInstance(x);
                });

            node.ShouldExecuteCallback<INode>(
                (x, c) => node.Add(NodeType.Object, metadata, c), x =>
                {
                    x.IsNamed.ShouldBeFalse();
                    x.NodeType.ShouldEqual(NodeType.Object);
                    x.Metadata.ShouldNotBeSameAs(metadata);
                    x.Metadata.Get<object>().ShouldBeSameAs(metadata.Get<object>());
                    node.ShouldContainInstance(x);
                });

            node.ShouldExecuteCallback<INode>(
                (x, c) => node.Add(NodeType.Array, metadata, c), x =>
                {
                    x.IsNamed.ShouldBeFalse();
                    x.NodeType.ShouldEqual(NodeType.Array);
                    x.Metadata.ShouldNotBeSameAs(metadata);
                    x.Metadata.Get<object>().ShouldBeSameAs(metadata.Get<object>());
                    node.ShouldContainInstance(x);
                });
        }
Ejemplo n.º 19
0
 public void should_insert_any_node_into_an_inode_member()
 {
     var instance = new NodeInsertion();
     var parent = CreateNode(value: new SimpleValue(instance, typeof(NodeInsertion).ToCachedType()));
     var child = new Node("NodeInterface");
     parent.ShouldNotExecuteCallback<INode>((s, c) => s.Add(child, c));
     parent.GetNode("NodeInterface").ShouldBeSameAs(child);
     instance.NodeInterface.ShouldBeSameAs(child);
 }