Testing a tree data structure in Perl

Test::Deep on the other hand provides us tools to test arbitrary data structures
in a very flexible way.

Tree data structure

For example what if we have a tree-like data structure like this one. Every level is an array of nodes. Every node is a hash
containing and id, a payload, which is in itself a hash, and optionally it contains a key called 'subtree' where the value
is another array containing nodes. The depth and the width of the tree can be anything.

How can we test that on various calls the data returns in the expected format?

In order to demonstrate, I've created a list of 2 test cases that are expected to pass
and a list of 7 cases where there is some problem. Either there is extra data or missing data
or the structure was not correct. Then in our test code we go through the test cases.

We expect the good ones to pass and the bad ones to fail. If you save the files as
lib/MyTee.pm and t/tree.t then you can run the tests as

First we create a hash called %end_node that is expected to match
a node without a subtree in it. We used the re function provided by
Test::Deep to create a regex that should match.

Then we created another hash called %mid_node that is expected to
match a node with a subtree in it. It is the same as the previous hash
with an additional key called subtree that has a value which is an array
of element. Each element is expected to be either another %mid_tree or an %end_tree.

The whole tree then can also be expressed as an array where each element is either a mid_node or an end_node.