github.com/cilium/ebpf@v0.16.0/cmd/bpf2go/gen/output_test.go (about) 1 package gen 2 3 import ( 4 "bytes" 5 "fmt" 6 "testing" 7 8 "github.com/go-quicktest/qt" 9 "github.com/google/go-cmp/cmp" 10 11 "github.com/cilium/ebpf/btf" 12 "github.com/cilium/ebpf/cmd/bpf2go/internal" 13 ) 14 15 func TestOrderTypes(t *testing.T) { 16 a := &btf.Int{} 17 b := &btf.Int{} 18 c := &btf.Int{} 19 20 for _, test := range []struct { 21 name string 22 in map[btf.Type]string 23 out []btf.Type 24 }{ 25 { 26 "order", 27 map[btf.Type]string{ 28 a: "foo", 29 b: "bar", 30 c: "baz", 31 }, 32 []btf.Type{b, c, a}, 33 }, 34 } { 35 t.Run(test.name, func(t *testing.T) { 36 result, err := sortTypes(test.in) 37 qt.Assert(t, qt.IsNil(err)) 38 qt.Assert(t, qt.Equals(len(result), len(test.out))) 39 for i, o := range test.out { 40 if result[i] != o { 41 t.Fatalf("Index %d: expected %p got %p", i, o, result[i]) 42 } 43 } 44 }) 45 } 46 47 for _, test := range []struct { 48 name string 49 in map[btf.Type]string 50 }{ 51 { 52 "duplicate names", 53 map[btf.Type]string{ 54 a: "foo", 55 b: "foo", 56 }, 57 }, 58 } { 59 t.Run(test.name, func(t *testing.T) { 60 result, err := sortTypes(test.in) 61 qt.Assert(t, qt.IsNotNil(err)) 62 qt.Assert(t, qt.IsNil(result)) 63 }) 64 } 65 } 66 67 func TestPackageImport(t *testing.T) { 68 var buf bytes.Buffer 69 err := Generate(GenerateArgs{ 70 Package: "foo", 71 Stem: "bar", 72 ObjectFile: "frob.o", 73 Output: &buf, 74 }) 75 qt.Assert(t, qt.IsNil(err)) 76 // NB: It'd be great to test that this is the case for callers outside of 77 // this module, but that is kind of tricky. 78 qt.Assert(t, qt.StringContains(buf.String(), fmt.Sprintf(`"%s"`, internal.CurrentModule))) 79 } 80 81 var typesEqualComparer = cmp.Comparer(func(a, b btf.Type) bool { 82 return a == b 83 })