1package client
2
3import (
4 "strings"
5 "testing"
6)
7
8const optionsXML = `<protocol name="opt"><copyright>Copyright test
9SPDX-License-Identifier: MIT</copyright><interface name="opt_thing" version="1"/></protocol>`
10
11func generateOptions(t *testing.T, mutate func(*Scanner)) string {
12 t.Helper()
13 s := NewScanner()
14 if err := s.Load([]byte(optionsXML), "opt.xml"); err != nil {
15 t.Fatal(err)
16 }
17 if mutate != nil {
18 mutate(s)
19 }
20 b, err := s.Generate("opt")
21 if err != nil {
22 t.Fatal(err)
23 }
24 return string(b)
25}
26
27func TestToolNameInHeader(t *testing.T) {
28 if got := generateOptions(t, nil); !strings.HasPrefix(got, "// Code generated by wlbgen. DO NOT EDIT.\n") {
29 t.Fatalf("default header: %q", strings.SplitN(got, "\n", 2)[0])
30 }
31 got := generateOptions(t, func(s *Scanner) { s.Tool = "wlturbo-scanner" })
32 if !strings.HasPrefix(got, "// Code generated by wlturbo-scanner. DO NOT EDIT.\n") {
33 t.Fatalf("custom header: %q", strings.SplitN(got, "\n", 2)[0])
34 }
35}
36
37func TestSPDXLineOnlyWhenRequested(t *testing.T) {
38 const line = "// SPDX-License-Identifier: LGPL-2.1-or-later\n"
39 if got := generateOptions(t, nil); strings.Contains(got, line) {
40 t.Fatal("SPDX line emitted without request")
41 }
42 got := generateOptions(t, func(s *Scanner) { s.SPDXLicense = "LGPL-2.1-or-later" })
43 header, _, _ := strings.Cut(got, "\npackage opt")
44 if strings.Count(header, line) != 1 || !strings.Contains(header, "// Copyright test") {
45 t.Fatalf("header: %s", header)
46 }
47}
48
49func TestSPDXNotDuplicatedWhenCopyrightHasIt(t *testing.T) {
50 got := generateOptions(t, func(s *Scanner) { s.SPDXLicense = "MIT" })
51 if n := strings.Count(got, "SPDX-License-Identifier: MIT"); n != 1 {
52 t.Fatalf("SPDX line count = %d", n)
53 }
54}
55
56func TestCoreDetectionFollowsWlDisplay(t *testing.T) {
57 const xml = `<protocol name="x"><interface name="wl_display" version="1"/><interface name="wl_registry" version="1"/><interface name="wl_thing" version="1"/></protocol>`
58 for _, pkg := range []string{"wayland", "core", "anything"} {
59 s := NewScanner()
60 if err := s.Load([]byte(xml), "x.xml"); err != nil {
61 t.Fatal(err)
62 }
63 b, err := s.Generate(pkg)
64 if err != nil {
65 t.Fatal(err)
66 }
67 if strings.Contains(string(b), "type Display struct") || strings.Contains(string(b), "type Registry struct") || !strings.Contains(string(b), "type Thing struct") {
68 t.Fatalf("package %s: bootstrap types not skipped", pkg)
69 }
70 }
71 // A package named "core" that does not define wl_display is not core.
72 s := NewScanner()
73 if err := s.Load([]byte(`<protocol name="x"><interface name="wl_registry" version="1"/></protocol>`), "x.xml"); err != nil {
74 t.Fatal(err)
75 }
76 b, err := s.Generate("core")
77 if err != nil {
78 t.Fatal(err)
79 }
80 if !strings.Contains(string(b), "type Registry struct") {
81 t.Fatal("core name alone must not trigger core handling")
82 }
83}