github.com/dolthub/go-mysql-server@v0.18.0/optgen/cmd/support/frame_factory_gen_test.go (about) 1 package support 2 3 import ( 4 "bytes" 5 "fmt" 6 "strings" 7 "testing" 8 ) 9 10 func TestFrameFactoryGen(t *testing.T) { 11 test := struct { 12 expected string 13 }{ 14 expected: ` 15 import ( 16 "fmt" 17 "github.com/dolthub/go-mysql-server/sql" 18 "github.com/dolthub/go-mysql-server/sql/plan" 19 ast "github.com/dolthub/vitess/go/vt/sqlparser" 20 ) 21 22 func (b *Builder) NewFrame(inScope *scope, f *ast.Frame) sql.WindowFrame { 23 if f == nil { 24 return nil 25 } 26 isRange := f.Unit == ast.RangeUnit 27 isRows := f.Unit == ast.RowsUnit 28 unboundedPreceding := b.getFrameUnboundedPreceding(inScope, f) 29 startNPreceding := b.getFrameStartNPreceding(inScope, f) 30 startCurrentRow := b.getFrameStartCurrentRow(inScope, f) 31 startNFollowing := b.getFrameStartNFollowing(inScope, f) 32 endNPreceding := b.getFrameEndNPreceding(inScope, f) 33 endCurrentRow := b.getFrameEndCurrentRow(inScope, f) 34 endNFollowing := b.getFrameEndNFollowing(inScope, f) 35 unboundedFollowing := b.getFrameUnboundedFollowing(inScope, f) 36 switch { 37 case isRows && unboundedPreceding && endNPreceding != nil: 38 return plan.NewRowsUnboundedPrecedingToNPrecedingFrame(endNPreceding) 39 default: 40 err := fmt.Errorf("no matching constructor found for frame: %v", f) 41 b.handleErr(err) 42 return nil 43 } 44 } 45 `, 46 } 47 48 gen := FrameFactoryGen{limit: 1} 49 var buf bytes.Buffer 50 gen.Generate(nil, &buf) 51 52 if testing.Verbose() { 53 fmt.Printf("\n=>\n\n%s\n", buf.String()) 54 } 55 56 if !strings.Contains(removeWhitespace(buf.String()), removeWhitespace(test.expected)) { 57 t.Fatalf("\nexpected:\n%s\nactual:\n%s", test.expected, buf.String()) 58 } 59 }