government / src / policy / draw_test.go

@ d87504922ed319f2e3fd703dca40b7219eeeada1 | history


package policy

import (
	"strings"
	"testing"
)

// TestDrawWorkedExample is the design doc's drawing of (+ 1 (* 2 3)).
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 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 3         |-- *  5  GOV.0.5.23",
		"depth 3         |-- 2  6  GOV.2.6.50",
		"depth 3         `-- 3  7  GOV.2.7.51",
		"",
	}, "\n")
	if got != want {
		t.Errorf("got\n%s\nwant\n%s", got, want)
	}
}

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 2     |-- +      2  GOV.0.2.21",
		"depth 2     |-- 1      3  GOV.2.3.49",
		"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",
		"",
	}, "\n")
	if got != want {
		t.Errorf("got\n%s\nwant\n%s", got, want)
	}
}

// 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 11 |                                       `-- j  20  GOV.1.20.106",
		"depth  2     `-- l                                      23  GOV.1.23.108",
	} {
		if !strings.Contains(got, want) {
			t.Errorf("missing %q in\n%s", want, got)
		}
	}
}

func TestDrawLabelsAndSharing(t *testing.T) {
	got := compiledGraph(t, `(f "hi" 2.5 'x)`).Draw(DrawOptions{})
	for _, want := range []string{`"hi"   3`, "2.5    4", "quote  6", "x      7"} {
		if !strings.Contains(got, want) {
			t.Errorf("missing %s in\n%s", want, got)
		}
	}
	shared := mustProcess(t, path(
		mapping(fakeCert("1.2.3", "1.2.4"), "1.2.3>1.2.5", "1.2.4>1.2.5"),
		fakeCert("any"),
	)).Draw(DrawOptions{})
	if strings.Count(shared, "1.2.5") != 2 || !strings.Contains(shared, "(shared, drawn above)") {
		t.Errorf("shared node:\n%s", shared)
	}
}

func TestDrawFoldsCopyChains(t *testing.T) {
	got := compiledGraph(t, "1 (a (b (c)))").Draw(DrawOptions{})
	if !strings.Contains(got, "1' copy, depths 2-4") {
		t.Errorf("chain of copies not folded:\n%s", got)
	}
	if n := strings.Count(got, "\n"); n != 11 {
		t.Errorf("%d lines, want 11:\n%s", n, got)
	}
}

func TestDrawShowsNotes(t *testing.T) {
	got := compiledGraph(t, "; the sum\n(+ 1\n  ; two\n  ; lines\n  2)").Draw(DrawOptions{})
	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)
	}
}