// Copyright 2021 PingCAP, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. //go:build !codes // +build !codes package testkit import ( "bytes" "fmt" "sort" "strings" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) // Result is the result returned by MustQuery. type Result struct { rows [][]string comment string require *require.Assertions assert *assert.Assertions } // Check asserts the result equals the expected results. func (res *Result) Check(expected [][]interface{}) { resBuff := bytes.NewBufferString("") for _, row := range res.rows { _, _ = fmt.Fprintf(resBuff, "%s\n", row) } needBuff := bytes.NewBufferString("") for _, row := range expected { _, _ = fmt.Fprintf(needBuff, "%s\n", row) } res.require.Equal(needBuff.String(), resBuff.String(), res.comment) } // Rows is similar to RowsWithSep, use white space as separator string. func Rows(args ...string) [][]interface{} { return RowsWithSep(" ", args...) } // Sort sorts and return the result. func (res *Result) Sort() *Result { sort.Slice(res.rows, func(i, j int) bool { a := res.rows[i] b := res.rows[j] for i := range a { if a[i] < b[i] { return true } else if a[i] > b[i] { return false } } return false }) return res } // RowsWithSep is a convenient function to wrap args to a slice of []interface. // The arg represents a row, split by sep. func RowsWithSep(sep string, args ...string) [][]interface{} { rows := make([][]interface{}, len(args)) for i, v := range args { parts := strings.Split(v, sep) row := make([]interface{}, len(parts)) for j, s := range parts { row[j] = s } rows[i] = row } return rows } // Rows returns the result data. func (res *Result) Rows() [][]interface{} { ifacesSlice := make([][]interface{}, len(res.rows)) for i := range res.rows { ifaces := make([]interface{}, len(res.rows[i])) for j := range res.rows[i] { ifaces[j] = res.rows[i][j] } ifacesSlice[i] = ifaces } return ifacesSlice }