Skip to content

Commit

Permalink
test: add unit tests for ReadContent function with bufio.Reader
Browse files Browse the repository at this point in the history
- Add unit tests for the ReadContent function using bufio.Reader to handle various input scenarios.
- Test cases include single line content, multiple lines content, empty content, and content with EOF error.
- Organize test cases using t.Run for better readability and maintainability.
  • Loading branch information
supitsdu committed Jun 29, 2024
1 parent 4a8a117 commit 2e91ad3
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions tests/clipper_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package clipper

import (
"bufio"
"os"
"strings"
"testing"

"github.com/atotto/clipboard"
Expand Down Expand Up @@ -94,6 +96,53 @@ func TestClipboardWriter(t *testing.T) {
})
}

func TestReadContent(t *testing.T) {
tests := []struct {
name string
input string
expected string
expectError bool
}{
{
name: "Single line content",
input: "Hello, World!",
expected: "Hello, World!",
expectError: false,
},
{
name: "Multiple lines content",
input: "Hello, World!\nThis is a test.",
expected: "Hello, World!\nThis is a test.",
expectError: false,
},
{
name: "Empty content",
input: "",
expected: "",
expectError: false,
},
{
name: "Content with EOF error",
input: "Hello, World!",
expected: "Hello, World!",
expectError: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
reader := bufio.NewReader(strings.NewReader(tt.input))
output, err := clipper.ReadContent(reader)
if (err != nil) != tt.expectError {
t.Fatalf("ReadContent() error = %v, expectError %v", err, tt.expectError)
}
if output != tt.expected {
t.Errorf("ReadContent() = %v, want %v", output, tt.expected)
}
})
}
}

func TestParseContent(t *testing.T) {
t.Run("MultipleFiles", func(t *testing.T) {
// Prepare test data with content from two files
Expand Down

0 comments on commit 2e91ad3

Please sign in to comment.