builtins_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. package tengo_test
  2. import (
  3. "errors"
  4. "reflect"
  5. "testing"
  6. "github.com/d5/tengo/v2"
  7. )
  8. func Test_builtinDelete(t *testing.T) {
  9. var builtinDelete func(args ...tengo.Object) (tengo.Object, error)
  10. for _, f := range tengo.GetAllBuiltinFunctions() {
  11. if f.Name == "delete" {
  12. builtinDelete = f.Value
  13. break
  14. }
  15. }
  16. if builtinDelete == nil {
  17. t.Fatal("builtin delete not found")
  18. }
  19. type args struct {
  20. args []tengo.Object
  21. }
  22. tests := []struct {
  23. name string
  24. args args
  25. want tengo.Object
  26. wantErr bool
  27. wantedErr error
  28. target interface{}
  29. }{
  30. {name: "invalid-arg", args: args{[]tengo.Object{&tengo.String{},
  31. &tengo.String{}}}, wantErr: true,
  32. wantedErr: tengo.ErrInvalidArgumentType{
  33. Name: "first",
  34. Expected: "map",
  35. Found: "string"},
  36. },
  37. {name: "no-args",
  38. wantErr: true, wantedErr: tengo.ErrWrongNumArguments},
  39. {name: "empty-args", args: args{[]tengo.Object{}}, wantErr: true,
  40. wantedErr: tengo.ErrWrongNumArguments,
  41. },
  42. {name: "3-args", args: args{[]tengo.Object{
  43. (*tengo.Map)(nil), (*tengo.String)(nil), (*tengo.String)(nil)}},
  44. wantErr: true, wantedErr: tengo.ErrWrongNumArguments,
  45. },
  46. {name: "nil-map-empty-key",
  47. args: args{[]tengo.Object{&tengo.Map{}, &tengo.String{}}},
  48. want: tengo.UndefinedValue,
  49. },
  50. {name: "nil-map-nonstr-key",
  51. args: args{[]tengo.Object{
  52. &tengo.Map{}, &tengo.Int{}}}, wantErr: true,
  53. wantedErr: tengo.ErrInvalidArgumentType{
  54. Name: "second", Expected: "string", Found: "int"},
  55. },
  56. {name: "nil-map-no-key",
  57. args: args{[]tengo.Object{&tengo.Map{}}}, wantErr: true,
  58. wantedErr: tengo.ErrWrongNumArguments,
  59. },
  60. {name: "map-missing-key",
  61. args: args{
  62. []tengo.Object{
  63. &tengo.Map{Value: map[string]tengo.Object{
  64. "key": &tengo.String{Value: "value"},
  65. }},
  66. &tengo.String{Value: "key1"}}},
  67. want: tengo.UndefinedValue,
  68. target: &tengo.Map{
  69. Value: map[string]tengo.Object{
  70. "key": &tengo.String{
  71. Value: "value"}}},
  72. },
  73. {name: "map-emptied",
  74. args: args{
  75. []tengo.Object{
  76. &tengo.Map{Value: map[string]tengo.Object{
  77. "key": &tengo.String{Value: "value"},
  78. }},
  79. &tengo.String{Value: "key"}}},
  80. want: tengo.UndefinedValue,
  81. target: &tengo.Map{Value: map[string]tengo.Object{}},
  82. },
  83. {name: "map-multi-keys",
  84. args: args{
  85. []tengo.Object{
  86. &tengo.Map{Value: map[string]tengo.Object{
  87. "key1": &tengo.String{Value: "value1"},
  88. "key2": &tengo.Int{Value: 10},
  89. }},
  90. &tengo.String{Value: "key1"}}},
  91. want: tengo.UndefinedValue,
  92. target: &tengo.Map{Value: map[string]tengo.Object{
  93. "key2": &tengo.Int{Value: 10}}},
  94. },
  95. }
  96. for _, tt := range tests {
  97. t.Run(tt.name, func(t *testing.T) {
  98. got, err := builtinDelete(tt.args.args...)
  99. if (err != nil) != tt.wantErr {
  100. t.Errorf("builtinDelete() error = %v, wantErr %v",
  101. err, tt.wantErr)
  102. return
  103. }
  104. if tt.wantErr && !errors.Is(err, tt.wantedErr) {
  105. if err.Error() != tt.wantedErr.Error() {
  106. t.Errorf("builtinDelete() error = %v, wantedErr %v",
  107. err, tt.wantedErr)
  108. return
  109. }
  110. }
  111. if got != tt.want {
  112. t.Errorf("builtinDelete() = %v, want %v", got, tt.want)
  113. return
  114. }
  115. if !tt.wantErr && tt.target != nil {
  116. switch v := tt.args.args[0].(type) {
  117. case *tengo.Map, *tengo.Array:
  118. if !reflect.DeepEqual(tt.target, tt.args.args[0]) {
  119. t.Errorf("builtinDelete() objects are not equal "+
  120. "got: %+v, want: %+v", tt.args.args[0], tt.target)
  121. }
  122. default:
  123. t.Errorf("builtinDelete() unsuporrted arg[0] type %s",
  124. v.TypeName())
  125. return
  126. }
  127. }
  128. })
  129. }
  130. }
  131. func Test_builtinSplice(t *testing.T) {
  132. var builtinSplice func(args ...tengo.Object) (tengo.Object, error)
  133. for _, f := range tengo.GetAllBuiltinFunctions() {
  134. if f.Name == "splice" {
  135. builtinSplice = f.Value
  136. break
  137. }
  138. }
  139. if builtinSplice == nil {
  140. t.Fatal("builtin splice not found")
  141. }
  142. tests := []struct {
  143. name string
  144. args []tengo.Object
  145. deleted tengo.Object
  146. Array *tengo.Array
  147. wantErr bool
  148. wantedErr error
  149. }{
  150. {name: "no args", args: []tengo.Object{}, wantErr: true,
  151. wantedErr: tengo.ErrWrongNumArguments,
  152. },
  153. {name: "invalid args", args: []tengo.Object{&tengo.Map{}},
  154. wantErr: true,
  155. wantedErr: tengo.ErrInvalidArgumentType{
  156. Name: "first", Expected: "array", Found: "map"},
  157. },
  158. {name: "invalid args",
  159. args: []tengo.Object{&tengo.Array{}, &tengo.String{}},
  160. wantErr: true,
  161. wantedErr: tengo.ErrInvalidArgumentType{
  162. Name: "second", Expected: "int", Found: "string"},
  163. },
  164. {name: "negative index",
  165. args: []tengo.Object{&tengo.Array{}, &tengo.Int{Value: -1}},
  166. wantErr: true,
  167. wantedErr: tengo.ErrIndexOutOfBounds},
  168. {name: "non int count",
  169. args: []tengo.Object{
  170. &tengo.Array{}, &tengo.Int{Value: 0},
  171. &tengo.String{Value: ""}},
  172. wantErr: true,
  173. wantedErr: tengo.ErrInvalidArgumentType{
  174. Name: "third", Expected: "int", Found: "string"},
  175. },
  176. {name: "negative count",
  177. args: []tengo.Object{
  178. &tengo.Array{Value: []tengo.Object{
  179. &tengo.Int{Value: 0},
  180. &tengo.Int{Value: 1},
  181. &tengo.Int{Value: 2}}},
  182. &tengo.Int{Value: 0},
  183. &tengo.Int{Value: -1}},
  184. wantErr: true,
  185. wantedErr: tengo.ErrIndexOutOfBounds,
  186. },
  187. {name: "insert with zero count",
  188. args: []tengo.Object{
  189. &tengo.Array{Value: []tengo.Object{
  190. &tengo.Int{Value: 0},
  191. &tengo.Int{Value: 1},
  192. &tengo.Int{Value: 2}}},
  193. &tengo.Int{Value: 0},
  194. &tengo.Int{Value: 0},
  195. &tengo.String{Value: "b"}},
  196. deleted: &tengo.Array{Value: []tengo.Object{}},
  197. Array: &tengo.Array{Value: []tengo.Object{
  198. &tengo.String{Value: "b"},
  199. &tengo.Int{Value: 0},
  200. &tengo.Int{Value: 1},
  201. &tengo.Int{Value: 2}}},
  202. },
  203. {name: "insert",
  204. args: []tengo.Object{
  205. &tengo.Array{Value: []tengo.Object{
  206. &tengo.Int{Value: 0},
  207. &tengo.Int{Value: 1},
  208. &tengo.Int{Value: 2}}},
  209. &tengo.Int{Value: 1},
  210. &tengo.Int{Value: 0},
  211. &tengo.String{Value: "c"},
  212. &tengo.String{Value: "d"}},
  213. deleted: &tengo.Array{Value: []tengo.Object{}},
  214. Array: &tengo.Array{Value: []tengo.Object{
  215. &tengo.Int{Value: 0},
  216. &tengo.String{Value: "c"},
  217. &tengo.String{Value: "d"},
  218. &tengo.Int{Value: 1},
  219. &tengo.Int{Value: 2}}},
  220. },
  221. {name: "insert with zero count",
  222. args: []tengo.Object{
  223. &tengo.Array{Value: []tengo.Object{
  224. &tengo.Int{Value: 0},
  225. &tengo.Int{Value: 1},
  226. &tengo.Int{Value: 2}}},
  227. &tengo.Int{Value: 1},
  228. &tengo.Int{Value: 0},
  229. &tengo.String{Value: "c"},
  230. &tengo.String{Value: "d"}},
  231. deleted: &tengo.Array{Value: []tengo.Object{}},
  232. Array: &tengo.Array{Value: []tengo.Object{
  233. &tengo.Int{Value: 0},
  234. &tengo.String{Value: "c"},
  235. &tengo.String{Value: "d"},
  236. &tengo.Int{Value: 1},
  237. &tengo.Int{Value: 2}}},
  238. },
  239. {name: "insert with delete",
  240. args: []tengo.Object{
  241. &tengo.Array{Value: []tengo.Object{
  242. &tengo.Int{Value: 0},
  243. &tengo.Int{Value: 1},
  244. &tengo.Int{Value: 2}}},
  245. &tengo.Int{Value: 1},
  246. &tengo.Int{Value: 1},
  247. &tengo.String{Value: "c"},
  248. &tengo.String{Value: "d"}},
  249. deleted: &tengo.Array{
  250. Value: []tengo.Object{&tengo.Int{Value: 1}}},
  251. Array: &tengo.Array{Value: []tengo.Object{
  252. &tengo.Int{Value: 0},
  253. &tengo.String{Value: "c"},
  254. &tengo.String{Value: "d"},
  255. &tengo.Int{Value: 2}}},
  256. },
  257. {name: "insert with delete multi",
  258. args: []tengo.Object{
  259. &tengo.Array{Value: []tengo.Object{
  260. &tengo.Int{Value: 0},
  261. &tengo.Int{Value: 1},
  262. &tengo.Int{Value: 2}}},
  263. &tengo.Int{Value: 1},
  264. &tengo.Int{Value: 2},
  265. &tengo.String{Value: "c"},
  266. &tengo.String{Value: "d"}},
  267. deleted: &tengo.Array{Value: []tengo.Object{
  268. &tengo.Int{Value: 1},
  269. &tengo.Int{Value: 2}}},
  270. Array: &tengo.Array{
  271. Value: []tengo.Object{
  272. &tengo.Int{Value: 0},
  273. &tengo.String{Value: "c"},
  274. &tengo.String{Value: "d"}}},
  275. },
  276. {name: "delete all with positive count",
  277. args: []tengo.Object{
  278. &tengo.Array{Value: []tengo.Object{
  279. &tengo.Int{Value: 0},
  280. &tengo.Int{Value: 1},
  281. &tengo.Int{Value: 2}}},
  282. &tengo.Int{Value: 0},
  283. &tengo.Int{Value: 3}},
  284. deleted: &tengo.Array{Value: []tengo.Object{
  285. &tengo.Int{Value: 0},
  286. &tengo.Int{Value: 1},
  287. &tengo.Int{Value: 2}}},
  288. Array: &tengo.Array{Value: []tengo.Object{}},
  289. },
  290. {name: "delete all with big count",
  291. args: []tengo.Object{
  292. &tengo.Array{Value: []tengo.Object{
  293. &tengo.Int{Value: 0},
  294. &tengo.Int{Value: 1},
  295. &tengo.Int{Value: 2}}},
  296. &tengo.Int{Value: 0},
  297. &tengo.Int{Value: 5}},
  298. deleted: &tengo.Array{Value: []tengo.Object{
  299. &tengo.Int{Value: 0},
  300. &tengo.Int{Value: 1},
  301. &tengo.Int{Value: 2}}},
  302. Array: &tengo.Array{Value: []tengo.Object{}},
  303. },
  304. {name: "nothing2",
  305. args: []tengo.Object{
  306. &tengo.Array{Value: []tengo.Object{
  307. &tengo.Int{Value: 0},
  308. &tengo.Int{Value: 1},
  309. &tengo.Int{Value: 2}}}},
  310. Array: &tengo.Array{Value: []tengo.Object{}},
  311. deleted: &tengo.Array{Value: []tengo.Object{
  312. &tengo.Int{Value: 0},
  313. &tengo.Int{Value: 1},
  314. &tengo.Int{Value: 2}}},
  315. },
  316. {name: "pop without count",
  317. args: []tengo.Object{
  318. &tengo.Array{Value: []tengo.Object{
  319. &tengo.Int{Value: 0},
  320. &tengo.Int{Value: 1},
  321. &tengo.Int{Value: 2}}},
  322. &tengo.Int{Value: 2}},
  323. deleted: &tengo.Array{Value: []tengo.Object{&tengo.Int{Value: 2}}},
  324. Array: &tengo.Array{Value: []tengo.Object{
  325. &tengo.Int{Value: 0}, &tengo.Int{Value: 1}}},
  326. },
  327. }
  328. for _, tt := range tests {
  329. t.Run(tt.name, func(t *testing.T) {
  330. got, err := builtinSplice(tt.args...)
  331. if (err != nil) != tt.wantErr {
  332. t.Errorf("builtinSplice() error = %v, wantErr %v",
  333. err, tt.wantErr)
  334. return
  335. }
  336. if !reflect.DeepEqual(got, tt.deleted) {
  337. t.Errorf("builtinSplice() = %v, want %v", got, tt.deleted)
  338. }
  339. if tt.wantErr && tt.wantedErr.Error() != err.Error() {
  340. t.Errorf("builtinSplice() error = %v, wantedErr %v",
  341. err, tt.wantedErr)
  342. }
  343. if tt.Array != nil && !reflect.DeepEqual(tt.Array, tt.args[0]) {
  344. t.Errorf("builtinSplice() arrays are not equal expected"+
  345. " %s, got %s", tt.Array, tt.args[0].(*tengo.Array))
  346. }
  347. })
  348. }
  349. }
  350. func Test_builtinRange(t *testing.T) {
  351. var builtinRange func(args ...tengo.Object) (tengo.Object, error)
  352. for _, f := range tengo.GetAllBuiltinFunctions() {
  353. if f.Name == "range" {
  354. builtinRange = f.Value
  355. break
  356. }
  357. }
  358. if builtinRange == nil {
  359. t.Fatal("builtin range not found")
  360. }
  361. tests := []struct {
  362. name string
  363. args []tengo.Object
  364. result *tengo.Array
  365. wantErr bool
  366. wantedErr error
  367. }{
  368. {name: "no args", args: []tengo.Object{}, wantErr: true,
  369. wantedErr: tengo.ErrWrongNumArguments,
  370. },
  371. {name: "single args", args: []tengo.Object{&tengo.Map{}},
  372. wantErr: true,
  373. wantedErr: tengo.ErrWrongNumArguments,
  374. },
  375. {name: "4 args", args: []tengo.Object{&tengo.Map{}, &tengo.String{}, &tengo.String{}, &tengo.String{}},
  376. wantErr: true,
  377. wantedErr: tengo.ErrWrongNumArguments,
  378. },
  379. {name: "invalid start",
  380. args: []tengo.Object{&tengo.String{}, &tengo.String{}},
  381. wantErr: true,
  382. wantedErr: tengo.ErrInvalidArgumentType{
  383. Name: "start", Expected: "int", Found: "string"},
  384. },
  385. {name: "invalid stop",
  386. args: []tengo.Object{&tengo.Int{}, &tengo.String{}},
  387. wantErr: true,
  388. wantedErr: tengo.ErrInvalidArgumentType{
  389. Name: "stop", Expected: "int", Found: "string"},
  390. },
  391. {name: "invalid step",
  392. args: []tengo.Object{&tengo.Int{}, &tengo.Int{}, &tengo.String{}},
  393. wantErr: true,
  394. wantedErr: tengo.ErrInvalidArgumentType{
  395. Name: "step", Expected: "int", Found: "string"},
  396. },
  397. {name: "zero step",
  398. args: []tengo.Object{&tengo.Int{}, &tengo.Int{}, &tengo.Int{}}, //must greate than 0
  399. wantErr: true,
  400. wantedErr: tengo.ErrInvalidRangeStep,
  401. },
  402. {name: "negative step",
  403. args: []tengo.Object{&tengo.Int{}, &tengo.Int{}, intObject(-2)}, //must greate than 0
  404. wantErr: true,
  405. wantedErr: tengo.ErrInvalidRangeStep,
  406. },
  407. {name: "same bound",
  408. args: []tengo.Object{&tengo.Int{}, &tengo.Int{}},
  409. wantErr: false,
  410. result: &tengo.Array{
  411. Value: nil,
  412. },
  413. },
  414. {name: "positive range",
  415. args: []tengo.Object{&tengo.Int{}, &tengo.Int{Value: 5}},
  416. wantErr: false,
  417. result: &tengo.Array{
  418. Value: []tengo.Object{
  419. intObject(0),
  420. intObject(1),
  421. intObject(2),
  422. intObject(3),
  423. intObject(4),
  424. },
  425. },
  426. },
  427. {name: "negative range",
  428. args: []tengo.Object{&tengo.Int{}, &tengo.Int{Value: -5}},
  429. wantErr: false,
  430. result: &tengo.Array{
  431. Value: []tengo.Object{
  432. intObject(0),
  433. intObject(-1),
  434. intObject(-2),
  435. intObject(-3),
  436. intObject(-4),
  437. },
  438. },
  439. },
  440. {name: "positive with step",
  441. args: []tengo.Object{&tengo.Int{}, &tengo.Int{Value: 5}, &tengo.Int{Value: 2}},
  442. wantErr: false,
  443. result: &tengo.Array{
  444. Value: []tengo.Object{
  445. intObject(0),
  446. intObject(2),
  447. intObject(4),
  448. },
  449. },
  450. },
  451. {name: "negative with step",
  452. args: []tengo.Object{&tengo.Int{}, &tengo.Int{Value: -10}, &tengo.Int{Value: 2}},
  453. wantErr: false,
  454. result: &tengo.Array{
  455. Value: []tengo.Object{
  456. intObject(0),
  457. intObject(-2),
  458. intObject(-4),
  459. intObject(-6),
  460. intObject(-8),
  461. },
  462. },
  463. },
  464. {name: "large range",
  465. args: []tengo.Object{intObject(-10), intObject(10), &tengo.Int{Value: 3}},
  466. wantErr: false,
  467. result: &tengo.Array{
  468. Value: []tengo.Object{
  469. intObject(-10),
  470. intObject(-7),
  471. intObject(-4),
  472. intObject(-1),
  473. intObject(2),
  474. intObject(5),
  475. intObject(8),
  476. },
  477. },
  478. },
  479. }
  480. for _, tt := range tests {
  481. t.Run(tt.name, func(t *testing.T) {
  482. got, err := builtinRange(tt.args...)
  483. if (err != nil) != tt.wantErr {
  484. t.Errorf("builtinRange() error = %v, wantErr %v",
  485. err, tt.wantErr)
  486. return
  487. }
  488. if tt.wantErr && tt.wantedErr.Error() != err.Error() {
  489. t.Errorf("builtinRange() error = %v, wantedErr %v",
  490. err, tt.wantedErr)
  491. }
  492. if tt.result != nil && !reflect.DeepEqual(tt.result, got) {
  493. t.Errorf("builtinRange() arrays are not equal expected"+
  494. " %s, got %s", tt.result, got.(*tengo.Array))
  495. }
  496. })
  497. }
  498. }