12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073 |
- package vm
- import (
- "fmt"
- "reflect"
- "testing"
- )
- func TestBasicOperators(t *testing.T) {
- t.Parallel()
- tests := []Test{
- {Script: `]`, ParseError: fmt.Errorf("syntax error")},
- {Script: `2 + 1`, RunOutput: int64(3)},
- {Script: `2 - 1`, RunOutput: int64(1)},
- {Script: `2 * 1`, RunOutput: int64(2)},
- {Script: `2 / 1`, RunOutput: float64(2)},
- {Script: `2.1 + 1.1`, RunOutput: float64(3.2)},
- {Script: `2.1 - 1.1`, RunOutput: float64(1)},
- {Script: `2 + 1.1`, RunOutput: float64(3.1)},
- {Script: `2.1 + 1`, RunOutput: float64(3.1)},
- {Script: `3 - 1.5`, RunOutput: float64(1.5)},
- {Script: `2.1 - 1`, RunOutput: float64(1.1)},
- {Script: `2.1 * 2.0`, RunOutput: float64(4.2)},
- {Script: `6.5 / 2.0`, RunOutput: float64(3.25)},
- {Script: `2-1`, RunOutput: int64(1)},
- {Script: `2 -1`, RunOutput: int64(1)},
- {Script: `2- 1`, RunOutput: int64(1)},
- {Script: `2 - -1`, RunOutput: int64(3)},
- {Script: `2- -1`, RunOutput: int64(3)},
- {Script: `2 - - 1`, RunOutput: int64(3)},
- {Script: `2- - 1`, RunOutput: int64(3)},
- {Script: `a + b`, Input: map[string]interface{}{"a": int64(2), "b": int64(1)}, RunOutput: int64(3)},
- {Script: `a - b`, Input: map[string]interface{}{"a": int64(2), "b": int64(1)}, RunOutput: int64(1)},
- {Script: `a * b`, Input: map[string]interface{}{"a": int64(2), "b": int64(1)}, RunOutput: int64(2)},
- {Script: `a / b`, Input: map[string]interface{}{"a": int64(2), "b": int64(1)}, RunOutput: float64(2)},
- {Script: `a + b`, Input: map[string]interface{}{"a": float64(2.1), "b": float64(1.1)}, RunOutput: float64(3.2)},
- {Script: `a - b`, Input: map[string]interface{}{"a": float64(2.1), "b": float64(1.1)}, RunOutput: float64(1)},
- {Script: `a * b`, Input: map[string]interface{}{"a": float64(2.1), "b": float64(2)}, RunOutput: float64(4.2)},
- {Script: `a / b`, Input: map[string]interface{}{"a": float64(6.5), "b": float64(2)}, RunOutput: float64(3.25)},
- {Script: `a + b`, Input: map[string]interface{}{"a": "a", "b": "b"}, RunOutput: "ab"},
- {Script: `a + b`, Input: map[string]interface{}{"a": "a", "b": int64(1)}, RunOutput: "a1"},
- {Script: `a + b`, Input: map[string]interface{}{"a": "a", "b": float64(1.1)}, RunOutput: "a1.1"},
- {Script: `a + b`, Input: map[string]interface{}{"a": int64(2), "b": "b"}, RunOutput: "2b"},
- {Script: `a + b`, Input: map[string]interface{}{"a": float64(2.5), "b": "b"}, RunOutput: "2.5b"},
- {Script: `a + z`, Input: map[string]interface{}{"a": "a"}, RunError: fmt.Errorf("undefined symbol 'z'"), RunOutput: nil},
- {Script: `z + b`, Input: map[string]interface{}{"a": "a"}, RunError: fmt.Errorf("undefined symbol 'z'"), RunOutput: nil},
- {Script: `c = a + b`, Input: map[string]interface{}{"a": int64(2), "b": int64(1)}, RunOutput: int64(3), Output: map[string]interface{}{"c": int64(3)}},
- {Script: `c = a - b`, Input: map[string]interface{}{"a": int64(2), "b": int64(1)}, RunOutput: int64(1), Output: map[string]interface{}{"c": int64(1)}},
- {Script: `c = a * b`, Input: map[string]interface{}{"a": int64(2), "b": int64(1)}, RunOutput: int64(2), Output: map[string]interface{}{"c": int64(2)}},
- {Script: `c = a / b`, Input: map[string]interface{}{"a": int64(2), "b": int64(1)}, RunOutput: float64(2), Output: map[string]interface{}{"c": float64(2)}},
- {Script: `c = a + b`, Input: map[string]interface{}{"a": float64(2.1), "b": float64(1.1)}, RunOutput: float64(3.2), Output: map[string]interface{}{"c": float64(3.2)}},
- {Script: `c = a - b`, Input: map[string]interface{}{"a": float64(2.1), "b": float64(1.1)}, RunOutput: float64(1), Output: map[string]interface{}{"c": float64(1)}},
- {Script: `c = a * b`, Input: map[string]interface{}{"a": float64(2.1), "b": float64(2)}, RunOutput: float64(4.2), Output: map[string]interface{}{"c": float64(4.2)}},
- {Script: `c = a / b`, Input: map[string]interface{}{"a": float64(6.5), "b": float64(2)}, RunOutput: float64(3.25), Output: map[string]interface{}{"c": float64(3.25)}},
- {Script: `a = nil; a++`, RunOutput: int64(1), Output: map[string]interface{}{"a": int64(1)}},
- {Script: `a = false; a++`, RunOutput: int64(1), Output: map[string]interface{}{"a": int64(1)}},
- {Script: `a = true; a++`, RunOutput: int64(2), Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a = 1; a++`, RunOutput: int64(2), Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a = 1.5; a++`, RunOutput: float64(2.5), Output: map[string]interface{}{"a": float64(2.5)}},
- {Script: `a = "1"; a++`, RunOutput: "11", Output: map[string]interface{}{"a": "11"}},
- {Script: `a = "a"; a++`, RunOutput: "a1", Output: map[string]interface{}{"a": "a1"}},
- {Script: `a = nil; a--`, RunOutput: int64(-1), Output: map[string]interface{}{"a": int64(-1)}},
- {Script: `a = false; a--`, RunOutput: int64(-1), Output: map[string]interface{}{"a": int64(-1)}},
- {Script: `a = true; a--`, RunOutput: int64(0), Output: map[string]interface{}{"a": int64(0)}},
- {Script: `a = 2; a--`, RunOutput: int64(1), Output: map[string]interface{}{"a": int64(1)}},
- {Script: `a = 2.5; a--`, RunOutput: float64(1.5), Output: map[string]interface{}{"a": float64(1.5)}},
- {Script: `a++`, Input: map[string]interface{}{"a": nil}, RunOutput: int64(1), Output: map[string]interface{}{"a": int64(1)}},
- {Script: `a++`, Input: map[string]interface{}{"a": false}, RunOutput: int64(1), Output: map[string]interface{}{"a": int64(1)}},
- {Script: `a++`, Input: map[string]interface{}{"a": true}, RunOutput: int64(2), Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a++`, Input: map[string]interface{}{"a": int32(1)}, RunOutput: int64(2), Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a++`, Input: map[string]interface{}{"a": int64(1)}, RunOutput: int64(2), Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a++`, Input: map[string]interface{}{"a": float32(3.5)}, RunOutput: float64(4.5), Output: map[string]interface{}{"a": float64(4.5)}},
- {Script: `a++`, Input: map[string]interface{}{"a": float64(4.5)}, RunOutput: float64(5.5), Output: map[string]interface{}{"a": float64(5.5)}},
- {Script: `a++`, Input: map[string]interface{}{"a": "2"}, RunOutput: "21", Output: map[string]interface{}{"a": "21"}},
- {Script: `a++`, Input: map[string]interface{}{"a": "a"}, RunOutput: "a1", Output: map[string]interface{}{"a": "a1"}},
- {Script: `a--`, Input: map[string]interface{}{"a": nil}, RunOutput: int64(-1), Output: map[string]interface{}{"a": int64(-1)}},
- {Script: `a--`, Input: map[string]interface{}{"a": false}, RunOutput: int64(-1), Output: map[string]interface{}{"a": int64(-1)}},
- {Script: `a--`, Input: map[string]interface{}{"a": true}, RunOutput: int64(0), Output: map[string]interface{}{"a": int64(0)}},
- {Script: `a--`, Input: map[string]interface{}{"a": int32(2)}, RunOutput: int64(1), Output: map[string]interface{}{"a": int64(1)}},
- {Script: `a--`, Input: map[string]interface{}{"a": int64(2)}, RunOutput: int64(1), Output: map[string]interface{}{"a": int64(1)}},
- {Script: `a--`, Input: map[string]interface{}{"a": float32(2.5)}, RunOutput: float64(1.5), Output: map[string]interface{}{"a": float64(1.5)}},
- {Script: `a--`, Input: map[string]interface{}{"a": float64(2.5)}, RunOutput: float64(1.5), Output: map[string]interface{}{"a": float64(1.5)}},
- {Script: `a--`, Input: map[string]interface{}{"a": "2"}, RunOutput: int64(1), Output: map[string]interface{}{"a": int64(1)}},
- {Script: `a--`, Input: map[string]interface{}{"a": "a"}, RunOutput: int64(-1), Output: map[string]interface{}{"a": int64(-1)}},
- {Script: `1++`, RunError: fmt.Errorf("invalid operation"), RunOutput: nil},
- {Script: `1--`, RunError: fmt.Errorf("invalid operation"), RunOutput: nil},
- {Script: `z++`, RunError: fmt.Errorf("undefined symbol 'z'"), RunOutput: nil},
- {Script: `z--`, RunError: fmt.Errorf("undefined symbol 'z'"), RunOutput: nil},
- {Script: `!(1++)`, RunError: fmt.Errorf("invalid operation"), RunOutput: nil},
- {Script: `1 + 1++`, RunError: fmt.Errorf("invalid operation"), RunOutput: nil},
- {Script: `1 - 1++`, RunError: fmt.Errorf("invalid operation"), RunOutput: nil},
- {Script: `1 * 1++`, RunError: fmt.Errorf("invalid operation"), RunOutput: nil},
- {Script: `1 / 1++`, RunError: fmt.Errorf("invalid operation"), RunOutput: nil},
- {Script: `1++ + 1`, RunError: fmt.Errorf("invalid operation"), RunOutput: nil},
- {Script: `1++ - 1`, RunError: fmt.Errorf("invalid operation"), RunOutput: nil},
- {Script: `1++ * 1`, RunError: fmt.Errorf("invalid operation"), RunOutput: nil},
- {Script: `1++ / 1`, RunError: fmt.Errorf("invalid operation"), RunOutput: nil},
- {Script: `a = 1; b = 2; a + b`, RunOutput: int64(3)},
- {Script: `a = [1]; b = 2; a[0] + b`, RunOutput: int64(3)},
- {Script: `a = 1; b = [2]; a + b[0]`, RunOutput: int64(3)},
- {Script: `a = 2; b = 1; a - b`, RunOutput: int64(1)},
- {Script: `a = [2]; b = 1; a[0] - b`, RunOutput: int64(1)},
- {Script: `a = 2; b = [1]; a - b[0]`, RunOutput: int64(1)},
- {Script: `a = 1; b = 2; a * b`, RunOutput: int64(2)},
- {Script: `a = [1]; b = 2; a[0] * b`, RunOutput: int64(2)},
- {Script: `a = 1; b = [2]; a * b[0]`, RunOutput: int64(2)},
- {Script: `a = 4; b = 2; a / b`, RunOutput: float64(2)},
- {Script: `a = [4]; b = 2; a[0] / b`, RunOutput: float64(2)},
- {Script: `a = 4; b = [2]; a / b[0]`, RunOutput: float64(2)},
- {Script: `a += 1`, Input: map[string]interface{}{"a": int64(2)}, RunOutput: int64(3), Output: map[string]interface{}{"a": int64(3)}},
- {Script: `a -= 1`, Input: map[string]interface{}{"a": int64(2)}, RunOutput: int64(1), Output: map[string]interface{}{"a": int64(1)}},
- {Script: `a *= 2`, Input: map[string]interface{}{"a": int64(2)}, RunOutput: int64(4), Output: map[string]interface{}{"a": int64(4)}},
- {Script: `a /= 2`, Input: map[string]interface{}{"a": int64(2)}, RunOutput: float64(1), Output: map[string]interface{}{"a": float64(1)}},
- {Script: `a += 1`, Input: map[string]interface{}{"a": 2.1}, RunOutput: float64(3.1), Output: map[string]interface{}{"a": float64(3.1)}},
- {Script: `a -= 1`, Input: map[string]interface{}{"a": 2.1}, RunOutput: float64(1.1), Output: map[string]interface{}{"a": float64(1.1)}},
- {Script: `a *= 2`, Input: map[string]interface{}{"a": 2.1}, RunOutput: float64(4.2), Output: map[string]interface{}{"a": float64(4.2)}},
- {Script: `a /= 2`, Input: map[string]interface{}{"a": 6.5}, RunOutput: float64(3.25), Output: map[string]interface{}{"a": float64(3.25)}},
- {Script: `a &= 1`, Input: map[string]interface{}{"a": int64(2)}, RunOutput: int64(0), Output: map[string]interface{}{"a": int64(0)}},
- {Script: `a &= 2`, Input: map[string]interface{}{"a": int64(2)}, RunOutput: int64(2), Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a &= 1`, Input: map[string]interface{}{"a": float64(2.1)}, RunOutput: int64(0), Output: map[string]interface{}{"a": int64(0)}},
- {Script: `a &= 2`, Input: map[string]interface{}{"a": float64(2.1)}, RunOutput: int64(2), Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a |= 1`, Input: map[string]interface{}{"a": int64(2)}, RunOutput: int64(3), Output: map[string]interface{}{"a": int64(3)}},
- {Script: `a |= 2`, Input: map[string]interface{}{"a": int64(2)}, RunOutput: int64(2), Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a |= 1`, Input: map[string]interface{}{"a": float64(2.1)}, RunOutput: int64(3), Output: map[string]interface{}{"a": int64(3)}},
- {Script: `a |= 2`, Input: map[string]interface{}{"a": float64(2.1)}, RunOutput: int64(2), Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a << 2`, Input: map[string]interface{}{"a": int64(2)}, RunOutput: int64(8), Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a >> 2`, Input: map[string]interface{}{"a": int64(8)}, RunOutput: int64(2), Output: map[string]interface{}{"a": int64(8)}},
- {Script: `a << 2`, Input: map[string]interface{}{"a": float64(2)}, RunOutput: int64(8), Output: map[string]interface{}{"a": float64(2)}},
- {Script: `a >> 2`, Input: map[string]interface{}{"a": float64(8)}, RunOutput: int64(2), Output: map[string]interface{}{"a": float64(8)}},
- {Script: `a % 2`, Input: map[string]interface{}{"a": int64(2)}, RunOutput: int64(0), Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a % 3`, Input: map[string]interface{}{"a": int64(2)}, RunOutput: int64(2), Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a % 2`, Input: map[string]interface{}{"a": float64(2.1)}, RunOutput: int64(0), Output: map[string]interface{}{"a": float64(2.1)}},
- {Script: `a % 3`, Input: map[string]interface{}{"a": float64(2.1)}, RunOutput: int64(2), Output: map[string]interface{}{"a": float64(2.1)}},
- {Script: `a * 4`, Input: map[string]interface{}{"a": "a"}, RunOutput: "aaaa", Output: map[string]interface{}{"a": "a"}},
- {Script: `a * 4.0`, Input: map[string]interface{}{"a": "a"}, RunOutput: float64(0), Output: map[string]interface{}{"a": "a"}},
- {Script: `-a`, Input: map[string]interface{}{"a": nil}, RunOutput: float64(-0), Output: map[string]interface{}{"a": nil}},
- {Script: `-a`, Input: map[string]interface{}{"a": int32(1)}, RunOutput: int64(-1), Output: map[string]interface{}{"a": int32(1)}},
- {Script: `-a`, Input: map[string]interface{}{"a": int64(2)}, RunOutput: int64(-2), Output: map[string]interface{}{"a": int64(2)}},
- {Script: `-a`, Input: map[string]interface{}{"a": float32(3.5)}, RunOutput: float64(-3.5), Output: map[string]interface{}{"a": float32(3.5)}},
- {Script: `-a`, Input: map[string]interface{}{"a": float64(4.5)}, RunOutput: float64(-4.5), Output: map[string]interface{}{"a": float64(4.5)}},
- {Script: `-a`, Input: map[string]interface{}{"a": "a"}, RunOutput: float64(-0), Output: map[string]interface{}{"a": "a"}},
- {Script: `-a`, Input: map[string]interface{}{"a": "1"}, RunOutput: float64(-1), Output: map[string]interface{}{"a": "1"}},
- {Script: `^a`, Input: map[string]interface{}{"a": nil}, RunOutput: int64(-1), Output: map[string]interface{}{"a": nil}},
- {Script: `^a`, Input: map[string]interface{}{"a": "a"}, RunOutput: int64(-1), Output: map[string]interface{}{"a": "a"}},
- {Script: `^a`, Input: map[string]interface{}{"a": int64(2)}, RunOutput: int64(-3), Output: map[string]interface{}{"a": int64(2)}},
- {Script: `^a`, Input: map[string]interface{}{"a": float64(2.1)}, RunOutput: int64(-3), Output: map[string]interface{}{"a": float64(2.1)}},
- {Script: `!true`, RunOutput: false},
- {Script: `!false`, RunOutput: true},
- {Script: `!1`, RunOutput: false},
- }
- runTests(t, tests, nil, &Options{Debug: true})
- }
- func TestComparisonOperators(t *testing.T) {
- t.Parallel()
- tests := []Test{
- {Script: `1++ == 2`, RunError: fmt.Errorf("invalid operation"), RunOutput: nil},
- {Script: `2 == 1++`, RunError: fmt.Errorf("invalid operation"), RunOutput: nil},
- {Script: `1++ || true`, RunError: fmt.Errorf("invalid operation"), RunOutput: nil},
- {Script: `false || 1++`, RunError: fmt.Errorf("invalid operation"), RunOutput: nil},
- {Script: `1++ && true`, RunError: fmt.Errorf("invalid operation"), RunOutput: nil},
- {Script: `true && 1++`, RunError: fmt.Errorf("invalid operation"), RunOutput: nil},
- {Script: `a == 1`, Input: map[string]interface{}{"a": int64(2)}, RunOutput: false, Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a == 2`, Input: map[string]interface{}{"a": int64(2)}, RunOutput: true, Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a != 1`, Input: map[string]interface{}{"a": int64(2)}, RunOutput: true, Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a != 2`, Input: map[string]interface{}{"a": int64(2)}, RunOutput: false, Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a == 1.0`, Input: map[string]interface{}{"a": int64(2)}, RunOutput: false, Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a == 2.0`, Input: map[string]interface{}{"a": int64(2)}, RunOutput: true, Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a != 1.0`, Input: map[string]interface{}{"a": int64(2)}, RunOutput: true, Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a != 2.0`, Input: map[string]interface{}{"a": int64(2)}, RunOutput: false, Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a == 1`, Input: map[string]interface{}{"a": float64(2)}, RunOutput: false, Output: map[string]interface{}{"a": float64(2)}},
- {Script: `a == 2`, Input: map[string]interface{}{"a": float64(2)}, RunOutput: true, Output: map[string]interface{}{"a": float64(2)}},
- {Script: `a != 1`, Input: map[string]interface{}{"a": float64(2)}, RunOutput: true, Output: map[string]interface{}{"a": float64(2)}},
- {Script: `a != 2`, Input: map[string]interface{}{"a": float64(2)}, RunOutput: false, Output: map[string]interface{}{"a": float64(2)}},
- {Script: `a == 1.0`, Input: map[string]interface{}{"a": float64(2)}, RunOutput: false, Output: map[string]interface{}{"a": float64(2)}},
- {Script: `a == 2.0`, Input: map[string]interface{}{"a": float64(2)}, RunOutput: true, Output: map[string]interface{}{"a": float64(2)}},
- {Script: `a != 1.0`, Input: map[string]interface{}{"a": float64(2)}, RunOutput: true, Output: map[string]interface{}{"a": float64(2)}},
- {Script: `a != 2.0`, Input: map[string]interface{}{"a": float64(2)}, RunOutput: false, Output: map[string]interface{}{"a": float64(2)}},
- {Script: `a == nil`, Input: map[string]interface{}{"a": nil}, RunOutput: true, Output: map[string]interface{}{"a": nil}},
- {Script: `a == nil`, Input: map[string]interface{}{"a": nil}, RunOutput: true, Output: map[string]interface{}{"a": nil}},
- {Script: `a == nil`, Input: map[string]interface{}{"a": int64(2)}, RunOutput: false, Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a == nil`, Input: map[string]interface{}{"a": float64(2)}, RunOutput: false, Output: map[string]interface{}{"a": float64(2)}},
- {Script: `a == 2`, Input: map[string]interface{}{"a": nil}, RunOutput: false, Output: map[string]interface{}{"a": nil}},
- {Script: `a == 2.0`, Input: map[string]interface{}{"a": nil}, RunOutput: false, Output: map[string]interface{}{"a": nil}},
- {Script: `1 == 1.0`, RunOutput: true},
- {Script: `1 != 1.0`, RunOutput: false},
- {Script: `"a" != "a"`, RunOutput: false},
- {Script: `a > 2`, Input: map[string]interface{}{"a": int64(2)}, RunOutput: false, Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a > 1`, Input: map[string]interface{}{"a": int64(2)}, RunOutput: true, Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a < 2`, Input: map[string]interface{}{"a": int64(2)}, RunOutput: false, Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a < 3`, Input: map[string]interface{}{"a": int64(2)}, RunOutput: true, Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a > 2.0`, Input: map[string]interface{}{"a": int64(2)}, RunOutput: false, Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a > 1.0`, Input: map[string]interface{}{"a": int64(2)}, RunOutput: true, Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a < 2.0`, Input: map[string]interface{}{"a": int64(2)}, RunOutput: false, Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a < 3.0`, Input: map[string]interface{}{"a": int64(2)}, RunOutput: true, Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a > 2`, Input: map[string]interface{}{"a": float64(2)}, RunOutput: false, Output: map[string]interface{}{"a": float64(2)}},
- {Script: `a > 1`, Input: map[string]interface{}{"a": float64(2)}, RunOutput: true, Output: map[string]interface{}{"a": float64(2)}},
- {Script: `a < 2`, Input: map[string]interface{}{"a": float64(2)}, RunOutput: false, Output: map[string]interface{}{"a": float64(2)}},
- {Script: `a < 3`, Input: map[string]interface{}{"a": float64(2)}, RunOutput: true, Output: map[string]interface{}{"a": float64(2)}},
- {Script: `a > 2.0`, Input: map[string]interface{}{"a": float64(2)}, RunOutput: false, Output: map[string]interface{}{"a": float64(2)}},
- {Script: `a > 1.0`, Input: map[string]interface{}{"a": float64(2)}, RunOutput: true, Output: map[string]interface{}{"a": float64(2)}},
- {Script: `a < 2.0`, Input: map[string]interface{}{"a": float64(2)}, RunOutput: false, Output: map[string]interface{}{"a": float64(2)}},
- {Script: `a < 3.0`, Input: map[string]interface{}{"a": float64(2)}, RunOutput: true, Output: map[string]interface{}{"a": float64(2)}},
- {Script: `a >= 2`, Input: map[string]interface{}{"a": int64(2)}, RunOutput: true, Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a >= 3`, Input: map[string]interface{}{"a": int64(2)}, RunOutput: false, Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a <= 2`, Input: map[string]interface{}{"a": int64(2)}, RunOutput: true, Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a <= 3`, Input: map[string]interface{}{"a": int64(2)}, RunOutput: true, Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a >= 2.0`, Input: map[string]interface{}{"a": int64(2)}, RunOutput: true, Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a >= 3.0`, Input: map[string]interface{}{"a": int64(2)}, RunOutput: false, Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a <= 2.0`, Input: map[string]interface{}{"a": int64(2)}, RunOutput: true, Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a <= 3.0`, Input: map[string]interface{}{"a": int64(2)}, RunOutput: true, Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a >= 2`, Input: map[string]interface{}{"a": float64(2)}, RunOutput: true, Output: map[string]interface{}{"a": float64(2)}},
- {Script: `a >= 3`, Input: map[string]interface{}{"a": float64(2)}, RunOutput: false, Output: map[string]interface{}{"a": float64(2)}},
- {Script: `a <= 2`, Input: map[string]interface{}{"a": float64(2)}, RunOutput: true, Output: map[string]interface{}{"a": float64(2)}},
- {Script: `a <= 3`, Input: map[string]interface{}{"a": float64(2)}, RunOutput: true, Output: map[string]interface{}{"a": float64(2)}},
- {Script: `a >= 2.0`, Input: map[string]interface{}{"a": float64(2)}, RunOutput: true, Output: map[string]interface{}{"a": float64(2)}},
- {Script: `a >= 3.0`, Input: map[string]interface{}{"a": float64(2)}, RunOutput: false, Output: map[string]interface{}{"a": float64(2)}},
- {Script: `a <= 2.0`, Input: map[string]interface{}{"a": float64(2)}, RunOutput: true, Output: map[string]interface{}{"a": float64(2)}},
- {Script: `a <= 3.0`, Input: map[string]interface{}{"a": float64(2)}, RunOutput: true, Output: map[string]interface{}{"a": float64(2)}},
- {Script: `a = false; b = false; a && b`, RunOutput: false},
- {Script: `a = false; b = true; a && b`, RunOutput: false},
- {Script: `a = true; b = false; a && b`, RunOutput: false},
- {Script: `a = true; b = true; a && b`, RunOutput: true},
- {Script: `a = false; b = false; a || b`, RunOutput: false},
- {Script: `a = false; b = true; a || b`, RunOutput: true},
- {Script: `a = true; b = false; a || b`, RunOutput: true},
- {Script: `a = true; b = true; a || b`, RunOutput: true},
- {Script: `a = [false]; b = false; a[0] && b`, RunOutput: false},
- {Script: `a = [false]; b = true; a[0] && b`, RunOutput: false},
- {Script: `a = [true]; b = false; a[0] && b`, RunOutput: false},
- {Script: `a = [true]; b = true; a[0] && b`, RunOutput: true},
- {Script: `a = [false]; b = false; a[0] || b`, RunOutput: false},
- {Script: `a = [false]; b = true; a[0] || b`, RunOutput: true},
- {Script: `a = [true]; b = false; a[0] || b`, RunOutput: true},
- {Script: `a = [true]; b = true; a[0] || b`, RunOutput: true},
- {Script: `a = false; b = [false]; a && b[0]`, RunOutput: false},
- {Script: `a = false; b = [true]; a && b[0]`, RunOutput: false},
- {Script: `a = true; b = [false]; a && b[0]`, RunOutput: false},
- {Script: `a = true; b = [true]; a && b[0]`, RunOutput: true},
- {Script: `a = false; b = [false]; a || b[0]`, RunOutput: false},
- {Script: `a = false; b = [true]; a || b[0]`, RunOutput: true},
- {Script: `a = true; b = [false]; a || b[0]`, RunOutput: true},
- {Script: `a = true; b = [true]; a || b[0]`, RunOutput: true},
- {Script: `0 && 0`, RunOutput: false},
- {Script: `0 && 1`, RunOutput: false},
- {Script: `1 && 0`, RunOutput: false},
- {Script: `1 && 1`, RunOutput: true},
- {Script: `0 || 0`, RunOutput: false},
- {Script: `0 || 1`, RunOutput: true},
- {Script: `1 || 0`, RunOutput: true},
- {Script: `1 || 1`, RunOutput: true},
- {Script: `1 == 1 && 1 == 1`, RunOutput: true},
- {Script: `1 == 1 && 1 == 2`, RunOutput: false},
- {Script: `1 == 2 && 1 == 1`, RunOutput: false},
- {Script: `1 == 2 && 1 == 2`, RunOutput: false},
- {Script: `1 == 1 || 1 == 1`, RunOutput: true},
- {Script: `1 == 1 || 1 == 2`, RunOutput: true},
- {Script: `1 == 2 || 1 == 1`, RunOutput: true},
- {Script: `1 == 2 || 1 == 2`, RunOutput: false},
- {Script: `true == "1"`, RunOutput: true},
- {Script: `true == "t"`, RunOutput: true},
- {Script: `true == "T"`, RunOutput: true},
- {Script: `true == "true"`, RunOutput: true},
- {Script: `true == "TRUE"`, RunOutput: true},
- {Script: `true == "True"`, RunOutput: true},
- {Script: `true == "false"`, RunOutput: false},
- {Script: `false == "0"`, RunOutput: true},
- {Script: `false == "f"`, RunOutput: true},
- {Script: `false == "F"`, RunOutput: true},
- {Script: `false == "false"`, RunOutput: true},
- {Script: `false == "false"`, RunOutput: true},
- {Script: `false == "FALSE"`, RunOutput: true},
- {Script: `false == "False"`, RunOutput: true},
- {Script: `false == "true"`, RunOutput: false},
- {Script: `false == "foo"`, RunOutput: false},
- {Script: `true == "foo"`, RunOutput: true},
- {Script: `0 == "0"`, RunOutput: true},
- {Script: `"1.0" == 1`, RunOutput: true},
- {Script: `1 == "1"`, RunOutput: true},
- {Script: `0.0 == "0"`, RunOutput: true},
- {Script: `0.0 == "0.0"`, RunOutput: true},
- {Script: `1.0 == "1.0"`, RunOutput: true},
- {Script: `1.2 == "1.2"`, RunOutput: true},
- {Script: `"7" == 7.2`, RunOutput: false},
- {Script: `1.2 == "1"`, RunOutput: false},
- {Script: `"1.1" == 1`, RunOutput: false},
- {Script: `0 == "1"`, RunOutput: false},
- {Script: `a == b`, Input: map[string]interface{}{"a": reflect.Value{}, "b": reflect.Value{}}, RunOutput: true, Output: map[string]interface{}{"a": reflect.Value{}, "b": reflect.Value{}}},
- {Script: `a == b`, Input: map[string]interface{}{"a": reflect.Value{}, "b": true}, RunOutput: false, Output: map[string]interface{}{"a": reflect.Value{}, "b": true}},
- {Script: `a == b`, Input: map[string]interface{}{"a": true, "b": reflect.Value{}}, RunOutput: false, Output: map[string]interface{}{"a": true, "b": reflect.Value{}}},
- {Script: `a == b`, Input: map[string]interface{}{"a": nil, "b": nil}, RunOutput: true, Output: map[string]interface{}{"a": nil, "b": nil}},
- {Script: `a == b`, Input: map[string]interface{}{"a": nil, "b": true}, RunOutput: false, Output: map[string]interface{}{"a": nil, "b": true}},
- {Script: `a == b`, Input: map[string]interface{}{"a": true, "b": nil}, RunOutput: false, Output: map[string]interface{}{"a": true, "b": nil}},
- {Script: `a == b`, Input: map[string]interface{}{"a": false, "b": false}, RunOutput: true, Output: map[string]interface{}{"a": false, "b": false}},
- {Script: `a == b`, Input: map[string]interface{}{"a": false, "b": true}, RunOutput: false, Output: map[string]interface{}{"a": false, "b": true}},
- {Script: `a == b`, Input: map[string]interface{}{"a": true, "b": false}, RunOutput: false, Output: map[string]interface{}{"a": true, "b": false}},
- {Script: `a == b`, Input: map[string]interface{}{"a": true, "b": true}, RunOutput: true, Output: map[string]interface{}{"a": true, "b": true}},
- {Script: `a == b`, Input: map[string]interface{}{"a": int32(1), "b": int32(1)}, RunOutput: true, Output: map[string]interface{}{"a": int32(1), "b": int32(1)}},
- {Script: `a == b`, Input: map[string]interface{}{"a": int32(1), "b": int32(2)}, RunOutput: false, Output: map[string]interface{}{"a": int32(1), "b": int32(2)}},
- {Script: `a == b`, Input: map[string]interface{}{"a": int32(2), "b": int32(1)}, RunOutput: false, Output: map[string]interface{}{"a": int32(2), "b": int32(1)}},
- {Script: `a == b`, Input: map[string]interface{}{"a": int32(2), "b": int32(2)}, RunOutput: true, Output: map[string]interface{}{"a": int32(2), "b": int32(2)}},
- {Script: `a == b`, Input: map[string]interface{}{"a": int64(1), "b": int64(1)}, RunOutput: true, Output: map[string]interface{}{"a": int64(1), "b": int64(1)}},
- {Script: `a == b`, Input: map[string]interface{}{"a": int64(1), "b": int64(2)}, RunOutput: false, Output: map[string]interface{}{"a": int64(1), "b": int64(2)}},
- {Script: `a == b`, Input: map[string]interface{}{"a": int64(2), "b": int64(1)}, RunOutput: false, Output: map[string]interface{}{"a": int64(2), "b": int64(1)}},
- {Script: `a == b`, Input: map[string]interface{}{"a": int64(2), "b": int64(2)}, RunOutput: true, Output: map[string]interface{}{"a": int64(2), "b": int64(2)}},
- {Script: `a == b`, Input: map[string]interface{}{"a": float32(1.1), "b": float32(1.1)}, RunOutput: true, Output: map[string]interface{}{"a": float32(1.1), "b": float32(1.1)}},
- {Script: `a == b`, Input: map[string]interface{}{"a": float32(1.1), "b": float32(2.2)}, RunOutput: false, Output: map[string]interface{}{"a": float32(1.1), "b": float32(2.2)}},
- {Script: `a == b`, Input: map[string]interface{}{"a": float32(2.2), "b": float32(1.1)}, RunOutput: false, Output: map[string]interface{}{"a": float32(2.2), "b": float32(1.1)}},
- {Script: `a == b`, Input: map[string]interface{}{"a": float32(2.2), "b": float32(2.2)}, RunOutput: true, Output: map[string]interface{}{"a": float32(2.2), "b": float32(2.2)}},
- {Script: `a == b`, Input: map[string]interface{}{"a": float64(1.1), "b": float64(1.1)}, RunOutput: true, Output: map[string]interface{}{"a": float64(1.1), "b": float64(1.1)}},
- {Script: `a == b`, Input: map[string]interface{}{"a": float64(1.1), "b": float64(2.2)}, RunOutput: false, Output: map[string]interface{}{"a": float64(1.1), "b": float64(2.2)}},
- {Script: `a == b`, Input: map[string]interface{}{"a": float64(2.2), "b": float64(1.1)}, RunOutput: false, Output: map[string]interface{}{"a": float64(2.2), "b": float64(1.1)}},
- {Script: `a == b`, Input: map[string]interface{}{"a": float64(2.2), "b": float64(2.2)}, RunOutput: true, Output: map[string]interface{}{"a": float64(2.2), "b": float64(2.2)}},
- {Script: `a == b`, Input: map[string]interface{}{"a": 'a', "b": 'a'}, RunOutput: true, Output: map[string]interface{}{"a": 'a', "b": 'a'}},
- {Script: `a == b`, Input: map[string]interface{}{"a": 'a', "b": 'b'}, RunOutput: false, Output: map[string]interface{}{"a": 'a', "b": 'b'}},
- {Script: `a == b`, Input: map[string]interface{}{"a": 'b', "b": 'a'}, RunOutput: false, Output: map[string]interface{}{"a": 'b', "b": 'a'}},
- {Script: `a == b`, Input: map[string]interface{}{"a": 'b', "b": 'b'}, RunOutput: true, Output: map[string]interface{}{"a": 'b', "b": 'b'}},
- {Script: `a == b`, Input: map[string]interface{}{"a": "a", "b": "a"}, RunOutput: true, Output: map[string]interface{}{"a": "a", "b": "a"}},
- {Script: `a == b`, Input: map[string]interface{}{"a": "a", "b": "b"}, RunOutput: false, Output: map[string]interface{}{"a": "a", "b": "b"}},
- {Script: `a == b`, Input: map[string]interface{}{"a": "b", "b": "a"}, RunOutput: false, Output: map[string]interface{}{"a": "b", "b": "a"}},
- {Script: `a == b`, Input: map[string]interface{}{"a": "b", "b": "b"}, RunOutput: true, Output: map[string]interface{}{"a": "b", "b": "b"}},
- {Script: `b = "\"a\""; a == b`, Input: map[string]interface{}{"a": `"a"`}, RunOutput: true, Output: map[string]interface{}{"a": `"a"`, "b": `"a"`}},
- {Script: `a = "test"; a == "test"`, RunOutput: true},
- {Script: `a = "test"; a[0:1] == "t"`, RunOutput: true},
- {Script: `a = "test"; a[0:2] == "te"`, RunOutput: true},
- {Script: `a = "test"; a[1:3] == "es"`, RunOutput: true},
- {Script: `a = "test"; a[0:4] == "test"`, RunOutput: true},
- {Script: `a = "a b"; a[1] == ' '`, RunOutput: true},
- {Script: `a = "test"; a[0] == 't'`, RunOutput: true},
- {Script: `a = "test"; a[1] == 'e'`, RunOutput: true},
- {Script: `a = "test"; a[3] == 't'`, RunOutput: true},
- {Script: `a = "a b"; a[1] != ' '`, RunOutput: false},
- {Script: `a = "test"; a[0] != 't'`, RunOutput: false},
- {Script: `a = "test"; a[1] != 'e'`, RunOutput: false},
- {Script: `a = "test"; a[3] != 't'`, RunOutput: false},
- }
- runTests(t, tests, nil, &Options{Debug: true})
- }
- func TestThrows(t *testing.T) {
- t.Parallel()
- tests := []Test{
- {Script: `throw(1++)`, RunError: fmt.Errorf("invalid operation")},
- // {Script: `throw(a)`, Input: map[string]interface{}{"a": reflect.Value{}}, RunError: fmt.Errorf("invalid operation")},
- {Script: `true && func(){throw('abcde')}()`, RunError: fmt.Errorf("abcde")},
- {Script: `false && func(){throw('abcde')}()`, RunOutput: false},
- {Script: `true || func(){throw('abcde')}()`, RunOutput: true},
- {Script: `false || func(){throw('abcde')}()`, RunError: fmt.Errorf("abcde")},
- {Script: `true && true && func(){throw('abcde')}()`, RunError: fmt.Errorf("abcde")},
- {Script: `true && false && func(){throw('abcde')}()`, RunOutput: false},
- {Script: `true && func(){throw('abcde')}() && true`, RunError: fmt.Errorf("abcde")},
- {Script: `false && func(){throw('abcde')}() && func(){throw('abcde')}() `, RunOutput: false},
- {Script: `true && func(){throw('abcde')}() || false`, RunError: fmt.Errorf("abcde")},
- {Script: `true && false || func(){throw('abcde')}()`, RunError: fmt.Errorf("abcde")},
- {Script: `true && true || func(){throw('abcde')}()`, RunOutput: true},
- {Script: `true || func(){throw('abcde')}() || func(){throw('abcde')}()`, RunOutput: true},
- {Script: `false || func(){throw('abcde')}() || true`, RunError: fmt.Errorf("abcde")},
- {Script: `false || true || func(){throw('abcde')}()`, RunOutput: true},
- {Script: `false || false || func(){throw('abcde')}()`, RunError: fmt.Errorf("abcde")},
- {Script: `false || false && func(){throw('abcde')}()`, RunOutput: false},
- {Script: `false || true && func(){throw('abcde')}()`, RunError: fmt.Errorf("abcde")},
- {Script: `false || func(){throw('abcde')}() || true`, RunError: fmt.Errorf("abcde")},
- {Script: `1 == 1 && func(){throw('abcde')}()`, RunError: fmt.Errorf("abcde")},
- {Script: `1 == 2 && func(){throw('abcde')}()`, RunOutput: false},
- {Script: `1 == 1 || func(){throw('abcde')}()`, RunOutput: true},
- {Script: `1 == 2 || func(){throw('abcde')}()`, RunError: fmt.Errorf("abcde")},
- {Script: `(true || func(){throw('abcde')}()) && (true || func(){throw('hello')}())`, RunOutput: true},
- {Script: `(true || func(){throw('abcde')}()) && (true && func(){throw('hello')}())`, RunError: fmt.Errorf("hello")},
- {Script: `(true || func(){throw('abcde')}()) || (true && func(){throw('hello')}())`, RunOutput: true},
- {Script: `(true && func(){throw('abcde')}()) && (true && func(){throw('hello')}())`, RunError: fmt.Errorf("abcde")},
- {Script: `(true || func(){throw('abcde')}()) && (false || func(){throw('hello')}())`, RunError: fmt.Errorf("hello")},
- }
- runTests(t, tests, nil, &Options{Debug: true})
- }
- func TestTernaryOperator(t *testing.T) {
- t.Parallel()
- tests := []Test{
- {Script: `a = a ? 1 : 2`, RunError: fmt.Errorf("undefined symbol 'a'")},
- {Script: `a = z ? 1 : 2`, RunError: fmt.Errorf("undefined symbol 'z'")},
- {Script: `a = 0; a = a ? 1 : z`, RunError: fmt.Errorf("undefined symbol 'z'")},
- {Script: `a = 1; a = a ? z : 1`, RunError: fmt.Errorf("undefined symbol 'z'")},
- {Script: `a = b[1] ? 2 : 1`, Input: map[string]interface{}{"b": []interface{}{}}, RunError: fmt.Errorf("index out of range")},
- {Script: `a = b[1][2] ? 2 : 1`, Input: map[string]interface{}{"b": []interface{}{}}, RunError: fmt.Errorf("index out of range")},
- {Script: `a = b["test"][1] ? 2 : 1`, Input: map[string]interface{}{"b": map[string]interface{}{"test": 2}}, RunError: fmt.Errorf("type int does not support index operation")},
- {Script: `a = 1 ? 2 : z`, RunOutput: int64(2), Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a = -1 ? 2 : 1`, RunOutput: int64(2), Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a = true ? 2 : 1`, RunOutput: int64(2), Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a = false ? 2 : 1`, RunOutput: int64(1), Output: map[string]interface{}{"a": int64(1)}},
- {Script: `a = "true" ? 2 : 1`, RunOutput: int64(2), Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a = "false" ? 2 : 1`, RunOutput: int64(1), Output: map[string]interface{}{"a": int64(1)}},
- {Script: `a = "-1" ? 2 : 1`, RunOutput: int64(2), Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a = "0" ? 2 : 1`, RunOutput: int64(1), Output: map[string]interface{}{"a": int64(1)}},
- {Script: `a = "0.0" ? 2 : 1`, RunOutput: int64(1), Output: map[string]interface{}{"a": int64(1)}},
- {Script: `a = "2" ? 2 : 1`, RunOutput: int64(2), Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a = b ? 2 : 1`, Input: map[string]interface{}{"b": int64(0)}, RunOutput: int64(1), Output: map[string]interface{}{"a": int64(1)}},
- {Script: `a = b ? 2 : 1`, Input: map[string]interface{}{"b": int64(2)}, RunOutput: int64(2), Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a = b ? 2 : 1`, Input: map[string]interface{}{"b": float64(0.0)}, RunOutput: int64(1), Output: map[string]interface{}{"a": int64(1)}},
- {Script: `a = b ? 2 : 1`, Input: map[string]interface{}{"b": float64(2.0)}, RunOutput: int64(2), Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a = b ? 2 : 1.0`, Input: map[string]interface{}{"b": float64(0.0)}, RunOutput: float64(1.0), Output: map[string]interface{}{"a": float64(1.0)}},
- {Script: `a = b ? 2 : 1.0`, Input: map[string]interface{}{"b": float64(0.1)}, RunOutput: int64(2), Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a = b ? 2 : 1.0`, Input: map[string]interface{}{"b": nil}, RunOutput: float64(1.0), Output: map[string]interface{}{"a": float64(1.0)}},
- {Script: `a = nil ? 2 : 1`, RunOutput: int64(1), Output: map[string]interface{}{"a": int64(1)}},
- {Script: `a = b ? 2 : 1`, Input: map[string]interface{}{"b": []interface{}{}}, RunOutput: int64(1), Output: map[string]interface{}{"a": int64(1)}},
- {Script: `a = b ? 2 : 1`, Input: map[string]interface{}{"b": map[string]interface{}{}}, RunOutput: int64(1), Output: map[string]interface{}{"a": int64(1)}},
- {Script: `a = [] ? 2 : 1`, RunOutput: int64(1), Output: map[string]interface{}{"a": int64(1)}},
- {Script: `a = [2] ? 2 : 1`, RunOutput: int64(2), Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a = b ? 2 : 1`, Input: map[string]interface{}{"b": map[string]interface{}{"test": int64(2)}}, RunOutput: int64(2), Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a = b["test"] ? 2 : 1`, Input: map[string]interface{}{"b": map[string]interface{}{"test": int64(2)}}, RunOutput: int64(2), Output: map[string]interface{}{"a": int64(2)}},
- {Script: `b = "test"; a = b ? 2 : "empty"`, RunOutput: int64(2), Output: map[string]interface{}{"a": int64(2)}},
- {Script: `b = "test"; a = b[1:3] ? 2 : "empty"`, RunOutput: int64(2), Output: map[string]interface{}{"a": int64(2)}},
- {Script: `b = "test"; a = b[2:2] ? 2 : "empty"`, RunOutput: "empty", Output: map[string]interface{}{"a": "empty"}},
- {Script: `b = "0.0"; a = false ? 2 : b ? 3 : 1`, RunOutput: int64(1), Output: map[string]interface{}{"a": int64(1)}},
- {Script: `b = "true"; a = false ? 2 : b ? 3 : 1`, RunOutput: int64(3), Output: map[string]interface{}{"a": int64(3)}},
- }
- runTests(t, tests, nil, &Options{Debug: true})
- }
- func TestNilCoalescingOperator(t *testing.T) {
- t.Parallel()
- tests := []Test{
- {Script: `nil ?? nil`, RunOutput: nil},
- {Script: `false ?? nil`, RunOutput: false},
- {Script: `true ?? nil`, RunOutput: true},
- {Script: `nil ?? false`, RunOutput: false},
- {Script: `nil ?? true`, RunOutput: true},
- {Script: `1 ?? nil`, RunOutput: int64(1)},
- {Script: `1 ?? 2`, RunOutput: int64(1)},
- {Script: `nil ?? 1`, RunOutput: int64(1)},
- {Script: `a ?? 1`, RunOutput: int64(1)},
- {Script: `a ?? b`, RunError: fmt.Errorf("undefined symbol 'b'")},
- {Script: `a ?? 2`, Input: map[string]interface{}{"a": int64(1)}, RunOutput: int64(1), Output: map[string]interface{}{"a": int64(1)}},
- {Script: `a ?? b`, Input: map[string]interface{}{"a": int64(1)}, RunOutput: int64(1), Output: map[string]interface{}{"a": int64(1)}},
- {Script: `a ?? b`, Input: map[string]interface{}{"a": int64(1), "b": int64(2)}, RunOutput: int64(1), Output: map[string]interface{}{"a": int64(1), "b": int64(2)}},
- {Script: `a ?? b`, Input: map[string]interface{}{"a": nil, "b": int64(2)}, RunOutput: int64(2), Output: map[string]interface{}{"a": nil, "b": int64(2)}},
- {Script: `[] ?? 1`, RunOutput: []interface{}{}},
- {Script: `{} ?? 1`, RunOutput: map[interface{}]interface{}{}},
- // test nil array and map
- {Script: `a ?? 5`, Input: map[string]interface{}{"a": testSliceEmpty}, RunOutput: int64(5), Output: map[string]interface{}{"a": testSliceEmpty}},
- {Script: `a ?? 6`, Input: map[string]interface{}{"a": testMapEmpty}, RunOutput: int64(6), Output: map[string]interface{}{"a": testMapEmpty}},
- }
- runTests(t, tests, nil, &Options{Debug: true})
- }
- func TestIf(t *testing.T) {
- t.Parallel()
- tests := []Test{
- {Script: `if 1++ {}`, RunError: fmt.Errorf("invalid operation")},
- {Script: `if false {} else if 1++ {}`, RunError: fmt.Errorf("invalid operation")},
- {Script: `if false {} else if true { 1++ }`, RunError: fmt.Errorf("invalid operation")},
- {Script: `if true {}`, Input: map[string]interface{}{"a": nil}, RunOutput: nil, Output: map[string]interface{}{"a": nil}},
- {Script: `if true {}`, Input: map[string]interface{}{"a": true}, RunOutput: nil, Output: map[string]interface{}{"a": true}},
- {Script: `if true {}`, Input: map[string]interface{}{"a": int64(1)}, RunOutput: nil, Output: map[string]interface{}{"a": int64(1)}},
- {Script: `if true {}`, Input: map[string]interface{}{"a": float64(1.1)}, RunOutput: nil, Output: map[string]interface{}{"a": float64(1.1)}},
- {Script: `if true {}`, Input: map[string]interface{}{"a": "a"}, RunOutput: nil, Output: map[string]interface{}{"a": "a"}},
- {Script: `if true {a = nil}`, Input: map[string]interface{}{"a": int64(2)}, RunOutput: nil, Output: map[string]interface{}{"a": nil}},
- {Script: `if true {a = true}`, Input: map[string]interface{}{"a": int64(2)}, RunOutput: true, Output: map[string]interface{}{"a": true}},
- {Script: `if true {a = 1}`, Input: map[string]interface{}{"a": int64(2)}, RunOutput: int64(1), Output: map[string]interface{}{"a": int64(1)}},
- {Script: `if true {a = 1.1}`, Input: map[string]interface{}{"a": int64(2)}, RunOutput: float64(1.1), Output: map[string]interface{}{"a": float64(1.1)}},
- {Script: `if true {a = "a"}`, Input: map[string]interface{}{"a": int64(2)}, RunOutput: "a", Output: map[string]interface{}{"a": "a"}},
- {Script: `if a == 1 {a = 1}`, Input: map[string]interface{}{"a": int64(2)}, RunOutput: false, Output: map[string]interface{}{"a": int64(2)}},
- {Script: `if a == 2 {a = 1}`, Input: map[string]interface{}{"a": int64(2)}, RunOutput: int64(1), Output: map[string]interface{}{"a": int64(1)}},
- {Script: `if a == 1 {a = nil}`, Input: map[string]interface{}{"a": int64(2)}, RunOutput: false, Output: map[string]interface{}{"a": int64(2)}},
- {Script: `if a == 2 {a = nil}`, Input: map[string]interface{}{"a": int64(2)}, RunOutput: nil, Output: map[string]interface{}{"a": nil}},
- {Script: `if a == 1 {a = 1} else {a = 3}`, Input: map[string]interface{}{"a": int64(2)}, RunOutput: int64(3), Output: map[string]interface{}{"a": int64(3)}},
- {Script: `if a == 2 {a = 1} else {a = 3}`, Input: map[string]interface{}{"a": int64(2)}, RunOutput: int64(1), Output: map[string]interface{}{"a": int64(1)}},
- {Script: `if a == 1 {a = 1} else if a == 3 {a = 3}`, Input: map[string]interface{}{"a": int64(2)}, RunOutput: false, Output: map[string]interface{}{"a": int64(2)}},
- {Script: `if a == 1 {a = 1} else if a == 2 {a = 3}`, Input: map[string]interface{}{"a": int64(2)}, RunOutput: int64(3), Output: map[string]interface{}{"a": int64(3)}},
- {Script: `if a == 1 {a = 1} else if a == 3 {a = 3} else {a = 4}`, Input: map[string]interface{}{"a": int64(2)}, RunOutput: int64(4), Output: map[string]interface{}{"a": int64(4)}},
- {Script: `if a == 1 {a = 1} else {a = nil}`, Input: map[string]interface{}{"a": int64(2)}, RunOutput: nil, Output: map[string]interface{}{"a": nil}},
- {Script: `if a == 2 {a = nil} else {a = 3}`, Input: map[string]interface{}{"a": int64(2)}, RunOutput: nil, Output: map[string]interface{}{"a": nil}},
- {Script: `if a == 1 {a = nil} else if a == 3 {a = nil}`, Input: map[string]interface{}{"a": int64(2)}, RunOutput: false, Output: map[string]interface{}{"a": int64(2)}},
- {Script: `if a == 1 {a = 1} else if a == 2 {a = nil}`, Input: map[string]interface{}{"a": int64(2)}, RunOutput: nil, Output: map[string]interface{}{"a": nil}},
- {Script: `if a == 1 {a = 1} else if a == 3 {a = 3} else {a = nil}`, Input: map[string]interface{}{"a": int64(2)}, RunOutput: nil, Output: map[string]interface{}{"a": nil}},
- {Script: `if a == 1 {a = 1} else if a == 3 {a = 3} else if a == 4 {a = 4} else {a = 5}`, Input: map[string]interface{}{"a": int64(2)}, RunOutput: int64(5), Output: map[string]interface{}{"a": int64(5)}},
- {Script: `if a == 1 {a = 1} else if a == 3 {a = 3} else if a == 4 {a = 4} else {a = nil}`, Input: map[string]interface{}{"a": int64(2)}, RunOutput: nil, Output: map[string]interface{}{"a": nil}},
- {Script: `if a == 1 {a = 1} else if a == 3 {a = 3} else if a == 2 {a = 4} else {a = 5}`, Input: map[string]interface{}{"a": int64(2)}, RunOutput: int64(4), Output: map[string]interface{}{"a": int64(4)}},
- {Script: `if a == 1 {a = 1} else if a == 3 {a = 3} else if a == 2 {a = nil} else {a = 5}`, Input: map[string]interface{}{"a": int64(2)}, RunOutput: nil, Output: map[string]interface{}{"a": nil}},
- // check scope
- {Script: `a = 1; if a == 1 { b = 2 }; b`, RunError: fmt.Errorf("undefined symbol 'b'"), Output: map[string]interface{}{"a": int64(1)}},
- {Script: `a = 1; if a == 2 { b = 3 } else { b = 4 }; b`, RunError: fmt.Errorf("undefined symbol 'b'"), Output: map[string]interface{}{"a": int64(1)}},
- {Script: `a = 1; if a == 2 { b = 3 } else if a == 1 { b = 4 }; b`, RunError: fmt.Errorf("undefined symbol 'b'"), Output: map[string]interface{}{"a": int64(1)}},
- {Script: `a = 1; if a == 2 { b = 4 } else if a == 5 { b = 6 } else if a == 1 { c = b }`, RunError: fmt.Errorf("undefined symbol 'b'"), Output: map[string]interface{}{"a": int64(1)}},
- {Script: `a = 1; if a == 2 { b = 4 } else if a == 5 { b = 6 } else if a == 1 { b = 7 }; b`, RunError: fmt.Errorf("undefined symbol 'b'"), Output: map[string]interface{}{"a": int64(1)}},
- }
- runTests(t, tests, nil, &Options{Debug: true})
- }
- func TestSwitch(t *testing.T) {
- t.Parallel()
- tests := []Test{
- // test parse errors
- {Script: `switch {}`, ParseError: fmt.Errorf("syntax error")},
- {Script: `a = 1; switch a; {}`, ParseError: fmt.Errorf("syntax error")},
- {Script: `a = 1; switch a = 2 {}`, ParseError: fmt.Errorf("syntax error")},
- {Script: `a = 1; switch a {default: return 6; default: return 7}`, ParseError: fmt.Errorf("multiple default statement"), RunOutput: int64(7)},
- {Script: `a = 1; switch a {case 1: return 5; default: return 6; default: return 7}`, ParseError: fmt.Errorf("multiple default statement"), RunOutput: int64(5)},
- // test run errors
- {Script: `a = 1; switch 1++ {}`, RunError: fmt.Errorf("invalid operation")},
- {Script: `a = 1; switch a {case 1++: return 2}`, RunError: fmt.Errorf("invalid operation")},
- // test no or empty cases
- {Script: `a = 1; switch a {}`, Output: map[string]interface{}{"a": int64(1)}},
- {Script: `a = 1; switch a {case: return 2}`, Output: map[string]interface{}{"a": int64(1)}},
- {Script: `a = 1; switch a {case: return 2; case: return 3}`, Output: map[string]interface{}{"a": int64(1)}},
- // test 1 case
- {Script: `a = 1; switch a {case 1: return 5}`, RunOutput: int64(5), Output: map[string]interface{}{"a": int64(1)}},
- {Script: `a = 2; switch a {case 1: return 5}`, Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a = 1; switch a {case 1,2: return 5}`, RunOutput: int64(5), Output: map[string]interface{}{"a": int64(1)}},
- {Script: `a = 2; switch a {case 1,2: return 5}`, RunOutput: int64(5), Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a = 3; switch a {case 1,2: return 5}`, Output: map[string]interface{}{"a": int64(3)}},
- {Script: `a = 1; switch a {case 1,2,3: return 5}`, RunOutput: int64(5), Output: map[string]interface{}{"a": int64(1)}},
- {Script: `a = 2; switch a {case 1,2,3: return 5}`, RunOutput: int64(5), Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a = 3; switch a {case 1,2,3: return 5}`, RunOutput: int64(5), Output: map[string]interface{}{"a": int64(3)}},
- {Script: `a = 4; switch a {case 1,2,3: return 5}`, Output: map[string]interface{}{"a": int64(4)}},
- {Script: `a = func() { return 1 }; switch a() {case 1: return 5}`, RunOutput: int64(5)},
- {Script: `a = func() { return 2 }; switch a() {case 1: return 5}`},
- {Script: `a = func() { return 5 }; b = 1; switch b {case 1: return a() }`, RunOutput: int64(5), Output: map[string]interface{}{"b": int64(1)}},
- {Script: `a = func() { return 6 }; b = 2; switch b {case 1: return a() }`, Output: map[string]interface{}{"b": int64(2)}},
- // test 2 cases
- {Script: `a = 1; switch a {case 1: return 5; case 2: return 6}`, RunOutput: int64(5), Output: map[string]interface{}{"a": int64(1)}},
- {Script: `a = 2; switch a {case 1: return 5; case 2: return 6}`, RunOutput: int64(6), Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a = 3; switch a {case 1: return 5; case 2: return 6}`, Output: map[string]interface{}{"a": int64(3)}},
- {Script: `a = 1; switch a {case 1: return 5; case 2,3: return 6}`, RunOutput: int64(5), Output: map[string]interface{}{"a": int64(1)}},
- {Script: `a = 2; switch a {case 1: return 5; case 2,3: return 6}`, RunOutput: int64(6), Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a = 3; switch a {case 1: return 5; case 2,3: return 6}`, RunOutput: int64(6), Output: map[string]interface{}{"a": int64(3)}},
- {Script: `a = 4; switch a {case 1: return 5; case 2,3: return 6}`, Output: map[string]interface{}{"a": int64(4)}},
- // test 3 cases
- {Script: `a = 1; switch a {case 1,2: return 5; case 3: return 6}`, RunOutput: int64(5), Output: map[string]interface{}{"a": int64(1)}},
- {Script: `a = 2; switch a {case 1,2: return 5; case 3: return 6}`, RunOutput: int64(5), Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a = 3; switch a {case 1,2: return 5; case 3: return 6}`, RunOutput: int64(6), Output: map[string]interface{}{"a": int64(3)}},
- {Script: `a = 4; switch a {case 1,2: return 5; case 3: return 6}`, Output: map[string]interface{}{"a": int64(4)}},
- {Script: `a = 1; switch a {case 1,2: return 5; case 2,3: return 6}`, RunOutput: int64(5), Output: map[string]interface{}{"a": int64(1)}},
- {Script: `a = 2; switch a {case 1,2: return 5; case 2,3: return 6}`, RunOutput: int64(5), Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a = 3; switch a {case 1,2: return 5; case 2,3: return 6}`, RunOutput: int64(6), Output: map[string]interface{}{"a": int64(3)}},
- {Script: `a = 4; switch a {case 1,2: return 5; case 2,3: return 6}`, Output: map[string]interface{}{"a": int64(4)}},
- // test default
- {Script: `a = 1; switch a {default: return 5}`, RunOutput: int64(5), Output: map[string]interface{}{"a": int64(1)}},
- {Script: `a = 1; switch a {case 1: return 5; case 2: return 6; default: return 7}`, RunOutput: int64(5), Output: map[string]interface{}{"a": int64(1)}},
- {Script: `a = 2; switch a {case 1: return 5; case 2: return 6; default: return 7}`, RunOutput: int64(6), Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a = 3; switch a {case 1: return 5; case 2: return 6; default: return 7}`, RunOutput: int64(7), Output: map[string]interface{}{"a": int64(3)}},
- {Script: `a = 1; switch a {case 1: return 5; case 2,3: return 6; default: return 7}`, RunOutput: int64(5), Output: map[string]interface{}{"a": int64(1)}},
- {Script: `a = 2; switch a {case 1: return 5; case 2,3: return 6; default: return 7}`, RunOutput: int64(6), Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a = 3; switch a {case 1: return 5; case 2,3: return 6; default: return 7}`, RunOutput: int64(6), Output: map[string]interface{}{"a": int64(3)}},
- {Script: `a = 4; switch a {case 1: return 5; case 2,3: return 6; default: return 7}`, RunOutput: int64(7), Output: map[string]interface{}{"a": int64(4)}},
- {Script: `a = 1; switch a {case 1,2: return 5; case 3: return 6; default: return 7}`, RunOutput: int64(5), Output: map[string]interface{}{"a": int64(1)}},
- {Script: `a = 2; switch a {case 1,2: return 5; case 3: return 6; default: return 7}`, RunOutput: int64(5), Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a = 3; switch a {case 1,2: return 5; case 3: return 6; default: return 7}`, RunOutput: int64(6), Output: map[string]interface{}{"a": int64(3)}},
- {Script: `a = 4; switch a {case 1,2: return 5; case 3: return 6; default: return 7}`, RunOutput: int64(7), Output: map[string]interface{}{"a": int64(4)}},
- // test scope
- {Script: `a = 1; switch a {case 1: a = 5}`, RunOutput: int64(5), Output: map[string]interface{}{"a": int64(5)}},
- {Script: `a = 2; switch a {case 1: a = 5}`, Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a = 1; b = 5; switch a {case 1: b = 6}`, RunOutput: int64(6), Output: map[string]interface{}{"a": int64(1), "b": int64(6)}},
- {Script: `a = 2; b = 5; switch a {case 1: b = 6}`, Output: map[string]interface{}{"a": int64(2), "b": int64(5)}},
- {Script: `a = 1; b = 5; switch a {case 1: b = 6; default: b = 7}`, RunOutput: int64(6), Output: map[string]interface{}{"a": int64(1), "b": int64(6)}},
- {Script: `a = 2; b = 5; switch a {case 1: b = 6; default: b = 7}`, RunOutput: int64(7), Output: map[string]interface{}{"a": int64(2), "b": int64(7)}},
- // test scope without define b
- {Script: `a = 1; switch a {case 1: b = 5}`, RunOutput: int64(5), Output: map[string]interface{}{"a": int64(1)}},
- {Script: `a = 2; switch a {case 1: b = 5}`, Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a = 1; switch a {case 1: b = 5}; b`, RunError: fmt.Errorf("undefined symbol 'b'"), RunOutput: nil, Output: map[string]interface{}{"a": int64(1)}},
- {Script: `a = 2; switch a {case 1: b = 5}; b`, RunError: fmt.Errorf("undefined symbol 'b'"), RunOutput: nil, Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a = 1; switch a {case 1: b = 5; default: b = 6}`, RunOutput: int64(5), Output: map[string]interface{}{"a": int64(1)}},
- {Script: `a = 2; switch a {case 1: b = 5; default: b = 6}`, RunOutput: int64(6), Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a = 1; switch a {case 1: b = 5; default: b = 6}; b`, RunError: fmt.Errorf("undefined symbol 'b'"), RunOutput: nil, Output: map[string]interface{}{"a": int64(1)}},
- {Script: `a = 2; switch a {case 1: b = 5; default: b = 6}; b`, RunError: fmt.Errorf("undefined symbol 'b'"), RunOutput: nil, Output: map[string]interface{}{"a": int64(2)}},
- // test new lines
- {Script: `
- a = 1;
- switch a {
- case 1:
- return 1
- }`, RunOutput: int64(1)},
- }
- runTests(t, tests, nil, &Options{Debug: true})
- }
- func TestForLoop(t *testing.T) {
- t.Parallel()
- tests := []Test{
- {Script: `for in [1] { }`, ParseError: fmt.Errorf("missing identifier")},
- {Script: `for a, b, c in [1] { }`, ParseError: fmt.Errorf("too many identifiers")},
- {Script: `break`, RunError: fmt.Errorf("unexpected break statement")},
- {Script: `continue`, RunError: fmt.Errorf("unexpected continue statement")},
- {Script: `for 1++ { }`, RunError: fmt.Errorf("invalid operation")},
- {Script: `for { 1++ }`, RunError: fmt.Errorf("invalid operation")},
- {Script: `for a in 1++ { }`, RunError: fmt.Errorf("invalid operation")},
- {Script: `for { break }`, RunOutput: nil},
- {Script: `for {a = 1; if a == 1 { break } }`, RunOutput: nil},
- {Script: `a = 1; for { if a == 1 { break } }`, RunOutput: nil, Output: map[string]interface{}{"a": int64(1)}},
- {Script: `a = 1; for { if a == 1 { break }; a++ }`, RunOutput: nil, Output: map[string]interface{}{"a": int64(1)}},
- {Script: `a = 1; for { if a == 3 { break }; a++ }`, RunOutput: nil, Output: map[string]interface{}{"a": int64(3)}},
- {Script: `a = 1; for { if a == 1 { return }; a++ }`, RunOutput: nil, Output: map[string]interface{}{"a": int64(1)}},
- {Script: `a = 1; for { if a == 3 { return }; a++ }`, RunOutput: nil, Output: map[string]interface{}{"a": int64(3)}},
- {Script: `a = 1; for { if a == 1 { return 2 }; a++ }`, RunOutput: int64(2), Output: map[string]interface{}{"a": int64(1)}},
- {Script: `a = 1; for { if a == 3 { return 2 }; a++ }`, RunOutput: int64(2), Output: map[string]interface{}{"a": int64(3)}},
- {Script: `a = 1; for { if a == 3 { return 3 }; a++ }; return 2`, RunOutput: int64(3), Output: map[string]interface{}{"a": int64(3)}},
- {Script: `a = 1; for { a++; if a == 2 { continue } else { break } }`, RunOutput: nil, Output: map[string]interface{}{"a": int64(3)}},
- {Script: `a = 1; for { a++; if a == 2 { continue }; if a == 3 { break } }`, RunOutput: nil, Output: map[string]interface{}{"a": int64(3)}},
- {Script: `for a in [1] { if a == 1 { break } }`, RunOutput: nil},
- {Script: `for a in [1, 2] { if a == 2 { break } }`, RunOutput: nil},
- {Script: `for a in [1, 2, 3] { if a == 3 { break } }`, RunOutput: nil},
- {Script: `for a in [1, 2, 3] { if a == 2 { return 2 } }; return 3`, RunOutput: int64(2)},
- {Script: `a = [1]; for b in a { if b == 1 { break } }`, RunOutput: nil, Output: map[string]interface{}{"a": []interface{}{int64(1)}}},
- {Script: `a = [1, 2]; for b in a { if b == 2 { break } }`, RunOutput: nil, Output: map[string]interface{}{"a": []interface{}{int64(1), int64(2)}}},
- {Script: `a = [1, 2, 3]; for b in a { if b == 3 { break } }`, RunOutput: nil, Output: map[string]interface{}{"a": []interface{}{int64(1), int64(2), int64(3)}}},
- {Script: `a = [1]; b = 0; for c in a { b = c }`, RunOutput: nil, Output: map[string]interface{}{"a": []interface{}{int64(1)}, "b": int64(1)}},
- {Script: `a = [1, 2]; b = 0; for c in a { b = c }`, RunOutput: nil, Output: map[string]interface{}{"a": []interface{}{int64(1), int64(2)}, "b": int64(2)}},
- {Script: `a = [1, 2, 3]; b = 0; for c in a { b = c }`, RunOutput: nil, Output: map[string]interface{}{"a": []interface{}{int64(1), int64(2), int64(3)}, "b": int64(3)}},
- {Script: `a = 1; for a < 2 { a++ }`, RunOutput: nil, Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a = 1; for a < 3 { a++ }`, RunOutput: nil, Output: map[string]interface{}{"a": int64(3)}},
- {Script: `a = 1; for nil { a++; if a > 2 { break } }`, RunOutput: nil, Output: map[string]interface{}{"a": int64(1)}},
- {Script: `a = 1; for nil { a++; if a > 3 { break } }`, RunOutput: nil, Output: map[string]interface{}{"a": int64(1)}},
- {Script: `a = 1; for true { a++; if a > 2 { break } }`, RunOutput: nil, Output: map[string]interface{}{"a": int64(3)}},
- {Script: `a = 1; for true { a++; if a > 3 { break } }`, RunOutput: nil, Output: map[string]interface{}{"a": int64(4)}},
- {Script: `func x() { return [1] }; for b in x() { if b == 1 { break } }`, RunOutput: nil},
- {Script: `func x() { return [1, 2] }; for b in x() { if b == 2 { break } }`, RunOutput: nil},
- {Script: `func x() { return [1, 2, 3] }; for b in x() { if b == 3 { break } }`, RunOutput: nil},
- {Script: `func x() { a = 1; for { if a == 1 { return } } }; x()`, RunOutput: nil},
- {Script: `func x() { a = 1; for { if a == 1 { return nil } } }; x()`, RunOutput: nil},
- {Script: `func x() { a = 1; for { if a == 1 { return true } } }; x()`, RunOutput: true},
- {Script: `func x() { a = 1; for { if a == 1 { return 1 } } }; x()`, RunOutput: int64(1)},
- {Script: `func x() { a = 1; for { if a == 1 { return 1.1 } } }; x()`, RunOutput: float64(1.1)},
- {Script: `func x() { a = 1; for { if a == 1 { return "a" } } }; x()`, RunOutput: "a"},
- {Script: `func x() { for a in [1, 2, 3] { if a == 3 { return } } }; x()`, RunOutput: nil},
- {Script: `func x() { for a in [1, 2, 3] { if a == 3 { return 3 } }; return 2 }; x()`, RunOutput: int64(3)},
- {Script: `func x() { for a in [1, 2, 3] { if a == 1 { continue } } }; x()`, RunOutput: nil},
- {Script: `func x() { for a in [1, 2, 3] { if a == 1 { continue }; if a == 3 { return } } }; x()`, RunOutput: nil},
- {Script: `func x() { return [1, 2] }; a = 1; for i in x() { a++ }`, RunOutput: nil, Output: map[string]interface{}{"a": int64(3)}},
- {Script: `func x() { return [1, 2, 3] }; a = 1; for i in x() { a++ }`, RunOutput: nil, Output: map[string]interface{}{"a": int64(4)}},
- {Script: `for a = 1; nil; nil { return }`},
- // TOFIX:
- // {Script: `for a, b = 1; nil; nil { return }`},
- // {Script: `for a, b = 1, 2; nil; nil { return }`},
- {Script: `var a = 1; for ; ; { return a }`, RunOutput: int64(1)},
- {Script: `var a = 1; for ; ; a++ { return a }`, RunOutput: int64(1)},
- {Script: `var a = 1; for ; a > 0 ; { return a }`, RunOutput: int64(1)},
- {Script: `var a = 1; for ; a > 0 ; a++ { return a }`, RunOutput: int64(1)},
- {Script: `for var a = 1 ; ; { return a }`, RunOutput: int64(1)},
- {Script: `for var a = 1 ; ; a++ { return a }`, RunOutput: int64(1)},
- {Script: `for var a = 1 ; a > 0 ; { return a }`, RunOutput: int64(1)},
- {Script: `for var a = 1 ; a > 0 ; a++ { return a }`, RunOutput: int64(1)},
- {Script: `for var a = 1; nil; nil { return }`},
- {Script: `for var a = 1, 2; nil; nil { return }`},
- {Script: `for var a, b = 1; nil; nil { return }`},
- {Script: `for var a, b = 1, 2; nil; nil { return }`},
- {Script: `for a.b = 1; nil; nil { return }`, RunError: fmt.Errorf("undefined symbol 'a'")},
- {Script: `for a = 1; nil; nil { if a == 1 { break } }`, RunOutput: nil},
- {Script: `for a = 1; nil; nil { if a == 2 { break }; a++ }`, RunOutput: nil},
- {Script: `for a = 1; nil; nil { a++; if a == 3 { break } }`, RunOutput: nil},
- {Script: `for a = 1; a < 1; nil { }`, RunOutput: nil},
- {Script: `for a = 1; a > 1; nil { }`, RunOutput: nil},
- {Script: `for a = 1; a == 1; nil { break }`, RunOutput: nil},
- {Script: `for a = 1; a == 1; a++ { }`, RunOutput: nil},
- {Script: `for a = 1; a < 2; a++ { }`, RunOutput: nil},
- {Script: `for a = 1; a < 3; a++ { }`, RunOutput: nil},
- {Script: `for a = 1; a < 5; a++ { if a == 3 { return 3 } }; return 2`, RunOutput: int64(3)},
- {Script: `a = 1; for b = 1; a < 1; a++ { }`, RunOutput: nil, Output: map[string]interface{}{"a": int64(1)}},
- {Script: `a = 1; for b = 1; a < 2; a++ { }`, RunOutput: nil, Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a = 1; for b = 1; a < 3; a++ { }`, RunOutput: nil, Output: map[string]interface{}{"a": int64(3)}},
- {Script: `a = 1; for b = 1; a < 1; a++ { if a == 1 { continue } }`, RunOutput: nil, Output: map[string]interface{}{"a": int64(1)}},
- {Script: `a = 1; for b = 1; a < 2; a++ { if a == 1 { continue } }`, RunOutput: nil, Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a = 1; for b = 1; a < 3; a++ { if a == 1 { continue } }`, RunOutput: nil, Output: map[string]interface{}{"a": int64(3)}},
- {Script: `a = 1; for b = 1; a < 1; a++ { if a == 1 { break } }`, RunOutput: nil, Output: map[string]interface{}{"a": int64(1)}},
- {Script: `a = 1; for b = 1; a < 2; a++ { if a == 1 { break } }`, RunOutput: nil, Output: map[string]interface{}{"a": int64(1)}},
- {Script: `a = 1; for b = 1; a < 3; a++ { if a == 1 { break } }`, RunOutput: nil, Output: map[string]interface{}{"a": int64(1)}},
- {Script: `a = 1; for b = 1; a < 1; a++ { if a == 2 { break } }`, RunOutput: nil, Output: map[string]interface{}{"a": int64(1)}},
- {Script: `a = 1; for b = 1; a < 2; a++ { if a == 2 { break } }`, RunOutput: nil, Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a = 1; for b = 1; a < 3; a++ { if a == 2 { break } }`, RunOutput: nil, Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a = 1; for b = 1; a < 1; a++ { if a == 3 { break } }`, RunOutput: nil, Output: map[string]interface{}{"a": int64(1)}},
- {Script: `a = 1; for b = 1; a < 2; a++ { if a == 3 { break } }`, RunOutput: nil, Output: map[string]interface{}{"a": int64(2)}},
- {Script: `a = 1; for b = 1; a < 3; a++ { if a == 3 { break } }`, RunOutput: nil, Output: map[string]interface{}{"a": int64(3)}},
- {Script: `a = ["123", "456", "789"]; b = ""; for i = 0; i < len(a); i++ { b += a[i][len(a[i]) - 2:]; b += a[i][:len(a[i]) - 2] }`, RunOutput: nil, Output: map[string]interface{}{"a": []interface{}{"123", "456", "789"}, "b": "231564897"}},
- {Script: `a = [[["123"], ["456"]], [["789"]]]; b = ""; for i = 0; i < len(a); i++ { for j = 0; j < len(a[i]); j++ { for k = 0; k < len(a[i][j]); k++ { for l = 0; l < len(a[i][j][k]); l++ { b += a[i][j][k][l] + "-" } } } }`,
- RunOutput: nil, Output: map[string]interface{}{"a": []interface{}{[]interface{}{[]interface{}{"123"}, []interface{}{"456"}}, []interface{}{[]interface{}{"789"}}}, "b": "1-2-3-4-5-6-7-8-9-"}},
- {Script: `func x() { for a = 1; a < 3; a++ { if a == 1 { return a } } }; x()`, RunOutput: int64(1)},
- {Script: `func x() { for a = 1; a < 3; a++ { if a == 2 { return a } } }; x()`, RunOutput: int64(2)},
- {Script: `func x() { for a = 1; a < 3; a++ { if a == 3 { return a } } }; x()`, RunOutput: nil},
- {Script: `func x() { for a = 1; a < 3; a++ { if a == 4 { return a } } }; x()`, RunOutput: nil},
- {Script: `func x() { a = 1; for b = 1; a < 3; a++ { if a == 1 { continue } }; return a }; x()`, RunOutput: int64(3)},
- {Script: `func x() { a = 1; for b = 1; a < 3; a++ { if a == 2 { continue } }; return a }; x()`, RunOutput: int64(3)},
- {Script: `func x() { a = 1; for b = 1; a < 3; a++ { if a == 3 { continue } }; return a }; x()`, RunOutput: int64(3)},
- {Script: `func x() { a = 1; for b = 1; a < 3; a++ { if a == 4 { continue } }; return a }; x()`, RunOutput: int64(3)},
- {Script: `b = 0; for i in a { b = i }`, Input: map[string]interface{}{"a": []interface{}{reflect.Value{}}}, RunOutput: nil, Output: map[string]interface{}{"a": []interface{}{reflect.Value{}}, "b": reflect.Value{}}},
- {Script: `b = 0; for i in a { b = i }`, Input: map[string]interface{}{"a": []interface{}{nil}}, RunOutput: nil, Output: map[string]interface{}{"a": []interface{}{nil}, "b": nil}},
- {Script: `b = 0; for i in a { b = i }`, Input: map[string]interface{}{"a": []interface{}{true}}, RunOutput: nil, Output: map[string]interface{}{"a": []interface{}{true}, "b": true}},
- {Script: `b = 0; for i in a { b = i }`, Input: map[string]interface{}{"a": []interface{}{int32(1)}}, RunOutput: nil, Output: map[string]interface{}{"a": []interface{}{int32(1)}, "b": int32(1)}},
- {Script: `b = 0; for i in a { b = i }`, Input: map[string]interface{}{"a": []interface{}{int64(1)}}, RunOutput: nil, Output: map[string]interface{}{"a": []interface{}{int64(1)}, "b": int64(1)}},
- {Script: `b = 0; for i in a { b = i }`, Input: map[string]interface{}{"a": []interface{}{float32(1.1)}}, RunOutput: nil, Output: map[string]interface{}{"a": []interface{}{float32(1.1)}, "b": float32(1.1)}},
- {Script: `b = 0; for i in a { b = i }`, Input: map[string]interface{}{"a": []interface{}{float64(1.1)}}, RunOutput: nil, Output: map[string]interface{}{"a": []interface{}{float64(1.1)}, "b": float64(1.1)}},
- {Script: `b = 0; for i in a { b = i }`, Input: map[string]interface{}{"a": []interface{}{interface{}(reflect.Value{})}}, RunOutput: nil, Output: map[string]interface{}{"a": []interface{}{interface{}(reflect.Value{})}, "b": interface{}(reflect.Value{})}},
- {Script: `b = 0; for i in a { b = i }`, Input: map[string]interface{}{"a": []interface{}{interface{}(nil)}}, RunOutput: nil, Output: map[string]interface{}{"a": []interface{}{interface{}(nil)}, "b": interface{}(nil)}},
- {Script: `b = 0; for i in a { b = i }`, Input: map[string]interface{}{"a": []interface{}{interface{}(true)}}, RunOutput: nil, Output: map[string]interface{}{"a": []interface{}{interface{}(true)}, "b": interface{}(true)}},
- {Script: `b = 0; for i in a { b = i }`, Input: map[string]interface{}{"a": []interface{}{interface{}(int32(1))}}, RunOutput: nil, Output: map[string]interface{}{"a": []interface{}{interface{}(int32(1))}, "b": interface{}(int32(1))}},
- {Script: `b = 0; for i in a { b = i }`, Input: map[string]interface{}{"a": []interface{}{interface{}(int64(1))}}, RunOutput: nil, Output: map[string]interface{}{"a": []interface{}{interface{}(int64(1))}, "b": interface{}(int64(1))}},
- {Script: `b = 0; for i in a { b = i }`, Input: map[string]interface{}{"a": []interface{}{interface{}(float32(1.1))}}, RunOutput: nil, Output: map[string]interface{}{"a": []interface{}{interface{}(float32(1.1))}, "b": interface{}(float32(1.1))}},
- {Script: `b = 0; for i in a { b = i }`, Input: map[string]interface{}{"a": []interface{}{interface{}(float64(1.1))}}, RunOutput: nil, Output: map[string]interface{}{"a": []interface{}{interface{}(float64(1.1))}, "b": interface{}(float64(1.1))}},
- {Script: `b = 0; for i in a { b = i }`, Input: map[string]interface{}{"a": interface{}([]interface{}{nil})}, RunOutput: nil, Output: map[string]interface{}{"a": interface{}([]interface{}{nil}), "b": nil}},
- {Script: `for i in nil { }`, RunError: fmt.Errorf("for cannot loop over type interface")},
- {Script: `for i in true { }`, RunError: fmt.Errorf("for cannot loop over type bool")},
- {Script: `for i in a { }`, Input: map[string]interface{}{"a": reflect.Value{}}, RunError: fmt.Errorf("for cannot loop over type struct"), Output: map[string]interface{}{"a": reflect.Value{}}},
- {Script: `for i in a { }`, Input: map[string]interface{}{"a": interface{}(nil)}, RunError: fmt.Errorf("for cannot loop over type interface"), Output: map[string]interface{}{"a": interface{}(nil)}},
- {Script: `for i in a { }`, Input: map[string]interface{}{"a": interface{}(true)}, RunError: fmt.Errorf("for cannot loop over type bool"), Output: map[string]interface{}{"a": interface{}(true)}},
- {Script: `for i in [1, 2, 3] { b++ }`, RunError: fmt.Errorf("undefined symbol 'b'")},
- {Script: `for a = 1; a < 3; a++ { b++ }`, RunError: fmt.Errorf("undefined symbol 'b'")},
- {Script: `for a = b; a < 3; a++ { }`, RunError: fmt.Errorf("undefined symbol 'b'")},
- {Script: `for a = 1; b < 3; a++ { }`, RunError: fmt.Errorf("undefined symbol 'b'")},
- {Script: `for a = 1; a < 3; b++ { }`, RunError: fmt.Errorf("undefined symbol 'b'")},
- {Script: `a = 1; b = [{"c": "c"}]; for i in b { a = i }`, RunOutput: nil, Output: map[string]interface{}{"a": map[interface{}]interface{}{"c": "c"}, "b": []interface{}{map[interface{}]interface{}{"c": "c"}}}},
- {Script: `a = 1; b = {"x": [{"y": "y"}]}; for i in b.x { a = i }`, RunOutput: nil, Output: map[string]interface{}{"a": map[interface{}]interface{}{"y": "y"}, "b": map[interface{}]interface{}{"x": []interface{}{map[interface{}]interface{}{"y": "y"}}}}},
- {Script: `a = {}; b = 1; for i in a { b = i }; b`, RunOutput: int64(1), Output: map[string]interface{}{"a": map[interface{}]interface{}{}, "b": int64(1)}},
- {Script: `a = {"x": 2}; b = 1; for i in a { b = i }; b`, RunOutput: "x", Output: map[string]interface{}{"a": map[interface{}]interface{}{"x": int64(2)}, "b": "x"}},
- {Script: `a = {"x": 2, "y": 3}; b = 0; for i in a { b++ }; b`, RunOutput: int64(2), Output: map[string]interface{}{"a": map[interface{}]interface{}{"x": int64(2), "y": int64(3)}, "b": int64(2)}},
- {Script: `a = {"x": 2, "y": 3}; for i in a { b++ }`, RunError: fmt.Errorf("undefined symbol 'b'"), Output: map[string]interface{}{"a": map[interface{}]interface{}{"x": int64(2), "y": int64(3)}}},
- {Script: `a = {"x": 2, "y": 3}; b = 0; for i in a { if i == "x" { continue }; b = i }; b`, RunOutput: "y", Output: map[string]interface{}{"a": map[interface{}]interface{}{"x": int64(2), "y": int64(3)}, "b": "y"}},
- {Script: `a = {"x": 2, "y": 3}; b = 0; for i in a { if i == "y" { continue }; b = i }; b`, RunOutput: "x", Output: map[string]interface{}{"a": map[interface{}]interface{}{"x": int64(2), "y": int64(3)}, "b": "x"}},
- {Script: `a = {"x": 2, "y": 3}; for i in a { if i == "x" { return 1 } }`, RunOutput: int64(1), Output: map[string]interface{}{"a": map[interface{}]interface{}{"x": int64(2), "y": int64(3)}}},
- {Script: `a = {"x": 2, "y": 3}; for i in a { if i == "y" { return 2 } }`, RunOutput: int64(2), Output: map[string]interface{}{"a": map[interface{}]interface{}{"x": int64(2), "y": int64(3)}}},
- {Script: `a = {"x": 2, "y": 3}; b = 0; for i in a { if i == "x" { break }; b++ }; if b > 1 { return false } else { return true }`, RunOutput: true, Output: map[string]interface{}{"a": map[interface{}]interface{}{"x": int64(2), "y": int64(3)}}},
- {Script: `a = {"x": 2, "y": 3}; b = 0; for i in a { if i == "y" { break }; b++ }; if b > 1 { return false } else { return true }`, RunOutput: true, Output: map[string]interface{}{"a": map[interface{}]interface{}{"x": int64(2), "y": int64(3)}}},
- {Script: `a = {"x": 2, "y": 3}; b = 1; for i in a { if (i == "x" || i == "y") { break }; b++ }; b`, RunOutput: int64(1), Output: map[string]interface{}{"a": map[interface{}]interface{}{"x": int64(2), "y": int64(3)}, "b": int64(1)}},
- {Script: `a = ["123", "456", "789"]; b = ""; for v in a { b += v[len(v) - 2:]; b += v[:len(v) - 2] }`, RunOutput: nil, Output: map[string]interface{}{"a": []interface{}{"123", "456", "789"}, "b": "231564897"}},
- {Script: `a = [[["123"], ["456"]], [["789"]]]; b = ""; for x in a { for y in x { for z in y { for i = 0; i < len(z); i++ { b += z[i] + "-" } } } }`,
- RunOutput: nil, Output: map[string]interface{}{"a": []interface{}{[]interface{}{[]interface{}{"123"}, []interface{}{"456"}}, []interface{}{[]interface{}{"789"}}}, "b": "1-2-3-4-5-6-7-8-9-"}},
- {Script: `a = {"x": 2}; b = 0; for k, v in a { b = k }; b`, RunOutput: "x", Output: map[string]interface{}{"a": map[interface{}]interface{}{"x": int64(2)}, "b": "x"}},
- {Script: `a = {"x": 2}; b = 0; for k, v in a { b = v }; b`, RunOutput: int64(2), Output: map[string]interface{}{"a": map[interface{}]interface{}{"x": int64(2)}, "b": int64(2)}},
- {Script: `a = make(chan int64, 2); a <- 1; v = 0; for val in a { v = val; break; }; v`, RunOutput: int64(1), Output: map[string]interface{}{"v": int64(1)}},
- {Script: `a = make(chan int64, 4); a <- 1; a <- 2; a <- 3; for i in a { if i == 2 { return 2 } }; return 4`, RunOutput: int64(2)},
- {Script: `a = make(chan int64, 2); a <- 1; for i in a { if i < 4 { a <- i + 1; continue }; return 4 }; return 6`, RunOutput: int64(4)},
- // test non-buffer and go func
- {Script: `a = make(chan int64); go func() { a <- 1; a <- 2; a <- 3 }(); b = []; for i in a { b += i; if i > 2 { break } }`, RunOutput: nil, Output: map[string]interface{}{"b": []interface{}{int64(1), int64(2), int64(3)}}},
- {Script: `a = make(chan int64); go func() { a <- 1; a <- 2; a <- 3; close(a) }(); b = []; for i in a { b += i }`, RunOutput: nil, Output: map[string]interface{}{"b": []interface{}{int64(1), int64(2), int64(3)}}},
- }
- runTests(t, tests, nil, &Options{Debug: true})
- }
- func TestItemInList(t *testing.T) {
- t.Parallel()
- tests := []Test{
- {Script: `"a" in ["a"]`, RunOutput: true},
- {Script: `"a" in ["b"]`, RunOutput: false},
- {Script: `"a" in ["c", "b", "a"]`, RunOutput: true},
- {Script: `"a" in ["a", "b", 1]`, RunOutput: true},
- {Script: `"a" in l`, Input: map[string]interface{}{"l": []interface{}{"a"}}, RunOutput: true},
- {Script: `"a" in l`, Input: map[string]interface{}{"l": []interface{}{"b"}}, RunOutput: false},
- {Script: `"a" in l`, Input: map[string]interface{}{"l": []interface{}{"c", "b", "a"}}, RunOutput: true},
- {Script: `"a" in l`, Input: map[string]interface{}{"l": []interface{}{"a", "b", 1}}, RunOutput: true},
- {Script: `1 in [1]`, RunOutput: true},
- {Script: `1 in [2]`, RunOutput: false},
- {Script: `1 in [3, 2, 1]`, RunOutput: true},
- {Script: `1 in ["1"]`, RunOutput: true},
- {Script: `1 in l`, Input: map[string]interface{}{"l": []interface{}{1}}, RunOutput: true},
- {Script: `"1" in l`, Input: map[string]interface{}{"l": []interface{}{1}}, RunOutput: true},
- {Script: `1 in l`, Input: map[string]interface{}{"l": []interface{}{2}}, RunOutput: false},
- {Script: `1 in l`, Input: map[string]interface{}{"l": []interface{}{3, 2, 1}}, RunOutput: true},
- {Script: `1 in l`, Input: map[string]interface{}{"l": []interface{}{"1"}}, RunOutput: true},
- {Script: `0.9 in [1]`, Input: map[string]interface{}{"l": []interface{}{1}}, RunOutput: false},
- {Script: `1.0 in [1]`, Input: map[string]interface{}{"l": []interface{}{1}}, RunOutput: true},
- {Script: `1.1 in [1]`, Input: map[string]interface{}{"l": []interface{}{1}}, RunOutput: false},
- {Script: `1 in [0.9]`, Input: map[string]interface{}{"l": []interface{}{0.9}}, RunOutput: false},
- {Script: `1 in [1.0]`, Input: map[string]interface{}{"l": []interface{}{1.0}}, RunOutput: true},
- {Script: `1 in [1.1]`, Input: map[string]interface{}{"l": []interface{}{1.1}}, RunOutput: false},
- {Script: `0.9 in [1]`, Input: map[string]interface{}{"l": []interface{}{1}}, RunOutput: false},
- {Script: `1.0 in [1]`, Input: map[string]interface{}{"l": []interface{}{1}}, RunOutput: true},
- {Script: `1.1 in [1]`, Input: map[string]interface{}{"l": []interface{}{1}}, RunOutput: false},
- {Script: `1 in [0.9]`, Input: map[string]interface{}{"l": []interface{}{0.9}}, RunOutput: false},
- {Script: `1 in [1.0]`, Input: map[string]interface{}{"l": []interface{}{1.0}}, RunOutput: true},
- {Script: `1 in [1.1]`, Input: map[string]interface{}{"l": []interface{}{1.1}}, RunOutput: false},
- {Script: `true in ["true"]`, RunOutput: true},
- {Script: `true in [true]`, RunOutput: true},
- {Script: `true in [true, false]`, RunOutput: true},
- {Script: `false in [false, true]`, RunOutput: true},
- {Script: `true in l`, Input: map[string]interface{}{"l": []interface{}{"true"}}, RunOutput: true},
- {Script: `true in l`, Input: map[string]interface{}{"l": []interface{}{true}}, RunOutput: true},
- {Script: `true in l`, Input: map[string]interface{}{"l": []interface{}{true, false}}, RunOutput: true},
- {Script: `false in l`, Input: map[string]interface{}{"l": []interface{}{false, true}}, RunOutput: true},
- {Script: `"a" in ["b", "a", "c"][1:]`, RunOutput: true},
- {Script: `"a" in ["b", "a", "c"][:1]`, RunOutput: false},
- {Script: `"a" in ["b", "a", "c"][1:2]`, RunOutput: true},
- {Script: `l = ["b", "a", "c"];"a" in l[1:]`, RunOutput: true},
- {Script: `l = ["b", "a", "c"];"a" in l[:1]`, RunOutput: false},
- {Script: `l = ["b", "a", "c"];"a" in l[1:2]`, RunOutput: true},
- {Script: `"a" in l[1:]`, Input: map[string]interface{}{"l": []interface{}{"b", "a", "c"}}, RunOutput: true},
- {Script: `"a" in l[:1]`, Input: map[string]interface{}{"l": []interface{}{"b", "a", "c"}}, RunOutput: false},
- {Script: `"a" in l[1:2]`, Input: map[string]interface{}{"l": []interface{}{"b", "a", "c"}}, RunOutput: true},
- // for i in list && item in list
- {Script: `list_of_list = [["a"]];for l in list_of_list { return "a" in l }`, RunOutput: true},
- {Script: `for l in list_of_list { return "a" in l }`, Input: map[string]interface{}{"list_of_list": []interface{}{[]interface{}{"a"}}}, RunOutput: true},
- // not slice or array
- // todo: support `"a" in "aaa"` ?
- {Script: `"a" in "aaa"`, RunError: fmt.Errorf("second argument must be slice or array; but have string")},
- {Script: `1 in 12345`, RunError: fmt.Errorf("second argument must be slice or array; but have int64")},
- // a in item in list
- {Script: `"a" in 5 in [1, 2, 3]`, RunError: fmt.Errorf("second argument must be slice or array; but have bool")},
- // applying a in b in several part of expresstion/statement
- {Script: `switch 1 in [1] {case true: return true;default: return false}`, RunOutput: true},
- {Script: `switch 1 in [2,3] {case true: return true;default: return false}`, RunOutput: false},
- {Script: `switch true {case 1 in [1]: return true;default: return false}`, RunOutput: true},
- {Script: `switch false {case 1 in [1]: return true;default: return false}`, RunOutput: false},
- {Script: `if 1 in [1] {return true} else {return false}`, RunOutput: true},
- {Script: `if 1 in [2,3] {return true} else {return false}`, RunOutput: false},
- {Script: `for i in [1,2,3] { i++ }`},
- {Script: `a=1; a=a in [1]`, RunOutput: true},
- {Script: `a=1; a=a in [2,3]`, RunOutput: false},
- {Script: `1 in [1] && true`, RunOutput: true},
- {Script: `1 in [1] && false`, RunOutput: false},
- {Script: `1 in [1] || true`, RunOutput: true},
- {Script: `1 in [1] || false`, RunOutput: true},
- {Script: `1 in [2,3] && true`, RunOutput: false},
- {Script: `1 in [2,3] && false`, RunOutput: false},
- {Script: `1 in [2,3] || true`, RunOutput: true},
- {Script: `1 in [2,3] || false`, RunOutput: false},
- {Script: `1++ in [1, 2, 3]`, RunError: fmt.Errorf("invalid operation")},
- {Script: `3++ in [1, 2, 3]`, RunError: fmt.Errorf("invalid operation")},
- {Script: `1 in 1++`, RunError: fmt.Errorf("invalid operation")},
- {Script: `a=1;a++ in [1, 2, 3]`, RunOutput: true},
- {Script: `a=3;a++ in [1, 2, 3]`, RunOutput: false},
- {Script: `switch 1 in l {case true: return true;default: return false}`, Input: map[string]interface{}{"l": []interface{}{1}}, RunOutput: true},
- {Script: `switch 1 in l {case true: return true;default: return false}`, Input: map[string]interface{}{"l": []interface{}{2, 3}}, RunOutput: false},
- {Script: `switch true {case 1 in l: return true;default: return false}`, Input: map[string]interface{}{"l": []interface{}{1}}, RunOutput: true},
- {Script: `switch false {case 1 in l: return true;default: return false}`, Input: map[string]interface{}{"l": []interface{}{1}}, RunOutput: false},
- {Script: `if 1 in l {return true} else {return false}`, Input: map[string]interface{}{"l": []interface{}{1}}, RunOutput: true},
- {Script: `if 1 in l {return true} else {return false}`, Input: map[string]interface{}{"l": []interface{}{2, 3}}, RunOutput: false},
- {Script: `for i in l { i++ }`, Input: map[string]interface{}{"l": []interface{}{1, 2, 3}}},
- {Script: `a=1; a=a in l`, Input: map[string]interface{}{"l": []interface{}{1}}, RunOutput: true},
- {Script: `a=1; a=a in l`, Input: map[string]interface{}{"l": []interface{}{2, 3}}, RunOutput: false},
- {Script: `1 in l && true`, Input: map[string]interface{}{"l": []interface{}{1}}, RunOutput: true},
- {Script: `1 in l && false`, Input: map[string]interface{}{"l": []interface{}{1}}, RunOutput: false},
- {Script: `1 in l || true`, Input: map[string]interface{}{"l": []interface{}{1}}, RunOutput: true},
- {Script: `1 in l || false`, Input: map[string]interface{}{"l": []interface{}{1}}, RunOutput: true},
- {Script: `1 in l && true`, Input: map[string]interface{}{"l": []interface{}{2, 3}}, RunOutput: false},
- {Script: `1 in l && false`, Input: map[string]interface{}{"l": []interface{}{2, 3}}, RunOutput: false},
- {Script: `1 in l || true`, Input: map[string]interface{}{"l": []interface{}{2, 3}}, RunOutput: true},
- {Script: `1 in l || false`, Input: map[string]interface{}{"l": []interface{}{2, 3}}, RunOutput: false},
- {Script: `1++ in l`, Input: map[string]interface{}{"l": []interface{}{1, 2, 3}}, RunError: fmt.Errorf("invalid operation")},
- {Script: `3++ in l`, Input: map[string]interface{}{"l": []interface{}{1, 2, 3}}, RunError: fmt.Errorf("invalid operation")},
- {Script: `a=1;a++ in l`, Input: map[string]interface{}{"l": []interface{}{1, 2, 3}}, RunOutput: true},
- {Script: `a=3;a++ in l`, Input: map[string]interface{}{"l": []interface{}{1, 2, 3}}, RunOutput: false},
- // multidimensional slice
- {Script: `1 in [1]`, RunOutput: true},
- {Script: `1 in [[1]]`, RunOutput: false},
- {Script: `1 in [[[1]]]`, RunOutput: false},
- {Script: `1 in [[1],[[1]],1]`, RunOutput: true},
- {Script: `[1] in [1]`, RunOutput: false},
- {Script: `[1] in [[1]]`, RunOutput: true},
- {Script: `[1] in [[[1]]]`, RunOutput: false},
- {Script: `[[1]] in [1]`, RunOutput: false},
- {Script: `[[1]] in [[1]]`, RunOutput: false},
- {Script: `[[1]] in [[[1]]]`, RunOutput: true},
- {Script: `1 in [1]`, Input: map[string]interface{}{"l": []interface{}{1}}, RunOutput: true},
- {Script: `1 in [[1]]`, Input: map[string]interface{}{"l": []interface{}{[]interface{}{1}}}, RunOutput: false},
- {Script: `1 in [[[1]]]`, Input: map[string]interface{}{"l": []interface{}{[]interface{}{[]interface{}{1}}}}, RunOutput: false},
- {Script: `1 in [[1],[[1]],1]`, Input: map[string]interface{}{"l": []interface{}{[]interface{}{1}, []interface{}{[]interface{}{1}}, 1}}, RunOutput: true},
- {Script: `[1] in [1]`, Input: map[string]interface{}{"l": []interface{}{1}}, RunOutput: false},
- {Script: `[1] in [[1]]`, Input: map[string]interface{}{"l": []interface{}{[]interface{}{1}}}, RunOutput: true},
- {Script: `[1] in [[[1]]]`, Input: map[string]interface{}{"l": []interface{}{[]interface{}{[]interface{}{1}}}}, RunOutput: false},
- {Script: `[[1]] in [1]`, Input: map[string]interface{}{"l": []interface{}{1}}, RunOutput: false},
- {Script: `[[1]] in [[1]]`, Input: map[string]interface{}{"l": []interface{}{[]interface{}{1}}}, RunOutput: false},
- {Script: `[[1]] in [[[1]]]`, Input: map[string]interface{}{"l": []interface{}{[]interface{}{[]interface{}{1}}}}, RunOutput: true},
- }
- runTests(t, tests, nil, &Options{Debug: true})
- }
- func TestOperatorPrecedence(t *testing.T) {
- t.Parallel()
- tests := []Test{
- // test && > ||
- {Script: `true || true && false`, RunOutput: true},
- {Script: `(true || true) && false`, RunOutput: false},
- {Script: `false && true || true`, RunOutput: true},
- {Script: `false && (true || true)`, RunOutput: false},
- // test == > ||
- {Script: `0 == 1 || 1 == 1`, RunOutput: true},
- {Script: `0 == (1 || 1) == 1`, RunOutput: false},
- // test + > ==
- {Script: `1 + 2 == 2 + 1`, RunOutput: true},
- {Script: `1 + (2 == 2) + 1`, RunOutput: int64(3)},
- // test * > +
- {Script: `2 * 3 + 4 * 5`, RunOutput: int64(26)},
- {Script: `2 * (3 + 4) * 5`, RunOutput: int64(70)},
- // test * > &&
- {Script: `2 * 0 && 3 * 4`, RunOutput: false},
- {Script: `2 * (0 && 3) * 4`, RunOutput: int64(0)},
- // test ++ > *
- {Script: `a = 1; b = 2; a++ * b++`, RunOutput: int64(6)},
- // test ++ > *
- {Script: `a = 1; b = 2; a++ * b++`, RunOutput: int64(6)},
- // test unary - > +
- {Script: `a = 1; b = 2; -a + b`, RunOutput: int64(1)},
- {Script: `a = 1; b = 2; -(a + b)`, RunOutput: int64(-3)},
- {Script: `a = 1; b = 2; a + -b`, RunOutput: int64(-1)},
- // test ! > ||
- {Script: `!true || true`, RunOutput: true},
- {Script: `!(true || true)`, RunOutput: false},
- }
- runTests(t, tests, nil, &Options{Debug: true})
- }
- func TestTry(t *testing.T) {
- t.Parallel()
- tests := []Test{
- {Script: `try { 1++ } catch { 1++ }`, RunError: fmt.Errorf("invalid operation")},
- {Script: `try { 1++ } catch a { return a }`, RunOutput: fmt.Errorf("invalid operation")},
- {Script: `try { 1++ } catch a { a = 2 }; return a`, RunError: fmt.Errorf("undefined symbol 'a'")},
- // test finally
- {Script: `try { 1++ } catch { 1++ } finally { return 1 }`, RunError: fmt.Errorf("invalid operation")},
- {Script: `try { } catch { } finally { 1++ }`, RunError: fmt.Errorf("invalid operation")},
- {Script: `try { } catch { 1 } finally { 1++ }`, RunError: fmt.Errorf("invalid operation")},
- {Script: `try { 1++ } catch { } finally { 1++ }`, RunError: fmt.Errorf("invalid operation")},
- {Script: `try { 1++ } catch a { } finally { return a }`, RunOutput: fmt.Errorf("invalid operation")},
- {Script: `try { 1++ } catch a { } finally { a = 2 }; return a`, RunError: fmt.Errorf("undefined symbol 'a'")},
- {Script: `try { } catch { }`, RunOutput: nil},
- {Script: `try { 1++ } catch { }`, RunOutput: nil},
- {Script: `try { } catch { 1++ }`, RunOutput: nil},
- {Script: `try { return 1 } catch { }`, RunOutput: int64(1)},
- {Script: `try { return 1 } catch { return 2 }`, RunOutput: int64(2)},
- {Script: `try { 1++ } catch { return 1 }`, RunOutput: int64(1)},
- // test finally
- {Script: `try { } catch { } finally { return 1 }`, RunOutput: int64(1)},
- {Script: `try { 1++ } catch { } finally { return 1 }`, RunOutput: int64(1)},
- {Script: `try { 1++ } catch { return 1 } finally { 1++ }`, RunOutput: int64(1)},
- // test variable scope
- {Script: `try { 1++ } catch a { if a.Error() == "invalid operation" { return 1 } else { return 2 } }`, RunOutput: int64(1)},
- {Script: `try { 1++ } catch a { } finally { if a.Error() == "invalid operation" { return 1 } else { return 2 } }`, RunOutput: int64(1)},
- }
- runTests(t, tests, nil, &Options{Debug: true})
- }
|