github.com/klaytn/klaytn@v1.10.2/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  	"io/ioutil"
    28  	"os"
    29  	"path/filepath"
    30  	"testing"
    31  )
    32  
    33  // Package RLP is loaded only once and reused for all tests.
    34  var (
    35  	testFset       = token.NewFileSet()
    36  	testImporter   = importer.ForCompiler(testFset, "source", nil).(types.ImporterFrom)
    37  	testPackageRLP *types.Package
    38  )
    39  
    40  func init() {
    41  	cwd, err := os.Getwd()
    42  	if err != nil {
    43  		panic(err)
    44  	}
    45  	testPackageRLP, err = testImporter.ImportFrom(pathOfPackageRLP, cwd, 0)
    46  	if err != nil {
    47  		panic(fmt.Errorf("can't load package RLP: %v", err))
    48  	}
    49  }
    50  
    51  var tests = []string{"uints", "nil", "rawvalue", "optional", "bigint"}
    52  
    53  func TestOutput(t *testing.T) {
    54  	for _, test := range tests {
    55  		test := test
    56  		t.Run(test, func(t *testing.T) {
    57  			inputFile := filepath.Join("testdata", test+".in.txt")
    58  			outputFile := filepath.Join("testdata", test+".out.txt")
    59  			bctx, typ, err := loadTestSource(inputFile, "Test")
    60  			if err != nil {
    61  				t.Fatal("error loading test source:", err)
    62  			}
    63  			output, err := bctx.generate(typ, true, true)
    64  			if err != nil {
    65  				t.Fatal("error in generate:", err)
    66  			}
    67  
    68  			// Set this environment variable to regenerate the test outputs.
    69  			if os.Getenv("WRITE_TEST_FILES") != "" {
    70  				ioutil.WriteFile(outputFile, output, 0o644)
    71  			}
    72  
    73  			// Check if output matches.
    74  			wantOutput, err := ioutil.ReadFile(outputFile)
    75  			if err != nil {
    76  				t.Fatal("error loading expected test output:", err)
    77  			}
    78  			if !bytes.Equal(output, wantOutput) {
    79  				t.Fatal("output mismatch:\n", string(output))
    80  			}
    81  		})
    82  	}
    83  }
    84  
    85  func loadTestSource(file string, typeName string) (*buildContext, *types.Named, error) {
    86  	// Load the test input.
    87  	content, err := ioutil.ReadFile(file)
    88  	if err != nil {
    89  		return nil, nil, err
    90  	}
    91  	f, err := parser.ParseFile(testFset, file, content, 0)
    92  	if err != nil {
    93  		return nil, nil, err
    94  	}
    95  	conf := types.Config{Importer: testImporter}
    96  	pkg, err := conf.Check("test", testFset, []*ast.File{f}, nil)
    97  	if err != nil {
    98  		return nil, nil, err
    99  	}
   100  
   101  	// Find the test struct.
   102  	bctx := newBuildContext(testPackageRLP)
   103  	typ, err := lookupStructType(pkg.Scope(), typeName)
   104  	if err != nil {
   105  		return nil, nil, fmt.Errorf("can't find type %s: %v", typeName, err)
   106  	}
   107  	return bctx, typ, nil
   108  }