github.com/hairyhenderson/gomplate/v3@v3.11.7/funcs/test.go (about)

     1  package funcs
     2  
     3  import (
     4  	"context"
     5  	"reflect"
     6  
     7  	"github.com/hairyhenderson/gomplate/v3/conv"
     8  	"github.com/pkg/errors"
     9  
    10  	"github.com/hairyhenderson/gomplate/v3/test"
    11  )
    12  
    13  // TestNS -
    14  // Deprecated: don't use
    15  func TestNS() *TestFuncs {
    16  	return &TestFuncs{}
    17  }
    18  
    19  // AddTestFuncs -
    20  // Deprecated: use CreateTestFuncs instead
    21  func AddTestFuncs(f map[string]interface{}) {
    22  	for k, v := range CreateTestFuncs(context.Background()) {
    23  		f[k] = v
    24  	}
    25  }
    26  
    27  // CreateTestFuncs -
    28  func CreateTestFuncs(ctx context.Context) map[string]interface{} {
    29  	f := map[string]interface{}{}
    30  
    31  	ns := &TestFuncs{ctx}
    32  	f["test"] = func() interface{} { return ns }
    33  
    34  	f["assert"] = ns.Assert
    35  	f["fail"] = ns.Fail
    36  	f["required"] = ns.Required
    37  	f["ternary"] = ns.Ternary
    38  	f["kind"] = ns.Kind
    39  	f["isKind"] = ns.IsKind
    40  	return f
    41  }
    42  
    43  // TestFuncs -
    44  type TestFuncs struct {
    45  	ctx context.Context
    46  }
    47  
    48  // Assert -
    49  func (TestFuncs) Assert(args ...interface{}) (string, error) {
    50  	input := conv.ToBool(args[len(args)-1])
    51  	switch len(args) {
    52  	case 1:
    53  		return test.Assert(input, "")
    54  	case 2:
    55  		message, ok := args[0].(string)
    56  		if !ok {
    57  			return "", errors.Errorf("at <1>: expected string; found %T", args[0])
    58  		}
    59  		return test.Assert(input, message)
    60  	default:
    61  		return "", errors.Errorf("wrong number of args: want 1 or 2, got %d", len(args))
    62  	}
    63  }
    64  
    65  // Fail -
    66  func (TestFuncs) Fail(args ...interface{}) (string, error) {
    67  	switch len(args) {
    68  	case 0:
    69  		return "", test.Fail("")
    70  	case 1:
    71  		return "", test.Fail(conv.ToString(args[0]))
    72  	default:
    73  		return "", errors.Errorf("wrong number of args: want 0 or 1, got %d", len(args))
    74  	}
    75  }
    76  
    77  // Required -
    78  func (TestFuncs) Required(args ...interface{}) (interface{}, error) {
    79  	switch len(args) {
    80  	case 1:
    81  		return test.Required("", args[0])
    82  	case 2:
    83  		message, ok := args[0].(string)
    84  		if !ok {
    85  			return nil, errors.Errorf("at <1>: expected string; found %T", args[0])
    86  		}
    87  		return test.Required(message, args[1])
    88  	default:
    89  		return nil, errors.Errorf("wrong number of args: want 1 or 2, got %d", len(args))
    90  	}
    91  }
    92  
    93  // Ternary -
    94  func (TestFuncs) Ternary(tval, fval, b interface{}) interface{} {
    95  	if conv.ToBool(b) {
    96  		return tval
    97  	}
    98  	return fval
    99  }
   100  
   101  // Kind - return the kind of the argument
   102  func (TestFuncs) Kind(arg interface{}) string {
   103  	return reflect.ValueOf(arg).Kind().String()
   104  }
   105  
   106  // IsKind - return whether or not the argument is of the given kind
   107  func (f TestFuncs) IsKind(kind string, arg interface{}) bool {
   108  	k := f.Kind(arg)
   109  	if kind == "number" {
   110  		switch k {
   111  		case "int", "int8", "int16", "int32", "int64",
   112  			"uint", "uint8", "uint16", "uint32", "uint64", "uintptr",
   113  			"float32", "float64",
   114  			"complex64", "complex128":
   115  			kind = k
   116  		}
   117  	}
   118  	return k == kind
   119  }