func_typedefs_test.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. package stdlib_test
  2. import (
  3. "errors"
  4. "strconv"
  5. "strings"
  6. "testing"
  7. "github.com/d5/tengo/v2"
  8. "github.com/d5/tengo/v2/require"
  9. "github.com/d5/tengo/v2/stdlib"
  10. )
  11. func TestFuncAIR(t *testing.T) {
  12. uf := stdlib.FuncAIR(func(int) {})
  13. ret, err := funcCall(uf, &tengo.Int{Value: 10})
  14. require.NoError(t, err)
  15. require.Equal(t, tengo.UndefinedValue, ret)
  16. _, err = funcCall(uf)
  17. require.Equal(t, tengo.ErrWrongNumArguments, err)
  18. }
  19. func TestFuncAR(t *testing.T) {
  20. uf := stdlib.FuncAR(func() {})
  21. ret, err := funcCall(uf)
  22. require.NoError(t, err)
  23. require.Equal(t, tengo.UndefinedValue, ret)
  24. _, err = funcCall(uf, tengo.TrueValue)
  25. require.Equal(t, tengo.ErrWrongNumArguments, err)
  26. }
  27. func TestFuncARI(t *testing.T) {
  28. uf := stdlib.FuncARI(func() int { return 10 })
  29. ret, err := funcCall(uf)
  30. require.NoError(t, err)
  31. require.Equal(t, &tengo.Int{Value: 10}, ret)
  32. _, err = funcCall(uf, tengo.TrueValue)
  33. require.Equal(t, tengo.ErrWrongNumArguments, err)
  34. }
  35. func TestFuncARE(t *testing.T) {
  36. uf := stdlib.FuncARE(func() error { return nil })
  37. ret, err := funcCall(uf)
  38. require.NoError(t, err)
  39. require.Equal(t, tengo.TrueValue, ret)
  40. uf = stdlib.FuncARE(func() error { return errors.New("some error") })
  41. ret, err = funcCall(uf)
  42. require.NoError(t, err)
  43. require.Equal(t, &tengo.Error{
  44. Value: &tengo.String{Value: "some error"},
  45. }, ret)
  46. _, err = funcCall(uf, tengo.TrueValue)
  47. require.Equal(t, tengo.ErrWrongNumArguments, err)
  48. }
  49. func TestFuncARIsE(t *testing.T) {
  50. uf := stdlib.FuncARIsE(func() ([]int, error) {
  51. return []int{1, 2, 3}, nil
  52. })
  53. ret, err := funcCall(uf)
  54. require.NoError(t, err)
  55. require.Equal(t, array(&tengo.Int{Value: 1},
  56. &tengo.Int{Value: 2}, &tengo.Int{Value: 3}), ret)
  57. uf = stdlib.FuncARIsE(func() ([]int, error) {
  58. return nil, errors.New("some error")
  59. })
  60. ret, err = funcCall(uf)
  61. require.NoError(t, err)
  62. require.Equal(t, &tengo.Error{
  63. Value: &tengo.String{Value: "some error"},
  64. }, ret)
  65. _, err = funcCall(uf, tengo.TrueValue)
  66. require.Equal(t, tengo.ErrWrongNumArguments, err)
  67. }
  68. func TestFuncARS(t *testing.T) {
  69. uf := stdlib.FuncARS(func() string { return "foo" })
  70. ret, err := funcCall(uf)
  71. require.NoError(t, err)
  72. require.Equal(t, &tengo.String{Value: "foo"}, ret)
  73. _, err = funcCall(uf, tengo.TrueValue)
  74. require.Equal(t, tengo.ErrWrongNumArguments, err)
  75. }
  76. func TestFuncARSE(t *testing.T) {
  77. uf := stdlib.FuncARSE(func() (string, error) { return "foo", nil })
  78. ret, err := funcCall(uf)
  79. require.NoError(t, err)
  80. require.Equal(t, &tengo.String{Value: "foo"}, ret)
  81. uf = stdlib.FuncARSE(func() (string, error) {
  82. return "", errors.New("some error")
  83. })
  84. ret, err = funcCall(uf)
  85. require.NoError(t, err)
  86. require.Equal(t, &tengo.Error{
  87. Value: &tengo.String{Value: "some error"},
  88. }, ret)
  89. _, err = funcCall(uf, tengo.TrueValue)
  90. require.Equal(t, tengo.ErrWrongNumArguments, err)
  91. }
  92. func TestFuncARSs(t *testing.T) {
  93. uf := stdlib.FuncARSs(func() []string { return []string{"foo", "bar"} })
  94. ret, err := funcCall(uf)
  95. require.NoError(t, err)
  96. require.Equal(t, array(&tengo.String{Value: "foo"},
  97. &tengo.String{Value: "bar"}), ret)
  98. _, err = funcCall(uf, tengo.TrueValue)
  99. require.Equal(t, tengo.ErrWrongNumArguments, err)
  100. }
  101. func TestFuncASRE(t *testing.T) {
  102. uf := stdlib.FuncASRE(func(a string) error { return nil })
  103. ret, err := funcCall(uf, &tengo.String{Value: "foo"})
  104. require.NoError(t, err)
  105. require.Equal(t, tengo.TrueValue, ret)
  106. uf = stdlib.FuncASRE(func(a string) error {
  107. return errors.New("some error")
  108. })
  109. ret, err = funcCall(uf, &tengo.String{Value: "foo"})
  110. require.NoError(t, err)
  111. require.Equal(t, &tengo.Error{
  112. Value: &tengo.String{Value: "some error"},
  113. }, ret)
  114. _, err = funcCall(uf)
  115. require.Equal(t, tengo.ErrWrongNumArguments, err)
  116. }
  117. func TestFuncASRS(t *testing.T) {
  118. uf := stdlib.FuncASRS(func(a string) string { return a })
  119. ret, err := funcCall(uf, &tengo.String{Value: "foo"})
  120. require.NoError(t, err)
  121. require.Equal(t, &tengo.String{Value: "foo"}, ret)
  122. _, err = funcCall(uf)
  123. require.Equal(t, tengo.ErrWrongNumArguments, err)
  124. }
  125. func TestFuncASRSs(t *testing.T) {
  126. uf := stdlib.FuncASRSs(func(a string) []string { return []string{a} })
  127. ret, err := funcCall(uf, &tengo.String{Value: "foo"})
  128. require.NoError(t, err)
  129. require.Equal(t, array(&tengo.String{Value: "foo"}), ret)
  130. _, err = funcCall(uf)
  131. require.Equal(t, tengo.ErrWrongNumArguments, err)
  132. }
  133. func TestFuncASI64RE(t *testing.T) {
  134. uf := stdlib.FuncASI64RE(func(a string, b int64) error { return nil })
  135. ret, err := funcCall(uf, &tengo.String{Value: "foo"}, &tengo.Int{Value: 5})
  136. require.NoError(t, err)
  137. require.Equal(t, tengo.TrueValue, ret)
  138. uf = stdlib.FuncASI64RE(func(a string, b int64) error {
  139. return errors.New("some error")
  140. })
  141. ret, err = funcCall(uf, &tengo.String{Value: "foo"}, &tengo.Int{Value: 5})
  142. require.NoError(t, err)
  143. require.Equal(t,
  144. &tengo.Error{Value: &tengo.String{Value: "some error"}}, ret)
  145. _, err = funcCall(uf)
  146. require.Equal(t, tengo.ErrWrongNumArguments, err)
  147. }
  148. func TestFuncAIIRE(t *testing.T) {
  149. uf := stdlib.FuncAIIRE(func(a, b int) error { return nil })
  150. ret, err := funcCall(uf, &tengo.Int{Value: 5}, &tengo.Int{Value: 7})
  151. require.NoError(t, err)
  152. require.Equal(t, tengo.TrueValue, ret)
  153. uf = stdlib.FuncAIIRE(func(a, b int) error {
  154. return errors.New("some error")
  155. })
  156. ret, err = funcCall(uf, &tengo.Int{Value: 5}, &tengo.Int{Value: 7})
  157. require.NoError(t, err)
  158. require.Equal(t,
  159. &tengo.Error{Value: &tengo.String{Value: "some error"}}, ret)
  160. _, err = funcCall(uf)
  161. require.Equal(t, tengo.ErrWrongNumArguments, err)
  162. }
  163. func TestFuncASIIRE(t *testing.T) {
  164. uf := stdlib.FuncASIIRE(func(a string, b, c int) error { return nil })
  165. ret, err := funcCall(uf, &tengo.String{Value: "foo"}, &tengo.Int{Value: 5},
  166. &tengo.Int{Value: 7})
  167. require.NoError(t, err)
  168. require.Equal(t, tengo.TrueValue, ret)
  169. uf = stdlib.FuncASIIRE(func(a string, b, c int) error {
  170. return errors.New("some error")
  171. })
  172. ret, err = funcCall(uf, &tengo.String{Value: "foo"}, &tengo.Int{Value: 5},
  173. &tengo.Int{Value: 7})
  174. require.NoError(t, err)
  175. require.Equal(t,
  176. &tengo.Error{Value: &tengo.String{Value: "some error"}}, ret)
  177. _, err = funcCall(uf)
  178. require.Equal(t, tengo.ErrWrongNumArguments, err)
  179. }
  180. func TestFuncASRSE(t *testing.T) {
  181. uf := stdlib.FuncASRSE(func(a string) (string, error) { return a, nil })
  182. ret, err := funcCall(uf, &tengo.String{Value: "foo"})
  183. require.NoError(t, err)
  184. require.Equal(t, &tengo.String{Value: "foo"}, ret)
  185. uf = stdlib.FuncASRSE(func(a string) (string, error) {
  186. return a, errors.New("some error")
  187. })
  188. ret, err = funcCall(uf, &tengo.String{Value: "foo"})
  189. require.NoError(t, err)
  190. require.Equal(t,
  191. &tengo.Error{Value: &tengo.String{Value: "some error"}}, ret)
  192. _, err = funcCall(uf)
  193. require.Equal(t, tengo.ErrWrongNumArguments, err)
  194. }
  195. func TestFuncASSRE(t *testing.T) {
  196. uf := stdlib.FuncASSRE(func(a, b string) error { return nil })
  197. ret, err := funcCall(uf, &tengo.String{Value: "foo"},
  198. &tengo.String{Value: "bar"})
  199. require.NoError(t, err)
  200. require.Equal(t, tengo.TrueValue, ret)
  201. uf = stdlib.FuncASSRE(func(a, b string) error {
  202. return errors.New("some error")
  203. })
  204. ret, err = funcCall(uf, &tengo.String{Value: "foo"},
  205. &tengo.String{Value: "bar"})
  206. require.NoError(t, err)
  207. require.Equal(t,
  208. &tengo.Error{Value: &tengo.String{Value: "some error"}}, ret)
  209. _, err = funcCall(uf, &tengo.String{Value: "foo"})
  210. require.Equal(t, tengo.ErrWrongNumArguments, err)
  211. }
  212. func TestFuncASsRS(t *testing.T) {
  213. uf := stdlib.FuncASsSRS(func(a []string, b string) string {
  214. return strings.Join(a, b)
  215. })
  216. ret, err := funcCall(uf, array(&tengo.String{Value: "foo"},
  217. &tengo.String{Value: "bar"}), &tengo.String{Value: " "})
  218. require.NoError(t, err)
  219. require.Equal(t, &tengo.String{Value: "foo bar"}, ret)
  220. _, err = funcCall(uf, &tengo.String{Value: "foo"})
  221. require.Equal(t, tengo.ErrWrongNumArguments, err)
  222. }
  223. func TestFuncARF(t *testing.T) {
  224. uf := stdlib.FuncARF(func() float64 { return 10.0 })
  225. ret, err := funcCall(uf)
  226. require.NoError(t, err)
  227. require.Equal(t, &tengo.Float{Value: 10.0}, ret)
  228. _, err = funcCall(uf, tengo.TrueValue)
  229. require.Equal(t, tengo.ErrWrongNumArguments, err)
  230. }
  231. func TestFuncAFRF(t *testing.T) {
  232. uf := stdlib.FuncAFRF(func(a float64) float64 { return a })
  233. ret, err := funcCall(uf, &tengo.Float{Value: 10.0})
  234. require.NoError(t, err)
  235. require.Equal(t, &tengo.Float{Value: 10.0}, ret)
  236. _, err = funcCall(uf)
  237. require.Equal(t, tengo.ErrWrongNumArguments, err)
  238. _, err = funcCall(uf, tengo.TrueValue, tengo.TrueValue)
  239. require.Equal(t, tengo.ErrWrongNumArguments, err)
  240. }
  241. func TestFuncAIRF(t *testing.T) {
  242. uf := stdlib.FuncAIRF(func(a int) float64 {
  243. return float64(a)
  244. })
  245. ret, err := funcCall(uf, &tengo.Int{Value: 10.0})
  246. require.NoError(t, err)
  247. require.Equal(t, &tengo.Float{Value: 10.0}, ret)
  248. _, err = funcCall(uf)
  249. require.Equal(t, tengo.ErrWrongNumArguments, err)
  250. _, err = funcCall(uf, tengo.TrueValue, tengo.TrueValue)
  251. require.Equal(t, tengo.ErrWrongNumArguments, err)
  252. }
  253. func TestFuncAFRI(t *testing.T) {
  254. uf := stdlib.FuncAFRI(func(a float64) int {
  255. return int(a)
  256. })
  257. ret, err := funcCall(uf, &tengo.Float{Value: 10.5})
  258. require.NoError(t, err)
  259. require.Equal(t, &tengo.Int{Value: 10}, ret)
  260. _, err = funcCall(uf)
  261. require.Equal(t, tengo.ErrWrongNumArguments, err)
  262. _, err = funcCall(uf, tengo.TrueValue, tengo.TrueValue)
  263. require.Equal(t, tengo.ErrWrongNumArguments, err)
  264. }
  265. func TestFuncAFRB(t *testing.T) {
  266. uf := stdlib.FuncAFRB(func(a float64) bool {
  267. return a > 0.0
  268. })
  269. ret, err := funcCall(uf, &tengo.Float{Value: 0.1})
  270. require.NoError(t, err)
  271. require.Equal(t, tengo.TrueValue, ret)
  272. _, err = funcCall(uf)
  273. require.Equal(t, tengo.ErrWrongNumArguments, err)
  274. _, err = funcCall(uf, tengo.TrueValue, tengo.TrueValue)
  275. require.Equal(t, tengo.ErrWrongNumArguments, err)
  276. }
  277. func TestFuncAFFRF(t *testing.T) {
  278. uf := stdlib.FuncAFFRF(func(a, b float64) float64 {
  279. return a + b
  280. })
  281. ret, err := funcCall(uf, &tengo.Float{Value: 10.0},
  282. &tengo.Float{Value: 20.0})
  283. require.NoError(t, err)
  284. require.Equal(t, &tengo.Float{Value: 30.0}, ret)
  285. _, err = funcCall(uf)
  286. require.Equal(t, tengo.ErrWrongNumArguments, err)
  287. _, err = funcCall(uf, tengo.TrueValue)
  288. require.Equal(t, tengo.ErrWrongNumArguments, err)
  289. }
  290. func TestFuncASIRS(t *testing.T) {
  291. uf := stdlib.FuncASIRS(func(a string, b int) string {
  292. return strings.Repeat(a, b)
  293. })
  294. ret, err := funcCall(uf, &tengo.String{Value: "ab"}, &tengo.Int{Value: 2})
  295. require.NoError(t, err)
  296. require.Equal(t, &tengo.String{Value: "abab"}, ret)
  297. _, err = funcCall(uf)
  298. require.Equal(t, tengo.ErrWrongNumArguments, err)
  299. _, err = funcCall(uf, tengo.TrueValue)
  300. require.Equal(t, tengo.ErrWrongNumArguments, err)
  301. }
  302. func TestFuncAIFRF(t *testing.T) {
  303. uf := stdlib.FuncAIFRF(func(a int, b float64) float64 {
  304. return float64(a) + b
  305. })
  306. ret, err := funcCall(uf, &tengo.Int{Value: 10}, &tengo.Float{Value: 20.0})
  307. require.NoError(t, err)
  308. require.Equal(t, &tengo.Float{Value: 30.0}, ret)
  309. _, err = funcCall(uf)
  310. require.Equal(t, tengo.ErrWrongNumArguments, err)
  311. _, err = funcCall(uf, tengo.TrueValue)
  312. require.Equal(t, tengo.ErrWrongNumArguments, err)
  313. }
  314. func TestFuncAFIRF(t *testing.T) {
  315. uf := stdlib.FuncAFIRF(func(a float64, b int) float64 {
  316. return a + float64(b)
  317. })
  318. ret, err := funcCall(uf, &tengo.Float{Value: 10.0}, &tengo.Int{Value: 20})
  319. require.NoError(t, err)
  320. require.Equal(t, &tengo.Float{Value: 30.0}, ret)
  321. _, err = funcCall(uf)
  322. require.Equal(t, tengo.ErrWrongNumArguments, err)
  323. _, err = funcCall(uf, tengo.TrueValue)
  324. require.Equal(t, tengo.ErrWrongNumArguments, err)
  325. }
  326. func TestFuncAFIRB(t *testing.T) {
  327. uf := stdlib.FuncAFIRB(func(a float64, b int) bool {
  328. return a < float64(b)
  329. })
  330. ret, err := funcCall(uf, &tengo.Float{Value: 10.0}, &tengo.Int{Value: 20})
  331. require.NoError(t, err)
  332. require.Equal(t, tengo.TrueValue, ret)
  333. _, err = funcCall(uf)
  334. require.Equal(t, tengo.ErrWrongNumArguments, err)
  335. _, err = funcCall(uf, tengo.TrueValue)
  336. require.Equal(t, tengo.ErrWrongNumArguments, err)
  337. }
  338. func TestFuncAIRSsE(t *testing.T) {
  339. uf := stdlib.FuncAIRSsE(func(a int) ([]string, error) {
  340. return []string{"foo", "bar"}, nil
  341. })
  342. ret, err := funcCall(uf, &tengo.Int{Value: 10})
  343. require.NoError(t, err)
  344. require.Equal(t, array(&tengo.String{Value: "foo"},
  345. &tengo.String{Value: "bar"}), ret)
  346. uf = stdlib.FuncAIRSsE(func(a int) ([]string, error) {
  347. return nil, errors.New("some error")
  348. })
  349. ret, err = funcCall(uf, &tengo.Int{Value: 10})
  350. require.NoError(t, err)
  351. require.Equal(t,
  352. &tengo.Error{Value: &tengo.String{Value: "some error"}}, ret)
  353. _, err = funcCall(uf)
  354. require.Equal(t, tengo.ErrWrongNumArguments, err)
  355. }
  356. func TestFuncASSRSs(t *testing.T) {
  357. uf := stdlib.FuncASSRSs(func(a, b string) []string {
  358. return []string{a, b}
  359. })
  360. ret, err := funcCall(uf, &tengo.String{Value: "foo"},
  361. &tengo.String{Value: "bar"})
  362. require.NoError(t, err)
  363. require.Equal(t, array(&tengo.String{Value: "foo"},
  364. &tengo.String{Value: "bar"}), ret)
  365. _, err = funcCall(uf)
  366. require.Equal(t, tengo.ErrWrongNumArguments, err)
  367. }
  368. func TestFuncASSIRSs(t *testing.T) {
  369. uf := stdlib.FuncASSIRSs(func(a, b string, c int) []string {
  370. return []string{a, b, strconv.Itoa(c)}
  371. })
  372. ret, err := funcCall(uf, &tengo.String{Value: "foo"},
  373. &tengo.String{Value: "bar"}, &tengo.Int{Value: 5})
  374. require.NoError(t, err)
  375. require.Equal(t, array(&tengo.String{Value: "foo"},
  376. &tengo.String{Value: "bar"}, &tengo.String{Value: "5"}), ret)
  377. _, err = funcCall(uf)
  378. require.Equal(t, tengo.ErrWrongNumArguments, err)
  379. }
  380. func TestFuncARB(t *testing.T) {
  381. uf := stdlib.FuncARB(func() bool { return true })
  382. ret, err := funcCall(uf)
  383. require.NoError(t, err)
  384. require.Equal(t, tengo.TrueValue, ret)
  385. _, err = funcCall(uf, tengo.TrueValue)
  386. require.Equal(t, tengo.ErrWrongNumArguments, err)
  387. }
  388. func TestFuncARYE(t *testing.T) {
  389. uf := stdlib.FuncARYE(func() ([]byte, error) {
  390. return []byte("foo bar"), nil
  391. })
  392. ret, err := funcCall(uf)
  393. require.NoError(t, err)
  394. require.Equal(t, &tengo.Bytes{Value: []byte("foo bar")}, ret)
  395. uf = stdlib.FuncARYE(func() ([]byte, error) {
  396. return nil, errors.New("some error")
  397. })
  398. ret, err = funcCall(uf)
  399. require.NoError(t, err)
  400. require.Equal(t,
  401. &tengo.Error{Value: &tengo.String{Value: "some error"}}, ret)
  402. _, err = funcCall(uf, tengo.TrueValue)
  403. require.Equal(t, tengo.ErrWrongNumArguments, err)
  404. }
  405. func TestFuncASRIE(t *testing.T) {
  406. uf := stdlib.FuncASRIE(func(a string) (int, error) { return 5, nil })
  407. ret, err := funcCall(uf, &tengo.String{Value: "foo"})
  408. require.NoError(t, err)
  409. require.Equal(t, &tengo.Int{Value: 5}, ret)
  410. uf = stdlib.FuncASRIE(func(a string) (int, error) {
  411. return 0, errors.New("some error")
  412. })
  413. ret, err = funcCall(uf, &tengo.String{Value: "foo"})
  414. require.NoError(t, err)
  415. require.Equal(t,
  416. &tengo.Error{Value: &tengo.String{Value: "some error"}}, ret)
  417. _, err = funcCall(uf)
  418. require.Equal(t, tengo.ErrWrongNumArguments, err)
  419. }
  420. func TestFuncAYRIE(t *testing.T) {
  421. uf := stdlib.FuncAYRIE(func(a []byte) (int, error) { return 5, nil })
  422. ret, err := funcCall(uf, &tengo.Bytes{Value: []byte("foo")})
  423. require.NoError(t, err)
  424. require.Equal(t, &tengo.Int{Value: 5}, ret)
  425. uf = stdlib.FuncAYRIE(func(a []byte) (int, error) {
  426. return 0, errors.New("some error")
  427. })
  428. ret, err = funcCall(uf, &tengo.Bytes{Value: []byte("foo")})
  429. require.NoError(t, err)
  430. require.Equal(t,
  431. &tengo.Error{Value: &tengo.String{Value: "some error"}}, ret)
  432. _, err = funcCall(uf)
  433. require.Equal(t, tengo.ErrWrongNumArguments, err)
  434. }
  435. func TestFuncASSRI(t *testing.T) {
  436. uf := stdlib.FuncASSRI(func(a, b string) int { return len(a) + len(b) })
  437. ret, err := funcCall(uf,
  438. &tengo.String{Value: "foo"}, &tengo.String{Value: "bar"})
  439. require.NoError(t, err)
  440. require.Equal(t, &tengo.Int{Value: 6}, ret)
  441. _, err = funcCall(uf, &tengo.String{Value: "foo"})
  442. require.Equal(t, tengo.ErrWrongNumArguments, err)
  443. }
  444. func TestFuncASSRS(t *testing.T) {
  445. uf := stdlib.FuncASSRS(func(a, b string) string { return a + b })
  446. ret, err := funcCall(uf,
  447. &tengo.String{Value: "foo"}, &tengo.String{Value: "bar"})
  448. require.NoError(t, err)
  449. require.Equal(t, &tengo.String{Value: "foobar"}, ret)
  450. _, err = funcCall(uf, &tengo.String{Value: "foo"})
  451. require.Equal(t, tengo.ErrWrongNumArguments, err)
  452. }
  453. func TestFuncASSRB(t *testing.T) {
  454. uf := stdlib.FuncASSRB(func(a, b string) bool { return len(a) > len(b) })
  455. ret, err := funcCall(uf,
  456. &tengo.String{Value: "123"}, &tengo.String{Value: "12"})
  457. require.NoError(t, err)
  458. require.Equal(t, tengo.TrueValue, ret)
  459. _, err = funcCall(uf, &tengo.String{Value: "foo"})
  460. require.Equal(t, tengo.ErrWrongNumArguments, err)
  461. }
  462. func TestFuncAIRS(t *testing.T) {
  463. uf := stdlib.FuncAIRS(func(a int) string { return strconv.Itoa(a) })
  464. ret, err := funcCall(uf, &tengo.Int{Value: 55})
  465. require.NoError(t, err)
  466. require.Equal(t, &tengo.String{Value: "55"}, ret)
  467. _, err = funcCall(uf)
  468. require.Equal(t, tengo.ErrWrongNumArguments, err)
  469. }
  470. func TestFuncAIRIs(t *testing.T) {
  471. uf := stdlib.FuncAIRIs(func(a int) []int { return []int{a, a} })
  472. ret, err := funcCall(uf, &tengo.Int{Value: 55})
  473. require.NoError(t, err)
  474. require.Equal(t, array(&tengo.Int{Value: 55}, &tengo.Int{Value: 55}), ret)
  475. _, err = funcCall(uf)
  476. require.Equal(t, tengo.ErrWrongNumArguments, err)
  477. }
  478. func TestFuncAI64R(t *testing.T) {
  479. uf := stdlib.FuncAIR(func(a int) {})
  480. ret, err := funcCall(uf, &tengo.Int{Value: 55})
  481. require.NoError(t, err)
  482. require.Equal(t, tengo.UndefinedValue, ret)
  483. _, err = funcCall(uf)
  484. require.Equal(t, tengo.ErrWrongNumArguments, err)
  485. }
  486. func TestFuncARI64(t *testing.T) {
  487. uf := stdlib.FuncARI64(func() int64 { return 55 })
  488. ret, err := funcCall(uf)
  489. require.NoError(t, err)
  490. require.Equal(t, &tengo.Int{Value: 55}, ret)
  491. _, err = funcCall(uf, &tengo.Int{Value: 55})
  492. require.Equal(t, tengo.ErrWrongNumArguments, err)
  493. }
  494. func TestFuncASsSRS(t *testing.T) {
  495. uf := stdlib.FuncASsSRS(func(a []string, b string) string {
  496. return strings.Join(a, b)
  497. })
  498. ret, err := funcCall(uf,
  499. array(&tengo.String{Value: "abc"}, &tengo.String{Value: "def"}),
  500. &tengo.String{Value: "-"})
  501. require.NoError(t, err)
  502. require.Equal(t, &tengo.String{Value: "abc-def"}, ret)
  503. _, err = funcCall(uf)
  504. require.Equal(t, tengo.ErrWrongNumArguments, err)
  505. }
  506. func TestFuncAI64RI64(t *testing.T) {
  507. uf := stdlib.FuncAI64RI64(func(a int64) int64 { return a * 2 })
  508. ret, err := funcCall(uf, &tengo.Int{Value: 55})
  509. require.NoError(t, err)
  510. require.Equal(t, &tengo.Int{Value: 110}, ret)
  511. _, err = funcCall(uf)
  512. require.Equal(t, tengo.ErrWrongNumArguments, err)
  513. }
  514. func funcCall(
  515. fn tengo.CallableFunc,
  516. args ...tengo.Object,
  517. ) (tengo.Object, error) {
  518. userFunc := &tengo.UserFunction{Value: fn}
  519. return userFunc.Call(args...)
  520. }
  521. func array(elements ...tengo.Object) *tengo.Array {
  522. return &tengo.Array{Value: elements}
  523. }