government / policy / draw_test.go

@ ba5c700b731db728fea52ac3080b57fbe2ed37b9 | 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()
	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 TestDrawLabelsAndSharing(t *testing.T) {
	got := compiledGraph(t, `(f "hi" 2.5 'x)`).Draw()
	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()
	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()
	if !strings.Contains(got, "`-- 1#1'") || !strings.Contains(got, "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()
	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)
	}
}