template_test.txt 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. Tests for {4} ({1} and []{1})
  3. */
  4. func Test{4}(t *testing.T) {
  5. val := {1}( {2} )
  6. m := map[string]interface{}{"value": val, "nothing": nil}
  7. assert.Equal(t, val, New(m).Get("value").{4}())
  8. assert.Equal(t, val, New(m).Get("value").Must{4}())
  9. assert.Equal(t, {1}({3}), New(m).Get("nothing").{4}())
  10. assert.Equal(t, val, New(m).Get("nothing").{4}({2}))
  11. assert.Panics(t, func() {
  12. New(m).Get("age").Must{4}()
  13. })
  14. }
  15. func Test{4}Slice(t *testing.T) {
  16. val := {1}( {2} )
  17. m := map[string]interface{}{"value": []{1}{ val }, "nothing": nil}
  18. assert.Equal(t, val, New(m).Get("value").{4}Slice()[0])
  19. assert.Equal(t, val, New(m).Get("value").Must{4}Slice()[0])
  20. assert.Equal(t, []{1}(nil), New(m).Get("nothing").{4}Slice())
  21. assert.Equal(t, val, New(m).Get("nothing").{4}Slice( []{1}{ {1}({2}) } )[0])
  22. assert.Panics(t, func() {
  23. New(m).Get("nothing").Must{4}Slice()
  24. })
  25. }
  26. func TestIs{4}(t *testing.T) {
  27. v := &Value{data: {1}({2})}
  28. assert.True(t, v.Is{4}())
  29. }
  30. func TestIs{4}Slice(t *testing.T) {
  31. v := &Value{data: []{1}{ {1}({2}) }}
  32. assert.True(t, v.Is{4}Slice())
  33. }
  34. func TestEach{4}(t *testing.T) {
  35. v := &Value{data: []{1}{ {1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2}) }}
  36. count := 0
  37. replacedVals := make([]{1}, 0)
  38. assert.Equal(t, v, v.Each{4}(func(i int, val {1}) bool {
  39. count++
  40. replacedVals = append(replacedVals, val)
  41. // abort early
  42. return i != 2
  43. }))
  44. assert.Equal(t, count, 3)
  45. assert.Equal(t, replacedVals[0], v.Must{4}Slice()[0])
  46. assert.Equal(t, replacedVals[1], v.Must{4}Slice()[1])
  47. assert.Equal(t, replacedVals[2], v.Must{4}Slice()[2])
  48. }
  49. func TestWhere{4}(t *testing.T) {
  50. v := &Value{data: []{1}{ {1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2}) }}
  51. selected := v.Where{4}(func(i int, val {1}) bool {
  52. return i%2==0
  53. }).Must{4}Slice()
  54. assert.Equal(t, 3, len(selected))
  55. }
  56. func TestGroup{4}(t *testing.T) {
  57. v := &Value{data: []{1}{ {1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2}) }}
  58. grouped := v.Group{4}(func(i int, val {1}) string {
  59. return fmt.Sprintf("%v", i%2==0)
  60. }).data.(map[string][]{1})
  61. assert.Equal(t, 2, len(grouped))
  62. assert.Equal(t, 3, len(grouped["true"]))
  63. assert.Equal(t, 3, len(grouped["false"]))
  64. }
  65. func TestReplace{4}(t *testing.T) {
  66. v := &Value{data: []{1}{ {1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2}) }}
  67. rawArr := v.Must{4}Slice()
  68. replaced := v.Replace{4}(func(index int, val {1}) {1} {
  69. if index < len(rawArr)-1 {
  70. return rawArr[index+1]
  71. }
  72. return rawArr[0]
  73. })
  74. replacedArr := replaced.Must{4}Slice()
  75. if assert.Equal(t, 6, len(replacedArr)) {
  76. assert.Equal(t, replacedArr[0], rawArr[1])
  77. assert.Equal(t, replacedArr[1], rawArr[2])
  78. assert.Equal(t, replacedArr[2], rawArr[3])
  79. assert.Equal(t, replacedArr[3], rawArr[4])
  80. assert.Equal(t, replacedArr[4], rawArr[5])
  81. assert.Equal(t, replacedArr[5], rawArr[0])
  82. }
  83. }
  84. func TestCollect{4}(t *testing.T) {
  85. v := &Value{data: []{1}{ {1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2}), {1}({2}) }}
  86. collected := v.Collect{4}(func(index int, val {1}) interface{} {
  87. return index
  88. })
  89. collectedArr := collected.MustInterSlice()
  90. if assert.Equal(t, 6, len(collectedArr)) {
  91. assert.Equal(t, collectedArr[0], 0)
  92. assert.Equal(t, collectedArr[1], 1)
  93. assert.Equal(t, collectedArr[2], 2)
  94. assert.Equal(t, collectedArr[3], 3)
  95. assert.Equal(t, collectedArr[4], 4)
  96. assert.Equal(t, collectedArr[5], 5)
  97. }
  98. }