github.com/jhump/protoreflect@v1.16.0/desc/protoparse/ast_roundtrip_test.go (about)

     1  package protoparse
     2  
     3  import (
     4  	"bytes"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  	"testing"
     9  
    10  	"github.com/jhump/protoreflect/desc/protoparse/ast"
    11  	"github.com/jhump/protoreflect/internal/testutil"
    12  )
    13  
    14  func TestASTRoundTrips(t *testing.T) {
    15  	err := filepath.Walk("../../internal/testprotos", func(path string, info os.FileInfo, err error) error {
    16  		if err != nil {
    17  			return err
    18  		}
    19  		if filepath.Base(path) == "desc_test_editions.proto" {
    20  			// The AST doesn't support editions yet
    21  			return nil
    22  		}
    23  		if filepath.Ext(path) == ".proto" {
    24  			t.Run(path, func(t *testing.T) {
    25  				b, err := ioutil.ReadFile(path)
    26  				testutil.Ok(t, err)
    27  				data := string(b)
    28  				filename := filepath.Base(path)
    29  				accessor := FileContentsFromMap(map[string]string{filename: data})
    30  				p := Parser{Accessor: accessor}
    31  				root, err := p.ParseToAST(filepath.Base(path))
    32  				testutil.Ok(t, err)
    33  				testutil.Eq(t, 1, len(root))
    34  				var buf bytes.Buffer
    35  				err = ast.Print(&buf, root[0])
    36  				testutil.Ok(t, err)
    37  				// see if file survived round trip!
    38  				testutil.Eq(t, data, buf.String())
    39  			})
    40  		}
    41  		return nil
    42  	})
    43  	testutil.Ok(t, err)
    44  }