Skip to content

Commit d0f25e5

Browse files
committed
feat: add unit tests for the helper function
1 parent b93c30e commit d0f25e5

1 file changed

Lines changed: 93 additions & 0 deletions

File tree

internal/stackitprovider/helper_test.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package stackitprovider
22

33
import (
44
"reflect"
5+
"strings"
56
"testing"
67

78
stackitdnsclient "github.com/stackitcloud/stackit-sdk-go/services/dns/v1api"
@@ -214,3 +215,95 @@ func TestGetStackitRRSetRecordPatch(t *testing.T) {
214215
t.Errorf("getStackitRRSetRecordPatch() = %v, want %v", got, expected)
215216
}
216217
}
218+
219+
func TestFormatTXTContent(t *testing.T) {
220+
t.Parallel()
221+
222+
// Generate strings of exact lengths for testing
223+
string255 := strings.Repeat("a", 255)
224+
string256 := strings.Repeat("a", 256)
225+
string511 := strings.Repeat("a", 511)
226+
227+
tests := []struct {
228+
name string
229+
content string
230+
want string
231+
}{
232+
{
233+
name: "Short string without quotes",
234+
content: "hello world",
235+
want: `"hello world"`,
236+
},
237+
{
238+
name: "Short string with existing quotes",
239+
content: `"hello world"`,
240+
want: `"hello world"`,
241+
},
242+
{
243+
name: "Exactly 255 characters",
244+
content: string255,
245+
want: `"` + string255 + `"`,
246+
},
247+
{
248+
name: "256 characters (requires 2 chunks)",
249+
content: string256,
250+
want: `"` + string255 + `" "a"`,
251+
},
252+
{
253+
name: "511 characters (requires 3 chunks)",
254+
content: string511,
255+
want: `"` + string255 + `" "` + string255 + `" "a"`,
256+
},
257+
}
258+
259+
for _, tt := range tests {
260+
tt := tt
261+
t.Run(tt.name, func(t *testing.T) {
262+
t.Parallel()
263+
if got := formatTXTContent(tt.content); got != tt.want {
264+
t.Errorf("formatTXTContent() = %v, want %v", got, tt.want)
265+
}
266+
})
267+
}
268+
}
269+
270+
func TestUnformatTXTContent(t *testing.T) {
271+
t.Parallel()
272+
273+
tests := []struct {
274+
name string
275+
content string
276+
want string
277+
}{
278+
{
279+
name: "Unquoted short string",
280+
content: "hello world",
281+
want: "hello world",
282+
},
283+
{
284+
name: "Single chunk quoted string",
285+
content: `"hello world"`,
286+
want: "hello world",
287+
},
288+
{
289+
name: "Two chunk string",
290+
content: `"hello" "world"`,
291+
want: "helloworld",
292+
},
293+
{
294+
name: "Three chunk string",
295+
content: `"chunk1" "chunk2" "chunk3"`,
296+
want: "chunk1chunk2chunk3",
297+
},
298+
}
299+
300+
for _, tt := range tests {
301+
tt := tt
302+
t.Run(tt.name, func(t *testing.T) {
303+
t.Parallel()
304+
if got := unformatTXTContent(tt.content); got != tt.want {
305+
t.Errorf("unformatTXTContent() = %v, want %v", got, tt.want)
306+
}
307+
})
308+
}
309+
}

0 commit comments

Comments
 (0)