File: string_test.go

package info (click to toggle)
golang-github-cretz-bine 0.2.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bookworm-backports, sid, trixie
  • size: 652 kB
  • sloc: makefile: 3
file content (106 lines) | stat: -rw-r--r-- 3,280 bytes parent folder | download
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package torutil

import (
	"testing"

	"github.com/stretchr/testify/require"
)

func TestPartitionString(t *testing.T) {
	assert := func(str string, ch byte, expectedA string, expectedB string, expectedOk bool) {
		a, b, ok := PartitionString(str, ch)
		require.Equal(t, expectedA, a)
		require.Equal(t, expectedB, b)
		require.Equal(t, expectedOk, ok)
	}
	assert("foo:bar", ':', "foo", "bar", true)
	assert(":bar", ':', "", "bar", true)
	assert("foo:", ':', "foo", "", true)
	assert("foo", ':', "foo", "", false)
	assert("foo:bar:baz", ':', "foo", "bar:baz", true)
}

func TestPartitionStringFromEnd(t *testing.T) {
	assert := func(str string, ch byte, expectedA string, expectedB string, expectedOk bool) {
		a, b, ok := PartitionStringFromEnd(str, ch)
		require.Equal(t, expectedA, a)
		require.Equal(t, expectedB, b)
		require.Equal(t, expectedOk, ok)
	}
	assert("foo:bar", ':', "foo", "bar", true)
	assert(":bar", ':', "", "bar", true)
	assert("foo:", ':', "foo", "", true)
	assert("foo", ':', "foo", "", false)
	assert("foo:bar:baz", ':', "foo:bar", "baz", true)
}

func TestEscapeSimpleQuotedStringIfNeeded(t *testing.T) {
	assert := func(str string, shouldBeDiff bool) {
		maybeEscaped := EscapeSimpleQuotedStringIfNeeded(str)
		if shouldBeDiff {
			require.NotEqual(t, str, maybeEscaped)
		} else {
			require.Equal(t, str, maybeEscaped)
		}
	}
	assert("foo", false)
	assert(" foo", true)
	assert("f\\oo", true)
	assert("fo\"o", true)
	assert("f\roo", true)
	assert("fo\no", true)
}

func TestEscapeSimpleQuotedString(t *testing.T) {
	require.Equal(t, "\"foo\"", EscapeSimpleQuotedString("foo"))
}

func TestEscapeSimpleQuotedStringContents(t *testing.T) {
	assert := func(str string, expected string) {
		require.Equal(t, expected, EscapeSimpleQuotedStringContents(str))
	}
	assert("foo", "foo")
	assert("f\\oo", "f\\\\oo")
	assert("f\\noo", "f\\\\noo")
	assert("f\n o\ro", "f\\n o\\ro")
	assert("fo\r\\\"o", "fo\\r\\\\\\\"o")
}

func TestUnescapeSimpleQuotedStringIfNeeded(t *testing.T) {
	assert := func(str string, expectedStr string, expectedErr bool) {
		actualStr, actualErr := UnescapeSimpleQuotedStringIfNeeded(str)
		require.Equal(t, expectedStr, actualStr)
		require.Equal(t, expectedErr, actualErr != nil)
	}
	assert("foo", "foo", false)
	assert("\"foo\"", "foo", false)
	assert("\"f\"oo\"", "", true)
}

func TestUnescapeSimpleQuotedString(t *testing.T) {
	assert := func(str string, expectedStr string, expectedErr bool) {
		actualStr, actualErr := UnescapeSimpleQuotedString(str)
		require.Equal(t, expectedStr, actualStr)
		require.Equal(t, expectedErr, actualErr != nil)
	}
	assert("foo", "", true)
	assert("\"foo\"", "foo", false)
	assert("\"f\"oo\"", "", true)
}

func TestUnescapeSimpleQuotedStringContents(t *testing.T) {
	assert := func(str string, expectedStr string, expectedErr bool) {
		actualStr, actualErr := UnescapeSimpleQuotedStringContents(str)
		require.Equal(t, expectedStr, actualStr)
		require.Equal(t, expectedErr, actualErr != nil)
	}
	assert("foo", "foo", false)
	assert("f\\\\oo", "f\\oo", false)
	assert("f\\\\noo", "f\\noo", false)
	assert("f\\n o\\ro", "f\n o\ro", false)
	assert("fo\\r\\\\\\\"o", "fo\r\\\"o", false)
	assert("f\"oo", "", true)
	assert("f\roo", "", true)
	assert("f\noo", "", true)
	assert("f\\oo", "", true)
}