github.com/visualfc/goembed@v0.3.3/parser/parser_test.go (about)

     1  package parser_test
     2  
     3  import (
     4  	"go/ast"
     5  	"go/build"
     6  	"go/parser"
     7  	"go/token"
     8  	"path/filepath"
     9  	"reflect"
    10  	"testing"
    11  
    12  	embedparser "github.com/visualfc/goembed/parser"
    13  )
    14  
    15  func TestParser(t *testing.T) {
    16  	bp, err := build.Import("github.com/visualfc/goembed", "", 0)
    17  	if err != nil {
    18  		t.Fatal(err)
    19  	}
    20  	t.Log("import test embed", bp.TestEmbedPatterns, bp.TestEmbedPatternPos)
    21  	fset := token.NewFileSet()
    22  	var files []*ast.File
    23  	for _, filename := range bp.TestGoFiles {
    24  		f, err := parser.ParseFile(fset, filepath.Join(bp.Dir, filename), nil, parser.ParseComments)
    25  		if err != nil {
    26  			t.Fatal(err)
    27  		}
    28  		files = append(files, f)
    29  	}
    30  	embed, err := embedparser.ParseEmbed(fset, files)
    31  	if err != nil {
    32  		t.Fatal(err)
    33  	}
    34  	t.Log("parser test embed", embed)
    35  	if !reflect.DeepEqual(bp.TestEmbedPatterns, embed.Patterns) {
    36  		t.Fatal("EmbedPatterns error")
    37  	}
    38  	if len(bp.TestEmbedPatternPos) != len(embed.PatternPos) {
    39  		t.Fatal("EmbedPatternPos len error")
    40  	}
    41  	for k, v := range bp.TestEmbedPatternPos {
    42  		v2, ok := embed.PatternPos[k]
    43  		if !ok {
    44  			t.Fatal("not found", k)
    45  		}
    46  		if !reflect.DeepEqual(v, v2) {
    47  			t.Fatal("not equal", v, v2)
    48  		}
    49  	}
    50  }