File: processor_test.go

package info (click to toggle)
golang-github-tideland-golib 4.24.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,144 kB
  • sloc: makefile: 4
file content (176 lines) | stat: -rw-r--r-- 5,215 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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Tideland Go Library - String Extensions - Unit Tests
//
// Copyright (C) 2015-2017 Frank Mueller / Tideland / Oldenburg / Germany
//
// All rights reserved. Use of this source code is governed
// by the new BSD license.

package stringex_test

//--------------------
// IMPORTS
//--------------------

import (
	"strings"
	"testing"

	"github.com/tideland/golib/audit"
	"github.com/tideland/golib/stringex"
)

//--------------------
// TESTS
//--------------------

// TestWrapping tests wrapping a standard function to a processor.
func TestWrapping(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)
	upperCaser := stringex.WrapProcessorFunc(strings.ToUpper)

	value, ok := upperCaser("test")
	assert.True(ok)
	assert.Equal(value, "TEST")
}

// TestSplitMapProcessor tests the splitting and mapping.
func TestSplitMapProcessor(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)
	sep := "the"
	upperCaser := stringex.WrapProcessorFunc(strings.ToUpper)
	splitMapper := stringex.NewSplitMapProcessor(sep, upperCaser)

	value, ok := splitMapper("the quick brown fox jumps over the lazy dog")
	assert.True(ok)
	assert.Equal(value, "the QUICK BROWN FOX JUMPS OVER the LAZY DOG")
}

// TestSubstringProcessor tests retrieving substrings.
func TestSubstringProcessor(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)
	tests := []struct {
		index  int
		length int
		in     string
		out    string
		ok     bool
	}{
		{0, 5, "yadda", "yadda", true},
		{0, 3, "yadda", "yad", true},
		{2, 3, "yadda", "dda", true},
		{2, 5, "yadda", "dda", true},
		{-1, 5, "yadda", "yadda", true},
		{-1, 10, "yadda", "yadda", true},
		{0, 0, "yadda", "", false},
	}
	for _, test := range tests {
		substringer := stringex.NewSubstringProcessor(test.index, test.length)
		out, ok := substringer(test.in)
		assert.Equal(ok, test.ok)
		assert.Equal(out, test.out)
	}
}

// TestMatchProcessor tests the matching of patterns.
func TestMatchProcessor(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)
	tests := []struct {
		pattern string
		in      string
		out     string
		ok      bool
	}{
		{"[0-9]+", "+++12345+++", "+++12345+++", true},
		{"^[0-9]+", "+++12345+++", "+++12345+++", false},
		{"[", "+++12345+++", "error processing '+++12345+++': error parsing regexp: missing closing ]: `[`", false},
	}
	for _, test := range tests {
		matcher := stringex.NewMatchProcessor(test.pattern)
		out, ok := matcher(test.in)
		assert.Equal(ok, test.ok)
		assert.Equal(out, test.out)
	}
}

// TestTrimmingProcessors tests the trimming.
func TestTrimmingProcessors(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)
	in := "+++++foo+++"

	// Prefix.
	plusPreTrimmer := stringex.NewTrimPrefixProcessor("+")
	plusPlusPreTrimmer := stringex.NewTrimPrefixProcessor("++")

	value, ok := plusPreTrimmer(in)
	assert.True(ok)
	assert.Equal(value, "foo+++")
	value, ok = plusPlusPreTrimmer(in)
	assert.True(ok)
	assert.Equal(value, "+foo+++")

	// Suffix.
	plusSufTrimmer := stringex.NewTrimSuffixProcessor("+")
	plusPlusSufTrimmer := stringex.NewTrimSuffixProcessor("++")

	value, ok = plusSufTrimmer(in)
	assert.True(ok)
	assert.Equal(value, "+++++foo")
	value, ok = plusPlusSufTrimmer(in)
	assert.True(ok)
	assert.Equal(value, "+++++foo+")

	// Chaining.
	plusTrimmer := stringex.NewChainProcessor(plusPreTrimmer, plusSufTrimmer)
	plusPlusTrimmer := stringex.NewChainProcessor(plusPlusPreTrimmer, plusPlusSufTrimmer)

	value, ok = plusTrimmer(in)
	assert.True(ok)
	assert.Equal(value, "foo")
	value, ok = plusPlusTrimmer(in)
	assert.True(ok)
	assert.Equal(value, "+foo+")
}

// TestUpperLowerProcessor tests converting strings to upper- or lowe-case.
func TestUpperLowerProcessor(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)
	in := "this IS in UPPER & lower cAsE"
	uppercaser := stringex.NewUpperProcessor()
	lowercaser := stringex.NewLowerProcessor()

	value, ok := uppercaser(in)
	assert.True(ok)
	assert.Equal(value, "THIS IS IN UPPER & LOWER CASE")
	value, ok = lowercaser(in)
	assert.True(ok)
	assert.Equal(value, "this is in upper & lower case")
}

// TestProcessorScenario tests the combination of multiple processors.
func TestProcessorScenario(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)
	in := "+++++Yadda+++/-----Foobar--/+-+-Testing-+-+/Out"
	trimmer := stringex.NewTrimFuncProcessor(func(r rune) bool {
		return r == '+' || r == '-'
	})
	substringer := stringex.NewSubstringProcessor(0, 4)
	omatcher := stringex.NewMatchProcessor("[Oo]+")
	uppercaser := stringex.NewUpperProcessor()
	lowercaser := stringex.NewLowerProcessor()
	matchcaser := stringex.NewConditionProcessor(omatcher, uppercaser, lowercaser)
	bracer := stringex.ProcessorFunc(func(in string) (string, bool) {
		return "(" + in + ")", true
	})
	updater := stringex.NewChainProcessor(trimmer, substringer, matchcaser, bracer)
	allUpdater := stringex.NewSplitMapProcessor("/", updater)
	replacer := stringex.ProcessorFunc(func(in string) (string, bool) {
		return strings.Replace(in, "/", "::", -1), true
	})
	fullUpdater := stringex.NewChainProcessor(allUpdater, replacer)

	value, ok := fullUpdater(in)
	assert.True(ok)
	assert.Equal(value, "(yadd)::(FOOB)::(test)::(OUT)")
}

// EOF