12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998 |
- package tengo_test
- import (
- "errors"
- "fmt"
- "math"
- "math/rand"
- "reflect"
- _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")
- expectError(t, `a := [1, 2, 3]; b := a[:"invalid"];`, nil,
- "Runtime Error: invalid slice index type: string")
- expectError(t, `a := immutable([4, 5, 6]); b := a[:false];`, nil,
- "Runtime Error: invalid slice index type: bool")
- expectError(t, `a := "hello"; b := a[:1.23];`, nil,
- "Runtime Error: invalid slice index type: float")
- expectError(t, `a := bytes("world"); b := a[:time(1)];`, nil,
- "Runtime Error: invalid slice index type: time")
- }
- 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.ObjectImpl
- 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.ObjectImpl
- 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.ObjectImpl
- 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.ObjectImpl
- 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 TestSliceIndex(t *testing.T) {
- expectError(t, `undefined[:1]`, nil, "Runtime Error: not indexable")
- expectError(t, `123[-1:2]`, nil, "Runtime Error: not indexable")
- expectError(t, `{}[:]`, nil, "Runtime Error: not indexable")
- expectError(t, `a := 123[-1:2] ; a += 1`, nil, "Runtime Error: not indexable")
- }
- 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(), reflect.TypeOf(global).Elem().Name(), 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()))
- }
- }
|