package engine import ( // "fmt" "strings" "testing" stmtd "github.com/tgckpg/mmqlengine/mmql/statements" ) func TestParse( t *testing.T ) { lexer := Lexer{} s, err := lexer.Parse( "\"ABC\"" ) if err != nil { t.Error( err ) } got := (*any( s ).( stmtd.QuotedValue ).RawStatement).Value if got != "ABC" { t.Errorf( "Expected ABC, got %s", got ) } s, err = lexer.Parse( "\"ABC" ) if err == nil { t.Errorf( "Expected error, got value %s", s ) } s, err = lexer.Parse( "( ABC )" ) got = (*any( s ).( stmtd.BracketedValue ).RawStatement).Value if got != " ABC " { t.Errorf( "Expected ABC, got %s", got ) } s, err = lexer.Parse( "( ABC" ) if err == nil { t.Errorf( "Expected error, got value %s", s ) } } func TestDecimal( t *testing.T ) { lexer := Lexer{} read_oks := map[string] float64 { "1,000k": 1000000, "10.8": 10.8, "10,000.60": 10000.6, "10,000.60 this_string_should_not_be_read": 10000.6, "10,000 this_string_should_not_be_read": 10000, "10.": 10, ".2": 0.2, ".2k": 200, "1 g": 1, " 1 g": 1, } for k := range read_oks { lexer.SetReader( strings.NewReader( k ) ) f, err := lexer.ReadDecimal() if err != nil { t.Errorf( "Expected number for %s, got %s", k, err ) continue } expects, _ := read_oks[ k ] if f != expects { t.Errorf( "Expected %f, got %f", expects, f ) } } read_fails := []string { "1,000ki", "10,000.60s", "10,,0", "100s", "10, 000", "10,", } for _, v := range read_fails { lexer.SetReader( strings.NewReader( v ) ) f, err := lexer.ReadDecimal() if err == nil { t.Errorf( "Expected error for %s, got %f", v, f ) } } } func TestAlpha( t *testing.T ) { lexer := Lexer{} read_oks := map[string] string { "abcd efgh": "abcd", " abcd": "abcd", } for k := range read_oks { lexer.SetReader( strings.NewReader( k ) ) f, err := lexer.ReadAlpha() if err != nil { t.Errorf( "Expected string for \"%s\", got %s", k, err ) continue } expects, _ := read_oks[ k ] if f != expects { t.Errorf( "Expected %s, got %s", expects, f ) } } read_fails := []string { "1", " ", } for _, v := range read_fails { lexer.SetReader( strings.NewReader( v ) ) f, err := lexer.ReadAlpha() if err == nil { t.Errorf( "Expected error for %s, got %s", v, f ) } } lexer.SetReader( strings.NewReader( "abcd efgh" ) ) f, err := lexer.ReadAlpha() f, err = lexer.ReadAlpha() if err != nil { t.Errorf( "Expected string for \"efgh\", got %s", err ) } if f != "efgh" { t.Errorf( "Expected \"efgh\", got %s", f ) } }