Skip to content

Commit

Permalink
test: add unit tests for content parsing and input reading (#17)
Browse files Browse the repository at this point in the history
- Add tests for `copyToClipboard` function to verify clipboard operations.
- Add tests for `readFromStdin` function to ensure proper stdin reading.
- Add tests for `readFromFile` function to verify file reading functionality.
- Add tests for `parseContent` function to check parsing of direct text input, stdin, and file inputs.

These tests enhance the robustness of the application by ensuring all core functionalities are working as expected.
  • Loading branch information
supitsdu authored Jun 25, 2024
1 parent 0b0c13e commit 0beb06d
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,60 @@ func TestCopyFromCommandLineArgument(t *testing.T) {
t.Errorf("Expected %s but got %s", content, copied)
}
}

func TestParseContentDirectText(t *testing.T) {
directText := "Direct text input"
contentStr, err := parseContent(&directText, []string{})
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
if contentStr != directText {
t.Errorf("Expected contentStr to be '%s', got '%s'", directText, contentStr)
}
}

func TestParseContentStdin(t *testing.T) {
stdinInput := "Stdin input\n"
r, w, _ := os.Pipe()
originalStdin := os.Stdin
defer func() {
os.Stdin = originalStdin
r.Close()
w.Close()
}()
os.Stdin = r

// Write the input to the pipe
w.WriteString(stdinInput)
w.Close()

contentStr, err := parseContent(nil, []string{})
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
if contentStr != stdinInput {
t.Errorf("Expected contentStr to be '%s', got '%s'", stdinInput, contentStr)
}
}

func TestParseContentFromFile(t *testing.T) {
contentFromFile := "Content from file"
fakeFile := createTempFile(contentFromFile)
defer os.Remove(fakeFile.Name())

contentStr, err := parseContent(nil, []string{fakeFile.Name()})
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
if contentStr != contentFromFile+"\n" {
t.Errorf("Expected contentStr to be '%s', got '%s'", contentFromFile, contentStr)
}
}

// Helper function to create temporary file for testing
func createTempFile(content string) *os.File {
file, _ := os.CreateTemp("", "testfile")
file.WriteString(content)
file.Close()
return file
}

0 comments on commit 0beb06d

Please sign in to comment.