Simplified tests

This commit is contained in:
Eduard Urbach 2024-07-09 12:13:59 +02:00
parent 59d6653eba
commit 8103faa8b6
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0

View File

@ -15,26 +15,26 @@ var programs = []struct {
ExpectedOutput string
ExpectedExitCode int
}{
{"empty.q", "", 0},
{"math.q", "", 10},
{"precedence.q", "", 10},
{"square-sum.q", "", 25},
{"chained-calls.q", "", 9},
{"nested-calls.q", "", 4},
{"overwrite.q", "", 3},
{"reuse.q", "", 3},
{"return.q", "", 6},
{"reassign.q", "", 2},
{"branch.q", "", 0},
{"branch-and.q", "", 0},
{"branch-or.q", "", 0},
{"branch-combined.q", "", 0},
{"empty", "", 0},
{"math", "", 10},
{"precedence", "", 10},
{"square-sum", "", 25},
{"chained-calls", "", 9},
{"nested-calls", "", 4},
{"overwrite", "", 3},
{"reuse", "", 3},
{"return", "", 6},
{"reassign", "", 2},
{"branch", "", 0},
{"branch-and", "", 0},
{"branch-or", "", 0},
{"branch-combined", "", 0},
}
func TestPrograms(t *testing.T) {
for _, test := range programs {
t.Run(test.Name, func(t *testing.T) {
run(t, filepath.Join("programs", test.Name), test.ExpectedOutput, test.ExpectedExitCode)
run(t, filepath.Join("programs", test.Name+".q"), test.ExpectedOutput, test.ExpectedExitCode)
})
}
}
@ -57,34 +57,30 @@ func run(t *testing.T, name string, expectedOutput string, expectedExitCode int)
b := build.New(name)
assert.True(t, len(b.Executable()) > 0)
t.Run("Compile", func(t *testing.T) {
result, err := b.Run()
assert.Nil(t, err)
result, err := b.Run()
assert.Nil(t, err)
err = result.Write(b.Executable())
assert.Nil(t, err)
err = result.Write(b.Executable())
assert.Nil(t, err)
stat, err := os.Stat(b.Executable())
assert.Nil(t, err)
assert.True(t, stat.Size() > 0)
})
stat, err := os.Stat(b.Executable())
assert.Nil(t, err)
assert.True(t, stat.Size() > 0)
t.Run("Output", func(t *testing.T) {
cmd := exec.Command(b.Executable())
output, err := cmd.Output()
exitCode := 0
cmd := exec.Command(b.Executable())
output, err := cmd.Output()
exitCode := 0
if err != nil {
exitError, ok := err.(*exec.ExitError)
if err != nil {
exitError, ok := err.(*exec.ExitError)
if !ok {
t.Fatal(exitError)
}
exitCode = exitError.ExitCode()
if !ok {
t.Fatal(exitError)
}
assert.Equal(t, exitCode, expectedExitCode)
assert.DeepEqual(t, string(output), expectedOutput)
})
exitCode = exitError.ExitCode()
}
assert.Equal(t, exitCode, expectedExitCode)
assert.DeepEqual(t, string(output), expectedOutput)
}