Files
llgo/cl/cltest/cltest.go

132 lines
3.2 KiB
Go
Raw Normal View History

2024-04-25 07:29:45 +08:00
/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cltest
import (
"go/ast"
"go/parser"
"go/token"
"go/types"
"log"
"os"
"path"
"strings"
"testing"
"github.com/goplus/gogen/packages"
"github.com/goplus/llgo/cl"
2024-04-26 02:05:49 +08:00
"github.com/goplus/llgo/internal/llgen"
2024-04-25 07:29:45 +08:00
"golang.org/x/tools/go/ssa"
"golang.org/x/tools/go/ssa/ssautil"
llssa "github.com/goplus/llgo/ssa"
)
func init() {
cl.SetDebug(cl.DbgFlagAll)
2024-05-06 00:17:39 +08:00
llssa.Initialize(llssa.InitAll | llssa.InitNative)
2024-04-25 07:29:45 +08:00
llssa.SetDebug(llssa.DbgFlagAll)
}
2024-04-26 02:05:49 +08:00
func FromDir(t *testing.T, sel, relDir string, byLLGen bool) {
2024-04-25 07:29:45 +08:00
dir, err := os.Getwd()
if err != nil {
t.Fatal("Getwd failed:", err)
}
dir = path.Join(dir, relDir)
fis, err := os.ReadDir(dir)
if err != nil {
t.Fatal("ReadDir failed:", err)
}
for _, fi := range fis {
name := fi.Name()
if !fi.IsDir() || strings.HasPrefix(name, "_") {
continue
}
t.Run(name, func(t *testing.T) {
2024-04-26 02:05:49 +08:00
testFrom(t, dir+"/"+name, sel, byLLGen)
2024-04-25 07:29:45 +08:00
})
}
}
2024-04-28 12:09:47 +08:00
func Pkg(t *testing.T, pkgPath, outFile string) {
b, err := os.ReadFile(outFile)
if err != nil {
t.Fatal("ReadFile failed:", err)
}
expected := string(b)
if v := llgen.GenFrom(pkgPath); v != expected {
t.Fatalf("\n==> got:\n%s\n==> expected:\n%s\n", v, expected)
}
}
2024-04-26 02:05:49 +08:00
func testFrom(t *testing.T, pkgDir, sel string, byLLGen bool) {
2024-04-25 07:29:45 +08:00
if sel != "" && !strings.Contains(pkgDir, sel) {
return
}
log.Println("Parsing", pkgDir)
in := pkgDir + "/in.go"
out := pkgDir + "/out.ll"
2024-04-26 02:05:49 +08:00
b, err := os.ReadFile(out)
2024-04-25 07:29:45 +08:00
if err != nil {
t.Fatal("ReadFile failed:", err)
}
2024-04-26 02:05:49 +08:00
expected := string(b)
if byLLGen {
2024-04-28 12:09:47 +08:00
if v := llgen.GenFrom(in); v != expected {
2024-04-26 02:05:49 +08:00
t.Fatalf("\n==> got:\n%s\n==> expected:\n%s\n", v, expected)
}
} else {
TestCompileEx(t, nil, in, expected)
}
2024-04-25 07:29:45 +08:00
}
func TestCompileEx(t *testing.T, src any, fname, expected string) {
t.Helper()
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, fname, src, parser.ParseComments)
if err != nil {
t.Fatal("ParseFile failed:", err)
}
files := []*ast.File{f}
name := f.Name.Name
pkg := types.NewPackage(name, name)
imp := packages.NewImporter(fset)
foo, _, err := ssautil.BuildPackage(
&types.Config{Importer: imp}, fset, pkg, files, ssa.SanityCheckFunctions)
if err != nil {
t.Fatal("BuildPackage failed:", err)
}
foo.WriteTo(os.Stderr)
prog := llssa.NewProgram(nil)
2024-04-30 14:16:46 +08:00
prog.SetRuntime(func() *types.Package {
rt, err := imp.Import(llssa.PkgRuntime)
if err != nil {
t.Fatal("load runtime failed:", err)
}
return rt
})
2024-04-25 07:29:45 +08:00
ret, err := cl.NewPackage(prog, foo, files)
if err != nil {
t.Fatal("cl.NewPackage failed:", err)
}
if v := ret.String(); v != expected {
t.Fatalf("\n==> got:\n%s\n==> expected:\n%s\n", v, expected)
}
}