123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981 |
- package tengo_test
- import (
- "errors"
- "fmt"
- "math"
- "math/rand"
- _runtime "runtime"
- "strings"
- "testing"
- "github.com/d5/tengo/v2"
- "github.com/d5/tengo/v2/parser"
- "github.com/d5/tengo/v2/require"
- "github.com/d5/tengo/v2/stdlib"
- "github.com/d5/tengo/v2/token"
- )
- const testOut = "out"
- type IARR []interface{}
- type IMAP map[string]interface{}
- type MAP = map[string]interface{}
- type ARR = []interface{}
- type testopts struct {
- modules *tengo.ModuleMap
- symbols map[string]tengo.Object
- maxAllocs int64
- skip2ndPass bool
- }
- func Opts() *testopts {
- return &testopts{
- modules: tengo.NewModuleMap(),
- symbols: make(map[string]tengo.Object),
- maxAllocs: -1,
- skip2ndPass: false,
- }
- }
- func (o *testopts) copy() *testopts {
- c := &testopts{
- modules: o.modules.Copy(),
- symbols: make(map[string]tengo.Object),
- maxAllocs: o.maxAllocs,
- skip2ndPass: o.skip2ndPass,
- }
- for k, v := range o.symbols {
- c.symbols[k] = v
- }
- return c
- }
- func (o *testopts) Stdlib() *testopts {
- o.modules.AddMap(stdlib.GetModuleMap(stdlib.AllModuleNames()...))
- return o
- }
- func (o *testopts) Module(name string, mod interface{}) *testopts {
- c := o.copy()
- switch mod := mod.(type) {
- case tengo.Importable:
- c.modules.Add(name, mod)
- case string:
- c.modules.AddSourceModule(name, []byte(mod))
- case []byte:
- c.modules.AddSourceModule(name, mod)
- default:
- panic(fmt.Errorf("invalid module type: %T", mod))
- }
- return c
- }
- func (o *testopts) Symbol(name string, value tengo.Object) *testopts {
- c := o.copy()
- c.symbols[name] = value
- return c
- }
- func (o *testopts) MaxAllocs(limit int64) *testopts {
- c := o.copy()
- c.maxAllocs = limit
- return c
- }
- func (o *testopts) Skip2ndPass() *testopts {
- c := o.copy()
- c.skip2ndPass = true
- return c
- }
- type customError struct {
- err error
- str string
- }
- func (c *customError) Error() string {
- return c.str
- }
- func (c *customError) Unwrap() error {
- return c.err
- }
- func TestArray(t *testing.T) {
- expectRun(t, `out = [1, 2 * 2, 3 + 3]`, nil, ARR{1, 4, 6})
- // array copy-by-reference
- expectRun(t, `a1 := [1, 2, 3]; a2 := a1; a1[0] = 5; out = a2`,
- nil, ARR{5, 2, 3})
- expectRun(t, `func () { a1 := [1, 2, 3]; a2 := a1; a1[0] = 5; out = a2 }()`,
- nil, ARR{5, 2, 3})
- // array index set
- expectError(t, `a1 := [1, 2, 3]; a1[3] = 5`,
- nil, "index out of bounds")
- // index operator
- arr := ARR{1, 2, 3, 4, 5, 6}
- arrStr := `[1, 2, 3, 4, 5, 6]`
- arrLen := 6
- for idx := 0; idx < arrLen; idx++ {
- expectRun(t, fmt.Sprintf("out = %s[%d]", arrStr, idx),
- nil, arr[idx])
- expectRun(t, fmt.Sprintf("out = %s[0 + %d]", arrStr, idx),
- nil, arr[idx])
- expectRun(t, fmt.Sprintf("out = %s[1 + %d - 1]", arrStr, idx),
- nil, arr[idx])
- expectRun(t, fmt.Sprintf("idx := %d; out = %s[idx]", idx, arrStr),
- nil, arr[idx])
- }
- expectRun(t, fmt.Sprintf("%s[%d]", arrStr, -1),
- nil, tengo.UndefinedValue)
- expectRun(t, fmt.Sprintf("%s[%d]", arrStr, arrLen),
- nil, tengo.UndefinedValue)
- // slice operator
- for low := 0; low < arrLen; low++ {
- expectRun(t, fmt.Sprintf("out = %s[%d:%d]", arrStr, low, low),
- nil, ARR{})
- for high := low; high <= arrLen; high++ {
- expectRun(t, fmt.Sprintf("out = %s[%d:%d]", arrStr, low, high),
- nil, arr[low:high])
- expectRun(t, fmt.Sprintf("out = %s[0 + %d : 0 + %d]",
- arrStr, low, high), nil, arr[low:high])
- expectRun(t, fmt.Sprintf("out = %s[1 + %d - 1 : 1 + %d - 1]",
- arrStr, low, high), nil, arr[low:high])
- expectRun(t, fmt.Sprintf("out = %s[:%d]", arrStr, high),
- nil, arr[:high])
- expectRun(t, fmt.Sprintf("out = %s[%d:]", arrStr, low),
- nil, arr[low:])
- }
- }
- expectRun(t, fmt.Sprintf("out = %s[:]", arrStr),
- nil, arr)
- expectRun(t, fmt.Sprintf("out = %s[%d:]", arrStr, -1),
- nil, arr)
- expectRun(t, fmt.Sprintf("out = %s[:%d]", arrStr, arrLen+1),
- nil, arr)
- expectRun(t, fmt.Sprintf("out = %s[%d:%d]", arrStr, 2, 2),
- nil, ARR{})
- expectError(t, fmt.Sprintf("%s[:%d]", arrStr, -1),
- nil, "invalid slice index")
- expectError(t, fmt.Sprintf("%s[%d:]", arrStr, arrLen+1),
- nil, "invalid slice index")
- expectError(t, fmt.Sprintf("%s[%d:%d]", arrStr, 0, -1),
- nil, "invalid slice index")
- expectError(t, fmt.Sprintf("%s[%d:%d]", arrStr, 2, 1),
- nil, "invalid slice index")
- }
- func TestAssignment(t *testing.T) {
- expectRun(t, `a := 1; a = 2; out = a`, nil, 2)
- expectRun(t, `a := 1; a = 2; out = a`, nil, 2)
- expectRun(t, `a := 1; a = a + 4; out = a`, nil, 5)
- expectRun(t, `a := 1; f1 := func() { a = 2; return a }; out = f1()`,
- nil, 2)
- expectRun(t, `a := 1; f1 := func() { a := 3; a = 2; return a }; out = f1()`,
- nil, 2)
- expectRun(t, `a := 1; out = a`, nil, 1)
- expectRun(t, `a := 1; a = 2; out = a`, nil, 2)
- expectRun(t, `a := 1; func() { a = 2 }(); out = a`, nil, 2)
- expectRun(t, `a := 1; func() { a := 2 }(); out = a`, nil, 1) // "a := 2" defines a new local variable 'a'
- expectRun(t, `a := 1; func() { b := 2; out = b }()`, nil, 2)
- expectRun(t, `
- out = func() {
- a := 2
- func() {
- a = 3 // captured from outer scope
- }()
- return a
- }()
- `, nil, 3)
- expectRun(t, `
- func() {
- a := 5
- out = func() {
- a := 4
- return a
- }()
- }()`, nil, 4)
- expectError(t, `a := 1; a := 2`, nil, "redeclared") // redeclared in the same scope
- expectError(t, `func() { a := 1; a := 2 }()`, nil, "redeclared") // redeclared in the same scope
- expectRun(t, `a := 1; a += 2; out = a`, nil, 3)
- expectRun(t, `a := 1; a += 4 - 2;; out = a`, nil, 3)
- expectRun(t, `a := 3; a -= 1;; out = a`, nil, 2)
- expectRun(t, `a := 3; a -= 5 - 4;; out = a`, nil, 2)
- expectRun(t, `a := 2; a *= 4;; out = a`, nil, 8)
- expectRun(t, `a := 2; a *= 1 + 3;; out = a`, nil, 8)
- expectRun(t, `a := 10; a /= 2;; out = a`, nil, 5)
- expectRun(t, `a := 10; a /= 5 - 3;; out = a`, nil, 5)
- // compound assignment operator does not define new variable
- expectError(t, `a += 4`, nil, "unresolved reference")
- expectError(t, `a -= 4`, nil, "unresolved reference")
- expectError(t, `a *= 4`, nil, "unresolved reference")
- expectError(t, `a /= 4`, nil, "unresolved reference")
- expectRun(t, `
- f1 := func() {
- f2 := func() {
- a := 1
- a += 2 // it's a statement, not an expression
- return a
- };
-
- return f2();
- };
- out = f1();`, nil, 3)
- expectRun(t, `f1 := func() { f2 := func() { a := 1; a += 4 - 2; return a }; return f2(); }; out = f1()`,
- nil, 3)
- expectRun(t, `f1 := func() { f2 := func() { a := 3; a -= 1; return a }; return f2(); }; out = f1()`,
- nil, 2)
- expectRun(t, `f1 := func() { f2 := func() { a := 3; a -= 5 - 4; return a }; return f2(); }; out = f1()`,
- nil, 2)
- expectRun(t, `f1 := func() { f2 := func() { a := 2; a *= 4; return a }; return f2(); }; out = f1()`,
- nil, 8)
- expectRun(t, `f1 := func() { f2 := func() { a := 2; a *= 1 + 3; return a }; return f2(); }; out = f1()`,
- nil, 8)
- expectRun(t, `f1 := func() { f2 := func() { a := 10; a /= 2; return a }; return f2(); }; out = f1()`,
- nil, 5)
- expectRun(t, `f1 := func() { f2 := func() { a := 10; a /= 5 - 3; return a }; return f2(); }; out = f1()`,
- nil, 5)
- expectRun(t, `a := 1; f1 := func() { f2 := func() { a += 2; return a }; return f2(); }; out = f1()`,
- nil, 3)
- expectRun(t, `
- f1 := func(a) {
- return func(b) {
- c := a
- c += b * 2
- return c
- }
- }
-
- out = f1(3)(4)
- `, nil, 11)
- expectRun(t, `
- out = func() {
- a := 1
- func() {
- a = 2
- func() {
- a = 3
- func() {
- a := 4 // declared new
- }()
- }()
- }()
- return a
- }()
- `, nil, 3)
- // write on free variables
- expectRun(t, `
- f1 := func() {
- a := 5
-
- return func() {
- a += 3
- return a
- }()
- }
- out = f1()
- `, nil, 8)
- expectRun(t, `
- out = func() {
- f1 := func() {
- a := 5
- add1 := func() { a += 1 }
- add2 := func() { a += 2 }
- a += 3
- return func() { a += 4; add1(); add2(); a += 5; return a }
- }
- return f1()
- }()()
- `, nil, 20)
- expectRun(t, `
- it := func(seq, fn) {
- fn(seq[0])
- fn(seq[1])
- fn(seq[2])
- }
-
- foo := func(a) {
- b := 0
- it([1, 2, 3], func(x) {
- b = x + a
- })
- return b
- }
-
- out = foo(2)
- `, nil, 5)
- expectRun(t, `
- it := func(seq, fn) {
- fn(seq[0])
- fn(seq[1])
- fn(seq[2])
- }
-
- foo := func(a) {
- b := 0
- it([1, 2, 3], func(x) {
- b += x + a
- })
- return b
- }
-
- out = foo(2)
- `, nil, 12)
- expectRun(t, `
- out = func() {
- a := 1
- func() {
- a = 2
- }()
- return a
- }()
- `, nil, 2)
- expectRun(t, `
- f := func() {
- a := 1
- return {
- b: func() { a += 3 },
- c: func() { a += 2 },
- d: func() { return a }
- }
- }
- m := f()
- m.b()
- m.c()
- out = m.d()
- `, nil, 6)
- expectRun(t, `
- each := func(s, x) { for i:=0; i<len(s); i++ { x(s[i]) } }
- out = func() {
- a := 100
- each([1, 2, 3], func(x) {
- a += x
- })
- a += 10
- return func(b) {
- return a + b
- }
- }()(20)
- `, nil, 136)
- // assigning different type value
- expectRun(t, `a := 1; a = "foo"; out = a`, nil, "foo") // global
- expectRun(t, `func() { a := 1; a = "foo"; out = a }()`, nil, "foo") // local
- expectRun(t, `
- out = func() {
- a := 5
- return func() {
- a = "foo"
- return a
- }()
- }()`, nil, "foo") // free
- // variables declared in if/for blocks
- expectRun(t, `for a:=0; a<5; a++ {}; a := "foo"; out = a`,
- nil, "foo")
- expectRun(t, `func() { for a:=0; a<5; a++ {}; a := "foo"; out = a }()`,
- nil, "foo")
- // selectors
- expectRun(t, `a:=[1,2,3]; a[1] = 5; out = a[1]`, nil, 5)
- expectRun(t, `a:=[1,2,3]; a[1] += 5; out = a[1]`, nil, 7)
- expectRun(t, `a:={b:1,c:2}; a.b = 5; out = a.b`, nil, 5)
- expectRun(t, `a:={b:1,c:2}; a.b += 5; out = a.b`, nil, 6)
- expectRun(t, `a:={b:1,c:2}; a.b += a.c; out = a.b`, nil, 3)
- expectRun(t, `a:={b:1,c:2}; a.b += a.c; out = a.c`, nil, 2)
- expectRun(t, `
- a := {
- b: [1, 2, 3],
- c: {
- d: 8,
- e: "foo",
- f: [9, 8]
- }
- }
- a.c.f[1] += 2
- out = a["c"]["f"][1]
- `, nil, 10)
- expectRun(t, `
- a := {
- b: [1, 2, 3],
- c: {
- d: 8,
- e: "foo",
- f: [9, 8]
- }
- }
- a.c.h = "bar"
- out = a.c.h
- `, nil, "bar")
- expectError(t, `
- a := {
- b: [1, 2, 3],
- c: {
- d: 8,
- e: "foo",
- f: [9, 8]
- }
- }
- a.x.e = "bar"`, nil, "not index-assignable")
- }
- func TestBitwise(t *testing.T) {
- expectRun(t, `out = 1 & 1`, nil, 1)
- expectRun(t, `out = 1 & 0`, nil, 0)
- expectRun(t, `out = 0 & 1`, nil, 0)
- expectRun(t, `out = 0 & 0`, nil, 0)
- expectRun(t, `out = 1 | 1`, nil, 1)
- expectRun(t, `out = 1 | 0`, nil, 1)
- expectRun(t, `out = 0 | 1`, nil, 1)
- expectRun(t, `out = 0 | 0`, nil, 0)
- expectRun(t, `out = 1 ^ 1`, nil, 0)
- expectRun(t, `out = 1 ^ 0`, nil, 1)
- expectRun(t, `out = 0 ^ 1`, nil, 1)
- expectRun(t, `out = 0 ^ 0`, nil, 0)
- expectRun(t, `out = 1 &^ 1`, nil, 0)
- expectRun(t, `out = 1 &^ 0`, nil, 1)
- expectRun(t, `out = 0 &^ 1`, nil, 0)
- expectRun(t, `out = 0 &^ 0`, nil, 0)
- expectRun(t, `out = 1 << 2`, nil, 4)
- expectRun(t, `out = 16 >> 2`, nil, 4)
- expectRun(t, `out = 1; out &= 1`, nil, 1)
- expectRun(t, `out = 1; out |= 0`, nil, 1)
- expectRun(t, `out = 1; out ^= 0`, nil, 1)
- expectRun(t, `out = 1; out &^= 0`, nil, 1)
- expectRun(t, `out = 1; out <<= 2`, nil, 4)
- expectRun(t, `out = 16; out >>= 2`, nil, 4)
- expectRun(t, `out = ^0`, nil, ^0)
- expectRun(t, `out = ^1`, nil, ^1)
- expectRun(t, `out = ^55`, nil, ^55)
- expectRun(t, `out = ^-55`, nil, ^-55)
- }
- func TestBoolean(t *testing.T) {
- expectRun(t, `out = true`, nil, true)
- expectRun(t, `out = false`, nil, false)
- expectRun(t, `out = 1 < 2`, nil, true)
- expectRun(t, `out = 1 > 2`, nil, false)
- expectRun(t, `out = 1 < 1`, nil, false)
- expectRun(t, `out = 1 > 2`, nil, false)
- expectRun(t, `out = 1 == 1`, nil, true)
- expectRun(t, `out = 1 != 1`, nil, false)
- expectRun(t, `out = 1 == 2`, nil, false)
- expectRun(t, `out = 1 != 2`, nil, true)
- expectRun(t, `out = 1 <= 2`, nil, true)
- expectRun(t, `out = 1 >= 2`, nil, false)
- expectRun(t, `out = 1 <= 1`, nil, true)
- expectRun(t, `out = 1 >= 2`, nil, false)
- expectRun(t, `out = true == true`, nil, true)
- expectRun(t, `out = false == false`, nil, true)
- expectRun(t, `out = true == false`, nil, false)
- expectRun(t, `out = true != false`, nil, true)
- expectRun(t, `out = false != true`, nil, true)
- expectRun(t, `out = (1 < 2) == true`, nil, true)
- expectRun(t, `out = (1 < 2) == false`, nil, false)
- expectRun(t, `out = (1 > 2) == true`, nil, false)
- expectRun(t, `out = (1 > 2) == false`, nil, true)
- expectError(t, `5 + true`, nil, "invalid operation")
- expectError(t, `5 + true; 5`, nil, "invalid operation")
- expectError(t, `-true`, nil, "invalid operation")
- expectError(t, `true + false`, nil, "invalid operation")
- expectError(t, `5; true + false; 5`, nil, "invalid operation")
- expectError(t, `if (10 > 1) { true + false; }`, nil, "invalid operation")
- expectError(t, `
- func() {
- if (10 > 1) {
- if (10 > 1) {
- return true + false;
- }
- return 1;
- }
- }()
- `, nil, "invalid operation")
- expectError(t, `if (true + false) { 10 }`, nil, "invalid operation")
- expectError(t, `10 + (true + false)`, nil, "invalid operation")
- expectError(t, `(true + false) + 20`, nil, "invalid operation")
- expectError(t, `!(true + false)`, nil, "invalid operation")
- }
- func TestUndefined(t *testing.T) {
- expectRun(t, `out = undefined`, nil, tengo.UndefinedValue)
- expectRun(t, `out = undefined.a`, nil, tengo.UndefinedValue)
- expectRun(t, `out = undefined[1]`, nil, tengo.UndefinedValue)
- expectRun(t, `out = undefined.a.b`, nil, tengo.UndefinedValue)
- expectRun(t, `out = undefined[1][2]`, nil, tengo.UndefinedValue)
- expectRun(t, `out = undefined ? 1 : 2`, nil, 2)
- expectRun(t, `out = undefined == undefined`, nil, true)
- expectRun(t, `out = undefined == 1`, nil, false)
- expectRun(t, `out = 1 == undefined`, nil, false)
- expectRun(t, `out = undefined == float([])`, nil, true)
- expectRun(t, `out = float([]) == undefined`, nil, true)
- }
- func TestBuiltinFunction(t *testing.T) {
- expectRun(t, `out = len("")`, nil, 0)
- expectRun(t, `out = len("four")`, nil, 4)
- expectRun(t, `out = len("hello world")`, nil, 11)
- expectRun(t, `out = len([])`, nil, 0)
- expectRun(t, `out = len([1, 2, 3])`, nil, 3)
- expectRun(t, `out = len({})`, nil, 0)
- expectRun(t, `out = len({a:1, b:2})`, nil, 2)
- expectRun(t, `out = len(immutable([]))`, nil, 0)
- expectRun(t, `out = len(immutable([1, 2, 3]))`, nil, 3)
- expectRun(t, `out = len(immutable({}))`, nil, 0)
- expectRun(t, `out = len(immutable({a:1, b:2}))`, nil, 2)
- expectError(t, `len(1)`, nil, "invalid type for argument")
- expectError(t, `len("one", "two")`, nil, "wrong number of arguments")
- expectRun(t, `out = copy(1)`, nil, 1)
- expectError(t, `copy(1, 2)`, nil, "wrong number of arguments")
- expectRun(t, `out = append([1, 2, 3], 4)`, nil, ARR{1, 2, 3, 4})
- expectRun(t, `out = append([1, 2, 3], 4, 5, 6)`, nil, ARR{1, 2, 3, 4, 5, 6})
- expectRun(t, `out = append([1, 2, 3], "foo", false)`,
- nil, ARR{1, 2, 3, "foo", false})
- expectRun(t, `out = int(1)`, nil, 1)
- expectRun(t, `out = int(1.8)`, nil, 1)
- expectRun(t, `out = int("-522")`, nil, -522)
- expectRun(t, `out = int(true)`, nil, 1)
- expectRun(t, `out = int(false)`, nil, 0)
- expectRun(t, `out = int('8')`, nil, 56)
- expectRun(t, `out = int([1])`, nil, tengo.UndefinedValue)
- expectRun(t, `out = int({a: 1})`, nil, tengo.UndefinedValue)
- expectRun(t, `out = int(undefined)`, nil, tengo.UndefinedValue)
- expectRun(t, `out = int("-522", 1)`, nil, -522)
- expectRun(t, `out = int(undefined, 1)`, nil, 1)
- expectRun(t, `out = int(undefined, 1.8)`, nil, 1.8)
- expectRun(t, `out = int(undefined, string(1))`, nil, "1")
- expectRun(t, `out = int(undefined, undefined)`, nil, tengo.UndefinedValue)
- expectRun(t, `out = string(1)`, nil, "1")
- expectRun(t, `out = string(1.8)`, nil, "1.8")
- expectRun(t, `out = string("-522")`, nil, "-522")
- expectRun(t, `out = string(true)`, nil, "true")
- expectRun(t, `out = string(false)`, nil, "false")
- expectRun(t, `out = string('8')`, nil, "8")
- expectRun(t, `out = string([1,8.1,true,3])`, nil, "[1, 8.1, true, 3]")
- expectRun(t, `out = string({b: "foo"})`, nil, `{b: "foo"}`)
- expectRun(t, `out = string(undefined)`, nil, tengo.UndefinedValue) // not "undefined"
- expectRun(t, `out = string(1, "-522")`, nil, "1")
- expectRun(t, `out = string(undefined, "-522")`, nil, "-522") // not "undefined"
- expectRun(t, `out = float(1)`, nil, 1.0)
- expectRun(t, `out = float(1.8)`, nil, 1.8)
- expectRun(t, `out = float("-52.2")`, nil, -52.2)
- expectRun(t, `out = float(true)`, nil, tengo.UndefinedValue)
- expectRun(t, `out = float(false)`, nil, tengo.UndefinedValue)
- expectRun(t, `out = float('8')`, nil, tengo.UndefinedValue)
- expectRun(t, `out = float([1,8.1,true,3])`, nil, tengo.UndefinedValue)
- expectRun(t, `out = float({a: 1, b: "foo"})`, nil, tengo.UndefinedValue)
- expectRun(t, `out = float(undefined)`, nil, tengo.UndefinedValue)
- expectRun(t, `out = float("-52.2", 1.8)`, nil, -52.2)
- expectRun(t, `out = float(undefined, 1)`, nil, 1)
- expectRun(t, `out = float(undefined, 1.8)`, nil, 1.8)
- expectRun(t, `out = float(undefined, "-52.2")`, nil, "-52.2")
- expectRun(t, `out = float(undefined, char(56))`, nil, '8')
- expectRun(t, `out = float(undefined, undefined)`, nil, tengo.UndefinedValue)
- expectRun(t, `out = char(56)`, nil, '8')
- expectRun(t, `out = char(1.8)`, nil, tengo.UndefinedValue)
- expectRun(t, `out = char("-52.2")`, nil, tengo.UndefinedValue)
- expectRun(t, `out = char(true)`, nil, tengo.UndefinedValue)
- expectRun(t, `out = char(false)`, nil, tengo.UndefinedValue)
- expectRun(t, `out = char('8')`, nil, '8')
- expectRun(t, `out = char([1,8.1,true,3])`, nil, tengo.UndefinedValue)
- expectRun(t, `out = char({a: 1, b: "foo"})`, nil, tengo.UndefinedValue)
- expectRun(t, `out = char(undefined)`, nil, tengo.UndefinedValue)
- expectRun(t, `out = char(56, 'a')`, nil, '8')
- expectRun(t, `out = char(undefined, '8')`, nil, '8')
- expectRun(t, `out = char(undefined, 56)`, nil, 56)
- expectRun(t, `out = char(undefined, "-52.2")`, nil, "-52.2")
- expectRun(t, `out = char(undefined, undefined)`, nil, tengo.UndefinedValue)
- expectRun(t, `out = bool(1)`, nil, true) // non-zero integer: true
- expectRun(t, `out = bool(0)`, nil, false) // zero: true
- expectRun(t, `out = bool(1.8)`, nil, true) // all floats (except for NaN): true
- expectRun(t, `out = bool(0.0)`, nil, true) // all floats (except for NaN): true
- expectRun(t, `out = bool("false")`, nil, true) // non-empty string: true
- expectRun(t, `out = bool("")`, nil, false) // empty string: false
- expectRun(t, `out = bool(true)`, nil, true) // true: true
- expectRun(t, `out = bool(false)`, nil, false) // false: false
- expectRun(t, `out = bool('8')`, nil, true) // non-zero chars: true
- expectRun(t, `out = bool(char(0))`, nil, false) // zero char: false
- expectRun(t, `out = bool([1])`, nil, true) // non-empty arrays: true
- expectRun(t, `out = bool([])`, nil, false) // empty array: false
- expectRun(t, `out = bool({a: 1})`, nil, true) // non-empty maps: true
- expectRun(t, `out = bool({})`, nil, false) // empty maps: false
- expectRun(t, `out = bool(undefined)`, nil, false) // undefined: false
- expectRun(t, `out = bytes(1)`, nil, []byte{0})
- expectRun(t, `out = bytes(1.8)`, nil, tengo.UndefinedValue)
- expectRun(t, `out = bytes("-522")`, nil, []byte{'-', '5', '2', '2'})
- expectRun(t, `out = bytes(true)`, nil, tengo.UndefinedValue)
- expectRun(t, `out = bytes(false)`, nil, tengo.UndefinedValue)
- expectRun(t, `out = bytes('8')`, nil, tengo.UndefinedValue)
- expectRun(t, `out = bytes([1])`, nil, tengo.UndefinedValue)
- expectRun(t, `out = bytes({a: 1})`, nil, tengo.UndefinedValue)
- expectRun(t, `out = bytes(undefined)`, nil, tengo.UndefinedValue)
- expectRun(t, `out = bytes("-522", ['8'])`, nil, []byte{'-', '5', '2', '2'})
- expectRun(t, `out = bytes(undefined, "-522")`, nil, "-522")
- expectRun(t, `out = bytes(undefined, 1)`, nil, 1)
- expectRun(t, `out = bytes(undefined, 1.8)`, nil, 1.8)
- expectRun(t, `out = bytes(undefined, int("-522"))`, nil, -522)
- expectRun(t, `out = bytes(undefined, undefined)`, nil, tengo.UndefinedValue)
- expectRun(t, `out = is_error(error(1))`, nil, true)
- expectRun(t, `out = is_error(1)`, nil, false)
- expectRun(t, `out = is_undefined(undefined)`, nil, true)
- expectRun(t, `out = is_undefined(error(1))`, nil, false)
- // type_name
- expectRun(t, `out = type_name(1)`, nil, "int")
- expectRun(t, `out = type_name(1.1)`, nil, "float")
- expectRun(t, `out = type_name("a")`, nil, "string")
- expectRun(t, `out = type_name([1,2,3])`, nil, "array")
- expectRun(t, `out = type_name({k:1})`, nil, "map")
- expectRun(t, `out = type_name('a')`, nil, "char")
- expectRun(t, `out = type_name(true)`, nil, "bool")
- expectRun(t, `out = type_name(false)`, nil, "bool")
- expectRun(t, `out = type_name(bytes( 1))`, nil, "bytes")
- expectRun(t, `out = type_name(undefined)`, nil, "undefined")
- expectRun(t, `out = type_name(error("err"))`, nil, "error")
- expectRun(t, `out = type_name(func() {})`, nil, "compiled-function")
- expectRun(t, `a := func(x) { return func() { return x } }; out = type_name(a(5))`,
- nil, "compiled-function") // closure
- // is_function
- expectRun(t, `out = is_function(1)`, nil, false)
- expectRun(t, `out = is_function(func() {})`, nil, true)
- expectRun(t, `out = is_function(func(x) { return x })`, nil, true)
- expectRun(t, `out = is_function(len)`, nil, false) // builtin function
- expectRun(t, `a := func(x) { return func() { return x } }; out = is_function(a)`,
- nil, true) // function
- expectRun(t, `a := func(x) { return func() { return x } }; out = is_function(a(5))`,
- nil, true) // closure
- expectRun(t, `out = is_function(x)`,
- Opts().Symbol("x", &StringArray{
- Value: []string{"foo", "bar"},
- }).Skip2ndPass(),
- false) // user object
- // is_callable
- expectRun(t, `out = is_callable(1)`, nil, false)
- expectRun(t, `out = is_callable(func() {})`, nil, true)
- expectRun(t, `out = is_callable(func(x) { return x })`, nil, true)
- expectRun(t, `out = is_callable(len)`, nil, true) // builtin function
- expectRun(t, `a := func(x) { return func() { return x } }; out = is_callable(a)`,
- nil, true) // function
- expectRun(t, `a := func(x) { return func() { return x } }; out = is_callable(a(5))`,
- nil, true) // closure
- expectRun(t, `out = is_callable(x)`,
- Opts().Symbol("x", &StringArray{
- Value: []string{"foo", "bar"},
- }).Skip2ndPass(), true) // user object
- expectRun(t, `out = format("")`, nil, "")
- expectRun(t, `out = format("foo")`, nil, "foo")
- expectRun(t, `out = format("foo %d %v %s", 1, 2, "bar")`,
- nil, "foo 1 2 bar")
- expectRun(t, `out = format("foo %v", [1, "bar", true])`,
- nil, `foo [1, "bar", true]`)
- expectRun(t, `out = format("foo %v %d", [1, "bar", true], 19)`,
- nil, `foo [1, "bar", true] 19`)
- expectRun(t, `out = format("foo %v", {"a": {"b": {"c": [1, 2, 3]}}})`,
- nil, `foo {a: {b: {c: [1, 2, 3]}}}`)
- expectRun(t, `out = format("%v", [1, [2, [3, 4]]])`,
- nil, `[1, [2, [3, 4]]]`)
- tengo.MaxStringLen = 9
- expectError(t, `format("%s", "1234567890")`,
- nil, "exceeding string size limit")
- tengo.MaxStringLen = 2147483647
- // delete
- expectError(t, `delete()`, nil, tengo.ErrWrongNumArguments.Error())
- expectError(t, `delete(1)`, nil, tengo.ErrWrongNumArguments.Error())
- expectError(t, `delete(1, 2, 3)`, nil, tengo.ErrWrongNumArguments.Error())
- expectError(t, `delete({}, "", 3)`, nil, tengo.ErrWrongNumArguments.Error())
- expectError(t, `delete(1, 1)`, nil, `invalid type for argument 'first'`)
- expectError(t, `delete(1.0, 1)`, nil, `invalid type for argument 'first'`)
- expectError(t, `delete("str", 1)`, nil, `invalid type for argument 'first'`)
- expectError(t, `delete(bytes("str"), 1)`, nil,
- `invalid type for argument 'first'`)
- expectError(t, `delete(error("err"), 1)`, nil,
- `invalid type for argument 'first'`)
- expectError(t, `delete(true, 1)`, nil, `invalid type for argument 'first'`)
- expectError(t, `delete(char('c'), 1)`, nil,
- `invalid type for argument 'first'`)
- expectError(t, `delete(undefined, 1)`, nil,
- `invalid type for argument 'first'`)
- expectError(t, `delete(time(1257894000), 1)`, nil,
- `invalid type for argument 'first'`)
- expectError(t, `delete(immutable({}), "key")`, nil,
- `invalid type for argument 'first'`)
- expectError(t, `delete(immutable([]), "")`, nil,
- `invalid type for argument 'first'`)
- expectError(t, `delete([], "")`, nil, `invalid type for argument 'first'`)
- expectError(t, `delete({}, 1)`, nil, `invalid type for argument 'second'`)
- expectError(t, `delete({}, 1.0)`, nil, `invalid type for argument 'second'`)
- expectError(t, `delete({}, undefined)`, nil,
- `invalid type for argument 'second'`)
- expectError(t, `delete({}, [])`, nil, `invalid type for argument 'second'`)
- expectError(t, `delete({}, {})`, nil, `invalid type for argument 'second'`)
- expectError(t, `delete({}, error("err"))`, nil,
- `invalid type for argument 'second'`)
- expectError(t, `delete({}, bytes("str"))`, nil,
- `invalid type for argument 'second'`)
- expectError(t, `delete({}, char(35))`, nil,
- `invalid type for argument 'second'`)
- expectError(t, `delete({}, time(1257894000))`, nil,
- `invalid type for argument 'second'`)
- expectError(t, `delete({}, immutable({}))`, nil,
- `invalid type for argument 'second'`)
- expectError(t, `delete({}, immutable([]))`, nil,
- `invalid type for argument 'second'`)
- expectRun(t, `out = delete({}, "")`, nil, tengo.UndefinedValue)
- expectRun(t, `out = {key1: 1}; delete(out, "key1")`, nil, MAP{})
- expectRun(t, `out = {key1: 1, key2: "2"}; delete(out, "key1")`, nil,
- MAP{"key2": "2"})
- expectRun(t, `out = [1, "2", {a: "b", c: 10}]; delete(out[2], "c")`, nil,
- ARR{1, "2", MAP{"a": "b"}})
- // splice
- expectError(t, `splice()`, nil, tengo.ErrWrongNumArguments.Error())
- expectError(t, `splice(1)`, nil, `invalid type for argument 'first'`)
- expectError(t, `splice(1.0)`, nil, `invalid type for argument 'first'`)
- expectError(t, `splice("str")`, nil, `invalid type for argument 'first'`)
- expectError(t, `splice(bytes("str"))`, nil,
- `invalid type for argument 'first'`)
- expectError(t, `splice(error("err"))`, nil,
- `invalid type for argument 'first'`)
- expectError(t, `splice(true)`, nil, `invalid type for argument 'first'`)
- expectError(t, `splice(char('c'))`, nil,
- `invalid type for argument 'first'`)
- expectError(t, `splice(undefined)`, nil,
- `invalid type for argument 'first'`)
- expectError(t, `splice(time(1257894000))`, nil,
- `invalid type for argument 'first'`)
- expectError(t, `splice(immutable({}))`, nil,
- `invalid type for argument 'first'`)
- expectError(t, `splice(immutable([]))`, nil,
- `invalid type for argument 'first'`)
- expectError(t, `splice({})`, nil, `invalid type for argument 'first'`)
- expectError(t, `splice([], 1.0)`, nil,
- `invalid type for argument 'second'`)
- expectError(t, `splice([], "str")`, nil,
- `invalid type for argument 'second'`)
- expectError(t, `splice([], bytes("str"))`, nil,
- `invalid type for argument 'second'`)
- expectError(t, `splice([], error("error"))`, nil,
- `invalid type for argument 'second'`)
- expectError(t, `splice([], false)`, nil,
- `invalid type for argument 'second'`)
- expectError(t, `splice([], char('d'))`, nil,
- `invalid type for argument 'second'`)
- expectError(t, `splice([], undefined)`, nil,
- `invalid type for argument 'second'`)
- expectError(t, `splice([], time(0))`, nil,
- `invalid type for argument 'second'`)
- expectError(t, `splice([], [])`, nil,
- `invalid type for argument 'second'`)
- expectError(t, `splice([], {})`, nil,
- `invalid type for argument 'second'`)
- expectError(t, `splice([], immutable([]))`, nil,
- `invalid type for argument 'second'`)
- expectError(t, `splice([], immutable({}))`, nil,
- `invalid type for argument 'second'`)
- expectError(t, `splice([], 0, 1.0)`, nil,
- `invalid type for argument 'third'`)
- expectError(t, `splice([], 0, "string")`, nil,
- `invalid type for argument 'third'`)
- expectError(t, `splice([], 0, bytes("string"))`, nil,
- `invalid type for argument 'third'`)
- expectError(t, `splice([], 0, error("string"))`, nil,
- `invalid type for argument 'third'`)
- expectError(t, `splice([], 0, true)`, nil,
- `invalid type for argument 'third'`)
- expectError(t, `splice([], 0, char('f'))`, nil,
- `invalid type for argument 'third'`)
- expectError(t, `splice([], 0, undefined)`, nil,
- `invalid type for argument 'third'`)
- expectError(t, `splice([], 0, time(0))`, nil,
- `invalid type for argument 'third'`)
- expectError(t, `splice([], 0, [])`, nil,
- `invalid type for argument 'third'`)
- expectError(t, `splice([], 0, {})`, nil,
- `invalid type for argument 'third'`)
- expectError(t, `splice([], 0, immutable([]))`, nil,
- `invalid type for argument 'third'`)
- expectError(t, `splice([], 0, immutable({}))`, nil,
- `invalid type for argument 'third'`)
- expectError(t, `splice([], 1)`, nil, tengo.ErrIndexOutOfBounds.Error())
- expectError(t, `splice([1, 2, 3], 0, -1)`, nil,
- tengo.ErrIndexOutOfBounds.Error())
- expectError(t, `splice([1, 2, 3], 99, 0, "a", "b")`, nil,
- tengo.ErrIndexOutOfBounds.Error())
- expectRun(t, `out = []; splice(out)`, nil, ARR{})
- expectRun(t, `out = ["a"]; splice(out, 1)`, nil, ARR{"a"})
- expectRun(t, `out = ["a"]; out = splice(out, 1)`, nil, ARR{})
- expectRun(t, `out = [1, 2, 3]; splice(out, 0, 1)`, nil, ARR{2, 3})
- expectRun(t, `out = [1, 2, 3]; out = splice(out, 0, 1)`, nil, ARR{1})
- expectRun(t, `out = [1, 2, 3]; splice(out, 0, 0, "a", "b")`, nil,
- ARR{"a", "b", 1, 2, 3})
- expectRun(t, `out = [1, 2, 3]; out = splice(out, 0, 0, "a", "b")`, nil,
- ARR{})
- expectRun(t, `out = [1, 2, 3]; splice(out, 1, 0, "a", "b")`, nil,
- ARR{1, "a", "b", 2, 3})
- expectRun(t, `out = [1, 2, 3]; out = splice(out, 1, 0, "a", "b")`, nil,
- ARR{})
- expectRun(t, `out = [1, 2, 3]; splice(out, 1, 0, "a", "b")`, nil,
- ARR{1, "a", "b", 2, 3})
- expectRun(t, `out = [1, 2, 3]; splice(out, 2, 0, "a", "b")`, nil,
- ARR{1, 2, "a", "b", 3})
- expectRun(t, `out = [1, 2, 3]; splice(out, 3, 0, "a", "b")`, nil,
- ARR{1, 2, 3, "a", "b"})
- expectRun(t, `array := [1, 2, 3]; deleted := splice(array, 1, 1, "a", "b");
- out = [deleted, array]`, nil, ARR{ARR{2}, ARR{1, "a", "b", 3}})
- expectRun(t, `array := [1, 2, 3]; deleted := splice(array, 1);
- out = [deleted, array]`, nil, ARR{ARR{2, 3}, ARR{1}})
- expectRun(t, `out = []; splice(out, 0, 0, "a", "b")`, nil, ARR{"a", "b"})
- expectRun(t, `out = []; splice(out, 0, 1, "a", "b")`, nil, ARR{"a", "b"})
- expectRun(t, `out = []; out = splice(out, 0, 0, "a", "b")`, nil, ARR{})
- expectRun(t, `out = splice(splice([1, 2, 3], 0, 3), 1, 3)`, nil, ARR{2, 3})
- // splice doc examples
- expectRun(t, `v := [1, 2, 3]; deleted := splice(v, 0);
- out = [deleted, v]`, nil, ARR{ARR{1, 2, 3}, ARR{}})
- expectRun(t, `v := [1, 2, 3]; deleted := splice(v, 1);
- out = [deleted, v]`, nil, ARR{ARR{2, 3}, ARR{1}})
- expectRun(t, `v := [1, 2, 3]; deleted := splice(v, 0, 1);
- out = [deleted, v]`, nil, ARR{ARR{1}, ARR{2, 3}})
- expectRun(t, `v := ["a", "b", "c"]; deleted := splice(v, 1, 2);
- out = [deleted, v]`, nil, ARR{ARR{"b", "c"}, ARR{"a"}})
- expectRun(t, `v := ["a", "b", "c"]; deleted := splice(v, 2, 1, "d");
- out = [deleted, v]`, nil, ARR{ARR{"c"}, ARR{"a", "b", "d"}})
- expectRun(t, `v := ["a", "b", "c"]; deleted := splice(v, 0, 0, "d", "e");
- out = [deleted, v]`, nil, ARR{ARR{}, ARR{"d", "e", "a", "b", "c"}})
- expectRun(t, `v := ["a", "b", "c"]; deleted := splice(v, 1, 1, "d", "e");
- out = [deleted, v]`, nil, ARR{ARR{"b"}, ARR{"a", "d", "e", "c"}})
- }
- func TestBytesN(t *testing.T) {
- curMaxBytesLen := tengo.MaxBytesLen
- defer func() { tengo.MaxBytesLen = curMaxBytesLen }()
- tengo.MaxBytesLen = 10
- expectRun(t, `out = bytes(0)`, nil, make([]byte, 0))
- expectRun(t, `out = bytes(10)`, nil, make([]byte, 10))
- expectError(t, `bytes(11)`, nil, "bytes size limit")
- tengo.MaxBytesLen = 1000
- expectRun(t, `out = bytes(1000)`, nil, make([]byte, 1000))
- expectError(t, `bytes(1001)`, nil, "bytes size limit")
- }
- func TestBytes(t *testing.T) {
- expectRun(t, `out = bytes("Hello World!")`, nil, []byte("Hello World!"))
- expectRun(t, `out = bytes("Hello") + bytes(" ") + bytes("World!")`,
- nil, []byte("Hello World!"))
- // bytes[] -> int
- expectRun(t, `out = bytes("abcde")[0]`, nil, 97)
- expectRun(t, `out = bytes("abcde")[1]`, nil, 98)
- expectRun(t, `out = bytes("abcde")[4]`, nil, 101)
- expectRun(t, `out = bytes("abcde")[10]`, nil, tengo.UndefinedValue)
- }
- func TestCall(t *testing.T) {
- expectRun(t, `a := { b: func(x) { return x + 2 } }; out = a.b(5)`,
- nil, 7)
- expectRun(t, `a := { b: { c: func(x) { return x + 2 } } }; out = a.b.c(5)`,
- nil, 7)
- expectRun(t, `a := { b: { c: func(x) { return x + 2 } } }; out = a["b"].c(5)`,
- nil, 7)
- expectError(t, `a := 1
- b := func(a, c) {
- c(a)
- }
- c := func(a) {
- a()
- }
- b(a, c)
- `, nil, "Runtime Error: not callable: int\n\tat test:7:4\n\tat test:3:4\n\tat test:9:1")
- }
- func TestChar(t *testing.T) {
- expectRun(t, `out = 'a'`, nil, 'a')
- expectRun(t, `out = '九'`, nil, rune(20061))
- expectRun(t, `out = 'Æ'`, nil, rune(198))
- expectRun(t, `out = '0' + '9'`, nil, rune(105))
- expectRun(t, `out = '0' + 9`, nil, '9')
- expectRun(t, `out = '9' - 4`, nil, '5')
- expectRun(t, `out = '0' == '0'`, nil, true)
- expectRun(t, `out = '0' != '0'`, nil, false)
- expectRun(t, `out = '2' < '4'`, nil, true)
- expectRun(t, `out = '2' > '4'`, nil, false)
- expectRun(t, `out = '2' <= '4'`, nil, true)
- expectRun(t, `out = '2' >= '4'`, nil, false)
- expectRun(t, `out = '4' < '4'`, nil, false)
- expectRun(t, `out = '4' > '4'`, nil, false)
- expectRun(t, `out = '4' <= '4'`, nil, true)
- expectRun(t, `out = '4' >= '4'`, nil, true)
- }
- func TestCondExpr(t *testing.T) {
- expectRun(t, `out = true ? 5 : 10`, nil, 5)
- expectRun(t, `out = false ? 5 : 10`, nil, 10)
- expectRun(t, `out = (1 == 1) ? 2 + 3 : 12 - 2`, nil, 5)
- expectRun(t, `out = (1 != 1) ? 2 + 3 : 12 - 2`, nil, 10)
- expectRun(t, `out = (1 == 1) ? true ? 10 - 8 : 1 + 3 : 12 - 2`, nil, 2)
- expectRun(t, `out = (1 == 1) ? false ? 10 - 8 : 1 + 3 : 12 - 2`, nil, 4)
- expectRun(t, `
- out = 0
- f1 := func() { out += 10 }
- f2 := func() { out = -out }
- true ? f1() : f2()
- `, nil, 10)
- expectRun(t, `
- out = 5
- f1 := func() { out += 10 }
- f2 := func() { out = -out }
- false ? f1() : f2()
- `, nil, -5)
- expectRun(t, `
- f1 := func(a) { return a + 2 }
- f2 := func(a) { return a - 2 }
- f3 := func(a) { return a + 10 }
- f4 := func(a) { return -a }
- f := func(c) {
- return c == 0 ? f1(c) : f2(c) ? f3(c) : f4(c)
- }
- out = [f(0), f(1), f(2)]
- `, nil, ARR{2, 11, -2})
- expectRun(t, `f := func(a) { return -a }; out = f(true ? 5 : 3)`, nil, -5)
- expectRun(t, `out = [false?5:10, true?1:2]`, nil, ARR{10, 1})
- expectRun(t, `
- out = 1 > 2 ?
- 1 + 2 + 3 :
- 10 - 5`, nil, 5)
- }
- func TestEquality(t *testing.T) {
- testEquality(t, `1`, `1`, true)
- testEquality(t, `1`, `2`, false)
- testEquality(t, `1.0`, `1.0`, true)
- testEquality(t, `1.0`, `1.1`, false)
- testEquality(t, `true`, `true`, true)
- testEquality(t, `true`, `false`, false)
- testEquality(t, `"foo"`, `"foo"`, true)
- testEquality(t, `"foo"`, `"bar"`, false)
- testEquality(t, `'f'`, `'f'`, true)
- testEquality(t, `'f'`, `'b'`, false)
- testEquality(t, `[]`, `[]`, true)
- testEquality(t, `[1]`, `[1]`, true)
- testEquality(t, `[1]`, `[1, 2]`, false)
- testEquality(t, `["foo", "bar"]`, `["foo", "bar"]`, true)
- testEquality(t, `["foo", "bar"]`, `["bar", "foo"]`, false)
- testEquality(t, `{}`, `{}`, true)
- testEquality(t, `{a: 1, b: 2}`, `{b: 2, a: 1}`, true)
- testEquality(t, `{a: 1, b: 2}`, `{b: 2}`, false)
- testEquality(t, `{a: 1, b: {}}`, `{b: {}, a: 1}`, true)
- testEquality(t, `1`, `"foo"`, false)
- testEquality(t, `1`, `true`, false)
- testEquality(t, `[1]`, `["1"]`, false)
- testEquality(t, `[1, [2]]`, `[1, ["2"]]`, false)
- testEquality(t, `{a: 1}`, `{a: "1"}`, false)
- testEquality(t, `{a: 1, b: {c: 2}}`, `{a: 1, b: {c: "2"}}`, false)
- }
- func testEquality(t *testing.T, lhs, rhs string, expected bool) {
- // 1. equality is commutative
- // 2. equality and inequality must be always opposite
- expectRun(t, fmt.Sprintf("out = %s == %s", lhs, rhs), nil, expected)
- expectRun(t, fmt.Sprintf("out = %s == %s", rhs, lhs), nil, expected)
- expectRun(t, fmt.Sprintf("out = %s != %s", lhs, rhs), nil, !expected)
- expectRun(t, fmt.Sprintf("out = %s != %s", rhs, lhs), nil, !expected)
- }
- func TestVMErrorInfo(t *testing.T) {
- expectError(t, `a := 5
- a + "boo"`,
- nil, "Runtime Error: invalid operation: int + string\n\tat test:2:1")
- expectError(t, `a := 5
- b := a(5)`,
- nil, "Runtime Error: not callable: int\n\tat test:2:6")
- expectError(t, `a := 5
- b := {}
- b.x.y = 10`,
- nil, "Runtime Error: not index-assignable: undefined\n\tat test:3:1")
- expectError(t, `
- a := func() {
- b := 5
- b += "foo"
- }
- a()`,
- nil, "Runtime Error: invalid operation: int + string\n\tat test:4:2")
- expectError(t, `a := 5
- a + import("mod1")`, Opts().Module(
- "mod1", `export "foo"`,
- ), ": invalid operation: int + string\n\tat test:2:1")
- expectError(t, `a := import("mod1")()`,
- Opts().Module(
- "mod1", `
- export func() {
- b := 5
- return b + "foo"
- }`), "Runtime Error: invalid operation: int + string\n\tat mod1:4:9")
- expectError(t, `a := import("mod1")()`,
- Opts().Module(
- "mod1", `export import("mod2")()`).
- Module(
- "mod2", `
- export func() {
- b := 5
- return b + "foo"
- }`), "Runtime Error: invalid operation: int + string\n\tat mod2:4:9")
- }
- func TestVMErrorUnwrap(t *testing.T) {
- userErr := errors.New("user runtime error")
- userFunc := func(err error) *tengo.UserFunction {
- return &tengo.UserFunction{Name: "user_func", Value: func(args ...tengo.Object) (tengo.Object, error) {
- return nil, err
- }}
- }
- userModule := func(err error) *tengo.BuiltinModule {
- return &tengo.BuiltinModule{
- Attrs: map[string]tengo.Object{
- "afunction": &tengo.UserFunction{
- Name: "afunction",
- Value: func(a ...tengo.Object) (tengo.Object, error) {
- return nil, err
- },
- },
- },
- }
- }
- expectError(t, `user_func()`,
- Opts().Symbol("user_func", userFunc(userErr)),
- "Runtime Error: "+userErr.Error(),
- )
- expectErrorIs(t, `user_func()`,
- Opts().Symbol("user_func", userFunc(userErr)),
- userErr,
- )
- wrapUserErr := &customError{err: userErr, str: "custom error"}
- expectErrorIs(t, `user_func()`,
- Opts().Symbol("user_func", userFunc(wrapUserErr)),
- wrapUserErr,
- )
- expectErrorIs(t, `user_func()`,
- Opts().Symbol("user_func", userFunc(wrapUserErr)),
- userErr,
- )
- var asErr1 *customError
- expectErrorAs(t, `user_func()`,
- Opts().Symbol("user_func", userFunc(wrapUserErr)),
- &asErr1,
- )
- require.True(t, asErr1.Error() == wrapUserErr.Error(),
- "expected error as:%v, got:%v", wrapUserErr, asErr1)
- expectError(t, `import("mod1").afunction()`,
- Opts().Module("mod1", userModule(userErr)),
- "Runtime Error: "+userErr.Error(),
- )
- expectErrorIs(t, `import("mod1").afunction()`,
- Opts().Module("mod1", userModule(userErr)),
- userErr,
- )
- expectError(t, `import("mod1").afunction()`,
- Opts().Module("mod1", userModule(wrapUserErr)),
- "Runtime Error: "+wrapUserErr.Error(),
- )
- expectErrorIs(t, `import("mod1").afunction()`,
- Opts().Module("mod1", userModule(wrapUserErr)),
- wrapUserErr,
- )
- expectErrorIs(t, `import("mod1").afunction()`,
- Opts().Module("mod1", userModule(wrapUserErr)),
- userErr,
- )
- var asErr2 *customError
- expectErrorAs(t, `import("mod1").afunction()`,
- Opts().Module("mod1", userModule(wrapUserErr)),
- &asErr2,
- )
- require.True(t, asErr2.Error() == wrapUserErr.Error(),
- "expected error as:%v, got:%v", wrapUserErr, asErr2)
- }
- func TestError(t *testing.T) {
- expectRun(t, `out = error(1)`, nil, errorObject(1))
- expectRun(t, `out = error(1).value`, nil, 1)
- expectRun(t, `out = error("some error")`, nil, errorObject("some error"))
- expectRun(t, `out = error("some" + " error")`, nil, errorObject("some error"))
- expectRun(t, `out = func() { return error(5) }()`, nil, errorObject(5))
- expectRun(t, `out = error(error("foo"))`, nil, errorObject(errorObject("foo")))
- expectRun(t, `out = error("some error")`, nil, errorObject("some error"))
- expectRun(t, `out = error("some error").value`, nil, "some error")
- expectRun(t, `out = error("some error")["value"]`, nil, "some error")
- expectError(t, `error("error").err`, nil, "invalid index on error")
- expectError(t, `error("error").value_`, nil, "invalid index on error")
- expectError(t, `error([1,2,3])[1]`, nil, "invalid index on error")
- }
- func TestFloat(t *testing.T) {
- expectRun(t, `out = 0.0`, nil, 0.0)
- expectRun(t, `out = -10.3`, nil, -10.3)
- expectRun(t, `out = 3.2 + 2.0 * -4.0`, nil, -4.8)
- expectRun(t, `out = 4 + 2.3`, nil, 6.3)
- expectRun(t, `out = 2.3 + 4`, nil, 6.3)
- expectRun(t, `out = +5.0`, nil, 5.0)
- expectRun(t, `out = -5.0 + +5.0`, nil, 0.0)
- }
- func TestForIn(t *testing.T) {
- // array
- expectRun(t, `out = 0; for x in [1, 2, 3] { out += x }`,
- nil, 6) // value
- expectRun(t, `out = 0; for i, x in [1, 2, 3] { out += i + x }`,
- nil, 9) // index, value
- expectRun(t, `out = 0; func() { for i, x in [1, 2, 3] { out += i + x } }()`,
- nil, 9) // index, value
- expectRun(t, `out = 0; for i, _ in [1, 2, 3] { out += i }`,
- nil, 3) // index, _
- expectRun(t, `out = 0; func() { for i, _ in [1, 2, 3] { out += i } }()`,
- nil, 3) // index, _
- // map
- expectRun(t, `out = 0; for v in {a:2,b:3,c:4} { out += v }`,
- nil, 9) // value
- expectRun(t, `out = ""; for k, v in {a:2,b:3,c:4} { out = k; if v==3 { break } }`,
- nil, "b") // key, value
- expectRun(t, `out = ""; for k, _ in {a:2} { out += k }`,
- nil, "a") // key, _
- expectRun(t, `out = 0; for _, v in {a:2,b:3,c:4} { out += v }`,
- nil, 9) // _, value
- expectRun(t, `out = ""; func() { for k, v in {a:2,b:3,c:4} { out = k; if v==3 { break } } }()`,
- nil, "b") // key, value
- // string
- expectRun(t, `out = ""; for c in "abcde" { out += c }`,
- nil, "abcde")
- expectRun(t, `out = ""; for i, c in "abcde" { if i == 2 { continue }; out += c }`,
- nil, "abde")
- }
- func TestFor(t *testing.T) {
- expectRun(t, `
- out = 0
- for {
- out++
- if out == 5 {
- break
- }
- }`, nil, 5)
- expectRun(t, `
- out = 0
- for {
- out++
- if out == 5 {
- break
- }
- }`, nil, 5)
- expectRun(t, `
- out = 0
- a := 0
- for {
- a++
- if a == 3 { continue }
- if a == 5 { break }
- out += a
- }`, nil, 7) // 1 + 2 + 4
- expectRun(t, `
- out = 0
- a := 0
- for {
- a++
- if a == 3 { continue }
- out += a
- if a == 5 { break }
- }`, nil, 12) // 1 + 2 + 4 + 5
- expectRun(t, `
- out = 0
- for true {
- out++
- if out == 5 {
- break
- }
- }`, nil, 5)
- expectRun(t, `
- a := 0
- for true {
- a++
- if a == 5 {
- break
- }
- }
- out = a`, nil, 5)
- expectRun(t, `
- out = 0
- a := 0
- for true {
- a++
- if a == 3 { continue }
- if a == 5 { break }
- out += a
- }`, nil, 7) // 1 + 2 + 4
- expectRun(t, `
- out = 0
- a := 0
- for true {
- a++
- if a == 3 { continue }
- out += a
- if a == 5 { break }
- }`, nil, 12) // 1 + 2 + 4 + 5
- expectRun(t, `
- out = 0
- func() {
- for true {
- out++
- if out == 5 {
- return
- }
- }
- }()`, nil, 5)
- expectRun(t, `
- out = 0
- for a:=1; a<=10; a++ {
- out += a
- }`, nil, 55)
- expectRun(t, `
- out = 0
- for a:=1; a<=3; a++ {
- for b:=3; b<=6; b++ {
- out += b
- }
- }`, nil, 54)
- expectRun(t, `
- out = 0
- func() {
- for {
- out++
- if out == 5 {
- break
- }
- }
- }()`, nil, 5)
- expectRun(t, `
- out = 0
- func() {
- for true {
- out++
- if out == 5 {
- break
- }
- }
- }()`, nil, 5)
- expectRun(t, `
- out = func() {
- a := 0
- for {
- a++
- if a == 5 {
- break
- }
- }
- return a
- }()`, nil, 5)
- expectRun(t, `
- out = func() {
- a := 0
- for true {
- a++
- if a== 5 {
- break
- }
- }
- return a
- }()`, nil, 5)
- expectRun(t, `
- out = func() {
- a := 0
- func() {
- for {
- a++
- if a == 5 {
- break
- }
- }
- }()
- return a
- }()`, nil, 5)
- expectRun(t, `
- out = func() {
- a := 0
- func() {
- for true {
- a++
- if a == 5 {
- break
- }
- }
- }()
- return a
- }()`, nil, 5)
- expectRun(t, `
- out = func() {
- sum := 0
- for a:=1; a<=10; a++ {
- sum += a
- }
- return sum
- }()`, nil, 55)
- expectRun(t, `
- out = func() {
- sum := 0
- for a:=1; a<=4; a++ {
- for b:=3; b<=5; b++ {
- sum += b
- }
- }
- return sum
- }()`, nil, 48) // (3+4+5) * 4
- expectRun(t, `
- a := 1
- for ; a<=10; a++ {
- if a == 5 {
- break
- }
- }
- out = a`, nil, 5)
- expectRun(t, `
- out = 0
- for a:=1; a<=10; a++ {
- if a == 3 {
- continue
- }
- out += a
- if a == 5 {
- break
- }
- }`, nil, 12) // 1 + 2 + 4 + 5
- expectRun(t, `
- out = 0
- for a:=1; a<=10; {
- if a == 3 {
- a++
- continue
- }
- out += a
- if a == 5 {
- break
- }
- a++
- }`, nil, 12) // 1 + 2 + 4 + 5
- }
- func TestFunction(t *testing.T) {
- // function with no "return" statement returns "invalid" value.
- expectRun(t, `f1 := func() {}; out = f1();`,
- nil, tengo.UndefinedValue)
- expectRun(t, `f1 := func() {}; f2 := func() { return f1(); }; f1(); out = f2();`,
- nil, tengo.UndefinedValue)
- expectRun(t, `f := func(x) { x; }; out = f(5);`,
- nil, tengo.UndefinedValue)
- expectRun(t, `f := func(...x) { return x; }; out = f(1,2,3);`,
- nil, ARR{1, 2, 3})
- expectRun(t, `f := func(a, b, ...x) { return [a, b, x]; }; out = f(8,9,1,2,3);`,
- nil, ARR{8, 9, ARR{1, 2, 3}})
- expectRun(t, `f := func(v) { x := 2; return func(a, ...b){ return [a, b, v+x]}; }; out = f(5)("a", "b");`,
- nil, ARR{"a", ARR{"b"}, 7})
- expectRun(t, `f := func(...x) { return x; }; out = f();`,
- nil, &tengo.Array{Value: []tengo.Object{}})
- expectRun(t, `f := func(a, b, ...x) { return [a, b, x]; }; out = f(8, 9);`,
- nil, ARR{8, 9, ARR{}})
- expectRun(t, `f := func(v) { x := 2; return func(a, ...b){ return [a, b, v+x]}; }; out = f(5)("a");`,
- nil, ARR{"a", ARR{}, 7})
- expectError(t, `f := func(a, b, ...x) { return [a, b, x]; }; f();`, nil,
- "Runtime Error: wrong number of arguments: want>=2, got=0\n\tat test:1:46")
- expectError(t, `f := func(a, b, ...x) { return [a, b, x]; }; f(1);`, nil,
- "Runtime Error: wrong number of arguments: want>=2, got=1\n\tat test:1:46")
- expectRun(t, `f := func(x) { return x; }; out = f(5);`, nil, 5)
- expectRun(t, `f := func(x) { return x * 2; }; out = f(5);`, nil, 10)
- expectRun(t, `f := func(x, y) { return x + y; }; out = f(5, 5);`, nil, 10)
- expectRun(t, `f := func(x, y) { return x + y; }; out = f(5 + 5, f(5, 5));`,
- nil, 20)
- expectRun(t, `out = func(x) { return x; }(5)`, nil, 5)
- expectRun(t, `x := 10; f := func(x) { return x; }; f(5); out = x;`, nil, 10)
- expectRun(t, `
- f2 := func(a) {
- f1 := func(a) {
- return a * 2;
- };
-
- return f1(a) * 3;
- };
-
- out = f2(10);
- `, nil, 60)
- expectRun(t, `
- f1 := func(f) {
- a := [undefined]
- a[0] = func() { return f(a) }
- return a[0]()
- }
- out = f1(func(a) { return 2 })
- `, nil, 2)
- // closures
- expectRun(t, `
- newAdder := func(x) {
- return func(y) { return x + y };
- };
-
- add2 := newAdder(2);
- out = add2(5);
- `, nil, 7)
- expectRun(t, `
- m := {a: 1}
- for k,v in m {
- func(){
- out = k
- }()
- }
- `, nil, "a")
- expectRun(t, `
- m := {a: 1}
- for k,v in m {
- func(){
- out = v
- }()
- }
- `, nil, 1)
- // function as a argument
- expectRun(t, `
- add := func(a, b) { return a + b };
- sub := func(a, b) { return a - b };
- applyFunc := func(a, b, f) { return f(a, b) };
-
- out = applyFunc(applyFunc(2, 2, add), 3, sub);
- `, nil, 1)
- expectRun(t, `f1 := func() { return 5 + 10; }; out = f1();`,
- nil, 15)
- expectRun(t, `f1 := func() { return 1 }; f2 := func() { return 2 }; out = f1() + f2()`,
- nil, 3)
- expectRun(t, `f1 := func() { return 1 }; f2 := func() { return f1() + 2 }; f3 := func() { return f2() + 3 }; out = f3()`,
- nil, 6)
- expectRun(t, `f1 := func() { return 99; 100 }; out = f1();`,
- nil, 99)
- expectRun(t, `f1 := func() { return 99; return 100 }; out = f1();`,
- nil, 99)
- expectRun(t, `f1 := func() { return 33; }; f2 := func() { return f1 }; out = f2()();`,
- nil, 33)
- expectRun(t, `one := func() { one = 1; return one }; out = one()`,
- nil, 1)
- expectRun(t, `three := func() { one := 1; two := 2; return one + two }; out = three()`,
- nil, 3)
- expectRun(t, `three := func() { one := 1; two := 2; return one + two }; seven := func() { three := 3; four := 4; return three + four }; out = three() + seven()`,
- nil, 10)
- expectRun(t, `
- foo1 := func() {
- foo := 50
- return foo
- }
- foo2 := func() {
- foo := 100
- return foo
- }
- out = foo1() + foo2()`, nil, 150)
- expectRun(t, `
- g := 50;
- minusOne := func() {
- n := 1;
- return g - n;
- };
- minusTwo := func() {
- n := 2;
- return g - n;
- };
- out = minusOne() + minusTwo()
- `, nil, 97)
- expectRun(t, `
- f1 := func() {
- f2 := func() { return 1; }
- return f2
- };
- out = f1()()
- `, nil, 1)
- expectRun(t, `
- f1 := func(a) { return a; };
- out = f1(4)`, nil, 4)
- expectRun(t, `
- f1 := func(a, b) { return a + b; };
- out = f1(1, 2)`, nil, 3)
- expectRun(t, `
- sum := func(a, b) {
- c := a + b;
- return c;
- };
- out = sum(1, 2);`, nil, 3)
- expectRun(t, `
- sum := func(a, b) {
- c := a + b;
- return c;
- };
- out = sum(1, 2) + sum(3, 4);`, nil, 10)
- expectRun(t, `
- sum := func(a, b) {
- c := a + b
- return c
- };
- outer := func() {
- return sum(1, 2) + sum(3, 4)
- };
- out = outer();`, nil, 10)
- expectRun(t, `
- g := 10;
-
- sum := func(a, b) {
- c := a + b;
- return c + g;
- }
-
- outer := func() {
- return sum(1, 2) + sum(3, 4) + g;
- }
-
- out = outer() + g
- `, nil, 50)
- expectError(t, `func() { return 1; }(1)`,
- nil, "wrong number of arguments")
- expectError(t, `func(a) { return a; }()`,
- nil, "wrong number of arguments")
- expectError(t, `func(a, b) { return a + b; }(1)`,
- nil, "wrong number of arguments")
- expectRun(t, `
- f1 := func(a) {
- return func() { return a; };
- };
- f2 := f1(99);
- out = f2()
- `, nil, 99)
- expectRun(t, `
- f1 := func(a, b) {
- return func(c) { return a + b + c };
- };
-
- f2 := f1(1, 2);
- out = f2(8);
- `, nil, 11)
- expectRun(t, `
- f1 := func(a, b) {
- c := a + b;
- return func(d) { return c + d };
- };
- f2 := f1(1, 2);
- out = f2(8);
- `, nil, 11)
- expectRun(t, `
- f1 := func(a, b) {
- c := a + b;
- return func(d) {
- e := d + c;
- return func(f) { return e + f };
- }
- };
- f2 := f1(1, 2);
- f3 := f2(3);
- out = f3(8);
- `, nil, 14)
- expectRun(t, `
- a := 1;
- f1 := func(b) {
- return func(c) {
- return func(d) { return a + b + c + d }
- };
- };
- f2 := f1(2);
- f3 := f2(3);
- out = f3(8);
- `, nil, 14)
- expectRun(t, `
- f1 := func(a, b) {
- one := func() { return a; };
- two := func() { return b; };
- return func() { return one() + two(); }
- };
- f2 := f1(9, 90);
- out = f2();
- `, nil, 99)
- // global function recursion
- expectRun(t, `
- fib := func(x) {
- if x == 0 {
- return 0
- } else if x == 1 {
- return 1
- } else {
- return fib(x-1) + fib(x-2)
- }
- }
- out = fib(15)`, nil, 610)
- // local function recursion
- expectRun(t, `
- out = func() {
- sum := func(x) {
- return x == 0 ? 0 : x + sum(x-1)
- }
- return sum(5)
- }()`, nil, 15)
- expectError(t, `return 5`, nil, "return not allowed outside function")
- // closure and block scopes
- expectRun(t, `
- func() {
- a := 10
- func() {
- b := 5
- if true {
- out = a + 5
- }
- }()
- }()`, nil, 15)
- expectRun(t, `
- func() {
- a := 10
- b := func() { return 5 }
- func() {
- if b() {
- out = a + b()
- }
- }()
- }()`, nil, 15)
- expectRun(t, `
- func() {
- a := 10
- func() {
- b := func() { return 5 }
- func() {
- if true {
- out = a + b()
- }
- }()
- }()
- }()`, nil, 15)
- // function skipping return
- expectRun(t, `out = func() {}()`,
- nil, tengo.UndefinedValue)
- expectRun(t, `out = func(v) { if v { return true } }(1)`,
- nil, true)
- expectRun(t, `out = func(v) { if v { return true } }(0)`,
- nil, tengo.UndefinedValue)
- expectRun(t, `out = func(v) { if v { } else { return true } }(1)`,
- nil, tengo.UndefinedValue)
- expectRun(t, `out = func(v) { if v { return } }(1)`,
- nil, tengo.UndefinedValue)
- expectRun(t, `out = func(v) { if v { return } }(0)`,
- nil, tengo.UndefinedValue)
- expectRun(t, `out = func(v) { if v { } else { return } }(1)`,
- nil, tengo.UndefinedValue)
- expectRun(t, `out = func(v) { for ;;v++ { if v == 3 { return true } } }(1)`,
- nil, true)
- expectRun(t, `out = func(v) { for ;;v++ { if v == 3 { break } } }(1)`,
- nil, tengo.UndefinedValue)
- // 'f' in RHS at line 4 must reference global variable 'f'
- // See https://github.com/d5/tengo/issues/314
- expectRun(t, `
- f := func() { return 2 }
- out = (func() {
- f := f()
- return f
- })()
- `, nil, 2)
- }
- func TestBlocksInGlobalScope(t *testing.T) {
- expectRun(t, `
- f := undefined
- if true {
- a := 1
- f = func() {
- a = 2
- }
- }
- b := 3
- f()
- out = b`,
- nil, 3)
- expectRun(t, `
- func() {
- f := undefined
- if true {
- a := 10
- f = func() {
- a = 20
- }
- }
- b := 5
- f()
- out = b
- }()
- `,
- nil, 5)
- expectRun(t, `
- f := undefined
- if true {
- a := 1
- b := 2
- f = func() {
- a = 3
- b = 4
- }
- }
- c := 5
- d := 6
- f()
- out = c + d`,
- nil, 11)
- expectRun(t, `
- fn := undefined
- if true {
- a := 1
- b := 2
- if true {
- c := 3
- d := 4
- fn = func() {
- a = 5
- b = 6
- c = 7
- d = 8
- }
- }
- }
- e := 9
- f := 10
- fn()
- out = e + f`,
- nil, 19)
- expectRun(t, `
- out = 0
- func() {
- for x in [1, 2, 3] {
- out += x
- }
- }()`,
- nil, 6)
- expectRun(t, `
- out = 0
- for x in [1, 2, 3] {
- out += x
- }`,
- nil, 6)
- }
- func TestIf(t *testing.T) {
- expectRun(t, `if (true) { out = 10 }`, nil, 10)
- expectRun(t, `if (false) { out = 10 }`, nil, tengo.UndefinedValue)
- expectRun(t, `if (false) { out = 10 } else { out = 20 }`, nil, 20)
- expectRun(t, `if (1) { out = 10 }`, nil, 10)
- expectRun(t, `if (0) { out = 10 } else { out = 20 }`, nil, 20)
- expectRun(t, `if (1 < 2) { out = 10 }`, nil, 10)
- expectRun(t, `if (1 > 2) { out = 10 }`, nil, tengo.UndefinedValue)
- expectRun(t, `if (1 < 2) { out = 10 } else { out = 20 }`, nil, 10)
- expectRun(t, `if (1 > 2) { out = 10 } else { out = 20 }`, nil, 20)
- expectRun(t, `if (1 < 2) { out = 10 } else if (1 > 2) { out = 20 } else { out = 30 }`,
- nil, 10)
- expectRun(t, `if (1 > 2) { out = 10 } else if (1 < 2) { out = 20 } else { out = 30 }`,
- nil, 20)
- expectRun(t, `if (1 > 2) { out = 10 } else if (1 == 2) { out = 20 } else { out = 30 }`,
- nil, 30)
- expectRun(t, `if (1 > 2) { out = 10 } else if (1 == 2) { out = 20 } else if (1 < 2) { out = 30 } else { out = 40 }`,
- nil, 30)
- expectRun(t, `if (1 > 2) { out = 10 } else if (1 < 2) { out = 20; out = 21; out = 22 } else { out = 30 }`,
- nil, 22)
- expectRun(t, `if (1 > 2) { out = 10 } else if (1 == 2) { out = 20 } else { out = 30; out = 31; out = 32}`,
- nil, 32)
- expectRun(t, `if (1 > 2) { out = 10 } else if (1 < 2) { if (1 == 2) { out = 21 } else { out = 22 } } else { out = 30 }`,
- nil, 22)
- expectRun(t, `if (1 > 2) { out = 10 } else if (1 < 2) { if (1 == 2) { out = 21 } else if (2 == 3) { out = 22 } else { out = 23 } } else { out = 30 }`,
- nil, 23)
- expectRun(t, `if (1 > 2) { out = 10 } else if (1 == 2) { if (1 == 2) { out = 21 } else if (2 == 3) { out = 22 } else { out = 23 } } else { out = 30 }`,
- nil, 30)
- expectRun(t, `if (1 > 2) { out = 10 } else if (1 == 2) { out = 20 } else { if (1 == 2) { out = 31 } else if (2 == 3) { out = 32 } else { out = 33 } }`,
- nil, 33)
- expectRun(t, `if a:=0; a<1 { out = 10 }`, nil, 10)
- expectRun(t, `a:=0; if a++; a==1 { out = 10 }`, nil, 10)
- expectRun(t, `
- func() {
- a := 1
- if a++; a > 1 {
- out = a
- }
- }()
- `, nil, 2)
- expectRun(t, `
- func() {
- a := 1
- if a++; a == 1 {
- out = 10
- } else {
- out = 20
- }
- }()
- `, nil, 20)
- expectRun(t, `
- func() {
- a := 1
- func() {
- if a++; a > 1 {
- a++
- }
- }()
- out = a
- }()
- `, nil, 3)
- // expression statement in init (should not leave objects on stack)
- expectRun(t, `a := 1; if a; a { out = a }`, nil, 1)
- expectRun(t, `a := 1; if a + 4; a { out = a }`, nil, 1)
- // dead code elimination
- expectRun(t, `
- out = func() {
- if false { return 1 }
- a := undefined
- a = 2
- if !a {
- b := func() {
- return is_callable(a) ? a(8) : a
- }()
- if is_error(b) {
- return b
- } else if !is_undefined(b) {
- return immutable(b)
- }
- }
-
- a = 3
- if a {
- b := func() {
- return is_callable(a) ? a(9) : a
- }()
- if is_error(b) {
- return b
- } else if !is_undefined(b) {
- return immutable(b)
- }
- }
- return a
- }()
- `, nil, 3)
- }
- func TestImmutable(t *testing.T) {
- // primitive types are already immutable values
- // immutable expression has no effects.
- expectRun(t, `a := immutable(1); out = a`, nil, 1)
- expectRun(t, `a := 5; b := immutable(a); out = b`, nil, 5)
- expectRun(t, `a := immutable(1); a = 5; out = a`, nil, 5)
- // array
- expectError(t, `a := immutable([1, 2, 3]); a[1] = 5`,
- nil, "not index-assignable")
- expectError(t, `a := immutable(["foo", [1,2,3]]); a[1] = "bar"`,
- nil, "not index-assignable")
- expectRun(t, `a := immutable(["foo", [1,2,3]]); a[1][1] = "bar"; out = a`,
- nil, IARR{"foo", ARR{1, "bar", 3}})
- expectError(t, `a := immutable(["foo", immutable([1,2,3])]); a[1][1] = "bar"`,
- nil, "not index-assignable")
- expectError(t, `a := ["foo", immutable([1,2,3])]; a[1][1] = "bar"`,
- nil, "not index-assignable")
- expectRun(t, `a := immutable([1,2,3]); b := copy(a); b[1] = 5; out = b`,
- nil, ARR{1, 5, 3})
- expectRun(t, `a := immutable([1,2,3]); b := copy(a); b[1] = 5; out = a`,
- nil, IARR{1, 2, 3})
- expectRun(t, `out = immutable([1,2,3]) == [1,2,3]`,
- nil, true)
- expectRun(t, `out = immutable([1,2,3]) == immutable([1,2,3])`,
- nil, true)
- expectRun(t, `out = [1,2,3] == immutable([1,2,3])`,
- nil, true)
- expectRun(t, `out = immutable([1,2,3]) == [1,2]`,
- nil, false)
- expectRun(t, `out = immutable([1,2,3]) == immutable([1,2])`,
- nil, false)
- expectRun(t, `out = [1,2,3] == immutable([1,2])`,
- nil, false)
- expectRun(t, `out = immutable([1, 2, 3, 4])[1]`,
- nil, 2)
- expectRun(t, `out = immutable([1, 2, 3, 4])[1:3]`,
- nil, ARR{2, 3})
- expectRun(t, `a := immutable([1,2,3]); a = 5; out = a`,
- nil, 5)
- expectRun(t, `a := immutable([1, 2, 3]); out = a[5]`,
- nil, tengo.UndefinedValue)
- // map
- expectError(t, `a := immutable({b: 1, c: 2}); a.b = 5`,
- nil, "not index-assignable")
- expectError(t, `a := immutable({b: 1, c: 2}); a["b"] = "bar"`,
- nil, "not index-assignable")
- expectRun(t, `a := immutable({b: 1, c: [1,2,3]}); a.c[1] = "bar"; out = a`,
- nil, IMAP{"b": 1, "c": ARR{1, "bar", 3}})
- expectError(t, `a := immutable({b: 1, c: immutable([1,2,3])}); a.c[1] = "bar"`,
- nil, "not index-assignable")
- expectError(t, `a := {b: 1, c: immutable([1,2,3])}; a.c[1] = "bar"`,
- nil, "not index-assignable")
- expectRun(t, `out = immutable({a:1,b:2}) == {a:1,b:2}`,
- nil, true)
- expectRun(t, `out = immutable({a:1,b:2}) == immutable({a:1,b:2})`,
- nil, true)
- expectRun(t, `out = {a:1,b:2} == immutable({a:1,b:2})`,
- nil, true)
- expectRun(t, `out = immutable({a:1,b:2}) == {a:1,b:3}`,
- nil, false)
- expectRun(t, `out = immutable({a:1,b:2}) == immutable({a:1,b:3})`,
- nil, false)
- expectRun(t, `out = {a:1,b:2} == immutable({a:1,b:3})`,
- nil, false)
- expectRun(t, `out = immutable({a:1,b:2}).b`,
- nil, 2)
- expectRun(t, `out = immutable({a:1,b:2})["b"]`,
- nil, 2)
- expectRun(t, `a := immutable({a:1,b:2}); a = 5; out = 5`,
- nil, 5)
- expectRun(t, `a := immutable({a:1,b:2}); out = a.c`,
- nil, tengo.UndefinedValue)
- expectRun(t, `a := immutable({b: 5, c: "foo"}); out = a.b`,
- nil, 5)
- expectError(t, `a := immutable({b: 5, c: "foo"}); a.b = 10`,
- nil, "not index-assignable")
- }
- func TestIncDec(t *testing.T) {
- expectRun(t, `out = 0; out++`, nil, 1)
- expectRun(t, `out = 0; out--`, nil, -1)
- expectRun(t, `a := 0; a++; out = a`, nil, 1)
- expectRun(t, `a := 0; a++; a--; out = a`, nil, 0)
- // this seems strange but it works because 'a += b' is
- // translated into 'a = a + b' and string type takes other types for + operator.
- expectRun(t, `a := "foo"; a++; out = a`, nil, "foo1")
- expectError(t, `a := "foo"; a--`, nil, "invalid operation")
- expectError(t, `a++`, nil, "unresolved reference") // not declared
- expectError(t, `a--`, nil, "unresolved reference") // not declared
- expectError(t, `4++`, nil, "unresolved reference")
- }
- type StringDict struct {
- tengo.PtrObjectImpl
- Value map[string]string
- }
- func (o *StringDict) String() string { return "" }
- func (o *StringDict) TypeName() string {
- return "string-dict"
- }
- func (o *StringDict) IndexGet(index tengo.Object) (tengo.Object, error) {
- strIdx, ok := index.(*tengo.String)
- if !ok {
- return nil, tengo.ErrInvalidIndexType
- }
- for k, v := range o.Value {
- if strings.EqualFold(strIdx.Value, k) {
- return &tengo.String{Value: v}, nil
- }
- }
- return tengo.UndefinedValue, nil
- }
- func (o *StringDict) IndexSet(index, value tengo.Object) error {
- strIdx, ok := index.(*tengo.String)
- if !ok {
- return tengo.ErrInvalidIndexType
- }
- strVal, ok := tengo.ToString(value)
- if !ok {
- return tengo.ErrInvalidIndexValueType
- }
- o.Value[strings.ToLower(strIdx.Value)] = strVal
- return nil
- }
- type StringCircle struct {
- tengo.PtrObjectImpl
- Value []string
- }
- func (o *StringCircle) TypeName() string {
- return "string-circle"
- }
- func (o *StringCircle) String() string {
- return ""
- }
- func (o *StringCircle) IndexGet(index tengo.Object) (tengo.Object, error) {
- intIdx, ok := index.(tengo.Int)
- if !ok {
- return nil, tengo.ErrInvalidIndexType
- }
- r := int(intIdx.Value) % len(o.Value)
- if r < 0 {
- r = len(o.Value) + r
- }
- return &tengo.String{Value: o.Value[r]}, nil
- }
- func (o *StringCircle) IndexSet(index, value tengo.Object) error {
- intIdx, ok := index.(tengo.Int)
- if !ok {
- return tengo.ErrInvalidIndexType
- }
- r := int(intIdx.Value) % len(o.Value)
- if r < 0 {
- r = len(o.Value) + r
- }
- strVal, ok := tengo.ToString(value)
- if !ok {
- return tengo.ErrInvalidIndexValueType
- }
- o.Value[r] = strVal
- return nil
- }
- type StringArray struct {
- tengo.PtrObjectImpl
- Value []string
- }
- func (o *StringArray) String() string {
- return strings.Join(o.Value, ", ")
- }
- func (o *StringArray) BinaryOp(
- op token.Token,
- rhs tengo.Object,
- ) (tengo.Object, error) {
- if rhs, ok := rhs.(*StringArray); ok {
- switch op {
- case token.Add:
- if len(rhs.Value) == 0 {
- return o, nil
- }
- return &StringArray{Value: append(o.Value, rhs.Value...)}, nil
- }
- }
- return nil, tengo.ErrInvalidOperator
- }
- func (o *StringArray) IsFalsy() bool {
- return len(o.Value) == 0
- }
- func (o *StringArray) Equals(x tengo.Object) bool {
- if x, ok := x.(*StringArray); ok {
- if len(o.Value) != len(x.Value) {
- return false
- }
- for i, v := range o.Value {
- if v != x.Value[i] {
- return false
- }
- }
- return true
- }
- return false
- }
- func (o *StringArray) Copy() tengo.Object {
- return &StringArray{
- Value: append([]string{}, o.Value...),
- }
- }
- func (o *StringArray) TypeName() string {
- return "string-array"
- }
- func (o *StringArray) IndexGet(index tengo.Object) (tengo.Object, error) {
- intIdx, ok := index.(tengo.Int)
- if ok {
- if intIdx.Value >= 0 && intIdx.Value < int64(len(o.Value)) {
- return &tengo.String{Value: o.Value[intIdx.Value]}, nil
- }
- return nil, tengo.ErrIndexOutOfBounds
- }
- strIdx, ok := index.(*tengo.String)
- if ok {
- for vidx, str := range o.Value {
- if strIdx.Value == str {
- return tengo.Int{Value: int64(vidx)}, nil
- }
- }
- return tengo.UndefinedValue, nil
- }
- return nil, tengo.ErrInvalidIndexType
- }
- func (o *StringArray) IndexSet(index, value tengo.Object) error {
- strVal, ok := tengo.ToString(value)
- if !ok {
- return tengo.ErrInvalidIndexValueType
- }
- intIdx, ok := index.(tengo.Int)
- if ok {
- if intIdx.Value >= 0 && intIdx.Value < int64(len(o.Value)) {
- o.Value[intIdx.Value] = strVal
- return nil
- }
- return tengo.ErrIndexOutOfBounds
- }
- return tengo.ErrInvalidIndexType
- }
- func (o *StringArray) Call(
- args ...tengo.Object,
- ) (ret tengo.Object, err error) {
- if len(args) != 1 {
- return nil, tengo.ErrWrongNumArguments
- }
- s1, ok := tengo.ToString(args[0])
- if !ok {
- return nil, tengo.ErrInvalidArgumentType{
- Name: "first",
- Expected: "string(compatible)",
- Found: args[0].TypeName(),
- }
- }
- for i, v := range o.Value {
- if v == s1 {
- return tengo.Int{Value: int64(i)}, nil
- }
- }
- return tengo.UndefinedValue, nil
- }
- func (o *StringArray) CanCall() bool {
- return true
- }
- func TestIndexable(t *testing.T) {
- dict := func() *StringDict {
- return &StringDict{Value: map[string]string{"a": "foo", "b": "bar"}}
- }
- expectRun(t, `out = dict["a"]`,
- Opts().Symbol("dict", dict()).Skip2ndPass(), "foo")
- expectRun(t, `out = dict["B"]`,
- Opts().Symbol("dict", dict()).Skip2ndPass(), "bar")
- expectRun(t, `out = dict["x"]`,
- Opts().Symbol("dict", dict()).Skip2ndPass(), tengo.UndefinedValue)
- expectError(t, `dict[0]`,
- Opts().Symbol("dict", dict()).Skip2ndPass(), "invalid index type")
- strCir := func() *StringCircle {
- return &StringCircle{Value: []string{"one", "two", "three"}}
- }
- expectRun(t, `out = cir[0]`,
- Opts().Symbol("cir", strCir()).Skip2ndPass(), "one")
- expectRun(t, `out = cir[1]`,
- Opts().Symbol("cir", strCir()).Skip2ndPass(), "two")
- expectRun(t, `out = cir[-1]`,
- Opts().Symbol("cir", strCir()).Skip2ndPass(), "three")
- expectRun(t, `out = cir[-2]`,
- Opts().Symbol("cir", strCir()).Skip2ndPass(), "two")
- expectRun(t, `out = cir[3]`,
- Opts().Symbol("cir", strCir()).Skip2ndPass(), "one")
- expectError(t, `cir["a"]`,
- Opts().Symbol("cir", strCir()).Skip2ndPass(), "invalid index type")
- strArr := func() *StringArray {
- return &StringArray{Value: []string{"one", "two", "three"}}
- }
- expectRun(t, `out = arr["one"]`,
- Opts().Symbol("arr", strArr()).Skip2ndPass(), 0)
- expectRun(t, `out = arr["three"]`,
- Opts().Symbol("arr", strArr()).Skip2ndPass(), 2)
- expectRun(t, `out = arr["four"]`,
- Opts().Symbol("arr", strArr()).Skip2ndPass(), tengo.UndefinedValue)
- expectRun(t, `out = arr[0]`,
- Opts().Symbol("arr", strArr()).Skip2ndPass(), "one")
- expectRun(t, `out = arr[1]`,
- Opts().Symbol("arr", strArr()).Skip2ndPass(), "two")
- expectError(t, `arr[-1]`,
- Opts().Symbol("arr", strArr()).Skip2ndPass(), "index out of bounds")
- }
- func TestIndexAssignable(t *testing.T) {
- dict := func() *StringDict {
- return &StringDict{Value: map[string]string{"a": "foo", "b": "bar"}}
- }
- expectRun(t, `dict["a"] = "1984"; out = dict["a"]`,
- Opts().Symbol("dict", dict()).Skip2ndPass(), "1984")
- expectRun(t, `dict["c"] = "1984"; out = dict["c"]`,
- Opts().Symbol("dict", dict()).Skip2ndPass(), "1984")
- expectRun(t, `dict["c"] = 1984; out = dict["C"]`,
- Opts().Symbol("dict", dict()).Skip2ndPass(), "1984")
- expectError(t, `dict[0] = "1984"`,
- Opts().Symbol("dict", dict()).Skip2ndPass(), "invalid index type")
- strCir := func() *StringCircle {
- return &StringCircle{Value: []string{"one", "two", "three"}}
- }
- expectRun(t, `cir[0] = "ONE"; out = cir[0]`,
- Opts().Symbol("cir", strCir()).Skip2ndPass(), "ONE")
- expectRun(t, `cir[1] = "TWO"; out = cir[1]`,
- Opts().Symbol("cir", strCir()).Skip2ndPass(), "TWO")
- expectRun(t, `cir[-1] = "THREE"; out = cir[2]`,
- Opts().Symbol("cir", strCir()).Skip2ndPass(), "THREE")
- expectRun(t, `cir[0] = "ONE"; out = cir[3]`,
- Opts().Symbol("cir", strCir()).Skip2ndPass(), "ONE")
- expectError(t, `cir["a"] = "ONE"`,
- Opts().Symbol("cir", strCir()).Skip2ndPass(), "invalid index type")
- strArr := func() *StringArray {
- return &StringArray{Value: []string{"one", "two", "three"}}
- }
- expectRun(t, `arr[0] = "ONE"; out = arr[0]`,
- Opts().Symbol("arr", strArr()).Skip2ndPass(), "ONE")
- expectRun(t, `arr[1] = "TWO"; out = arr[1]`,
- Opts().Symbol("arr", strArr()).Skip2ndPass(), "TWO")
- expectError(t, `arr["one"] = "ONE"`,
- Opts().Symbol("arr", strArr()).Skip2ndPass(), "invalid index type")
- }
- func TestInteger(t *testing.T) {
- expectRun(t, `out = 5`, nil, 5)
- expectRun(t, `out = 10`, nil, 10)
- expectRun(t, `out = -5`, nil, -5)
- expectRun(t, `out = -10`, nil, -10)
- expectRun(t, `out = 5 + 5 + 5 + 5 - 10`, nil, 10)
- expectRun(t, `out = 2 * 2 * 2 * 2 * 2`, nil, 32)
- expectRun(t, `out = -50 + 100 + -50`, nil, 0)
- expectRun(t, `out = 5 * 2 + 10`, nil, 20)
- expectRun(t, `out = 5 + 2 * 10`, nil, 25)
- expectRun(t, `out = 20 + 2 * -10`, nil, 0)
- expectRun(t, `out = 50 / 2 * 2 + 10`, nil, 60)
- expectRun(t, `out = 2 * (5 + 10)`, nil, 30)
- expectRun(t, `out = 3 * 3 * 3 + 10`, nil, 37)
- expectRun(t, `out = 3 * (3 * 3) + 10`, nil, 37)
- expectRun(t, `out = (5 + 10 * 2 + 15 /3) * 2 + -10`, nil, 50)
- expectRun(t, `out = 5 % 3`, nil, 2)
- expectRun(t, `out = 5 % 3 + 4`, nil, 6)
- expectRun(t, `out = +5`, nil, 5)
- expectRun(t, `out = +5 + -5`, nil, 0)
- expectRun(t, `out = 9 + '0'`, nil, '9')
- expectRun(t, `out = '9' - 5`, nil, '4')
- }
- type StringArrayIterator struct {
- tengo.PtrObjectImpl
- strArr *StringArray
- idx int
- }
- func (i *StringArrayIterator) TypeName() string {
- return "string-array-iterator"
- }
- func (i *StringArrayIterator) String() string {
- return ""
- }
- func (i *StringArrayIterator) Next() bool {
- i.idx++
- return i.idx <= len(i.strArr.Value)
- }
- func (i *StringArrayIterator) Key() tengo.Object {
- return tengo.Int{Value: int64(i.idx - 1)}
- }
- func (i *StringArrayIterator) Value() tengo.Object {
- return &tengo.String{Value: i.strArr.Value[i.idx-1]}
- }
- func (o *StringArray) Iterate() tengo.Iterator {
- return &StringArrayIterator{
- strArr: o,
- }
- }
- func (o *StringArray) CanIterate() bool {
- return true
- }
- func TestIterable(t *testing.T) {
- strArr := func() *StringArray {
- return &StringArray{Value: []string{"one", "two", "three"}}
- }
- expectRun(t, `for i, s in arr { out += i }`,
- Opts().Symbol("arr", strArr()).Skip2ndPass(), 3)
- expectRun(t, `for i, s in arr { out += s }`,
- Opts().Symbol("arr", strArr()).Skip2ndPass(), "onetwothree")
- expectRun(t, `for i, s in arr { out += s + i }`,
- Opts().Symbol("arr", strArr()).Skip2ndPass(), "one0two1three2")
- }
- func TestLogical(t *testing.T) {
- expectRun(t, `out = true && true`, nil, true)
- expectRun(t, `out = true && false`, nil, false)
- expectRun(t, `out = false && true`, nil, false)
- expectRun(t, `out = false && false`, nil, false)
- expectRun(t, `out = !true && true`, nil, false)
- expectRun(t, `out = !true && false`, nil, false)
- expectRun(t, `out = !false && true`, nil, true)
- expectRun(t, `out = !false && false`, nil, false)
- expectRun(t, `out = true || true`, nil, true)
- expectRun(t, `out = true || false`, nil, true)
- expectRun(t, `out = false || true`, nil, true)
- expectRun(t, `out = false || false`, nil, false)
- expectRun(t, `out = !true || true`, nil, true)
- expectRun(t, `out = !true || false`, nil, false)
- expectRun(t, `out = !false || true`, nil, true)
- expectRun(t, `out = !false || false`, nil, true)
- expectRun(t, `out = 1 && 2`, nil, 2)
- expectRun(t, `out = 1 || 2`, nil, 1)
- expectRun(t, `out = 1 && 0`, nil, 0)
- expectRun(t, `out = 1 || 0`, nil, 1)
- expectRun(t, `out = 1 && (0 || 2)`, nil, 2)
- expectRun(t, `out = 0 || (0 || 2)`, nil, 2)
- expectRun(t, `out = 0 || (0 && 2)`, nil, 0)
- expectRun(t, `out = 0 || (2 && 0)`, nil, 0)
- expectRun(t, `t:=func() {out = 3; return true}; f:=func() {out = 7; return false}; t() && f()`,
- nil, 7)
- expectRun(t, `t:=func() {out = 3; return true}; f:=func() {out = 7; return false}; f() && t()`,
- nil, 7)
- expectRun(t, `t:=func() {out = 3; return true}; f:=func() {out = 7; return false}; f() || t()`,
- nil, 3)
- expectRun(t, `t:=func() {out = 3; return true}; f:=func() {out = 7; return false}; t() || f()`,
- nil, 3)
- expectRun(t, `t:=func() {out = 3; return true}; f:=func() {out = 7; return false}; !t() && f()`,
- nil, 3)
- expectRun(t, `t:=func() {out = 3; return true}; f:=func() {out = 7; return false}; !f() && t()`,
- nil, 3)
- expectRun(t, `t:=func() {out = 3; return true}; f:=func() {out = 7; return false}; !f() || t()`,
- nil, 7)
- expectRun(t, `t:=func() {out = 3; return true}; f:=func() {out = 7; return false}; !t() || f()`,
- nil, 7)
- }
- func TestMap(t *testing.T) {
- expectRun(t, `
- out = {
- one: 10 - 9,
- two: 1 + 1,
- three: 6 / 2
- }`, nil, MAP{
- "one": 1,
- "two": 2,
- "three": 3,
- })
- expectRun(t, `
- out = {
- "one": 10 - 9,
- "two": 1 + 1,
- "three": 6 / 2
- }`, nil, MAP{
- "one": 1,
- "two": 2,
- "three": 3,
- })
- expectRun(t, `out = {foo: 5}["foo"]`, nil, 5)
- expectRun(t, `out = {foo: 5}["bar"]`, nil, tengo.UndefinedValue)
- expectRun(t, `key := "foo"; out = {foo: 5}[key]`, nil, 5)
- expectRun(t, `out = {}["foo"]`, nil, tengo.UndefinedValue)
- expectRun(t, `
- m := {
- foo: func(x) {
- return x * 2
- }
- }
- out = m["foo"](2) + m["foo"](3)
- `, nil, 10)
- // map assignment is copy-by-reference
- expectRun(t, `m1 := {k1: 1, k2: "foo"}; m2 := m1; m1.k1 = 5; out = m2.k1`,
- nil, 5)
- expectRun(t, `m1 := {k1: 1, k2: "foo"}; m2 := m1; m2.k1 = 3; out = m1.k1`,
- nil, 3)
- expectRun(t, `func() { m1 := {k1: 1, k2: "foo"}; m2 := m1; m1.k1 = 5; out = m2.k1 }()`,
- nil, 5)
- expectRun(t, `func() { m1 := {k1: 1, k2: "foo"}; m2 := m1; m2.k1 = 3; out = m1.k1 }()`,
- nil, 3)
- }
- func TestBuiltin(t *testing.T) {
- m := Opts().Module("math",
- &tengo.BuiltinModule{
- Attrs: map[string]tengo.Object{
- "abs": &tengo.UserFunction{
- Name: "abs",
- Value: func(a ...tengo.Object) (tengo.Object, error) {
- v, _ := tengo.ToFloat64(a[0])
- return tengo.Float{Value: math.Abs(v)}, nil
- },
- },
- },
- })
- // builtin
- expectRun(t, `math := import("math"); out = math.abs(1)`, m, 1.0)
- expectRun(t, `math := import("math"); out = math.abs(-1)`, m, 1.0)
- expectRun(t, `math := import("math"); out = math.abs(1.0)`, m, 1.0)
- expectRun(t, `math := import("math"); out = math.abs(-1.0)`, m, 1.0)
- }
- func TestUserModules(t *testing.T) {
- // export none
- expectRun(t, `out = import("mod1")`,
- Opts().Module("mod1", `fn := func() { return 5.0 }; a := 2`),
- tengo.UndefinedValue)
- // export values
- expectRun(t, `out = import("mod1")`,
- Opts().Module("mod1", `export 5`), 5)
- expectRun(t, `out = import("mod1")`,
- Opts().Module("mod1", `export "foo"`), "foo")
- // export compound types
- expectRun(t, `out = import("mod1")`,
- Opts().Module("mod1", `export [1, 2, 3]`), IARR{1, 2, 3})
- expectRun(t, `out = import("mod1")`,
- Opts().Module("mod1", `export {a: 1, b: 2}`), IMAP{"a": 1, "b": 2})
- // export value is immutable
- expectError(t, `m1 := import("mod1"); m1.a = 5`,
- Opts().Module("mod1", `export {a: 1, b: 2}`), "not index-assignable")
- expectError(t, `m1 := import("mod1"); m1[1] = 5`,
- Opts().Module("mod1", `export [1, 2, 3]`), "not index-assignable")
- // code after export statement will not be executed
- expectRun(t, `out = import("mod1")`,
- Opts().Module("mod1", `a := 10; export a; a = 20`), 10)
- expectRun(t, `out = import("mod1")`,
- Opts().Module("mod1", `a := 10; export a; a = 20; export a`), 10)
- // export function
- expectRun(t, `out = import("mod1")()`,
- Opts().Module("mod1", `export func() { return 5.0 }`), 5.0)
- // export function that reads module-global variable
- expectRun(t, `out = import("mod1")()`,
- Opts().Module("mod1", `a := 1.5; export func() { return a + 5.0 }`), 6.5)
- // export function that read local variable
- expectRun(t, `out = import("mod1")()`,
- Opts().Module("mod1", `export func() { a := 1.5; return a + 5.0 }`), 6.5)
- // export function that read free variables
- expectRun(t, `out = import("mod1")()`,
- Opts().Module("mod1", `export func() { a := 1.5; return func() { return a + 5.0 }() }`), 6.5)
- // recursive function in module
- expectRun(t, `out = import("mod1")`,
- Opts().Module(
- "mod1", `
- a := func(x) {
- return x == 0 ? 0 : x + a(x-1)
- }
- export a(5)
- `), 15)
- expectRun(t, `out = import("mod1")`,
- Opts().Module(
- "mod1", `
- export func() {
- a := func(x) {
- return x == 0 ? 0 : x + a(x-1)
- }
- return a(5)
- }()
- `), 15)
- // (main) -> mod1 -> mod2
- expectRun(t, `out = import("mod1")()`,
- Opts().Module("mod1", `export import("mod2")`).
- Module("mod2", `export func() { return 5.0 }`),
- 5.0)
- // (main) -> mod1 -> mod2
- // -> mod2
- expectRun(t, `import("mod1"); out = import("mod2")()`,
- Opts().Module("mod1", `export import("mod2")`).
- Module("mod2", `export func() { return 5.0 }`),
- 5.0)
- // (main) -> mod1 -> mod2 -> mod3
- // -> mod2 -> mod3
- expectRun(t, `import("mod1"); out = import("mod2")()`,
- Opts().Module("mod1", `export import("mod2")`).
- Module("mod2", `export import("mod3")`).
- Module("mod3", `export func() { return 5.0 }`),
- 5.0)
- // cyclic imports
- // (main) -> mod1 -> mod2 -> mod1
- expectError(t, `import("mod1")`,
- Opts().Module("mod1", `import("mod2")`).
- Module("mod2", `import("mod1")`),
- "Compile Error: cyclic module import: mod1\n\tat mod2:1:1")
- // (main) -> mod1 -> mod2 -> mod3 -> mod1
- expectError(t, `import("mod1")`,
- Opts().Module("mod1", `import("mod2")`).
- Module("mod2", `import("mod3")`).
- Module("mod3", `import("mod1")`),
- "Compile Error: cyclic module import: mod1\n\tat mod3:1:1")
- // (main) -> mod1 -> mod2 -> mod3 -> mod2
- expectError(t, `import("mod1")`,
- Opts().Module("mod1", `import("mod2")`).
- Module("mod2", `import("mod3")`).
- Module("mod3", `import("mod2")`),
- "Compile Error: cyclic module import: mod2\n\tat mod3:1:1")
- // unknown modules
- expectError(t, `import("mod0")`,
- Opts().Module("mod1", `a := 5`), "module 'mod0' not found")
- expectError(t, `import("mod1")`,
- Opts().Module("mod1", `import("mod2")`), "module 'mod2' not found")
- // module is immutable but its variables is not necessarily immutable.
- expectRun(t, `m1 := import("mod1"); m1.a.b = 5; out = m1.a.b`,
- Opts().Module("mod1", `export {a: {b: 3}}`),
- 5)
- // make sure module has same builtin functions
- expectRun(t, `out = import("mod1")`,
- Opts().Module("mod1", `export func() { return type_name(0) }()`),
- "int")
- // 'export' statement is ignored outside module
- expectRun(t, `a := 5; export func() { a = 10 }(); out = a`,
- Opts().Skip2ndPass(), 5)
- // 'export' must be in the top-level
- expectError(t, `import("mod1")`,
- Opts().Module("mod1", `func() { export 5 }()`),
- "Compile Error: export not allowed inside function\n\tat mod1:1:10")
- expectError(t, `import("mod1")`,
- Opts().Module("mod1", `func() { func() { export 5 }() }()`),
- "Compile Error: export not allowed inside function\n\tat mod1:1:19")
- // module cannot access outer scope
- expectError(t, `a := 5; import("mod1")`,
- Opts().Module("mod1", `export a`),
- "Compile Error: unresolved reference 'a'\n\tat mod1:1:8")
- // runtime error within modules
- expectError(t, `
- a := 1;
- b := import("mod1");
- b(a)`,
- Opts().Module("mod1", `
- export func(a) {
- a()
- }
- `), "Runtime Error: not callable: int\n\tat mod1:3:4\n\tat test:4:1")
- // module skipping export
- expectRun(t, `out = import("mod0")`,
- Opts().Module("mod0", ``), tengo.UndefinedValue)
- expectRun(t, `out = import("mod0")`,
- Opts().Module("mod0", `if 1 { export true }`), true)
- expectRun(t, `out = import("mod0")`,
- Opts().Module("mod0", `if 0 { export true }`),
- tengo.UndefinedValue)
- expectRun(t, `out = import("mod0")`,
- Opts().Module("mod0", `if 1 { } else { export true }`),
- tengo.UndefinedValue)
- expectRun(t, `out = import("mod0")`,
- Opts().Module("mod0", `for v:=0;;v++ { if v == 3 { export true } } }`),
- true)
- expectRun(t, `out = import("mod0")`,
- Opts().Module("mod0", `for v:=0;;v++ { if v == 3 { break } } }`),
- tengo.UndefinedValue)
- // duplicate compiled functions
- // NOTE: module "mod" has a function with some local variable, and it's
- // imported twice by the main script. That causes the same CompiledFunction
- // put in constants twice and the Bytecode optimization (removing duplicate
- // constants) should still work correctly.
- expectRun(t, `
- m1 := import("mod")
- m2 := import("mod")
- out = m1.x
- `,
- Opts().Module("mod", `
- f1 := func(a, b) {
- c := a + b + 1
- return a + b + 1
- }
- export { x: 1 }
- `),
- 1)
- }
- func TestModuleBlockScopes(t *testing.T) {
- m := Opts().Module("rand",
- &tengo.BuiltinModule{
- Attrs: map[string]tengo.Object{
- "intn": &tengo.UserFunction{
- Name: "abs",
- Value: func(a ...tengo.Object) (tengo.Object, error) {
- v, _ := tengo.ToInt64(a[0])
- return tengo.Int{Value: rand.Int63n(v)}, nil
- },
- },
- },
- })
- // block scopes in module
- expectRun(t, `out = import("mod1")()`, m.Module(
- "mod1", `
- rand := import("rand")
- foo := func() { return 1 }
- export func() {
- rand.intn(3)
- return foo()
- }`), 1)
- expectRun(t, `out = import("mod1")()`, m.Module(
- "mod1", `
- rand := import("rand")
- foo := func() { return 1 }
- export func() {
- rand.intn(3)
- if foo() {}
- return 10
- }
- `), 10)
- expectRun(t, `out = import("mod1")()`, m.Module(
- "mod1", `
- rand := import("rand")
- foo := func() { return 1 }
- export func() {
- rand.intn(3)
- if true { foo() }
- return 10
- }
- `), 10)
- }
- func TestBangOperator(t *testing.T) {
- expectRun(t, `out = !true`, nil, false)
- expectRun(t, `out = !false`, nil, true)
- expectRun(t, `out = !0`, nil, true)
- expectRun(t, `out = !5`, nil, false)
- expectRun(t, `out = !!true`, nil, true)
- expectRun(t, `out = !!false`, nil, false)
- expectRun(t, `out = !!5`, nil, true)
- }
- func TestObjectsLimit(t *testing.T) {
- testAllocsLimit(t, `5`, 0)
- testAllocsLimit(t, `5 + 5`, 1)
- testAllocsLimit(t, `a := [1, 2, 3]`, 1)
- testAllocsLimit(t, `a := 1; b := 2; c := 3; d := [a, b, c]`, 1)
- testAllocsLimit(t, `a := {foo: 1, bar: 2}`, 1)
- testAllocsLimit(t, `a := 1; b := 2; c := {foo: a, bar: b}`, 1)
- testAllocsLimit(t, `
- f := func() {
- return 5 + 5
- }
- a := f() + 5
- `, 2)
- testAllocsLimit(t, `
- f := func() {
- return 5 + 5
- }
- a := f()
- `, 1)
- testAllocsLimit(t, `
- a := []
- f := func() {
- a = append(a, 5)
- }
- f()
- f()
- f()
- `, 4)
- }
- func testAllocsLimit(t *testing.T, src string, limit int64) {
- expectRun(t, src,
- Opts().Skip2ndPass(), tengo.UndefinedValue) // no limit
- expectRun(t, src,
- Opts().MaxAllocs(limit).Skip2ndPass(), tengo.UndefinedValue)
- expectRun(t, src,
- Opts().MaxAllocs(limit+1).Skip2ndPass(), tengo.UndefinedValue)
- if limit > 1 {
- expectError(t, src,
- Opts().MaxAllocs(limit-1).Skip2ndPass(),
- "allocation limit exceeded")
- }
- if limit > 2 {
- expectError(t, src,
- Opts().MaxAllocs(limit-2).Skip2ndPass(),
- "allocation limit exceeded")
- }
- }
- func TestReturn(t *testing.T) {
- expectRun(t, `out = func() { return 10; }()`, nil, 10)
- expectRun(t, `out = func() { return 10; return 9; }()`, nil, 10)
- expectRun(t, `out = func() { return 2 * 5; return 9 }()`, nil, 10)
- expectRun(t, `out = func() { 9; return 2 * 5; return 9 }()`, nil, 10)
- expectRun(t, `
- out = func() {
- if (10 > 1) {
- if (10 > 1) {
- return 10;
- }
- return 1;
- }
- }()`, nil, 10)
- expectRun(t, `f1 := func() { return 2 * 5; }; out = f1()`, nil, 10)
- }
- func TestVMScopes(t *testing.T) {
- // shadowed global variable
- expectRun(t, `
- c := 5
- if a := 3; a {
- c := 6
- } else {
- c := 7
- }
- out = c
- `, nil, 5)
- // shadowed local variable
- expectRun(t, `
- func() {
- c := 5
- if a := 3; a {
- c := 6
- } else {
- c := 7
- }
- out = c
- }()
- `, nil, 5)
- // 'b' is declared in 2 separate blocks
- expectRun(t, `
- c := 5
- if a := 3; a {
- b := 8
- c = b
- } else {
- b := 9
- c = b
- }
- out = c
- `, nil, 8)
- // shadowing inside for statement
- expectRun(t, `
- a := 4
- b := 5
- for i:=0;i<3;i++ {
- b := 6
- for j:=0;j<2;j++ {
- b := 7
- a = i*j
- }
- }
- out = a`, nil, 2)
- // shadowing variable declared in init statement
- expectRun(t, `
- if a := 5; a {
- a := 6
- out = a
- }`, nil, 6)
- expectRun(t, `
- a := 4
- if a := 5; a {
- a := 6
- out = a
- }`, nil, 6)
- expectRun(t, `
- a := 4
- if a := 0; a {
- a := 6
- out = a
- } else {
- a := 7
- out = a
- }`, nil, 7)
- expectRun(t, `
- a := 4
- if a := 0; a {
- out = a
- } else {
- out = a
- }`, nil, 0)
- // shadowing function level
- expectRun(t, `
- a := 5
- func() {
- a := 6
- a = 7
- }()
- out = a
- `, nil, 5)
- expectRun(t, `
- a := 5
- func() {
- if a := 7; true {
- a = 8
- }
- }()
- out = a
- `, nil, 5)
- }
- func TestSelector(t *testing.T) {
- expectRun(t, `a := {k1: 5, k2: "foo"}; out = a.k1`,
- nil, 5)
- expectRun(t, `a := {k1: 5, k2: "foo"}; out = a.k2`,
- nil, "foo")
- expectRun(t, `a := {k1: 5, k2: "foo"}; out = a.k3`,
- nil, tengo.UndefinedValue)
- expectRun(t, `
- a := {
- b: {
- c: 4,
- a: false
- },
- c: "foo bar"
- }
- out = a.b.c`, nil, 4)
- expectRun(t, `
- a := {
- b: {
- c: 4,
- a: false
- },
- c: "foo bar"
- }
- b := a.x.c`, nil, tengo.UndefinedValue)
- expectRun(t, `
- a := {
- b: {
- c: 4,
- a: false
- },
- c: "foo bar"
- }
- b := a.x.y`, nil, tengo.UndefinedValue)
- expectRun(t, `a := {b: 1, c: "foo"}; a.b = 2; out = a.b`,
- nil, 2)
- expectRun(t, `a := {b: 1, c: "foo"}; a.c = 2; out = a.c`,
- nil, 2) // type not checked on sub-field
- expectRun(t, `a := {b: {c: 1}}; a.b.c = 2; out = a.b.c`,
- nil, 2)
- expectRun(t, `a := {b: 1}; a.c = 2; out = a`,
- nil, MAP{"b": 1, "c": 2})
- expectRun(t, `a := {b: {c: 1}}; a.b.d = 2; out = a`,
- nil, MAP{"b": MAP{"c": 1, "d": 2}})
- expectRun(t, `func() { a := {b: 1, c: "foo"}; a.b = 2; out = a.b }()`,
- nil, 2)
- expectRun(t, `func() { a := {b: 1, c: "foo"}; a.c = 2; out = a.c }()`,
- nil, 2) // type not checked on sub-field
- expectRun(t, `func() { a := {b: {c: 1}}; a.b.c = 2; out = a.b.c }()`,
- nil, 2)
- expectRun(t, `func() { a := {b: 1}; a.c = 2; out = a }()`,
- nil, MAP{"b": 1, "c": 2})
- expectRun(t, `func() { a := {b: {c: 1}}; a.b.d = 2; out = a }()`,
- nil, MAP{"b": MAP{"c": 1, "d": 2}})
- expectRun(t, `func() { a := {b: 1, c: "foo"}; func() { a.b = 2 }(); out = a.b }()`,
- nil, 2)
- expectRun(t, `func() { a := {b: 1, c: "foo"}; func() { a.c = 2 }(); out = a.c }()`,
- nil, 2) // type not checked on sub-field
- expectRun(t, `func() { a := {b: {c: 1}}; func() { a.b.c = 2 }(); out = a.b.c }()`,
- nil, 2)
- expectRun(t, `func() { a := {b: 1}; func() { a.c = 2 }(); out = a }()`,
- nil, MAP{"b": 1, "c": 2})
- expectRun(t, `func() { a := {b: {c: 1}}; func() { a.b.d = 2 }(); out = a }()`,
- nil, MAP{"b": MAP{"c": 1, "d": 2}})
- expectRun(t, `
- a := {
- b: [1, 2, 3],
- c: {
- d: 8,
- e: "foo",
- f: [9, 8]
- }
- }
- out = [a.b[2], a.c.d, a.c.e, a.c.f[1]]
- `, nil, ARR{3, 8, "foo", 8})
- expectRun(t, `
- func() {
- a := [1, 2, 3]
- b := 9
- a[1] = b
- b = 7 // make sure a[1] has a COPY of value of 'b'
- out = a[1]
- }()
- `, nil, 9)
- expectError(t, `a := {b: {c: 1}}; a.d.c = 2`,
- nil, "not index-assignable")
- expectError(t, `a := [1, 2, 3]; a.b = 2`,
- nil, "invalid index type")
- expectError(t, `a := "foo"; a.b = 2`,
- nil, "not index-assignable")
- expectError(t, `func() { a := {b: {c: 1}}; a.d.c = 2 }()`,
- nil, "not index-assignable")
- expectError(t, `func() { a := [1, 2, 3]; a.b = 2 }()`,
- nil, "invalid index type")
- expectError(t, `func() { a := "foo"; a.b = 2 }()`,
- nil, "not index-assignable")
- }
- func TestSourceModules(t *testing.T) {
- testEnumModule(t, `out = enum.key(0, 20)`, 0)
- testEnumModule(t, `out = enum.key(10, 20)`, 10)
- testEnumModule(t, `out = enum.value(0, 0)`, 0)
- testEnumModule(t, `out = enum.value(10, 20)`, 20)
- testEnumModule(t, `out = enum.all([], enum.value)`, true)
- testEnumModule(t, `out = enum.all([1], enum.value)`, true)
- testEnumModule(t, `out = enum.all([true, 1], enum.value)`, true)
- testEnumModule(t, `out = enum.all([true, 0], enum.value)`, false)
- testEnumModule(t, `out = enum.all([true, 0, 1], enum.value)`, false)
- testEnumModule(t, `out = enum.all(immutable([true, 0, 1]), enum.value)`,
- false) // immutable-array
- testEnumModule(t, `out = enum.all({}, enum.value)`, true)
- testEnumModule(t, `out = enum.all({a:1}, enum.value)`, true)
- testEnumModule(t, `out = enum.all({a:true, b:1}, enum.value)`, true)
- testEnumModule(t, `out = enum.all(immutable({a:true, b:1}), enum.value)`,
- true) // immutable-map
- testEnumModule(t, `out = enum.all({a:true, b:0}, enum.value)`, false)
- testEnumModule(t, `out = enum.all({a:true, b:0, c:1}, enum.value)`, false)
- testEnumModule(t, `out = enum.all(0, enum.value)`,
- tengo.UndefinedValue) // non-enumerable: undefined
- testEnumModule(t, `out = enum.all("123", enum.value)`,
- tengo.UndefinedValue) // non-enumerable: undefined
- testEnumModule(t, `out = enum.any([], enum.value)`, false)
- testEnumModule(t, `out = enum.any([1], enum.value)`, true)
- testEnumModule(t, `out = enum.any([true, 1], enum.value)`, true)
- testEnumModule(t, `out = enum.any([true, 0], enum.value)`, true)
- testEnumModule(t, `out = enum.any([true, 0, 1], enum.value)`, true)
- testEnumModule(t, `out = enum.any(immutable([true, 0, 1]), enum.value)`,
- true) // immutable-array
- testEnumModule(t, `out = enum.any([false], enum.value)`, false)
- testEnumModule(t, `out = enum.any([false, 0], enum.value)`, false)
- testEnumModule(t, `out = enum.any({}, enum.value)`, false)
- testEnumModule(t, `out = enum.any({a:1}, enum.value)`, true)
- testEnumModule(t, `out = enum.any({a:true, b:1}, enum.value)`, true)
- testEnumModule(t, `out = enum.any({a:true, b:0}, enum.value)`, true)
- testEnumModule(t, `out = enum.any({a:true, b:0, c:1}, enum.value)`, true)
- testEnumModule(t, `out = enum.any(immutable({a:true, b:0, c:1}), enum.value)`,
- true) // immutable-map
- testEnumModule(t, `out = enum.any({a:false}, enum.value)`, false)
- testEnumModule(t, `out = enum.any({a:false, b:0}, enum.value)`, false)
- testEnumModule(t, `out = enum.any(0, enum.value)`,
- tengo.UndefinedValue) // non-enumerable: undefined
- testEnumModule(t, `out = enum.any("123", enum.value)`,
- tengo.UndefinedValue) // non-enumerable: undefined
- testEnumModule(t, `out = enum.chunk([], 1)`, ARR{})
- testEnumModule(t, `out = enum.chunk([1], 1)`, ARR{ARR{1}})
- testEnumModule(t, `out = enum.chunk([1,2,3], 1)`,
- ARR{ARR{1}, ARR{2}, ARR{3}})
- testEnumModule(t, `out = enum.chunk([1,2,3], 2)`,
- ARR{ARR{1, 2}, ARR{3}})
- testEnumModule(t, `out = enum.chunk([1,2,3], 3)`,
- ARR{ARR{1, 2, 3}})
- testEnumModule(t, `out = enum.chunk([1,2,3], 4)`,
- ARR{ARR{1, 2, 3}})
- testEnumModule(t, `out = enum.chunk([1,2,3,4], 3)`,
- ARR{ARR{1, 2, 3}, ARR{4}})
- testEnumModule(t, `out = enum.chunk([], 0)`,
- tengo.UndefinedValue) // size=0: undefined
- testEnumModule(t, `out = enum.chunk([1], 0)`,
- tengo.UndefinedValue) // size=0: undefined
- testEnumModule(t, `out = enum.chunk([1,2,3], 0)`,
- tengo.UndefinedValue) // size=0: undefined
- testEnumModule(t, `out = enum.chunk({a:1,b:2,c:3}, 1)`,
- tengo.UndefinedValue) // map: undefined
- testEnumModule(t, `out = enum.chunk(0, 1)`,
- tengo.UndefinedValue) // non-enumerable: undefined
- testEnumModule(t, `out = enum.chunk("123", 1)`,
- tengo.UndefinedValue) // non-enumerable: undefined
- testEnumModule(t, `out = enum.at([], 0)`,
- tengo.UndefinedValue)
- testEnumModule(t, `out = enum.at([], 1)`,
- tengo.UndefinedValue)
- testEnumModule(t, `out = enum.at([], -1)`,
- tengo.UndefinedValue)
- testEnumModule(t, `out = enum.at(["one"], 0)`,
- "one")
- testEnumModule(t, `out = enum.at(["one"], 1)`,
- tengo.UndefinedValue)
- testEnumModule(t, `out = enum.at(["one"], -1)`,
- tengo.UndefinedValue)
- testEnumModule(t, `out = enum.at(["one","two","three"], 0)`,
- "one")
- testEnumModule(t, `out = enum.at(["one","two","three"], 1)`,
- "two")
- testEnumModule(t, `out = enum.at(["one","two","three"], 2)`,
- "three")
- testEnumModule(t, `out = enum.at(["one","two","three"], -1)`,
- tengo.UndefinedValue)
- testEnumModule(t, `out = enum.at(["one","two","three"], 3)`,
- tengo.UndefinedValue)
- testEnumModule(t, `out = enum.at(["one","two","three"], "1")`,
- tengo.UndefinedValue) // non-int index: undefined
- testEnumModule(t, `out = enum.at({}, "a")`,
- tengo.UndefinedValue)
- testEnumModule(t, `out = enum.at({a:"one"}, "a")`,
- "one")
- testEnumModule(t, `out = enum.at({a:"one"}, "b")`,
- tengo.UndefinedValue)
- testEnumModule(t, `out = enum.at({a:"one",b:"two",c:"three"}, "a")`,
- "one")
- testEnumModule(t, `out = enum.at({a:"one",b:"two",c:"three"}, "b")`,
- "two")
- testEnumModule(t, `out = enum.at({a:"one",b:"two",c:"three"}, "c")`,
- "three")
- testEnumModule(t, `out = enum.at({a:"one",b:"two",c:"three"}, "d")`,
- tengo.UndefinedValue)
- testEnumModule(t, `out = enum.at({a:"one",b:"two",c:"three"}, 'a')`,
- tengo.UndefinedValue) // non-string index: undefined
- testEnumModule(t, `out = enum.at(0, 1)`,
- tengo.UndefinedValue) // non-enumerable: undefined
- testEnumModule(t, `out = enum.at("abc", 1)`,
- tengo.UndefinedValue) // non-enumerable: undefined
- testEnumModule(t, `out=0; enum.each([],func(k,v){out+=v})`, 0)
- testEnumModule(t, `out=0; enum.each([1,2,3],func(k,v){out+=v})`, 6)
- testEnumModule(t, `out=0; enum.each([1,2,3],func(k,v){out+=k})`, 3)
- testEnumModule(t, `out=0; enum.each({a:1,b:2,c:3},func(k,v){out+=v})`, 6)
- testEnumModule(t, `out=""; enum.each({a:1,b:2,c:3},func(k,v){out+=k}); out=len(out)`,
- 3)
- testEnumModule(t, `out=0; enum.each(5,func(k,v){out+=v})`, 0) // non-enumerable: no iteration
- testEnumModule(t, `out=0; enum.each("123",func(k,v){out+=v})`, 0) // non-enumerable: no iteration
- testEnumModule(t, `out = enum.filter([], enum.value)`,
- ARR{})
- testEnumModule(t, `out = enum.filter([false,1,2], enum.value)`,
- ARR{1, 2})
- testEnumModule(t, `out = enum.filter([false,1,0,2], enum.value)`,
- ARR{1, 2})
- testEnumModule(t, `out = enum.filter({}, enum.value)`,
- tengo.UndefinedValue) // non-array: undefined
- testEnumModule(t, `out = enum.filter(0, enum.value)`,
- tengo.UndefinedValue) // non-array: undefined
- testEnumModule(t, `out = enum.filter("123", enum.value)`,
- tengo.UndefinedValue) // non-array: undefined
- testEnumModule(t, `out = enum.find([], enum.value)`,
- tengo.UndefinedValue)
- testEnumModule(t, `out = enum.find([0], enum.value)`,
- tengo.UndefinedValue)
- testEnumModule(t, `out = enum.find([1], enum.value)`, 1)
- testEnumModule(t, `out = enum.find([false,0,undefined,1], enum.value)`, 1)
- testEnumModule(t, `out = enum.find([1,2,3], enum.value)`, 1)
- testEnumModule(t, `out = enum.find({}, enum.value)`,
- tengo.UndefinedValue)
- testEnumModule(t, `out = enum.find({a:0}, enum.value)`,
- tengo.UndefinedValue)
- testEnumModule(t, `out = enum.find({a:1}, enum.value)`, 1)
- testEnumModule(t, `out = enum.find({a:false,b:0,c:undefined,d:1}, enum.value)`,
- 1)
- //testEnumModule(t, `out = enum.find({a:1,b:2,c:3}, enum.value)`, 1)
- testEnumModule(t, `out = enum.find(0, enum.value)`,
- tengo.UndefinedValue) // non-enumerable: undefined
- testEnumModule(t, `out = enum.find("123", enum.value)`,
- tengo.UndefinedValue) // non-enumerable: undefined
- testEnumModule(t, `out = enum.find_key([], enum.value)`,
- tengo.UndefinedValue)
- testEnumModule(t, `out = enum.find_key([0], enum.value)`,
- tengo.UndefinedValue)
- testEnumModule(t, `out = enum.find_key([1], enum.value)`, 0)
- testEnumModule(t, `out = enum.find_key([false,0,undefined,1], enum.value)`,
- 3)
- testEnumModule(t, `out = enum.find_key([1,2,3], enum.value)`, 0)
- testEnumModule(t, `out = enum.find_key({}, enum.value)`,
- tengo.UndefinedValue)
- testEnumModule(t, `out = enum.find_key({a:0}, enum.value)`,
- tengo.UndefinedValue)
- testEnumModule(t, `out = enum.find_key({a:1}, enum.value)`,
- "a")
- testEnumModule(t, `out = enum.find_key({a:false,b:0,c:undefined,d:1}, enum.value)`,
- "d")
- //testEnumModule(t, `out = enum.find_key({a:1,b:2,c:3}, enum.value)`, "a")
- testEnumModule(t, `out = enum.find_key(0, enum.value)`,
- tengo.UndefinedValue) // non-enumerable: undefined
- testEnumModule(t, `out = enum.find_key("123", enum.value)`,
- tengo.UndefinedValue) // non-enumerable: undefined
- testEnumModule(t, `out = enum.map([], enum.value)`,
- ARR{})
- testEnumModule(t, `out = enum.map([1,2,3], enum.value)`,
- ARR{1, 2, 3})
- testEnumModule(t, `out = enum.map([1,2,3], enum.key)`,
- ARR{0, 1, 2})
- testEnumModule(t, `out = enum.map([1,2,3], func(k,v) { return v*2 })`,
- ARR{2, 4, 6})
- testEnumModule(t, `out = enum.map({}, enum.value)`,
- ARR{})
- testEnumModule(t, `out = enum.map({a:1}, func(k,v) { return v*2 })`,
- ARR{2})
- testEnumModule(t, `out = enum.map(0, enum.value)`,
- tengo.UndefinedValue) // non-enumerable: undefined
- testEnumModule(t, `out = enum.map("123", enum.value)`,
- tengo.UndefinedValue) // non-enumerable: undefined
- }
- func testEnumModule(t *testing.T, input string, expected interface{}) {
- expectRun(t, `enum := import("enum"); `+input,
- Opts().Module("enum", stdlib.SourceModules["enum"]),
- expected)
- }
- func TestSrcModEnum(t *testing.T) {
- expectRun(t, `
- x := import("enum")
- out = x.all([1, 2, 3], func(_, v) { return v >= 1 })
- `, Opts().Stdlib(), true)
- expectRun(t, `
- x := import("enum")
- out = x.all([1, 2, 3], func(_, v) { return v >= 2 })
- `, Opts().Stdlib(), false)
- expectRun(t, `
- x := import("enum")
- out = x.any([1, 2, 3], func(_, v) { return v >= 1 })
- `, Opts().Stdlib(), true)
- expectRun(t, `
- x := import("enum")
- out = x.any([1, 2, 3], func(_, v) { return v >= 2 })
- `, Opts().Stdlib(), true)
- expectRun(t, `
- x := import("enum")
- out = x.chunk([1, 2, 3], 1)
- `, Opts().Stdlib(), ARR{ARR{1}, ARR{2}, ARR{3}})
- expectRun(t, `
- x := import("enum")
- out = x.chunk([1, 2, 3], 2)
- `, Opts().Stdlib(), ARR{ARR{1, 2}, ARR{3}})
- expectRun(t, `
- x := import("enum")
- out = x.chunk([1, 2, 3], 3)
- `, Opts().Stdlib(), ARR{ARR{1, 2, 3}})
- expectRun(t, `
- x := import("enum")
- out = x.chunk([1, 2, 3], 4)
- `, Opts().Stdlib(), ARR{ARR{1, 2, 3}})
- expectRun(t, `
- x := import("enum")
- out = x.chunk([1, 2, 3, 4, 5, 6], 2)
- `, Opts().Stdlib(), ARR{ARR{1, 2}, ARR{3, 4}, ARR{5, 6}})
- expectRun(t, `
- x := import("enum")
- out = x.at([1, 2, 3], 0)
- `, Opts().Stdlib(), 1)
- }
- func TestVMStackOverflow(t *testing.T) {
- expectError(t, `f := func() { return f() + 1 }; f()`,
- nil, "stack overflow")
- }
- func TestString(t *testing.T) {
- expectRun(t, `out = "Hello World!"`, nil, "Hello World!")
- expectRun(t, `out = "Hello" + " " + "World!"`, nil, "Hello World!")
- expectRun(t, `out = "Hello" == "Hello"`, nil, true)
- expectRun(t, `out = "Hello" == "World"`, nil, false)
- expectRun(t, `out = "Hello" != "Hello"`, nil, false)
- expectRun(t, `out = "Hello" != "World"`, nil, true)
- expectRun(t, `out = "Hello" > "World"`, nil, false)
- expectRun(t, `out = "World" < "Hello"`, nil, false)
- expectRun(t, `out = "Hello" < "World"`, nil, true)
- expectRun(t, `out = "World" > "Hello"`, nil, true)
- expectRun(t, `out = "Hello" >= "World"`, nil, false)
- expectRun(t, `out = "Hello" <= "World"`, nil, true)
- expectRun(t, `out = "Hello" >= "Hello"`, nil, true)
- expectRun(t, `out = "World" <= "World"`, nil, true)
- // index operator
- str := "abcdef"
- strStr := `"abcdef"`
- strLen := 6
- for idx := 0; idx < strLen; idx++ {
- expectRun(t, fmt.Sprintf("out = %s[%d]", strStr, idx),
- nil, str[idx])
- expectRun(t, fmt.Sprintf("out = %s[0 + %d]", strStr, idx),
- nil, str[idx])
- expectRun(t, fmt.Sprintf("out = %s[1 + %d - 1]", strStr, idx),
- nil, str[idx])
- expectRun(t, fmt.Sprintf("idx := %d; out = %s[idx]", idx, strStr),
- nil, str[idx])
- }
- expectRun(t, fmt.Sprintf("%s[%d]", strStr, -1),
- nil, tengo.UndefinedValue)
- expectRun(t, fmt.Sprintf("%s[%d]", strStr, strLen),
- nil, tengo.UndefinedValue)
- // slice operator
- for low := 0; low <= strLen; low++ {
- expectRun(t, fmt.Sprintf("out = %s[%d:%d]", strStr, low, low),
- nil, "")
- for high := low; high <= strLen; high++ {
- expectRun(t, fmt.Sprintf("out = %s[%d:%d]", strStr, low, high),
- nil, str[low:high])
- expectRun(t,
- fmt.Sprintf("out = %s[0 + %d : 0 + %d]", strStr, low, high),
- nil, str[low:high])
- expectRun(t,
- fmt.Sprintf("out = %s[1 + %d - 1 : 1 + %d - 1]",
- strStr, low, high),
- nil, str[low:high])
- expectRun(t,
- fmt.Sprintf("out = %s[:%d]", strStr, high),
- nil, str[:high])
- expectRun(t,
- fmt.Sprintf("out = %s[%d:]", strStr, low),
- nil, str[low:])
- }
- }
- expectRun(t, fmt.Sprintf("out = %s[:]", strStr),
- nil, str[:])
- expectRun(t, fmt.Sprintf("out = %s[:]", strStr),
- nil, str)
- expectRun(t, fmt.Sprintf("out = %s[%d:]", strStr, -1),
- nil, str)
- expectRun(t, fmt.Sprintf("out = %s[:%d]", strStr, strLen+1),
- nil, str)
- expectRun(t, fmt.Sprintf("out = %s[%d:%d]", strStr, 2, 2),
- nil, "")
- expectError(t, fmt.Sprintf("%s[:%d]", strStr, -1),
- nil, "invalid slice index")
- expectError(t, fmt.Sprintf("%s[%d:]", strStr, strLen+1),
- nil, "invalid slice index")
- expectError(t, fmt.Sprintf("%s[%d:%d]", strStr, 0, -1),
- nil, "invalid slice index")
- expectError(t, fmt.Sprintf("%s[%d:%d]", strStr, 2, 1),
- nil, "invalid slice index")
- // string concatenation with other types
- expectRun(t, `out = "foo" + 1`, nil, "foo1")
- // Float.String() returns the smallest number of digits
- // necessary such that ParseFloat will return f exactly.
- expectRun(t, `out = "foo" + 1.0`, nil, "foo1") // <- note '1' instead of '1.0'
- expectRun(t, `out = "foo" + 1.5`, nil, "foo1.5")
- expectRun(t, `out = "foo" + true`, nil, "footrue")
- expectRun(t, `out = "foo" + 'X'`, nil, "fooX")
- expectRun(t, `out = "foo" + error(5)`, nil, "fooerror: 5")
- expectRun(t, `out = "foo" + undefined`, nil, "foo<undefined>")
- expectRun(t, `out = "foo" + [1,2,3]`, nil, "foo[1, 2, 3]")
- // also works with "+=" operator
- expectRun(t, `out = "foo"; out += 1.5`, nil, "foo1.5")
- // string concats works only when string is LHS
- expectError(t, `1 + "foo"`, nil, "invalid operation")
- expectError(t, `"foo" - "bar"`, nil, "invalid operation")
- }
- func TestTailCall(t *testing.T) {
- expectRun(t, `
- fac := func(n, a) {
- if n == 1 {
- return a
- }
- return fac(n-1, n*a)
- }
- out = fac(5, 1)`, nil, 120)
- expectRun(t, `
- fac := func(n, a) {
- if n == 1 {
- return a
- }
- x := {foo: fac} // indirection for test
- return x.foo(n-1, n*a)
- }
- out = fac(5, 1)`, nil, 120)
- expectRun(t, `
- fib := func(x, s) {
- if x == 0 {
- return 0 + s
- } else if x == 1 {
- return 1 + s
- }
- return fib(x-1, fib(x-2, s))
- }
- out = fib(15, 0)`, nil, 610)
- expectRun(t, `
- fib := func(n, a, b) {
- if n == 0 {
- return a
- } else if n == 1 {
- return b
- }
- return fib(n-1, b, a + b)
- }
- out = fib(15, 0, 1)`, nil, 610)
- // global variable and no return value
- expectRun(t, `
- out = 0
- foo := func(a) {
- if a == 0 {
- return
- }
- out += a
- foo(a-1)
- }
- foo(10)`, nil, 55)
- expectRun(t, `
- f1 := func() {
- f2 := 0 // TODO: this might be fixed in the future
- f2 = func(n, s) {
- if n == 0 { return s }
- return f2(n-1, n + s)
- }
- return f2(5, 0)
- }
- out = f1()`, nil, 15)
- // tail-call replacing loop
- // without tail-call optimization, this code will cause stack overflow
- expectRun(t, `
- iter := func(n, max) {
- if n == max {
- return n
- }
- return iter(n+1, max)
- }
- out = iter(0, 9999)
- `, nil, 9999)
- expectRun(t, `
- c := 0
- iter := func(n, max) {
- if n == max {
- return
- }
- c++
- iter(n+1, max)
- }
- iter(0, 9999)
- out = c
- `, nil, 9999)
- }
- // tail call with free vars
- func TestTailCallFreeVars(t *testing.T) {
- expectRun(t, `
- func() {
- a := 10
- f2 := 0
- f2 = func(n, s) {
- if n == 0 {
- return s + a
- }
- return f2(n-1, n+s)
- }
- out = f2(5, 0)
- }()`, nil, 25)
- }
- func TestSpread(t *testing.T) {
- expectRun(t, `
- f := func(...a) {
- return append(a, 3)
- }
- out = f([1, 2]...)
- `, nil, ARR{1, 2, 3})
- expectRun(t, `
- f := func(a, ...b) {
- return append([a], append(b, 3)...)
- }
- out = f([1, 2]...)
- `, nil, ARR{1, 2, 3})
- expectRun(t, `
- f := func(a, ...b) {
- return append(append([a], b), 3)
- }
- out = f(1, [2]...)
- `, nil, ARR{1, ARR{2}, 3})
- expectRun(t, `
- f1 := func(...a){
- return append([3], a...)
- }
- f2 := func(a, ...b) {
- return f1(append([a], b...)...)
- }
- out = f2([1, 2]...)
- `, nil, ARR{3, 1, 2})
- expectRun(t, `
- f := func(a, ...b) {
- return func(...a) {
- return append([3], append(a, 4)...)
- }(a, b...)
- }
- out = f([1, 2]...)
- `, nil, ARR{3, 1, 2, 4})
- expectRun(t, `
- f := func(a, ...b) {
- c := append(b, 4)
- return func(){
- return append(append([a], b...), c...)
- }()
- }
- out = f(1, immutable([2, 3])...)
- `, nil, ARR{1, 2, 3, 2, 3, 4})
- expectError(t, `func(a) {}([1, 2]...)`, nil,
- "Runtime Error: wrong number of arguments: want=1, got=2")
- expectError(t, `func(a, b, c) {}([1, 2]...)`, nil,
- "Runtime Error: wrong number of arguments: want=3, got=2")
- }
- func expectRun(
- t *testing.T,
- input string,
- opts *testopts,
- expected interface{},
- ) {
- if opts == nil {
- opts = Opts()
- }
- symbols := opts.symbols
- modules := opts.modules
- maxAllocs := opts.maxAllocs
- expectedObj := toObject(expected)
- if symbols == nil {
- symbols = make(map[string]tengo.Object)
- }
- symbols[testOut] = objectZeroCopy(expectedObj)
- // first pass: run the code normally
- {
- // parse
- file := parse(t, input)
- if file == nil {
- return
- }
- // compiler/VM
- res, trace, err := traceCompileRun(file, symbols, modules, maxAllocs)
- require.NoError(t, err, "\n"+strings.Join(trace, "\n"))
- require.Equal(t, expectedObj, res[testOut],
- "\n"+strings.Join(trace, "\n"))
- }
- // second pass: run the code as import module
- if !opts.skip2ndPass {
- file := parse(t, `out = import("__code__")`)
- if file == nil {
- return
- }
- expectedObj := toObject(expected)
- switch eo := expectedObj.(type) {
- case *tengo.Array:
- expectedObj = &tengo.ImmutableArray{Value: eo.Value}
- case *tengo.Map:
- expectedObj = &tengo.ImmutableMap{Value: eo.Value}
- }
- modules.AddSourceModule("__code__",
- []byte(fmt.Sprintf("out := undefined; %s; export out", input)))
- res, trace, err := traceCompileRun(file, symbols, modules, maxAllocs)
- require.NoError(t, err, "\n"+strings.Join(trace, "\n"))
- require.Equal(t, expectedObj, res[testOut],
- "\n"+strings.Join(trace, "\n"))
- }
- }
- func expectError(
- t *testing.T,
- input string,
- opts *testopts,
- expected string,
- ) {
- if opts == nil {
- opts = Opts()
- }
- symbols := opts.symbols
- modules := opts.modules
- maxAllocs := opts.maxAllocs
- expected = strings.TrimSpace(expected)
- if expected == "" {
- panic("expected must not be empty")
- }
- // parse
- program := parse(t, input)
- if program == nil {
- return
- }
- // compiler/VM
- _, trace, err := traceCompileRun(program, symbols, modules, maxAllocs)
- require.Error(t, err, "\n"+strings.Join(trace, "\n"))
- require.True(t, strings.Contains(err.Error(), expected),
- "expected error string: %s, got: %s\n%s",
- expected, err.Error(), strings.Join(trace, "\n"))
- }
- func expectErrorIs(
- t *testing.T,
- input string,
- opts *testopts,
- expected error,
- ) {
- if opts == nil {
- opts = Opts()
- }
- symbols := opts.symbols
- modules := opts.modules
- maxAllocs := opts.maxAllocs
- // parse
- program := parse(t, input)
- if program == nil {
- return
- }
- // compiler/VM
- _, trace, err := traceCompileRun(program, symbols, modules, maxAllocs)
- require.Error(t, err, "\n"+strings.Join(trace, "\n"))
- require.True(t, errors.Is(err, expected),
- "expected error is: %s, got: %s\n%s",
- expected.Error(), err.Error(), strings.Join(trace, "\n"))
- }
- func expectErrorAs(
- t *testing.T,
- input string,
- opts *testopts,
- expected interface{},
- ) {
- if opts == nil {
- opts = Opts()
- }
- symbols := opts.symbols
- modules := opts.modules
- maxAllocs := opts.maxAllocs
- // parse
- program := parse(t, input)
- if program == nil {
- return
- }
- // compiler/VM
- _, trace, err := traceCompileRun(program, symbols, modules, maxAllocs)
- require.Error(t, err, "\n"+strings.Join(trace, "\n"))
- require.True(t, errors.As(err, expected),
- "expected error as: %v, got: %v\n%s",
- expected, err, strings.Join(trace, "\n"))
- }
- type vmTracer struct {
- Out []string
- }
- func (o *vmTracer) Write(p []byte) (n int, err error) {
- o.Out = append(o.Out, string(p))
- return len(p), nil
- }
- func traceCompileRun(
- file *parser.File,
- symbols map[string]tengo.Object,
- modules *tengo.ModuleMap,
- maxAllocs int64,
- ) (res map[string]tengo.Object, trace []string, err error) {
- var v *tengo.VM
- defer func() {
- if e := recover(); e != nil {
- err = fmt.Errorf("panic: %v", e)
- // stack trace
- var stackTrace []string
- for i := 2; ; i += 1 {
- _, file, line, ok := _runtime.Caller(i)
- if !ok {
- break
- }
- stackTrace = append(stackTrace,
- fmt.Sprintf(" %s:%d", file, line))
- }
- trace = append(trace,
- fmt.Sprintf("[Error Trace]\n\n %s\n",
- strings.Join(stackTrace, "\n ")))
- }
- }()
- globals := make([]tengo.Object, tengo.GlobalsSize)
- symTable := tengo.NewSymbolTable()
- for name, value := range symbols {
- sym := symTable.Define(name)
- // should not store pointer to 'value' variable
- // which is re-used in each iteration.
- valueCopy := value
- globals[sym.Index] = valueCopy
- }
- for idx, fn := range tengo.GetAllBuiltinFunctions() {
- symTable.DefineBuiltin(idx, fn.Name)
- }
- tr := &vmTracer{}
- c := tengo.NewCompiler(file.InputFile, symTable, nil, modules, tr)
- err = c.Compile(file)
- trace = append(trace,
- fmt.Sprintf("\n[Compiler Trace]\n\n%s",
- strings.Join(tr.Out, "")))
- if err != nil {
- return
- }
- bytecode := c.Bytecode()
- bytecode.RemoveDuplicates()
- trace = append(trace, fmt.Sprintf("\n[Compiled Constants]\n\n%s",
- strings.Join(bytecode.FormatConstants(), "\n")))
- trace = append(trace, fmt.Sprintf("\n[Compiled Instructions]\n\n%s\n",
- strings.Join(bytecode.FormatInstructions(), "\n")))
- v = tengo.NewVM(bytecode, globals, maxAllocs)
- err = v.Run()
- {
- res = make(map[string]tengo.Object)
- for name := range symbols {
- sym, depth, ok := symTable.Resolve(name, false)
- if !ok || depth != 0 {
- err = fmt.Errorf("symbol not found: %s", name)
- return
- }
- res[name] = globals[sym.Index]
- }
- trace = append(trace, fmt.Sprintf("\n[Globals]\n\n%s",
- strings.Join(formatGlobals(globals), "\n")))
- }
- if err == nil && !v.IsStackEmpty() {
- err = errors.New("non empty stack after execution")
- }
- return
- }
- func formatGlobals(globals []tengo.Object) (formatted []string) {
- for idx, global := range globals {
- if global == nil {
- return
- }
- formatted = append(formatted, fmt.Sprintf("[% 3d] %s (%s|%p)",
- idx, global.String(), global.TypeName(), global))
- }
- return
- }
- func parse(t *testing.T, input string) *parser.File {
- testFileSet := parser.NewFileSet()
- testFile := testFileSet.AddFile("test", -1, len(input))
- p := parser.NewParser(testFile, []byte(input), nil)
- file, err := p.ParseFile()
- require.NoError(t, err)
- return file
- }
- func errorObject(v interface{}) *tengo.Error {
- return &tengo.Error{Value: toObject(v)}
- }
- func toObject(v interface{}) tengo.Object {
- switch v := v.(type) {
- case tengo.Object:
- return v
- case string:
- return &tengo.String{Value: v}
- case int64:
- return tengo.Int{Value: v}
- case int: // for convenience
- return tengo.Int{Value: int64(v)}
- case bool:
- if v {
- return tengo.TrueValue
- }
- return tengo.FalseValue
- case rune:
- return tengo.Char{Value: v}
- case byte: // for convenience
- return tengo.Char{Value: rune(v)}
- case float64:
- return tengo.Float{Value: v}
- case []byte:
- return &tengo.Bytes{Value: v}
- case MAP:
- objs := make(map[string]tengo.Object)
- for k, v := range v {
- objs[k] = toObject(v)
- }
- return &tengo.Map{Value: objs}
- case ARR:
- var objs []tengo.Object
- for _, e := range v {
- objs = append(objs, toObject(e))
- }
- return &tengo.Array{Value: objs}
- case IMAP:
- objs := make(map[string]tengo.Object)
- for k, v := range v {
- objs[k] = toObject(v)
- }
- return &tengo.ImmutableMap{Value: objs}
- case IARR:
- var objs []tengo.Object
- for _, e := range v {
- objs = append(objs, toObject(e))
- }
- return &tengo.ImmutableArray{Value: objs}
- }
- panic(fmt.Errorf("unknown type: %T", v))
- }
- func objectZeroCopy(o tengo.Object) tengo.Object {
- switch o.(type) {
- case tengo.Int:
- return tengo.Int{}
- case tengo.Float:
- return tengo.Float{}
- case tengo.Bool:
- return tengo.Bool{}
- case tengo.Char:
- return tengo.Char{}
- case *tengo.String:
- return &tengo.String{}
- case *tengo.Array:
- return &tengo.Array{}
- case *tengo.Map:
- return &tengo.Map{}
- case *tengo.Undefined:
- return tengo.UndefinedValue
- case *tengo.Error:
- return &tengo.Error{}
- case *tengo.Bytes:
- return &tengo.Bytes{}
- case *tengo.ImmutableArray:
- return &tengo.ImmutableArray{}
- case *tengo.ImmutableMap:
- return &tengo.ImmutableMap{}
- case nil:
- panic("nil")
- default:
- panic(fmt.Errorf("unknown object type: %s", o.TypeName()))
- }
- }
|