api: add tests modifying the tree

This commit is contained in:
Joao Paulo Magalhaes
2019-03-21 14:32:46 +00:00
parent df5a095753
commit 48da84dd66
3 changed files with 81 additions and 10 deletions

View File

@@ -83,6 +83,73 @@ class TestSubstrInterop(unittest.TestCase):
self.assertEqual(s, m)
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
def _addmap(t, node, k=None):
m = t.append_child(node)
if k is None:
t.to_map(m)
else:
t.to_map(m, k)
return m
def _addseq(t, node, k=None):
m = t.append_child(node)
if k is None:
t.to_seq(m)
else:
t.to_seq(m, k)
return m
def _addleaf(t, node, k, v=None):
ch = t.append_child(node)
if v is None:
t.to_val(ch, k)
else:
t.to_keyval(ch, k, v)
return ch
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
def check_tree_mod(ut, t):
# some convenient shorthands
eq = ut.assertEqual
def _leaf(node, k, v=None):
ch = _addleaf(t, node, k, v)
pos = t.child_pos(node, ch)
eq(t.child(node, pos), ch)
if v is not None:
eq(t.find_child(node, k), ch)
eq(t.child(node, pos), t.find_child(node, k))
return ch
def _seq(node, k):
ch = _addseq(t, node, k)
eq(t.find_child(node, k), ch)
return ch
def _map(node, k):
ch = _addmap(t, node, k)
eq(t.find_child(node, k), ch)
return ch
m = _map(t.root_id(), "check_tree_mod_map")
_leaf(m, "k1", "v1")
_leaf(m, "k2", "v2")
_leaf(m, "k3", "v3")
eq(t.num_children(m), 3)
eq(t.num_siblings(t.first_child(m)), 3)
s = _seq(t.root_id(), "check_tree_mod_seq")
_leaf(s, "v1")
_leaf(s, "v2")
_leaf(s, "v3")
eq(t.num_children(s), 3)
eq(t.num_siblings(t.first_child(m)), 3)
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
@@ -91,7 +158,6 @@ class SimpleHardcoded:
yaml = "{HELLO: a, foo: b, bar: c, baz: d, seq: [0, 1, 2, 3]}"
def check(self, ut, t):
# some convenient shorthands
eq = ut.assertEqual
ne = ut.assertNotEqual
@@ -232,6 +298,8 @@ class SimpleHardcoded:
num += 1
eq(num, 1)
check_tree_mod(ut, t)
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------

View File

@@ -85,6 +85,9 @@ using csubstr = c4::csubstr;
#error no "in" typemap defined for this export language
#endif
};
// Copy the typecheck code for "char *".
%typemap(typecheck) c4::substr = char *;
%typemap(typecheck) c4::csubstr = const char *;
%typemap(out) c4::csubstr {
@@ -298,13 +301,13 @@ public:
c4::csubstr key_tag (size_t node) const;
c4::csubstr key_ref (size_t node) const;
c4::csubstr key_anchor(size_t node) const;
c4::yml::NodeScalar keysc (size_t node) const;
c4::yml::NodeScalar keysc(size_t node) const;
c4::csubstr val (size_t node) const;
c4::csubstr val_tag (size_t node) const;
c4::csubstr val_ref (size_t node) const;
c4::csubstr val_anchor(size_t node) const;
c4::yml::NodeScalar valsc (size_t node) const;
c4::yml::NodeScalar valsc(size_t node) const;
public:
@@ -371,22 +374,22 @@ public:
public:
void to_keyval(size_t node, c4::csubstr const& key, c4::csubstr const& val, int more_flags=0);
void to_map(size_t node, c4::csubstr const& key, int more_flags=0);
void to_seq(size_t node, c4::csubstr const& key, int more_flags=0);
void to_val(size_t node, c4::csubstr const& val, int more_flags=0);
void to_keyval(size_t node, c4::csubstr key, c4::csubstr val, int more_flags=0);
void to_map(size_t node, c4::csubstr key, int more_flags=0);
void to_seq(size_t node, c4::csubstr key, int more_flags=0);
void to_val(size_t node, c4::csubstr val, int more_flags=0);
void to_stream(size_t node, int more_flags=0);
void to_map(size_t node, int more_flags=0);
void to_seq(size_t node, int more_flags=0);
void to_doc(size_t node, int more_flags=0);
void set_key_tag(size_t node, c4::csubstr const& tag);
void set_key_tag(size_t node, c4::csubstr tag);
void set_key_anchor(size_t node, c4::csubstr anchor);
void set_val_anchor(size_t node, c4::csubstr anchor);
void set_key_ref (size_t node, c4::csubstr ref );
void set_val_ref (size_t node, c4::csubstr ref );
void set_val_tag(size_t node, c4::csubstr const& tag);
void set_val_tag(size_t node, c4::csubstr tag);
void rem_key_anchor(size_t node);
void rem_val_anchor(size_t node);
void rem_key_ref (size_t node);

2
extern/c4core vendored