|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved. |
| 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 test |
| 18 | + |
| 19 | +import ( |
| 20 | + "log" |
| 21 | + "os" |
| 22 | + "testing" |
| 23 | +) |
| 24 | + |
| 25 | +// ----------------------------------------------------------------------------- |
| 26 | + |
| 27 | +// Fatal is a wrapper for log.Panicln. |
| 28 | +func Fatal(v ...any) { |
| 29 | + log.Panicln(v...) |
| 30 | +} |
| 31 | + |
| 32 | +// Fatalf is a wrapper for log.Panicf. |
| 33 | +func Fatalf(format string, v ...any) { |
| 34 | + log.Panicf(format, v...) |
| 35 | +} |
| 36 | + |
| 37 | +// ----------------------------------------------------------------------------- |
| 38 | + |
| 39 | +// Diff compares the dst and src byte slices. |
| 40 | +// If they are different, it writes the dst to the outfile and logs the differences. |
| 41 | +func Diff(t *testing.T, outfile string, dst, src []byte) bool { |
| 42 | + line := 1 |
| 43 | + offs := 0 // line offset |
| 44 | + for i := 0; i < len(dst) && i < len(src); i++ { |
| 45 | + d := dst[i] |
| 46 | + s := src[i] |
| 47 | + if d != s { |
| 48 | + os.WriteFile(outfile, dst, 0644) |
| 49 | + t.Errorf("dst:%d: %s\n", line, dst[offs:]) |
| 50 | + t.Errorf("src:%d: %s\n", line, src[offs:]) |
| 51 | + return true |
| 52 | + } |
| 53 | + if s == '\n' { |
| 54 | + line++ |
| 55 | + offs = i + 1 |
| 56 | + } |
| 57 | + } |
| 58 | + if len(dst) != len(src) { |
| 59 | + os.WriteFile(outfile, dst, 0644) |
| 60 | + t.Errorf("len(dst) = %d, len(src) = %d\ndst = %q\nsrc = %q", len(dst), len(src), dst, src) |
| 61 | + return true |
| 62 | + } |
| 63 | + return false |
| 64 | +} |
| 65 | + |
| 66 | +// ----------------------------------------------------------------------------- |
0 commit comments