Diff compares the two terminals, returning an empty string if there is not difference. If a difference is found, returns a human readable description of the differences.
(want, got *Terminal)
| 39 | // difference. If a difference is found, returns a human readable description |
| 40 | // of the differences. |
| 41 | func Diff(want, got *Terminal) string { |
| 42 | if reflect.DeepEqual(want.BackBuffer(), got.BackBuffer()) { |
| 43 | return "" |
| 44 | } |
| 45 | |
| 46 | var b strings.Builder |
| 47 | b.WriteString("found differences between the two fake terminals.\n") |
| 48 | b.WriteString(" got:\n") |
| 49 | b.WriteString(got.String()) |
| 50 | b.WriteString(" want:\n") |
| 51 | b.WriteString(want.String()) |
| 52 | b.WriteString(" diff (unexpected cells highlighted with rune '࿃')\n") |
| 53 | b.WriteString(" note - this excludes cell options:\n") |
| 54 | |
| 55 | size := got.Size() |
| 56 | var optDiffs []*optDiff |
| 57 | cellsDiffer := false |
| 58 | for row := 0; row < size.Y; row++ { |
| 59 | for col := 0; col < size.X; col++ { |
| 60 | p := image.Point{col, row} |
| 61 | partial, err := got.BackBuffer().IsPartial(p) |
| 62 | if err != nil { |
| 63 | panic(fmt.Errorf("unable to determine if point %v is a partial rune: %v", p, err)) |
| 64 | } |
| 65 | |
| 66 | gotCell := got.BackBuffer()[col][row] |
| 67 | wantCell := want.BackBuffer()[col][row] |
| 68 | r := gotCell.Rune |
| 69 | if r != wantCell.Rune { |
| 70 | r = '࿃' |
| 71 | cellsDiffer = true |
| 72 | } else if r == 0 && !partial { |
| 73 | r = ' ' |
| 74 | } |
| 75 | b.WriteRune(r) |
| 76 | |
| 77 | if !reflect.DeepEqual(gotCell.Opts, wantCell.Opts) { |
| 78 | optDiffs = append(optDiffs, &optDiff{ |
| 79 | point: image.Point{col, row}, |
| 80 | got: gotCell.Opts, |
| 81 | want: wantCell.Opts, |
| 82 | }) |
| 83 | } |
| 84 | } |
| 85 | b.WriteRune('\n') |
| 86 | } |
| 87 | |
| 88 | if len(optDiffs) > 0 { |
| 89 | b.WriteString(" Found differences in options on some of the cells:\n") |
| 90 | for _, od := range optDiffs { |
| 91 | if diff := pretty.Compare(od.want, od.got); diff != "" { |
| 92 | b.WriteString(fmt.Sprintf("cell %v, diff (-want +got):\n%s\n", od.point, diff)) |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | if cellsDiffer { |
| 98 | b.WriteString(" Found differences in some of the cell runes:\n") |