vitess.io/vitess@v0.16.2/go/mysql/collations/internal/testutil/golden.go (about) 1 /* 2 Copyright 2021 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package testutil 18 19 import ( 20 "compress/gzip" 21 "encoding/gob" 22 "io" 23 "os" 24 ) 25 26 type GoldenCase struct { 27 Lang Lang 28 Text []byte 29 Weights map[string][]byte 30 } 31 32 type GoldenTest struct { 33 Name string 34 Cases []GoldenCase 35 } 36 37 func (golden *GoldenTest) Encode(w io.Writer) error { 38 gw := gzip.NewWriter(w) 39 defer gw.Close() 40 41 enc := gob.NewEncoder(gw) 42 return enc.Encode(golden) 43 } 44 45 func (golden *GoldenTest) EncodeToFile(path string) error { 46 f, err := os.Create(path) 47 if err != nil { 48 return err 49 } 50 defer f.Close() 51 return golden.Encode(f) 52 } 53 54 func (golden *GoldenTest) Decode(r io.Reader) error { 55 gr, err := gzip.NewReader(r) 56 if err != nil { 57 return err 58 } 59 defer gr.Close() 60 61 dec := gob.NewDecoder(gr) 62 return dec.Decode(golden) 63 } 64 65 func (golden *GoldenTest) DecodeFromFile(path string) error { 66 f, err := os.Open(path) 67 if err != nil { 68 return err 69 } 70 defer f.Close() 71 return golden.Decode(f) 72 }