1 /**
2  * Copyright: (c) 2015-2020, Milofon Project.
3  * License: Subject to the terms of the BSD 3-Clause License, as written in the included LICENSE.md file.
4  * Author: <m.galanin@milofon.pro> Maksim Galanin
5  * Date: 2020-03-03
6  */
7 
8 module uninode.test_tree;
9 
10 private
11 {
12     import std.exception : assertThrown, collectException;
13 
14     import uninode.tree;
15 }
16 
17 
18 version(unittest)
19 {
20     enum SimpleConfigs = q{
21         auto chObj = UniTree(["one": UniTree(1), "two": UniTree(2)]);
22         const chArr = UniTree([UniTree(3), UniTree(4), UniTree(5)]);
23         auto root = UniTree(["obj": chObj, "arr": chArr]);
24     };
25 }
26 
27 
28 @("Should size normal")
29 @safe unittest
30 {
31     const node = UniTree(1);
32     assert (node.sizeof == 24);
33 }
34 
35 
36 @("Should work get method with path")
37 @safe unittest
38 {
39     mixin (SimpleConfigs);
40     assert (root.get!int("obj.one") == 1);
41     assert (!root.opt!int("obj.one").isNull);
42     assert (root.opt!int("obj.three").isNull);
43     assert (root.get!UniTree("obj").canMapping);
44     assert (root.get!UniTree("obj").get!int("one") == 1);
45 }
46 
47 
48 @("Should work getOrThrown")
49 @safe unittest
50 {
51     mixin (SimpleConfigs);
52     auto e = collectException(root.getOrThrown!int("not found"));
53     assert (e.msg == "not found");
54     e = collectException(root.getOrThrown!UniTree("one", "not obj"));
55     assert (e.msg == "not obj");
56     assert (root.getOrThrown!UniTree("obj", "not found obj").canMapping);
57 }
58 
59 
60 @("Should work getOrElse")
61 @safe unittest
62 {
63     mixin (SimpleConfigs);
64     assert (root.getOrElse("one", 10) == 10);
65 }
66 
67 
68 @("Should work in operator")
69 @safe unittest
70 {
71     mixin(SimpleConfigs);
72     assert("obj.one" in root);
73     assert("obj.two" in root);
74     assert("obj.tree" !in root);
75 }
76 
77 
78 @("Should work getSequence")
79 @safe unittest
80 {
81     mixin(SimpleConfigs);
82     assert (root.getSequence("arr").length == 3);
83     assertThrown(root.getSequence("obj.one"));
84 }
85 
86 
87 @("Should work merge method")
88 @safe unittest
89 {
90     mixin(SimpleConfigs);
91     auto node = UniTree(["five": UniTree(5)]);
92     UniTree root2 = UniTree(["obj": node, "arr": UniTree([UniTree(6)])]);
93     UniTree nilRoot = UniTree(null);
94     assert ((root ~ nilRoot) == root);
95     assert ((nilRoot ~ root) == root);
96 
97     auto res = root ~ root2;
98     assert(res.get!UniTree("arr").length == 4);
99     assert(res.getSequence("arr")[3].get!int == 6);
100     assert(res.get!int("obj.five") == 5);
101 }
102 
103 
104 @("Should work getOrElse method")
105 @safe unittest
106 {
107     mixin(SimpleConfigs);
108     assert (root.getOrElse("one", 10) == 10);
109 }
110