go-hep.org/x/hep@v0.38.1/groot/cmd/root-gen-rfunc/main_test.go (about)

     1  // Copyright ©2020 The go-hep Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"fmt"
     9  	"os"
    10  	"testing"
    11  
    12  	"go-hep.org/x/hep/internal/diff"
    13  )
    14  
    15  func TestGenerate(t *testing.T) {
    16  	for _, tc := range []struct {
    17  		test string
    18  		pkg  string
    19  		fct  string
    20  		name string
    21  		want string
    22  		err  error
    23  	}{
    24  		{
    25  			test: "math_abs",
    26  			pkg:  "math",
    27  			fct:  "Abs",
    28  			name: "MyAbs",
    29  			want: "./testdata/math_abs_golden.txt",
    30  		},
    31  		{
    32  			test: "math_hypot",
    33  			pkg:  "math",
    34  			fct:  "Hypot",
    35  			name: "",
    36  			want: "./testdata/math_hypot_golden.txt",
    37  		},
    38  		{
    39  			test: "func1",
    40  			pkg:  "",
    41  			fct:  "func(x, y float64) bool",
    42  			name: "",
    43  			want: "./testdata/func1_golden.txt",
    44  		},
    45  		{
    46  			test: "func2",
    47  			pkg:  "",
    48  			fct:  "func(float64, float64) bool",
    49  			name: "MyFunc",
    50  			want: "./testdata/func2_golden.txt",
    51  		},
    52  		{
    53  			test: "invalid-arg-pkg",
    54  			pkg:  "",
    55  			fct:  "",
    56  			err:  fmt.Errorf("missing package import path and/or function name"),
    57  		},
    58  		{
    59  			test: "invalid-arg-func",
    60  			pkg:  "",
    61  			fct:  "math.Abs",
    62  			err:  fmt.Errorf("missing function signature"),
    63  		},
    64  		{
    65  			test: "invalid-expr",
    66  			pkg:  "",
    67  			fct:  "func F()",
    68  			err:  fmt.Errorf(`could not generate rfunc formula: genroot: could not create rfunc generator: genroot: could not parse function signature: genroot: could not parse "func F()": 1:6: expected '(', found F`),
    69  		},
    70  		{
    71  			test: "invalid-func-name",
    72  			pkg:  "math",
    73  			fct:  "AbsXXX",
    74  			err:  fmt.Errorf(`could not generate rfunc formula: genroot: could not create rfunc generator: genroot: could not find AbsXXX in package "math"`),
    75  		},
    76  		{
    77  			test: "invalid-func-obj",
    78  			pkg:  "math",
    79  			fct:  "Pi",
    80  			err:  fmt.Errorf(`could not generate rfunc formula: genroot: could not create rfunc generator: genroot: object Pi in package "math" is not a func (*types.Const)`),
    81  		},
    82  	} {
    83  		t.Run(tc.test, func(t *testing.T) {
    84  			tmp, err := os.CreateTemp("", "groot-gen-rfunc-")
    85  			if err != nil {
    86  				t.Fatal(err)
    87  			}
    88  			_ = tmp.Close()
    89  			defer os.Remove(tmp.Name())
    90  
    91  			usage := func() {}
    92  			err = generate(tmp.Name(), tc.pkg, tc.fct, tc.name, usage)
    93  			switch {
    94  			case err != nil && tc.err != nil:
    95  				if got, want := err.Error(), tc.err.Error(); got != want {
    96  					t.Fatalf("invalid error:\ngot= %v\nwant=%v", got, want)
    97  				}
    98  				return
    99  			case err != nil && tc.err == nil:
   100  				t.Fatalf("could not generate: %+v", err)
   101  			case err == nil && tc.err != nil:
   102  				t.Fatalf("expected an error: %v", err)
   103  			case err == nil && tc.err == nil:
   104  				// ok.
   105  			}
   106  
   107  			got, err := os.ReadFile(tmp.Name())
   108  			if err != nil {
   109  				t.Fatalf("could not read generated rfunc: %+v", err)
   110  			}
   111  
   112  			want, err := os.ReadFile(tc.want)
   113  			if err != nil {
   114  				t.Fatalf("could not read reference rfunc: %+v", err)
   115  			}
   116  
   117  			if got, want := string(got), string(want); got != want {
   118  				t.Fatalf("invalid generated code:\n%s\n", diff.Format(got, want))
   119  			}
   120  		})
   121  	}
   122  }