github.com/ethereum/go-ethereum@v1.16.1/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", "uint256"} 51 52 func TestOutput(t *testing.T) { 53 for _, test := range tests { 54 t.Run(test, func(t *testing.T) { 55 inputFile := filepath.Join("testdata", test+".in.txt") 56 outputFile := filepath.Join("testdata", test+".out.txt") 57 bctx, typ, err := loadTestSource(inputFile, "Test") 58 if err != nil { 59 t.Fatal("error loading test source:", err) 60 } 61 output, err := bctx.generate(typ, true, true) 62 if err != nil { 63 t.Fatal("error in generate:", err) 64 } 65 66 // Set this environment variable to regenerate the test outputs. 67 if os.Getenv("WRITE_TEST_FILES") != "" { 68 os.WriteFile(outputFile, output, 0644) 69 } 70 71 // Check if output matches. 72 wantOutput, err := os.ReadFile(outputFile) 73 if err != nil { 74 t.Fatal("error loading expected test output:", err) 75 } 76 if !bytes.Equal(output, wantOutput) { 77 t.Fatalf("output mismatch, want: %v got %v", string(wantOutput), string(output)) 78 } 79 }) 80 } 81 } 82 83 func loadTestSource(file string, typeName string) (*buildContext, *types.Named, error) { 84 // Load the test input. 85 content, err := os.ReadFile(file) 86 if err != nil { 87 return nil, nil, err 88 } 89 f, err := parser.ParseFile(testFset, file, content, 0) 90 if err != nil { 91 return nil, nil, err 92 } 93 conf := types.Config{Importer: testImporter} 94 pkg, err := conf.Check("test", testFset, []*ast.File{f}, nil) 95 if err != nil { 96 return nil, nil, err 97 } 98 99 // Find the test struct. 100 bctx := newBuildContext(testPackageRLP) 101 typ, err := lookupStructType(pkg.Scope(), typeName) 102 if err != nil { 103 return nil, nil, fmt.Errorf("can't find type %s: %v", typeName, err) 104 } 105 return bctx, typ, nil 106 }