forked from zzet/gortex
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpp_smartptr_test.go
More file actions
56 lines (51 loc) · 1.67 KB
/
Copy pathcpp_smartptr_test.go
File metadata and controls
56 lines (51 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package languages
import (
"testing"
"github.com/zzet/gortex/internal/graph"
)
// cppReturnTypeOf extracts src and returns the return_type meta of the first
// function/method node with the given name.
func cppReturnTypeOf(t *testing.T, src, name string) (string, bool) {
t.Helper()
res, err := NewCppExtractor().Extract("t.cpp", []byte(src))
if err != nil {
t.Fatalf("extract: %v", err)
}
for _, n := range res.Nodes {
if (n.Kind == graph.KindFunction || n.Kind == graph.KindMethod) && n.Name == name {
rt, ok := n.Meta["return_type"].(string)
return rt, ok
}
}
t.Fatalf("function/method %q not found", name)
return "", false
}
// TestCppReturnType_SmartPointerFreeFunction pins the smart-pointer pointee
// unwrap: a factory returning unique_ptr<Widget> records Widget as its return
// type (not unique_ptr), so a chained `make_widget()->draw()` infers Widget as
// the receiver.
func TestCppReturnType_SmartPointerFreeFunction(t *testing.T) {
src := `#include <memory>
struct Widget { void draw(); };
std::unique_ptr<Widget> make_widget() { return nullptr; }
`
rt, ok := cppReturnTypeOf(t, src, "make_widget")
if !ok || rt != "Widget" {
t.Errorf("make_widget return_type = %q (ok=%v), want Widget", rt, ok)
}
}
// TestCppReturnType_SmartPointerMethod pins the same unwrap on a class method
// returning shared_ptr<Widget>.
func TestCppReturnType_SmartPointerMethod(t *testing.T) {
src := `#include <memory>
struct Widget { void draw(); };
class Factory {
public:
std::shared_ptr<Widget> build() { return nullptr; }
};
`
rt, ok := cppReturnTypeOf(t, src, "build")
if !ok || rt != "Widget" {
t.Errorf("Factory::build return_type = %q (ok=%v), want Widget", rt, ok)
}
}