-
Notifications
You must be signed in to change notification settings - Fork 11
new package cl/cltest #702
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| unsigned f(int a); | ||
|
|
||
| void g(); | ||
|
|
||
| signed int xprintf(const char* fmt, ...); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| /* | ||
| * Copyright (c) 2026 The XGo Authors (xgo.dev). 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 ( | ||
| "os" | ||
| "path" | ||
| "strings" | ||
| "testing" | ||
| ) | ||
|
|
||
| // ----------------------------------------------------------------------------- | ||
|
|
||
| // TestFromDir runs testFunc for each subdirectory of relDir. If sel is not empty, only subdirectories | ||
| // whose path contains sel will be tested. | ||
| func TestFromDir(t *testing.T, sel, relDir string, testFunc func(t *testing.T, pkgDir string)) { | ||
| 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 strings.HasPrefix(name, "_") { | ||
| continue | ||
| } | ||
| t.Run(name, func(t *testing.T) { | ||
| pkgDir := dir + "/" + name | ||
| if sel != "" && !strings.Contains(pkgDir, sel) { | ||
| return | ||
| } | ||
| testFunc(t, pkgDir) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // ----------------------------------------------------------------------------- | ||
|
|
||
| // MockNameLookup is a mock implementation of the NameLookup function. It returns a fixed archive | ||
| // path and true for any input. | ||
| func MockNameLookup(manglingName string) (archivePath string, ok bool) { | ||
| return "libfoo.a", true | ||
| } | ||
|
|
||
| // ----------------------------------------------------------------------------- | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -56,7 +56,7 @@ func (p *nodeInterp) Position(start token.Pos) token.Position { | |
| } | ||
|
|
||
| func (p *nodeInterp) LoadExpr(v ast.Node) string { | ||
| panic("todo") | ||
| panic("todo: nodeInterp.LoadExpr") | ||
| } | ||
|
|
||
| // ----------------------------------------------------------------------------- | ||
|
|
@@ -91,13 +91,16 @@ type Config struct { | |
|
|
||
| // Reused specifies to reuse the Package instance between processing multiple C/C++ header files. | ||
| *Reused | ||
|
|
||
| // NameLookup looks up the archive path for a given mangling name. It returns the archive path and a boolean indicating whether the lookup was successful. | ||
| NameLookup func(manglingName string) (archivePath string, ok bool) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [P2] Config.NameLookup is declared but never consumed The new |
||
| } | ||
|
|
||
| // ----------------------------------------------------------------------------- | ||
|
|
||
| // Source represents a C/C++ header to compile. | ||
| type Source struct { | ||
| clang.TranslationUnit | ||
| TU clang.TranslationUnit | ||
| PresumedFile *c.Char | ||
| } | ||
|
|
||
|
|
@@ -139,7 +142,7 @@ func loadFile(p *gogen.Package, conf *Config, file Source) (pi *PkgInfo, err err | |
| pkg: p, cb: p.CB(), fset: p.Fset, | ||
| } | ||
| _ = conf | ||
| clang.VisitChildren(file.Cursor(), func(decl, parent clang.Cursor) clang.ChildVisitResult { | ||
| clang.VisitChildren(file.TU.Cursor(), func(decl, parent clang.Cursor) clang.ChildVisitResult { | ||
| compileDecl(ctx, decl) | ||
| return clang.Continue | ||
| }) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,47 +14,64 @@ | |
| * limitations under the License. | ||
| */ | ||
|
|
||
| package cl | ||
| package cl_test | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "os" | ||
| "path" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/goplus/gogen" | ||
| "github.com/goplus/llcppg/cl" | ||
| "github.com/goplus/llcppg/cl/cltest" | ||
| "github.com/goplus/llcppg/clang" | ||
| "github.com/qiniu/x/test" | ||
| ) | ||
|
|
||
| // ----------------------------------------------------------------------------- | ||
|
|
||
| func DoTestFromDir(t *testing.T, sel, relDir string, testFunc func(t *testing.T, pkgDir string)) { | ||
| dir, err := os.Getwd() | ||
| if err != nil { | ||
| t.Fatal("Getwd failed:", err) | ||
| func testDiff(t *testing.T, dir string, outfname string, b *bytes.Buffer, exp any) { | ||
| if expected, ok := exp.(string); ok { | ||
| result := b.String() | ||
| if result != expected { | ||
| t.Errorf("\nResult:\n%s\nExpected:\n%s\n", result, expected) | ||
| } | ||
| } else if test.Diff(t, dir+outfname, b.Bytes(), exp.([]byte)) { | ||
| t.Error(dir, ": unexpect result") | ||
| } | ||
| dir = path.Join(dir, relDir) | ||
| fis, err := os.ReadDir(dir) | ||
| } | ||
|
|
||
| func testGenGo(t *testing.T, pkg *gogen.Package, dir string, exp any) { | ||
| var b bytes.Buffer | ||
| err := pkg.WriteTo(&b) | ||
| if err != nil { | ||
| t.Fatal("ReadDir failed:", err) | ||
| } | ||
| for _, fi := range fis { | ||
| name := fi.Name() | ||
| if strings.HasPrefix(name, "_") { | ||
| continue | ||
| } | ||
| t.Run(name, func(t *testing.T) { | ||
| pkgDir := dir + "/" + name | ||
| if sel != "" && !strings.Contains(pkgDir, sel) { | ||
| return | ||
| } | ||
| testFunc(t, pkgDir) | ||
| }) | ||
| t.Fatal("gogen.WriteTo failed:", err) | ||
| } | ||
| testDiff(t, dir, "/result.txt", &b, exp) | ||
| } | ||
|
|
||
| // ----------------------------------------------------------------------------- | ||
| /* | ||
| func testFromDir(t *testing.T, sel, relDir string) { | ||
| DoTestFromDir(t, sel, relDir, func(t *testing.T, pkgDir string) { | ||
| cltest.TestFromDir(t, sel, relDir, func(t *testing.T, pkgDir string) { | ||
| idx := clang.CreateIndex(0, 0) | ||
| defer idx.Dispose() | ||
|
|
||
| u := idx.ParseTranslationUnit(0, pkgDir+"/in.h", "-x", "c") | ||
| defer u.Dispose() | ||
|
|
||
| pkg, err := cl.NewPackage("", "foo", cl.Source{TU: u}, &cl.Config{ | ||
| NameLookup: cltest.MockNameLookup, | ||
| }) | ||
| if err != nil { | ||
| t.Error("cl.NewPackage:", err) | ||
| return | ||
| } | ||
| exp, _ := os.ReadFile(pkgDir + "/out.go") | ||
| testGenGo(t, pkg.Package, pkgDir, exp) | ||
| }) | ||
| } | ||
| */ | ||
|
|
||
| func _TestMockC(t *testing.T) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [P2] Only test of new path (_TestMockC) is disabled
|
||
| testFromDir(t, "", "./_testmockc") | ||
| } | ||
|
|
||
| // ----------------------------------------------------------------------------- | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,4 +5,5 @@ go 1.27.0 | |
| require ( | ||
| github.com/goplus/gogen v1.23.5 | ||
| github.com/goplus/lib v0.5.2 | ||
| github.com/qiniu/x v1.18.3 | ||
| ) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P3] TestFromDir sel filter runs inside t.Run, emitting empty subtests
The
sel != "" && !strings.Contains(pkgDir, sel)check is inside thet.Run(name, ...)closure, so every non-matching directory still spawns an (empty, passing) subtest and returns early rather than being skipped. The doc says "only subdirectories whose path contains sel will be tested," which reads as full exclusion. Behavior is unchanged from the original, but now that this is a shared exported helper, consider filtering beforet.Runor usingt.Skipto avoid noise-level subtests.