github.com/chain5j/chain5j-pkg@v1.0.7/codec/rlp/rlpgen/gen_test.go (about)

     1  // Copyright 2022 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package main
    18  
    19  import (
    20  	"bytes"
    21  	"fmt"
    22  	"go/ast"
    23  	"go/importer"
    24  	"go/parser"
    25  	"go/token"
    26  	"go/types"
    27  	"os"
    28  	"path/filepath"
    29  	"testing"
    30  )
    31  
    32  // Package RLP is loaded only once and reused for all tests.
    33  var (
    34  	testFset       = token.NewFileSet()
    35  	testImporter   = importer.ForCompiler(testFset, "source", nil).(types.ImporterFrom)
    36  	testPackageRLP *types.Package
    37  )
    38  
    39  func init() {
    40  	cwd, err := os.Getwd()
    41  	if err != nil {
    42  		panic(err)
    43  	}
    44  	testPackageRLP, err = testImporter.ImportFrom(pathOfPackageRLP, cwd, 0)
    45  	if err != nil {
    46  		panic(fmt.Errorf("can't load package RLP: %v", err))
    47  	}
    48  }
    49  
    50  var tests = []string{"uints", "nil", "rawvalue", "optional", "bigint"}
    51  
    52  func TestOutput(t *testing.T) {
    53  	for _, test := range tests {
    54  		test := test
    55  		t.Run(test, func(t *testing.T) {
    56  			inputFile := filepath.Join("testdata", test+".in.txt")
    57  			outputFile := filepath.Join("testdata", test+".out.txt")
    58  			bctx, typ, err := loadTestSource(inputFile, "Test")
    59  			if err != nil {
    60  				t.Fatal("error loading test source:", err)
    61  			}
    62  			output, err := bctx.generate(typ, true, true)
    63  			if err != nil {
    64  				t.Fatal("error in generate:", err)
    65  			}
    66  
    67  			// Set this environment variable to regenerate the test outputs.
    68  			if os.Getenv("WRITE_TEST_FILES") != "" {
    69  				os.WriteFile(outputFile, output, 0644)
    70  			}
    71  
    72  			// Check if output matches.
    73  			wantOutput, err := os.ReadFile(outputFile)
    74  			if err != nil {
    75  				t.Fatal("error loading expected test output:", err)
    76  			}
    77  			if !bytes.Equal(output, wantOutput) {
    78  				t.Fatal("output mismatch:\n", string(output))
    79  			}
    80  		})
    81  	}
    82  }
    83  
    84  func loadTestSource(file string, typeName string) (*buildContext, *types.Named, error) {
    85  	// Load the test input.
    86  	content, err := os.ReadFile(file)
    87  	if err != nil {
    88  		return nil, nil, err
    89  	}
    90  	f, err := parser.ParseFile(testFset, file, content, 0)
    91  	if err != nil {
    92  		return nil, nil, err
    93  	}
    94  	conf := types.Config{Importer: testImporter}
    95  	pkg, err := conf.Check("test", testFset, []*ast.File{f}, nil)
    96  	if err != nil {
    97  		return nil, nil, err
    98  	}
    99  
   100  	// Find the test struct.
   101  	bctx := newBuildContext(testPackageRLP)
   102  	typ, err := lookupStructType(pkg.Scope(), typeName)
   103  	if err != nil {
   104  		return nil, nil, fmt.Errorf("can't find type %s: %v", typeName, err)
   105  	}
   106  	return bctx, typ, nil
   107  }