github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/gen/codegen/gen_z_unit_test.go (about)

     1  package codegen_test
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	. "github.com/machinefi/w3bstream/pkg/depends/gen/codegen"
     8  )
     9  
    10  func CreateDemoFile() *File {
    11  	filename := "examples/hello/hello.go"
    12  	f := NewFile("main", filename)
    13  	f.WriteSnippet(Func().Named("main").Do(
    14  		Call(f.Use("fmt", "Println"), f.Value("Hello, 世界")),
    15  		Call(f.Use("github.com/some/pkg", "Println"), f.Value("Hello World!")),
    16  		Call(f.Use("github.com/another/pkg", "Println"), f.Value("Hello World!")),
    17  		Call(f.Use("github.com/one_more/pkg", "Println"), f.Value("Hello World!")),
    18  
    19  		Assign(AnonymousIdent).By(Call(f.Use("bytes", "NewBuffer"), f.Value(nil))),
    20  	))
    21  
    22  	return f
    23  }
    24  
    25  func ExampleNewFileFullFormat() {
    26  	f := CreateDemoFile()
    27  
    28  	defer os.RemoveAll("examples")
    29  
    30  	if _, err := f.Write(); err != nil {
    31  		panic(err)
    32  	}
    33  	if raw, err := os.ReadFile(f.Name); err != nil {
    34  		panic(err)
    35  	} else {
    36  		// NOTE: this test should always FAILED because the generated file
    37  		// contains `time` and `version` information
    38  		fmt.Println(string(raw))
    39  	}
    40  	// Output:
    41  	// // This is a generated source file. DO NOT EDIT
    42  	// // Source: main/hello.go
    43  	//
    44  	// package main
    45  	//
    46  	// import (
    47  	// 	"bytes"
    48  	// 	"fmt"
    49  	//
    50  	// 	gen_pkg_1 "github.com/another/pkg"
    51  	// 	gen_pkg_2 "github.com/one_more/pkg"
    52  	// 	"github.com/some/pkg"
    53  	// )
    54  	//
    55  	// func main() {
    56  	// 	fmt.Println("Hello, 世界")
    57  	// 	pkg.Println("Hello World!")
    58  	// 	gen_pkg_1.Println("Hello World!")
    59  	// 	gen_pkg_2.Println("Hello World!")
    60  	// 	_ = bytes.NewBuffer(nil)
    61  	// }
    62  }