...

Source file src/github.com/noirbizarre/gonja/exec/context_test.go

Documentation: github.com/noirbizarre/gonja/exec

     1  package exec_test
     2  
     3  import (
     4  	// "fmt"
     5  
     6  	"testing"
     7  
     8  	"github.com/noirbizarre/gonja/exec"
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  var ctxData = map[string]interface{}{
    13  	"nil":    nil,
    14  	"string": "Hello World",
    15  	"int":    42,
    16  	"float":  42.,
    17  	"true":   true,
    18  	"false":  false,
    19  	"func":   func() {},
    20  }
    21  
    22  var ctxCases = []struct {
    23  	name     string
    24  	value    interface{}
    25  	asString string
    26  	flags    flags
    27  }{
    28  	{"nil", nil, "", flags{IsNil: true}},
    29  	{"string", "Hello World", "Hello World", flags{IsString: true, IsTrue: true}},
    30  	{"int", 42, "42", flags{IsInteger: true, IsNumber: true, IsTrue: true}},
    31  	{"int 0", 0, "0", flags{IsInteger: true, IsNumber: true}},
    32  	{"float", 42., "42.000000", flags{IsFloat: true, IsNumber: true, IsTrue: true}},
    33  	{"float 0.0", 0., "0.000000", flags{IsFloat: true, IsNumber: true}},
    34  	{"true", true, "True", flags{IsBool: true, IsTrue: true}},
    35  	{"false", false, "False", flags{IsBool: true}},
    36  }
    37  
    38  func TestContext(t *testing.T) {
    39  	for _, cc := range ctxCases {
    40  		test := cc
    41  		t.Run(test.name, func(t *testing.T) {
    42  			defer func() {
    43  				if err := recover(); err != nil {
    44  					t.Error(err)
    45  				}
    46  			}()
    47  			assert := assert.New(t)
    48  
    49  			ctx := exec.EmptyContext()
    50  			ctx.Set(test.name, test.value)
    51  			value := ctx.Get(test.name)
    52  
    53  			// value := exec.AsValue(test.value)
    54  			assert.Equal(test.value, value)
    55  
    56  			// assert.Equal(test.asString, value.String())
    57  			// test.flags.assert(t, value)
    58  		})
    59  	}
    60  }
    61  
    62  func TestSubContext(t *testing.T) {
    63  	for _, cc := range ctxCases {
    64  		test := cc
    65  		t.Run(test.name, func(t *testing.T) {
    66  			defer func() {
    67  				if err := recover(); err != nil {
    68  					t.Error(err)
    69  				}
    70  			}()
    71  			assert := assert.New(t)
    72  
    73  			ctx := exec.EmptyContext()
    74  			ctx.Set(test.name, test.value)
    75  			sub := ctx.Inherit()
    76  			value := sub.Get(test.name)
    77  
    78  			// value := exec.AsValue(test.value)
    79  			assert.Equal(test.value, value)
    80  
    81  			// assert.Equal(test.asString, value.String())
    82  			// test.flags.assert(t, value)
    83  		})
    84  	}
    85  }
    86  
    87  func TestFuncContext(t *testing.T) {
    88  	ctx := exec.EmptyContext()
    89  	ctx.Set("func", func() {})
    90  
    91  	cases := []struct {
    92  		name string
    93  		ctx  *exec.Context
    94  	}{
    95  		{"top context", ctx},
    96  		{"sub context", ctx.Inherit()},
    97  	}
    98  
    99  	for _, c := range cases {
   100  		test := c
   101  		t.Run(test.name, func(t *testing.T) {
   102  			defer func() {
   103  				if err := recover(); err != nil {
   104  					t.Error(err)
   105  				}
   106  			}()
   107  			assert := assert.New(t)
   108  
   109  			value := test.ctx.Get("func")
   110  			_, ok := value.(func())
   111  			assert.True(ok)
   112  		})
   113  	}
   114  }
   115  
   116  // func TestValueFromMap(t *testing.T) {
   117  // 	for _, lc := range valueCases {
   118  // 		test := lc
   119  // 		t.Run(test.name, func(t *testing.T) {
   120  // 			defer func() {
   121  // 				if err := recover(); err != nil {
   122  // 					t.Error(err)
   123  // 				}
   124  // 			}()
   125  // 			assert := assert.New(t)
   126  
   127  // 			data := map[string]interface{}{"value": test.value}
   128  // 			value := exec.AsValue(data["value"])
   129  
   130  // 			assert.Equal(test.asString, value.String())
   131  // 			test.flags.assert(t, value)
   132  // 		})
   133  // 	}
   134  // }
   135  
   136  // type testStruct struct {
   137  // 	Attr string
   138  // }
   139  
   140  // var getattrCases = []struct {
   141  // 	name     string
   142  // 	value    interface{}
   143  // 	attr     string
   144  // 	found    bool
   145  // 	asString string
   146  // 	flags    flags
   147  // }{
   148  // 	{"nil", nil, "missing", false, "", flags{IsError: true}},
   149  // 	{"attr found", testStruct{"test"}, "Attr", true, "test", flags{IsString: true, IsTrue: true}},
   150  // 	{"item", map[string]interface{}{"Attr": "test"}, "Attr", false, "", flags{IsError: true}},
   151  // }
   152  
   153  // func TestValueGetAttr(t *testing.T) {
   154  // 	for _, lc := range getattrCases {
   155  // 		test := lc
   156  // 		t.Run(test.name, func(t *testing.T) {
   157  // 			defer func() {
   158  // 				if err := recover(); err != nil {
   159  // 					t.Error(err)
   160  // 				}
   161  // 			}()
   162  // 			assert := assert.New(t)
   163  
   164  // 			value := exec.AsValue(test.value)
   165  // 			out, found := value.Getattr(test.attr)
   166  
   167  // 			if !test.flags.IsError && out.IsError() {
   168  // 				t.Fatalf(`Unexpected error: %s`, out.Error())
   169  // 			}
   170  
   171  // 			if test.found {
   172  // 				assert.Truef(found, `Attribute '%s' should be found on %s`, test.attr, value)
   173  // 				assert.Equal(test.asString, out.String())
   174  // 			} else {
   175  // 				assert.Falsef(found, `Attribute '%s' should not be found on %s`, test.attr, value)
   176  // 			}
   177  
   178  // 			test.flags.assert(t, out)
   179  // 		})
   180  // 	}
   181  // }
   182  
   183  // var getitemCases = []struct {
   184  // 	name     string
   185  // 	value    interface{}
   186  // 	key      interface{}
   187  // 	found    bool
   188  // 	asString string
   189  // 	flags    flags
   190  // }{
   191  // 	{"nil", nil, "missing", false, "", flags{IsError: true}},
   192  // 	{"item found", map[string]interface{}{"Attr": "test"}, "Attr", true, "test", flags{IsString: true, IsTrue: true}},
   193  // 	{"attr", testStruct{"test"}, "Attr", false, "", flags{IsError: true}},
   194  // }
   195  
   196  // func TestValueGetitem(t *testing.T) {
   197  // 	for _, lc := range getitemCases {
   198  // 		test := lc
   199  // 		t.Run(test.name, func(t *testing.T) {
   200  // 			defer func() {
   201  // 				if err := recover(); err != nil {
   202  // 					t.Error(err)
   203  // 				}
   204  // 			}()
   205  // 			assert := assert.New(t)
   206  
   207  // 			value := exec.AsValue(test.value)
   208  // 			out, found := value.Getitem(test.key)
   209  
   210  // 			if !test.flags.IsError && out.IsError() {
   211  // 				t.Fatalf(`Unexpected error: %s`, out.Error())
   212  // 			}
   213  
   214  // 			if test.found {
   215  // 				assert.Truef(found, `Key '%s' should be found on %s`, test.key, value)
   216  // 				assert.Equal(test.asString, out.String())
   217  // 			} else {
   218  // 				assert.Falsef(found, `Key '%s' should not be found on %s`, test.key, value)
   219  // 			}
   220  
   221  // 			test.flags.assert(t, out)
   222  // 		})
   223  // 	}
   224  // }
   225  

View as plain text