Source file src/simd/archsimd/_gen/specdoc/comment_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  	"go/ast"
     9  	"go/parser"
    10  	"go/token"
    11  	"simd/archsimd/_gen/specgen"
    12  	"testing"
    13  )
    14  
    15  func TestParseCommentAndFormat(t *testing.T) {
    16  	doc := `// Add adds elements of
    17  // x and y.
    18  //
    19  //	z[i] = x[i] + y[i]
    20  //
    21  // Asm: VPADDD, CPU Feature: AVX
    22  //
    23  // Deprecated: use Add2 instead.
    24  //
    25  //go:noescape
    26  //go:noinline
    27  `
    28  	src := "package p\n\n" + doc + "func Add()"
    29  
    30  	fset := token.NewFileSet()
    31  	f, err := parser.ParseFile(fset, "test.go", src, parser.ParseComments)
    32  	if err != nil {
    33  		t.Fatal(err)
    34  	}
    35  
    36  	fd := f.Decls[0].(*ast.FuncDecl)
    37  	paras := parseCommentGroup(fd.Doc)
    38  
    39  	expected := []struct {
    40  		kind specgen.ParagraphKind
    41  		text string
    42  	}{
    43  		{specgen.SpecOwned, "Add adds elements of\nx and y."},
    44  		{specgen.SpecOwned, "\tz[i] = x[i] + y[i]"},
    45  		{specgen.ImplementationNote, "Asm: VPADDD, CPU Feature: AVX"},
    46  		{specgen.ImplementationNote, "Deprecated: use Add2 instead."},
    47  		{specgen.DirectiveComment, "go:noescape"},
    48  		{specgen.DirectiveComment, "go:noinline"},
    49  	}
    50  	if len(paras) != len(expected) {
    51  		t.Fatalf("expected %d paragraphs, got %d", len(expected), len(paras))
    52  	}
    53  	for i, want := range expected {
    54  		if paras[i].Kind != want.kind {
    55  			t.Errorf("para %d: got kind %v, want %v", i, paras[i].Kind, want.kind)
    56  		}
    57  		if paras[i].Text != want.text {
    58  			t.Errorf("para %d: got text %q, want %q", i, paras[i].Text, want.text)
    59  		}
    60  	}
    61  
    62  	// Test FormatComment round-trip
    63  	formatted, err := specgen.FormatComment(paras)
    64  	if err != nil {
    65  		t.Fatalf("unexpected error formatting comment: %v", err)
    66  	}
    67  	if formatted != doc {
    68  		t.Errorf("FormatComment mismatch:\ngot:\n%s\nwant:\n%s", formatted, doc)
    69  	}
    70  }
    71  

View as plain text