From 697426a78b015790d4e90298267287c311bfe670 Mon Sep 17 00:00:00 2001 From: Umputun Date: Wed, 18 Dec 2024 11:21:38 -0600 Subject: [PATCH] Add unit tests for string array manipulation and HTML pruning Introduce comprehensive test cases for stringArr methods (Push, Pop, Unshift, Shift, String) to ensure correct behavior and state management. Additionally, add tests for HTML pruning functions (pruneHTML, pruneStringToWord) to validate handling of length constraints and formatting scenarios. --- backend/app/notify/prune_html_test.go | 310 ++++++++++++++++++++++++++ 1 file changed, 310 insertions(+) create mode 100644 backend/app/notify/prune_html_test.go diff --git a/backend/app/notify/prune_html_test.go b/backend/app/notify/prune_html_test.go new file mode 100644 index 0000000000..15883e1ec2 --- /dev/null +++ b/backend/app/notify/prune_html_test.go @@ -0,0 +1,310 @@ +package notify + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestStringArr_Push(t *testing.T) { + tests := []struct { + name string + initial []string + push string + expected []string + expLen int + }{ + { + name: "push to empty array", + initial: []string{}, + push: "hello", + expected: []string{"hello"}, + expLen: 5, + }, + { + name: "push to non-empty array", + initial: []string{"hello"}, + push: "world", + expected: []string{"hello", "world"}, + expLen: 10, + }, + { + name: "push empty string", + initial: []string{"hello"}, + push: "", + expected: []string{"hello", ""}, + expLen: 5, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + s := &stringArr{data: tt.initial, len: 0} + for _, str := range tt.initial { + s.len += len(str) + } + + s.Push(tt.push) + + assert.Equal(t, tt.expected, s.data) + assert.Equal(t, tt.expLen, s.len) + }) + } +} + +func TestStringArr_Pop(t *testing.T) { + tests := []struct { + name string + initial []string + expectedPop string + remaining []string + expLen int + }{ + { + name: "pop from array with multiple elements", + initial: []string{"hello", "world"}, + expectedPop: "world", + remaining: []string{"hello"}, + expLen: 5, + }, + { + name: "pop from array with one element", + initial: []string{"hello"}, + expectedPop: "hello", + remaining: []string{}, + expLen: 0, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + s := &stringArr{data: tt.initial, len: 0} + for _, str := range tt.initial { + s.len += len(str) + } + + popped := s.Pop() + + assert.Equal(t, tt.expectedPop, popped) + assert.Equal(t, tt.remaining, s.data) + assert.Equal(t, tt.expLen, s.len) + }) + } +} + +func TestStringArr_Unshift(t *testing.T) { + tests := []struct { + name string + initial []string + unshift string + expected []string + expLen int + }{ + { + name: "unshift to empty array", + initial: []string{}, + unshift: "hello", + expected: []string{"hello"}, + expLen: 5, + }, + { + name: "unshift to non-empty array", + initial: []string{"world"}, + unshift: "hello", + expected: []string{"hello", "world"}, + expLen: 10, + }, + { + name: "unshift empty string", + initial: []string{"world"}, + unshift: "", + expected: []string{"", "world"}, + expLen: 5, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + s := &stringArr{data: tt.initial, len: 0} + for _, str := range tt.initial { + s.len += len(str) + } + + s.Unshift(tt.unshift) + + assert.Equal(t, tt.expected, s.data) + assert.Equal(t, tt.expLen, s.len) + }) + } +} + +func TestStringArr_Shift(t *testing.T) { + tests := []struct { + name string + initial []string + expectedShift string + remaining []string + expLen int + }{ + { + name: "shift from array with multiple elements", + initial: []string{"hello", "world"}, + expectedShift: "hello", + remaining: []string{"world"}, + expLen: 5, + }, + { + name: "shift from array with one element", + initial: []string{"hello"}, + expectedShift: "hello", + remaining: []string{}, + expLen: 0, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + s := &stringArr{data: tt.initial, len: 0} + for _, str := range tt.initial { + s.len += len(str) + } + + shifted := s.Shift() + + assert.Equal(t, tt.expectedShift, shifted) + assert.Equal(t, tt.remaining, s.data) + assert.Equal(t, tt.expLen, s.len) + }) + } +} + +func TestStringArr_String(t *testing.T) { + tests := []struct { + name string + data []string + expected string + }{ + { + name: "empty array", + data: []string{}, + expected: "", + }, + { + name: "single element", + data: []string{"hello"}, + expected: "hello", + }, + { + name: "multiple elements", + data: []string{"hello", " ", "world"}, + expected: "hello world", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + s := &stringArr{data: tt.data} + assert.Equal(t, tt.expected, s.String()) + }) + } +} + +func TestPruneHTML(t *testing.T) { + tests := []struct { + name string + html string + maxLength int + expected string + }{ + { + name: "simple text within limit", + html: "

Hello

", + maxLength: 20, + expected: "

Hello

", + }, + { + name: "text exceeding limit", + html: "

Hello world, this is a long text

", + maxLength: 15, + expected: "

...

", + }, + { + name: "nested tags within limit", + html: "

Hello

", + maxLength: 30, + expected: "

Hello

", + }, + { + name: "nested tags exceeding limit", + html: "

Hello world

More text

", + maxLength: 20, + expected: "
...
", + }, + { + name: "with comment", + html: "

Hello

", + maxLength: 20, + expected: "

Hello

", + }, + { + name: "self-closing tag", + html: "

Hello
World

", + maxLength: 20, + expected: "

Hello
...

", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + result := pruneHTML(tt.html, tt.maxLength) + assert.Equal(t, tt.expected, result) + }) + } +} + +func TestPruneStringToWord(t *testing.T) { + tests := []struct { + name string + text string + maxLength int + expected string + }{ + { + name: "within limit", + text: "hello world", + maxLength: 15, + expected: "hello world", + }, + { + name: "exact limit", + text: "hello world", + maxLength: 11, + expected: "hello", + }, + { + name: "cut at word boundary", + text: "hello world and more", + maxLength: 11, + expected: "hello", + }, + { + name: "zero length", + text: "hello", + maxLength: 0, + expected: "", + }, + { + name: "negative length", + text: "hello", + maxLength: -1, + expected: "", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + result := pruneStringToWord(tt.text, tt.maxLength) + assert.Equal(t, tt.expected, result) + }) + } +}