~mna/snow

99bb688cfa30b155f39a2522316df9e3a84de3df — Martin Angers 1 year, 11 months ago 9f40f10
refactor source-file-based tests to use a helper package
M pkg/codegen/codegen_test.go => pkg/codegen/codegen_test.go +4 -12
@@ 9,6 9,7 @@ import (
	"path/filepath"
	"testing"

	"git.sr.ht/~mna/snow/pkg/internal/filetest"
	"github.com/kylelemons/godebug/diff"
)



@@ 16,20 17,11 @@ var testUpdateCodegenTests = flag.Bool("test.update-codegen-tests", false, "If s

func TestGenerator(t *testing.T) {
	baseDir := "testdata"
	fis, err := ioutil.ReadDir(baseDir)
	if err != nil {
		t.Fatal(err)
	}

	for _, fi := range fis {
		if !fi.Mode().IsRegular() {
			continue
		}
		if filepath.Ext(fi.Name()) != ".snow" {
			continue
		}

	for _, fi := range filetest.SourceFiles(t, baseDir) {
		t.Run(fi.Name(), func(t *testing.T) {
			t.Parallel()

			dir, err := ioutil.TempDir(baseDir, "")
			if err != nil {
				t.Fatal(err)

A pkg/internal/filetest/filetest.go => pkg/internal/filetest/filetest.go +30 -0
@@ 0,0 1,30 @@
package filetest

import (
	"io/ioutil"
	"os"
	"path/filepath"
	"testing"
)

// SourceFiles returns the list of source .snow files in dir.
func SourceFiles(t *testing.T, dir string) []os.FileInfo {
	t.Helper()

	fis, err := ioutil.ReadDir(dir)
	if err != nil {
		t.Fatal(err)
	}

	res := make([]os.FileInfo, 0, len(fis))
	for _, fi := range fis {
		if !fi.Mode().IsRegular() {
			continue
		}
		if filepath.Ext(fi.Name()) != ".snow" {
			continue
		}
		res = append(res, fi)
	}
	return res
}

M pkg/typecheck/check_pass_test.go => pkg/typecheck/check_pass_test.go +4 -12
@@ 8,6 8,7 @@ import (
	"path/filepath"
	"testing"

	"git.sr.ht/~mna/snow/pkg/internal/filetest"
	"git.sr.ht/~mna/snow/pkg/printer"
	"git.sr.ht/~mna/snow/pkg/scanner"
	"git.sr.ht/~mna/snow/pkg/typecheck"


@@ 19,20 20,11 @@ var testUpdateCheckTests = flag.Bool("test.update-check-tests", false, "If set, 
func TestCheck(t *testing.T) {
	baseDir := "testdata"
	expectDir := filepath.Join(baseDir, "check")
	fis, err := ioutil.ReadDir(baseDir)
	if err != nil {
		t.Fatal(err)
	}

	for _, fi := range fis {
		if !fi.Mode().IsRegular() {
			continue
		}
		if filepath.Ext(fi.Name()) != ".snow" {
			continue
		}

	for _, fi := range filetest.SourceFiles(t, baseDir) {
		t.Run(fi.Name(), func(t *testing.T) {
			t.Parallel()

			unit, err := typecheck.CheckFiles(typecheck.TypeCheckPass, filepath.Join(baseDir, fi.Name()))
			if unit == nil && err != nil {
				t.Fatal(err)

M pkg/typecheck/query_test.go => pkg/typecheck/query_test.go +4 -13
@@ 1,33 1,24 @@
package typecheck

import (
	"io/ioutil"
	"path/filepath"
	"strconv"
	"strings"
	"testing"

	"git.sr.ht/~mna/snow/pkg/ast"
	"git.sr.ht/~mna/snow/pkg/internal/filetest"
)

//var testUpdateQueryTests = flag.Bool("test.update-query-tests", false, "If set, replace expected typecheck query test results with actual results.")

func TestQuery_Fallthrough(t *testing.T) {
	baseDir := filepath.Join("testdata", "query", "fallthrough")
	fis, err := ioutil.ReadDir(baseDir)
	if err != nil {
		t.Fatal(err)
	}

	for _, fi := range fis {
		if !fi.Mode().IsRegular() {
			continue
		}
		if filepath.Ext(fi.Name()) != ".snow" {
			continue
		}

	for _, fi := range filetest.SourceFiles(t, baseDir) {
		t.Run(fi.Name(), func(t *testing.T) {
			t.Parallel()

			unit, err := CheckFiles(ScopePass, filepath.Join(baseDir, fi.Name()))
			if err != nil {
				t.Fatal(err)

M pkg/typecheck/scope_pass_test.go => pkg/typecheck/scope_pass_test.go +4 -12
@@ 8,6 8,7 @@ import (
	"path/filepath"
	"testing"

	"git.sr.ht/~mna/snow/pkg/internal/filetest"
	"git.sr.ht/~mna/snow/pkg/scanner"
	"github.com/kylelemons/godebug/diff"
)


@@ 17,20 18,11 @@ var testUpdateScopesTests = flag.Bool("test.update-scopes-tests", false, "If set
func TestScopes(t *testing.T) {
	baseDir := "testdata"
	expectDir := filepath.Join(baseDir, "scopes")
	fis, err := ioutil.ReadDir(baseDir)
	if err != nil {
		t.Fatal(err)
	}

	for _, fi := range fis {
		if !fi.Mode().IsRegular() {
			continue
		}
		if filepath.Ext(fi.Name()) != ".snow" {
			continue
		}

	for _, fi := range filetest.SourceFiles(t, baseDir) {
		t.Run(fi.Name(), func(t *testing.T) {
			t.Parallel()

			unit, err := CheckFiles(ScopePass, filepath.Join(baseDir, fi.Name()))
			if unit == nil && err != nil {
				t.Fatal(err)

M pkg/typecheck/static_pass_test.go => pkg/typecheck/static_pass_test.go +4 -12
@@ 8,6 8,7 @@ import (
	"path/filepath"
	"testing"

	"git.sr.ht/~mna/snow/pkg/internal/filetest"
	"git.sr.ht/~mna/snow/pkg/scanner"
	"git.sr.ht/~mna/snow/pkg/typecheck"
	"github.com/kylelemons/godebug/diff"


@@ 18,20 19,11 @@ var testUpdateStaticTests = flag.Bool("test.update-static-tests", false, "If set
func TestStatic(t *testing.T) {
	baseDir := "testdata"
	expectDir := filepath.Join(baseDir, "static")
	fis, err := ioutil.ReadDir(baseDir)
	if err != nil {
		t.Fatal(err)
	}

	for _, fi := range fis {
		if !fi.Mode().IsRegular() {
			continue
		}
		if filepath.Ext(fi.Name()) != ".snow" {
			continue
		}

	for _, fi := range filetest.SourceFiles(t, baseDir) {
		t.Run(fi.Name(), func(t *testing.T) {
			t.Parallel()

			unit, err := typecheck.CheckFiles(typecheck.StaticAnalysisPass, filepath.Join(baseDir, fi.Name()))
			if unit == nil && err != nil {
				t.Fatal(err)

M pkg/typecheck/type_pass_test.go => pkg/typecheck/type_pass_test.go +4 -12
@@ 8,6 8,7 @@ import (
	"path/filepath"
	"testing"

	"git.sr.ht/~mna/snow/pkg/internal/filetest"
	"git.sr.ht/~mna/snow/pkg/printer"
	"git.sr.ht/~mna/snow/pkg/scanner"
	"git.sr.ht/~mna/snow/pkg/typecheck"


@@ 19,20 20,11 @@ var testUpdateTypesTests = flag.Bool("test.update-types-tests", false, "If set, 
func TestTypes(t *testing.T) {
	baseDir := "testdata"
	expectDir := filepath.Join(baseDir, "types")
	fis, err := ioutil.ReadDir(baseDir)
	if err != nil {
		t.Fatal(err)
	}

	for _, fi := range fis {
		if !fi.Mode().IsRegular() {
			continue
		}
		if filepath.Ext(fi.Name()) != ".snow" {
			continue
		}

	for _, fi := range filetest.SourceFiles(t, baseDir) {
		t.Run(fi.Name(), func(t *testing.T) {
			t.Parallel()

			unit, err := typecheck.CheckFiles(typecheck.TypeAssignPass, filepath.Join(baseDir, fi.Name()))
			if unit == nil && err != nil {
				t.Fatal(err)