parent:
675167640370859e58d24a1f76126760c2e61968
nmyk <nick@nmyk.io>
2026-11-15T15:41:08-05:00
improve tree printer again
diff --git a/SPEC.md b/SPEC.md
index 1c4d00e4c72fa0fa15d24a942a0edb0833545ff5..f08818d27e4386fe206a5a2a6a3fb2e0341dc91b 100644
--- a/SPEC.md
+++ b/SPEC.md
@@ -524,17 +524,17 @@ | 2 | `anyPolicy` | `GOV.0.4.1` → `GOV.0.5.23`, `GOV.0.4.1` → `GOV.2.6.50`, `GOV.0.4.1` → `GOV.2.7.51` |
| 3 | `anyPolicy` | None |
Policy processing makes this graph, as `government tree` prints it. Each line
-gives the depth, the node (`()` for a list node), its id and its OID. A `'`
-after the id marks a copy:
+gives the depth, the node, its id and its OID. A list node is `(`, and its
+elements are the lines indented below it. A `'` after the id marks a copy:
```
depth 0 anyPolicy
-depth 1 `-- () 1 GOV.0.1.1
+depth 1 `-- ( 1 GOV.0.1.1
depth 2 |-- + 2 GOV.0.2.21
depth 3 | `-- + 2' copy
depth 2 |-- 1 3 GOV.2.3.49
depth 3 | `-- 1 3' copy
-depth 2 `-- () 4 GOV.0.4.1
+depth 2 `-- ( 4 GOV.0.4.1
depth 3 |-- * 5 GOV.0.5.23
depth 3 |-- 2 6 GOV.2.6.50
depth 3 `-- 3 7 GOV.2.7.51
diff --git a/src/cmd/government/inspect_test.go b/src/cmd/government/inspect_test.go
index 43abf713df262d6d6454608a2077b44e06dca921..ea010c0f470479cbb28615da8f64827cd0e40c4d 100644
--- a/src/cmd/government/inspect_test.go
+++ b/src/cmd/government/inspect_test.go
@@ -11,7 +11,7 @@ code, stdout, stderr := runCmd("tree", gov)
if code != exitOK {
t.Fatalf("exit %d: %s", code, stderr)
}
- for _, want := range []string{"depth 0 anyPolicy\n", "`-- () 1 GOV.0.1.1\n", "`-- + 2' copy\n", "`-- 3 7 GOV.2.7.51\n"} {
+ for _, want := range []string{"depth 0 anyPolicy\n", "`-- ( 1 GOV.0.1.1\n", "`-- + 2' copy\n", "`-- 3 7 GOV.2.7.51\n"} {
if !strings.Contains(stdout, want) {
t.Errorf("missing %q in\n%s", want, stdout)
}
diff --git a/src/policy/draw.go b/src/policy/draw.go
index b353fdf0b296120fe77cb93343ef38dc7a2dee83..8e0a48a3e484e1d4bda076e817b716582ecb06bb 100644
--- a/src/policy/draw.go
+++ b/src/policy/draw.go
@@ -22,11 +22,12 @@
// Draw prints the graph as a tree, one node per line, with each node's
// depth, its decoded label, its id and its OID:
//
-// depth 1 `-- () 1 GOV.0.1.1
+// depth 1 `-- ( 1 GOV.0.1.1
// depth 2 |-- + 2 GOV.0.2.21
// depth 3 | `-- + 2' copy
//
-// A list node is drawn as (). A ' after the id marks a copy the validator
+// A list node is drawn as (, since the indented lines below it are its
+// elements; () is always the empty list, as in source. A ' after the id marks a copy the validator
// made, which carries an atom down to the bottom; a chain of copies is one
// line giving the depths it spans. A node with several parents is drawn in
// full under the first and named again, without its children, under the
@@ -136,7 +137,7 @@ switch {
case ns == ast.NSString:
text = sexpr.Print(sexpr.String(text))
case ns == ast.NSBuiltin && text == builtin.ListNode:
- text = "()"
+ text = "("
}
d := description{label: text, id: strconv.FormatUint(id, 10), where: short(n.Policy)}
if parent != nil && parent.Policy.Equal(n.Policy) {
diff --git a/src/policy/draw_test.go b/src/policy/draw_test.go
index b097406d7ff6ead7397fc2e8ea69afdac1433730..cdf3f39f3bfbd262b18e992cf85b17abe71ba4c7 100644
--- a/src/policy/draw_test.go
+++ b/src/policy/draw_test.go
@@ -10,12 +10,12 @@ func TestDrawWorkedExample(t *testing.T) {
got := compiledGraph(t, "(+ 1 (* 2 3))").Draw(DrawOptions{})
want := strings.Join([]string{
"depth 0 anyPolicy",
- "depth 1 `-- () 1 GOV.0.1.1",
+ "depth 1 `-- ( 1 GOV.0.1.1",
"depth 2 |-- + 2 GOV.0.2.21",
"depth 3 | `-- + 2' copy",
"depth 2 |-- 1 3 GOV.2.3.49",
"depth 3 | `-- 1 3' copy",
- "depth 2 `-- () 4 GOV.0.4.1",
+ "depth 2 `-- ( 4 GOV.0.4.1",
"depth 3 |-- * 5 GOV.0.5.23",
"depth 3 |-- 2 6 GOV.2.6.50",
"depth 3 `-- 3 7 GOV.2.7.51",
@@ -30,10 +30,10 @@ func TestDrawNoCopies(t *testing.T) {
got := compiledGraph(t, "(+ 1 (* 2 3))").Draw(DrawOptions{NoCopies: true})
want := strings.Join([]string{
"depth 0 anyPolicy",
- "depth 1 `-- () 1 GOV.0.1.1",
+ "depth 1 `-- ( 1 GOV.0.1.1",
"depth 2 |-- + 2 GOV.0.2.21",
"depth 2 |-- 1 3 GOV.2.3.49",
- "depth 2 `-- () 4 GOV.0.4.1",
+ "depth 2 `-- ( 4 GOV.0.4.1",
"depth 3 |-- * 5 GOV.0.5.23",
"depth 3 |-- 2 6 GOV.2.6.50",
"depth 3 `-- 3 7 GOV.2.7.51",
@@ -48,7 +48,7 @@ // Ids and depths with more digits keep their columns aligned.
func TestDrawAlignsWideColumns(t *testing.T) {
got := compiledGraph(t, "(a (b (c (d (e (f (g (h (i (j)))))))))) (k l)").Draw(DrawOptions{NoCopies: true})
for _, want := range []string{
- "depth 1 |-- () 1 GOV.0.1.1",
+ "depth 1 |-- ( 1 GOV.0.1.1",
"depth 11 | `-- j 20 GOV.1.20.106",
"depth 2 `-- l 23 GOV.1.23.108",
} {
@@ -90,3 +90,18 @@ if !strings.Contains(got, "GOV.0.1.1 ; the sum") || !strings.Contains(got, "GOV.2.4.50 ; two / lines") {
t.Errorf("notes missing:\n%s", got)
}
}
+
+// A list node is drawn as ( so that () always means the empty list.
+func TestDrawEmptyList(t *testing.T) {
+ got := compiledGraph(t, "(f ())").Draw(DrawOptions{NoCopies: true})
+ want := strings.Join([]string{
+ "depth 0 anyPolicy",
+ "depth 1 `-- ( 1 GOV.0.1.1",
+ "depth 2 |-- f 2 GOV.1.2.102",
+ "depth 2 `-- () 3 GOV.0.3.5",
+ "",
+ }, "\n")
+ if got != want {
+ t.Errorf("got\n%s\nwant\n%s", got, want)
+ }
+}