aboutsummaryrefslogtreecommitdiffstats
path: root/commands/cmdtest/regex.go
blob: 0b9cb672811ccddf778fe5cae61a5580df601604 (plain) (blame)
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 cmdtest

import (
	"regexp"
	"strings"
)

const ExpId = "\x07id\x07"
const ExpHumanId = "\x07human-id\x07"
const ExpTimestamp = "\x07timestamp\x07"
const ExpISO8601 = "\x07iso8601\x07"

const ExpOrgModeDate = "\x07org-mode-date\x07"

// MakeExpectedRegex transform a raw string of an expected output into a regex suitable for testing.
// Some markers like ExpId are available to substitute the appropriate regex for element that can vary randomly.
func MakeExpectedRegex(input string) string {
	var substitutes = map[string]string{
		ExpId:          `[0-9a-f]{64}`,
		ExpHumanId:     `[0-9a-f]{7}`,
		ExpTimestamp:   `[0-9]{7,10}`,
		ExpISO8601:     `\d{4}(-\d\d(-\d\d(T\d\d:\d\d(:\d\d)?(\.\d+)?(([+-]\d\d:\d\d)|Z)?)?)?)?`,
		ExpOrgModeDate: `\d\d\d\d-\d\d-\d\d [[:alpha:]]{3} \d\d:\d\d`,
	}

	escaped := []rune(regexp.QuoteMeta(input))

	var result strings.Builder
	var inSubstitute bool
	var substitute strings.Builder

	result.WriteString("^")

	for i := 0; i < len(escaped); i++ {
		r := escaped[i]
		if !inSubstitute && r == '\x07' {
			substitute.Reset()
			substitute.WriteRune(r)
			inSubstitute = true
			continue
		}
		if inSubstitute && r == '\x07' {
			substitute.WriteRune(r)
			sub, ok := substitutes[substitute.String()]
			if !ok {
				panic("unknown substitute: " + substitute.String())
			}
			result.WriteString(sub)
			inSubstitute = false
			continue
		}
		if inSubstitute {
			substitute.WriteRune(r)
		} else {
			result.WriteRune(r)
		}
	}

	result.WriteString("$")

	return result.String()
}