File: diff_test.go

package info (click to toggle)
golang-github-itchyny-timefmt-go 0.1.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 152 kB
  • sloc: makefile: 17
file content (62 lines) | stat: -rw-r--r-- 1,085 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package timefmt_test

import "strings"

type stringBuilder struct {
	strings.Builder
}

func (sb *stringBuilder) writeDiff(s string) {
	sb.WriteString("\x1b[1;4m")
	sb.WriteString(s)
	sb.WriteString("\x1b[0m")
}

func diff(expected, got string) string {
	var sbx, sby stringBuilder
	xs := strings.Split(expected, " ")
	ys := strings.Split(got, " ")
	for i, j := 0, 0; ; i, j = i+1, j+1 {
		if i >= len(xs) {
			if j >= len(ys) {
				break
			}
			if j > 0 {
				sby.writeDiff(" ")
			}
			sby.writeDiff(ys[j])
			continue
		} else if j >= len(ys) {
			if i > 0 {
				sbx.writeDiff(" ")
			}
			sbx.writeDiff(xs[i])
			continue
		}
		if xs[i] == "" {
			if ys[j] != "" {
				sbx.writeDiff(" ")
				j--
				continue
			}
		} else if ys[j] == "" {
			sby.writeDiff(" ")
			i--
			continue
		}
		if i > 0 {
			sbx.WriteByte(' ')
		}
		if j > 0 {
			sby.WriteByte(' ')
		}
		if xs[i] == ys[j] {
			sbx.WriteString(xs[i])
			sby.WriteString(ys[j])
		} else {
			sbx.writeDiff(xs[i])
			sby.writeDiff(ys[j])
		}
	}
	return "diff:\nexpected: " + sbx.String() + "\n     got: " + sby.String()
}