numeric_test.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. package sprig
  2. import (
  3. "fmt"
  4. "github.com/stretchr/testify/assert"
  5. "strconv"
  6. "testing"
  7. )
  8. func TestUntil(t *testing.T) {
  9. tests := map[string]string{
  10. `{{range $i, $e := until 5}}{{$i}}{{$e}}{{end}}`: "0011223344",
  11. `{{range $i, $e := until -5}}{{$i}}{{$e}} {{end}}`: "00 1-1 2-2 3-3 4-4 ",
  12. }
  13. for tpl, expect := range tests {
  14. if err := runt(tpl, expect); err != nil {
  15. t.Error(err)
  16. }
  17. }
  18. }
  19. func TestUntilStep(t *testing.T) {
  20. tests := map[string]string{
  21. `{{range $i, $e := untilStep 0 5 1}}{{$i}}{{$e}}{{end}}`: "0011223344",
  22. `{{range $i, $e := untilStep 3 6 1}}{{$i}}{{$e}}{{end}}`: "031425",
  23. `{{range $i, $e := untilStep 0 -10 -2}}{{$i}}{{$e}} {{end}}`: "00 1-2 2-4 3-6 4-8 ",
  24. `{{range $i, $e := untilStep 3 0 1}}{{$i}}{{$e}}{{end}}`: "",
  25. `{{range $i, $e := untilStep 3 99 0}}{{$i}}{{$e}}{{end}}`: "",
  26. `{{range $i, $e := untilStep 3 99 -1}}{{$i}}{{$e}}{{end}}`: "",
  27. `{{range $i, $e := untilStep 3 0 0}}{{$i}}{{$e}}{{end}}`: "",
  28. }
  29. for tpl, expect := range tests {
  30. if err := runt(tpl, expect); err != nil {
  31. t.Error(err)
  32. }
  33. }
  34. }
  35. func TestBiggest(t *testing.T) {
  36. tpl := `{{ biggest 1 2 3 345 5 6 7}}`
  37. if err := runt(tpl, `345`); err != nil {
  38. t.Error(err)
  39. }
  40. tpl = `{{ max 345}}`
  41. if err := runt(tpl, `345`); err != nil {
  42. t.Error(err)
  43. }
  44. }
  45. func TestMaxf(t *testing.T) {
  46. tpl := `{{ maxf 1 2 3 345.7 5 6 7}}`
  47. if err := runt(tpl, `345.7`); err != nil {
  48. t.Error(err)
  49. }
  50. tpl = `{{ max 345 }}`
  51. if err := runt(tpl, `345`); err != nil {
  52. t.Error(err)
  53. }
  54. }
  55. func TestMin(t *testing.T) {
  56. tpl := `{{ min 1 2 3 345 5 6 7}}`
  57. if err := runt(tpl, `1`); err != nil {
  58. t.Error(err)
  59. }
  60. tpl = `{{ min 345}}`
  61. if err := runt(tpl, `345`); err != nil {
  62. t.Error(err)
  63. }
  64. }
  65. func TestMinf(t *testing.T) {
  66. tpl := `{{ minf 1.4 2 3 345.6 5 6 7}}`
  67. if err := runt(tpl, `1.4`); err != nil {
  68. t.Error(err)
  69. }
  70. tpl = `{{ minf 345 }}`
  71. if err := runt(tpl, `345`); err != nil {
  72. t.Error(err)
  73. }
  74. }
  75. func TestToFloat64(t *testing.T) {
  76. target := float64(102)
  77. if target != toFloat64(int8(102)) {
  78. t.Errorf("Expected 102")
  79. }
  80. if target != toFloat64(int(102)) {
  81. t.Errorf("Expected 102")
  82. }
  83. if target != toFloat64(int32(102)) {
  84. t.Errorf("Expected 102")
  85. }
  86. if target != toFloat64(int16(102)) {
  87. t.Errorf("Expected 102")
  88. }
  89. if target != toFloat64(int64(102)) {
  90. t.Errorf("Expected 102")
  91. }
  92. if target != toFloat64("102") {
  93. t.Errorf("Expected 102")
  94. }
  95. if toFloat64("frankie") != 0 {
  96. t.Errorf("Expected 0")
  97. }
  98. if target != toFloat64(uint16(102)) {
  99. t.Errorf("Expected 102")
  100. }
  101. if target != toFloat64(uint64(102)) {
  102. t.Errorf("Expected 102")
  103. }
  104. if toFloat64(float64(102.1234)) != 102.1234 {
  105. t.Errorf("Expected 102.1234")
  106. }
  107. if toFloat64(true) != 1 {
  108. t.Errorf("Expected 102")
  109. }
  110. }
  111. func TestToInt64(t *testing.T) {
  112. target := int64(102)
  113. if target != toInt64(int8(102)) {
  114. t.Errorf("Expected 102")
  115. }
  116. if target != toInt64(int(102)) {
  117. t.Errorf("Expected 102")
  118. }
  119. if target != toInt64(int32(102)) {
  120. t.Errorf("Expected 102")
  121. }
  122. if target != toInt64(int16(102)) {
  123. t.Errorf("Expected 102")
  124. }
  125. if target != toInt64(int64(102)) {
  126. t.Errorf("Expected 102")
  127. }
  128. if target != toInt64("102") {
  129. t.Errorf("Expected 102")
  130. }
  131. if toInt64("frankie") != 0 {
  132. t.Errorf("Expected 0")
  133. }
  134. if target != toInt64(uint16(102)) {
  135. t.Errorf("Expected 102")
  136. }
  137. if target != toInt64(uint64(102)) {
  138. t.Errorf("Expected 102")
  139. }
  140. if target != toInt64(float64(102.1234)) {
  141. t.Errorf("Expected 102")
  142. }
  143. if toInt64(true) != 1 {
  144. t.Errorf("Expected 102")
  145. }
  146. }
  147. func TestToInt(t *testing.T) {
  148. target := int(102)
  149. if target != toInt(int8(102)) {
  150. t.Errorf("Expected 102")
  151. }
  152. if target != toInt(int(102)) {
  153. t.Errorf("Expected 102")
  154. }
  155. if target != toInt(int32(102)) {
  156. t.Errorf("Expected 102")
  157. }
  158. if target != toInt(int16(102)) {
  159. t.Errorf("Expected 102")
  160. }
  161. if target != toInt(int64(102)) {
  162. t.Errorf("Expected 102")
  163. }
  164. if target != toInt("102") {
  165. t.Errorf("Expected 102")
  166. }
  167. if toInt("frankie") != 0 {
  168. t.Errorf("Expected 0")
  169. }
  170. if target != toInt(uint16(102)) {
  171. t.Errorf("Expected 102")
  172. }
  173. if target != toInt(uint64(102)) {
  174. t.Errorf("Expected 102")
  175. }
  176. if target != toInt(float64(102.1234)) {
  177. t.Errorf("Expected 102")
  178. }
  179. if toInt(true) != 1 {
  180. t.Errorf("Expected 102")
  181. }
  182. }
  183. func TestToDecimal(t *testing.T) {
  184. tests := map[any]int64{
  185. "777": 511,
  186. 777: 511,
  187. 770: 504,
  188. 755: 493,
  189. }
  190. for input, expectedResult := range tests {
  191. result := toDecimal(input)
  192. if result != expectedResult {
  193. t.Errorf("Expected %v but got %v", expectedResult, result)
  194. }
  195. }
  196. }
  197. func TestAdd1(t *testing.T) {
  198. tpl := `{{ 3 | add1 }}`
  199. if err := runt(tpl, `4`); err != nil {
  200. t.Error(err)
  201. }
  202. }
  203. func TestAdd(t *testing.T) {
  204. tpl := `{{ 3 | add 1 2}}`
  205. if err := runt(tpl, `6`); err != nil {
  206. t.Error(err)
  207. }
  208. }
  209. func TestDiv(t *testing.T) {
  210. tpl := `{{ 4 | div 5 }}`
  211. if err := runt(tpl, `1`); err != nil {
  212. t.Error(err)
  213. }
  214. }
  215. func TestMul(t *testing.T) {
  216. tpl := `{{ 1 | mul "2" 3 "4"}}`
  217. if err := runt(tpl, `24`); err != nil {
  218. t.Error(err)
  219. }
  220. }
  221. func TestSub(t *testing.T) {
  222. tpl := `{{ 3 | sub 14 }}`
  223. if err := runt(tpl, `11`); err != nil {
  224. t.Error(err)
  225. }
  226. }
  227. func TestCeil(t *testing.T) {
  228. assert.Equal(t, 123.0, ceil(123))
  229. assert.Equal(t, 123.0, ceil("123"))
  230. assert.Equal(t, 124.0, ceil(123.01))
  231. assert.Equal(t, 124.0, ceil("123.01"))
  232. }
  233. func TestFloor(t *testing.T) {
  234. assert.Equal(t, 123.0, floor(123))
  235. assert.Equal(t, 123.0, floor("123"))
  236. assert.Equal(t, 123.0, floor(123.9999))
  237. assert.Equal(t, 123.0, floor("123.9999"))
  238. }
  239. func TestRound(t *testing.T) {
  240. assert.Equal(t, 123.556, round(123.5555, 3))
  241. assert.Equal(t, 123.556, round("123.55555", 3))
  242. assert.Equal(t, 124.0, round(123.500001, 0))
  243. assert.Equal(t, 123.0, round(123.49999999, 0))
  244. assert.Equal(t, 123.23, round(123.2329999, 2, .3))
  245. assert.Equal(t, 123.24, round(123.233, 2, .3))
  246. }
  247. func TestRandomInt(t *testing.T) {
  248. var tests = []struct {
  249. min int
  250. max int
  251. }{
  252. {10, 11},
  253. {10, 13},
  254. {0, 1},
  255. {5, 50},
  256. }
  257. for _, v := range tests {
  258. x, _ := runRaw(fmt.Sprintf(`{{ randInt %d %d }}`, v.min, v.max), nil)
  259. r, err := strconv.Atoi(x)
  260. assert.NoError(t, err)
  261. assert.True(t, func(min, max, r int) bool {
  262. return r >= v.min && r < v.max
  263. }(v.min, v.max, r))
  264. }
  265. }
  266. func TestSeq(t *testing.T) {
  267. tests := map[string]string{
  268. `{{seq 0 1 3}}`: "0 1 2 3",
  269. `{{seq 0 3 10}}`: "0 3 6 9",
  270. `{{seq 3 3 2}}`: "",
  271. `{{seq 3 -3 2}}`: "3",
  272. `{{seq}}`: "",
  273. `{{seq 0 4}}`: "0 1 2 3 4",
  274. `{{seq 5}}`: "1 2 3 4 5",
  275. `{{seq -5}}`: "1 0 -1 -2 -3 -4 -5",
  276. `{{seq 0}}`: "1 0",
  277. `{{seq 0 1 2 3}}`: "",
  278. `{{seq 0 -4}}`: "0 -1 -2 -3 -4",
  279. }
  280. for tpl, expect := range tests {
  281. if err := runt(tpl, expect); err != nil {
  282. t.Error(err)
  283. }
  284. }
  285. }