github.com/suntong/easygen@v5.3.0+incompatible/cmd/easygen/main_test.go (about)

     1  package main_test
     2  
     3  import (
     4  	"bytes"
     5  	"os"
     6  	"os/exec"
     7  	"strings"
     8  	"testing"
     9  )
    10  
    11  const (
    12  	cmdEasygen = "easygen"
    13  	dirTest    = "../../test/"
    14  	extRef     = ".ref" // extension for reference file
    15  	extGot     = ".got" // extension for generated file
    16  )
    17  
    18  // testEasygen runs @cmdEasyGen with @argv and compares the generated
    19  // output for @name with the corresponding @extRef
    20  func testEasygen(t *testing.T, name string, argv ...string) {
    21  	var (
    22  		diffOut         bytes.Buffer
    23  		generatedOutput = name + extGot
    24  		cmd             = exec.Command(cmdEasygen, argv...)
    25  	)
    26  
    27  	t.Logf("Testing %s:\n\t%s %s", name, cmdEasygen, strings.Join(argv, " "))
    28  
    29  	// open the out file for writing
    30  	outfile, err := os.Create(generatedOutput)
    31  	if err != nil {
    32  		t.Errorf("write error [%s: %s] %s.", name, argv, err)
    33  	}
    34  	defer outfile.Close()
    35  	cmd.Stdout = outfile
    36  
    37  	err = cmd.Start()
    38  	if err != nil {
    39  		t.Errorf("start error [%s: %s] %s.", name, argv, err)
    40  	}
    41  	err = cmd.Wait()
    42  	if err != nil {
    43  		t.Errorf("exit error [%s: %s] %s.", name, argv, err)
    44  	}
    45  
    46  	cmd = exec.Command("diff", "-U1", name+extRef, generatedOutput)
    47  	cmd.Stdout = &diffOut
    48  
    49  	err = cmd.Start()
    50  	if err != nil {
    51  		t.Errorf("start error %s [%s: %s]", err, name, argv)
    52  	}
    53  	err = cmd.Wait()
    54  	if err != nil {
    55  		t.Errorf("cmp error %s [%s: %s]\n%s", err, name, argv, diffOut.String())
    56  	}
    57  	os.Remove(generatedOutput)
    58  }
    59  
    60  func TestExec(t *testing.T) {
    61  	os.Chdir(dirTest)
    62  
    63  	//Test Basic Functions
    64  	testEasygen(t, "list0", "list0")
    65  	// Filename suffixes are optional
    66  	testEasygen(t, "list0", "list0.yaml")
    67  	testEasygen(t, "list0", "list0", "list0")
    68  	testEasygen(t, "list0", "list0.tmpl", "list0")
    69  	testEasygen(t, "list0", "list0.tmpl", "list0.yaml")
    70  	//testEasygen(t, "list0E", "list0E", "list0")
    71  
    72  	testEasygen(t, "list1", "list1")
    73  	testEasygen(t, "listfunc1", "listfunc1")
    74  	testEasygen(t, "listfunc2", "listfunc2")
    75  	testEasygen(t, "tf-calc", "tf-calc")
    76  
    77  	//Test Basic Json Functions
    78  	testEasygen(t, "list0j", "list0j")
    79  	testEasygen(t, "list0j", "list0j", "list0j")
    80  	testEasygen(t, "list0j", "list0j", "list0j.json")
    81  
    82  	// template_name can be a comma-separated list
    83  	testEasygen(t, "list01", "list0,listfunc1", "list0")
    84  
    85  	//Test HTML
    86  	testEasygen(t, "list1HTML", "list1HTML", "list1")
    87  
    88  	//Test lowercase keys
    89  	testEasygen(t, "example-lowercase", "example-lowercase")
    90  
    91  	// varcaser string functions
    92  	testEasygen(t, "var0", "-ts", "{{.Name}}", "var0")
    93  	testEasygen(t, "var1", "-ts", "{{clk2uc .Name}}", "var0")
    94  	testEasygen(t, "var2", "-ts", "{{clk2ss .Name}}", "var0")
    95  
    96  	//Test Bigger files
    97  	testEasygen(t, "commandlineCLI-024", "commandlineCLI-024")
    98  	testEasygen(t, "commandlineCLI-027", "commandlineCLI-027")
    99  	testEasygen(t, "commandlineCLI-027s", "commandlineCLI-027", "commandlineCLI-027s")
   100  
   101  	testEasygen(t, "commandlineCVFull", "commandlineCVFull")
   102  	testEasygen(t, "commandlineCV", "commandlineCV")
   103  	testEasygen(t, "commandlineFlag", "commandlineFlag")
   104  
   105  	// Enum generation: (a) run template with multiple data inputs,
   106  	//                  (b) run the same input with multiple template files:
   107  	testEasygen(t, "enum_multiple_data_files", "enum_c-header", "raid_type", "raid_driver")
   108  	testEasygen(t, "enum_multiple_template_files", "enum_c-header,enum_c-source", "raid_type.yaml")
   109  	testEasygen(t, "enum_multiple_template_and_data", "enum_c-header,enum_c-to_str", "raid_type", "raid_driver.yaml")
   110  
   111  	// Test with multiple data inputs files:
   112  	testEasygen(t, "lists", "lists", "list0.yaml,listfunc2.yaml")
   113  	testEasygen(t, "lists", "lists", "list0,listfunc2")
   114  	testEasygen(t, "lists", "lists", "listfunc2.yaml,list0.yaml")
   115  
   116  	//Test nested templates
   117  	//testEasygen(t, "nested_header_footer", "nested_header.tmpl,nested_footer.tmpl,nested_thanks.tmpl", "nested_data.yaml")
   118  	testEasygen(t, "nested_demo_argsa", "nested_demo_argsa", "nested_data.yaml")
   119  	testEasygen(t, "nested_demo_argsm", "nested_demo_argsm", "nested_data.yaml")
   120  	testEasygen(t, "nested_demo_argsm_iterate", "nested_demo_argsm_iterate", "nested_data.yaml")
   121  }