@ efc0e8ad4d88be48a5cdedd9b62e88420b8b6e5e | history
package main
import (
"strings"
"testing"
)
func TestTreeCommand(t *testing.T) {
gov := compileTo(t, t.TempDir(), "worked", "(+ 1 (* 2 3))")
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", "+#2' copy", "`-- 3#7 GOV.2.7.51\n"} {
if !strings.Contains(stdout, want) {
t.Errorf("missing %q in\n%s", want, stdout)
}
}
}
func TestDecompileCommand(t *testing.T) {
gov := compileTo(t, t.TempDir(), "sq", sqSource)
code, stdout, stderr := runCmd("decompile", gov)
if code != exitOK || stdout != "; sq.gov.lisp: the square of a number.\n(define sq (lambda (x) (* x x)))\n(sq 12)\n" {
t.Errorf("exit %d, stdout %q, stderr %s", code, stdout, stderr)
}
}
func TestInspectRefusals(t *testing.T) {
gov := compileTo(t, t.TempDir(), "p", "(+ 1 2)")
for _, cmd := range []string{"tree", "decompile"} {
if code, _, stderr := runCmd(cmd, gov, "--at", "1999-01-01T00:00:00Z"); code != exitInvalid || !strings.Contains(stderr, "E-NOT-IN-FORCE") {
t.Errorf("%s before the window: exit %d, %s", cmd, code, stderr)
}
if code, _, stderr := runCmd(cmd); code != exitInvalid || !strings.Contains(stderr, "usage") {
t.Errorf("%s with no file: exit %d, %s", cmd, code, stderr)
}
}
}
// TestNoCommentsCannotBeDecompiled: --no-comments drops the notes for
// good, so decompile and tree have none to show, and the program still runs.
func TestNoCommentsCannotBeDecompiled(t *testing.T) {
dir := t.TempDir()
src := "; double it\n(define twice (lambda (x) (* 2 x))) ; the rule\n(twice 21)\n"
with := compileTo(t, dir, "with", src)
without := compileTo(t, dir, "without", src, "--no-comments")
_, a, _ := runCmd("decompile", with)
_, b, _ := runCmd("decompile", without)
if !strings.Contains(a, "; double it") || !strings.Contains(a, "; the rule") {
t.Errorf("normal build lost its comments:\n%s", a)
}
if strings.Contains(b, ";") {
t.Errorf("--no-comments build decompiles with comments:\n%s", b)
}
if _, tree, _ := runCmd("tree", without); strings.Contains(tree, ";") {
t.Errorf("tree shows notes:\n%s", tree)
}
if _, out, _ := runCmd(without); out != "42\n" {
t.Errorf("run printed %q", out)
}
}