diff --git a/source/dyaml/node.d b/source/dyaml/node.d index bfb7121..6d242dc 100644 --- a/source/dyaml/node.d +++ b/source/dyaml/node.d @@ -1534,6 +1534,10 @@ struct Node */ void add(T)(T value) { + if (!isValid) + { + setValue(Node[].init); + } enforce(isSequence(), new NodeException("Trying to add an element to a " ~ nodeTypeString ~ " node", startMark_)); @@ -1549,6 +1553,19 @@ struct Node add(5.0f); assert(opIndex(4).as!float == 5.0f); } + with(Node()) + { + add(5.0f); + assert(opIndex(0).as!float == 5.0f); + } + with(Node(5.0f)) + { + assertThrown!NodeException(add(5.0f)); + } + with(Node([5.0f : true])) + { + assertThrown!NodeException(add(5.0f)); + } } /** Add a key-value pair to a mapping. @@ -1568,6 +1585,10 @@ struct Node */ void add(K, V)(K key, V value) { + if (!isValid) + { + setValue(Node.Pair[].init); + } enforce(isMapping(), new NodeException("Trying to add a key-value pair to a " ~ nodeTypeString ~ " node", @@ -1584,6 +1605,19 @@ struct Node add(5, "6"); assert(opIndex(5).as!string == "6"); } + with(Node()) + { + add(5, "6"); + assert(opIndex(5).as!string == "6"); + } + with(Node(5.0f)) + { + assertThrown!NodeException(add(5, "6")); + } + with(Node([5.0f])) + { + assertThrown!NodeException(add(5, "6")); + } } /** Determine whether a key is in a mapping, and access its value.