Source file src/simd/archsimd/_gen/specdoc/report_test.go

     1  // Copyright 2026 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package specdoc
     6  
     7  import (
     8  	"errors"
     9  	"go/token"
    10  	"strings"
    11  	"testing"
    12  )
    13  
    14  // Verify that all report types implement Finding and Report implements error.
    15  var (
    16  	_ Finding = TypeMismatch{}
    17  	_ Finding = NameMismatch{}
    18  	_ Finding = DocOrderViolation{}
    19  	_ Finding = UnknownDecl{}
    20  	_ Finding = UnexpectedDoc{}
    21  	_ error   = (*Report)(nil)
    22  )
    23  
    24  func TestReportEmpty(t *testing.T) {
    25  	var r Report
    26  	if !r.Empty() {
    27  		t.Errorf("expected empty report")
    28  	}
    29  	if s := r.Error(); s != "" {
    30  		t.Errorf("expected empty string, got: %q", s)
    31  	}
    32  }
    33  
    34  func TestReportPositionOrder(t *testing.T) {
    35  	r := Report{
    36  		UnknownDecls: []UnknownDecl{
    37  			{
    38  				D: Decl{
    39  					Pos:  token.Position{Filename: "b.go", Line: 20, Column: 1},
    40  					Recv: "T",
    41  					Name: "UnknownMethod",
    42  				},
    43  			},
    44  		},
    45  		TypeMismatches: []TypeMismatch{
    46  			{
    47  				D: Decl{
    48  					Pos:  token.Position{Filename: "a.go", Line: 10, Column: 1},
    49  					Recv: "T",
    50  					Name: "TypeOp",
    51  				},
    52  				DeclSig: "func (T) TypeOp(int)",
    53  				SpecSig: "func (T) TypeOp(int32)",
    54  			},
    55  		},
    56  		DocOrderViolations: []DocOrderViolation{
    57  			{
    58  				D: Decl{
    59  					Pos:  token.Position{Filename: "a.go", Line: 5, Column: 1},
    60  					Recv: "T",
    61  					Name: "DocOp",
    62  				},
    63  				Err: errors.New("comment order violation"),
    64  			},
    65  		},
    66  		NameMismatches: []NameMismatch{
    67  			{
    68  				D: Decl{
    69  					Pos:  token.Position{Filename: "b.go", Line: 10, Column: 1},
    70  					Recv: "T",
    71  					Name: "NameOp",
    72  				},
    73  				DeclSig: "func (T) NameOp(x int)",
    74  				SpecSig: "func (T) NameOp(y int)",
    75  			},
    76  		},
    77  		UnexpectedDocs: []UnexpectedDoc{
    78  			{
    79  				D: Decl{
    80  					Pos:  token.Position{Filename: "b.go", Line: 15, Column: 1},
    81  					Recv: "T",
    82  					Name: "UnexpectedDocOp",
    83  				},
    84  			},
    85  		},
    86  	}
    87  
    88  	s := r.Error()
    89  
    90  	// Should not have any markdown section headers.
    91  	if strings.Contains(s, "##") {
    92  		t.Errorf("expected no section headers in report, got:\n%s", s)
    93  	}
    94  
    95  	// Should report in position order:
    96  	// 1. a.go:5
    97  	// 2. a.go:10
    98  	// 3. b.go:10
    99  	// 4. b.go:15
   100  	// 5. b.go:20
   101  	idxA5 := strings.Index(s, "a.go:5")
   102  	idxA10 := strings.Index(s, "a.go:10")
   103  	idxB10 := strings.Index(s, "b.go:10")
   104  	idxB15 := strings.Index(s, "b.go:15")
   105  	idxB20 := strings.Index(s, "b.go:20")
   106  
   107  	if idxA5 < 0 || idxA10 < 0 || idxB10 < 0 || idxB15 < 0 || idxB20 < 0 {
   108  		t.Fatalf("missing expected entries in report:\n%s", s)
   109  	}
   110  
   111  	if !(idxA5 < idxA10 && idxA10 < idxB10 && idxB10 < idxB15 && idxB15 < idxB20) {
   112  		t.Errorf("expected positions in order a.go:5 < a.go:10 < b.go:10 < b.go:15 < b.go:20, got indices: a.go:5=%d, a.go:10=%d, b.go:10=%d, b.go:15=%d, b.go:20=%d\nReport:\n%s",
   113  			idxA5, idxA10, idxB10, idxB15, idxB20, s)
   114  	}
   115  }
   116  

View as plain text