github.com/bakjos/protoreflect@v1.9.2/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/bakjos/protoreflect/desc/protoparse/ast"
    11  	"github.com/bakjos/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.Ext(path) == ".proto" {
    20  			t.Run(path, func(t *testing.T) {
    21  				b, err := ioutil.ReadFile(path)
    22  				testutil.Ok(t, err)
    23  				data := string(b)
    24  				filename := filepath.Base(path)
    25  				accessor := FileContentsFromMap(map[string]string{filename: data})
    26  				p := Parser{Accessor: accessor}
    27  				root, err := p.ParseToAST(filepath.Base(path))
    28  				testutil.Ok(t, err)
    29  				testutil.Eq(t, 1, len(root))
    30  				var buf bytes.Buffer
    31  				err = ast.Print(&buf, root[0])
    32  				testutil.Ok(t, err)
    33  				// see if file survived round trip!
    34  				testutil.Eq(t, data, buf.String())
    35  			})
    36  		}
    37  		return nil
    38  	})
    39  	testutil.Ok(t, err)
    40  }