github.com/MontFerret/ferret@v0.18.0/pkg/runtime/core/function_test.go (about)

     1  package core_test
     2  
     3  import (
     4  	"context"
     5  	"strings"
     6  	"testing"
     7  
     8  	. "github.com/smartystreets/goconvey/convey"
     9  
    10  	"github.com/MontFerret/ferret/pkg/runtime/core"
    11  	"github.com/MontFerret/ferret/pkg/runtime/values"
    12  )
    13  
    14  func TestValidateArgs(t *testing.T) {
    15  	Convey("Should match", t, func() {
    16  		a := []core.Value{values.NewInt(1), values.NewInt(2)}
    17  
    18  		e := core.ValidateArgs(a, 1, 2)
    19  		So(e, ShouldBeNil)
    20  
    21  		e = core.ValidateArgs(a, 3, 4)
    22  		So(e, ShouldNotBeNil)
    23  	})
    24  }
    25  
    26  func TestFunctions(t *testing.T) {
    27  
    28  	fnTrue := func(ctx context.Context, args ...core.Value) (core.Value, error) {
    29  		return values.True, nil
    30  	}
    31  
    32  	Convey(".Set", t, func() {
    33  
    34  		Convey("Should set function by name", func() {
    35  			fns := core.NewFunctions()
    36  			fname := "F"
    37  
    38  			fns.Set(fname, fnTrue)
    39  
    40  			names := fns.Names()
    41  
    42  			So(names, ShouldHaveLength, 1)
    43  			So(names[0], ShouldEqual, fname)
    44  		})
    45  
    46  		Convey("Should set function by name at uppercase", func() {
    47  			fns := core.NewFunctions()
    48  			fname := "f"
    49  
    50  			fns.Set(fname, fnTrue)
    51  
    52  			names := fns.Names()
    53  
    54  			So(names, ShouldHaveLength, 1)
    55  			So(names[0], ShouldEqual, strings.ToUpper(fname))
    56  		})
    57  
    58  		Convey("Should set when Functions created not by NewFunctions", func() {
    59  			fns := core.Functions{}
    60  			fname := "F"
    61  
    62  			fns.Set(fname, fnTrue)
    63  
    64  			names := fns.Names()
    65  
    66  			So(names, ShouldHaveLength, 1)
    67  			So(names[0], ShouldEqual, fname)
    68  		})
    69  	})
    70  
    71  	Convey(".Get", t, func() {
    72  
    73  		Convey("Should get function by name", func() {
    74  			fns := core.NewFunctions()
    75  			fname := "F"
    76  			fns.Set(fname, fnTrue)
    77  
    78  			fn, exists := fns.Get(fname)
    79  
    80  			So(exists, ShouldBeTrue)
    81  			So(fn, ShouldEqual, fnTrue)
    82  		})
    83  
    84  		Convey("Should get function by name at uppercase", func() {
    85  			fns := core.NewFunctions()
    86  			fname := "f"
    87  			fns.Set(fname, fnTrue)
    88  
    89  			fn, exists := fns.Get(strings.ToUpper(fname))
    90  
    91  			So(exists, ShouldBeTrue)
    92  			So(fn, ShouldEqual, fnTrue)
    93  		})
    94  
    95  		Convey("Should not panic when Functions created not by NewFunctions", func() {
    96  			fns := core.Functions{}
    97  
    98  			fn, exists := fns.Get("f")
    99  
   100  			So(exists, ShouldBeFalse)
   101  			So(fn, ShouldBeNil)
   102  		})
   103  	})
   104  
   105  	Convey(".Unset", t, func() {
   106  
   107  		Convey("Should unset function by name", func() {
   108  			fns := core.NewFunctions()
   109  			fname := "F"
   110  			fns.Set(fname, fnTrue)
   111  
   112  			fns.Unset(fname)
   113  
   114  			So(fns.Names(), ShouldHaveLength, 0)
   115  		})
   116  
   117  		Convey("Should get function by name at uppercase", func() {
   118  			fns := core.NewFunctions()
   119  			fname := "f"
   120  			fns.Set(fname, fnTrue)
   121  
   122  			fns.Unset(strings.ToUpper(fname))
   123  
   124  			So(fns.Names(), ShouldHaveLength, 0)
   125  		})
   126  
   127  		Convey("Should not panic when Functions created not by NewFunctions", func() {
   128  			fns := core.Functions{}
   129  			fname := "F"
   130  			fns.Set(fname, fnTrue)
   131  
   132  			fns.Unset(fname)
   133  
   134  			So(fns.Names(), ShouldHaveLength, 0)
   135  		})
   136  	})
   137  
   138  	Convey(".Names", t, func() {
   139  
   140  		Convey("Should return name", func() {
   141  			fns := core.NewFunctions()
   142  			fname := "F"
   143  			fns.Set(fname, fnTrue)
   144  
   145  			names := fns.Names()
   146  
   147  			So(names, ShouldHaveLength, 1)
   148  			So(names, ShouldContain, fname)
   149  		})
   150  
   151  		Convey("Should return name at uppercase", func() {
   152  			fns := core.NewFunctions()
   153  			fname := "f"
   154  			fns.Set(fname, fnTrue)
   155  
   156  			names := fns.Names()
   157  
   158  			So(names, ShouldHaveLength, 1)
   159  			So(names, ShouldContain, strings.ToUpper(fname))
   160  		})
   161  
   162  		Convey("Should not panic when Functions created not by NewFunctions", func() {
   163  			fns := core.Functions{}
   164  			So(fns.Names(), ShouldHaveLength, 0)
   165  		})
   166  	})
   167  }