// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com). // All rights reserved. // t_scanner_test.go package expr import ( "errors" "fmt" "reflect" "strings" "testing" ) func TestScanner(t *testing.T) { type inputType struct { source string wantSym Symbol wantValue any wantErr error } inputs := []inputType{ /* 1 */ {`123`, SymInteger, int64(123), nil}, /* 2 */ {"=", SymEqual, nil, nil}, /* 3 */ {`--`, SymDoubleMinus, nil, nil}, /* 4 */ {`++`, SymDoublePlus, nil, nil}, /* 5 */ {`**`, SymDoubleStar, nil, nil}, /* 6 */ {`&&`, SymDoubleAmpersand, nil, nil}, /* 7 */ {`||`, SymDoubleVertBar, nil, nil}, /* 8 */ {`NOT`, SymKwNot, nil, nil}, /* 9 */ {`AND`, SymKwAnd, nil, nil}, /* 10 */ {`or`, SymKwOr, nil, nil}, /* 11 */ {`+=`, SymPlusEqual, nil, nil}, /* 12 */ {`-=`, SymMinusEqual, nil, nil}, /* 13 */ {`|`, SymVertBar, nil, nil}, /* 14 */ {`:`, SymColon, nil, nil}, /* 15 */ {`;`, SymSemiColon, nil, nil}, /* 16 */ {`.`, SymDot, nil, nil}, /* 17 */ {`0.5`, SymFloat, float64(0.5), nil}, /* 18 */ {`\\`, SymBackSlash, nil, nil}, /* 19 */ {"`", SymBackTick, nil, nil}, /* 20 */ {"?", SymQuestion, nil, nil}, /* 21 */ {"&", SymAmpersand, nil, nil}, /* 22 */ {"@", SymAt, nil, nil}, /* 23 */ {`#`, SymHash, nil, nil}, /* 24 */ {`%`, SymPercent, nil, nil}, /* 25 */ {`\'`, SymQuote, nil, nil}, /* 26 */ {`\"`, SymDoubleQuote, nil, nil}, /* 27 */ {`_`, SymUndescore, nil, nil}, /* 28 */ {`<>`, SymLessGreater, nil, nil}, /* 29 */ {`[`, SymOpenSquare, nil, nil}, /* 30 */ {`]`, SymClosedSquare, nil, nil}, /* 31 */ {`{`, SymOpenBrace, nil, nil}, /* 32 */ {`}`, SymClosedBrace, nil, nil}, /* 33 */ {`(`, SymOpenRound, nil, nil}, /* 34 */ {`)`, SymClosedRound, nil, nil}, /* 35 */ {`1E+2`, SymFloat, float64(100), nil}, /* 36 */ {`1E+x`, SymError, errors.New("[1:5] expected integer exponent, got x"), nil}, /* 37 */ {`$`, SymDollar, nil, nil}, /* 38 */ {`\`, SymError, errors.New("incomplete escape sequence"), nil}, /* 39 */ {`"string"`, SymString, "string", nil}, /* 40 */ {`identifier`, SymIdentifier, "identifier", nil}, /* 41 */ {`1.2(3)`, SymFraction, newFraction(37, 30), nil}, } for i, input := range inputs { // if i != 40 { // continue // } if input.wantErr == nil { t.Log(fmt.Sprintf("[+]Test nr %2d -- %q", i+1, input.source)) } else { t.Log(fmt.Sprintf("[-]Test nr %2d -- %q", i+1, input.source)) } r := strings.NewReader(input.source) scanner := NewScanner(r, nil) if tk := scanner.Next(); tk == nil { t.Errorf("%d: %q -> got = (nil), want %v (value %v [%T])", i+1, input.source, input.wantSym, input.wantValue, input.wantValue) // } else if tk.Sym != input.wantSym || tk.Value != input.wantValue { } else if tk.Sym != input.wantSym || !reflect.DeepEqual(tk.Value, input.wantValue) { if tk.Sym == SymError && input.wantSym == tk.Sym { if tkErr, tkOk := tk.Value.(error); tkOk { if inputErr, inputOk := input.wantValue.(error); inputOk { if tkErr.Error() != inputErr.Error() { t.Errorf("%d: %q -> got-error = %v, want-error: %v", i+1, input.source, tk.Value, input.wantValue) } } } } else { t.Errorf("%d: %q -> got = %v (value=%v [%T]), want %v (value=%v [%T])", i+1, input.source, tk.Sym, tk.Value, tk.Value, input.wantSym, input.wantValue, input.wantValue) } } } }