Skip to content

Add unit tests for modules/subreddit (0% → 98.6% coverage) - #2166

Open
Seanstoppable wants to merge 1 commit into
wtfutil:trunkfrom
Seanstoppable:seanstoppable-subreddit-test-coverage
Open

Add unit tests for modules/subreddit (0% → 98.6% coverage)#2166
Seanstoppable wants to merge 1 commit into
wtfutil:trunkfrom
Seanstoppable:seanstoppable-subreddit-test-coverage

Conversation

@Seanstoppable

Copy link
Copy Markdown
Collaborator

Summary

Adds comprehensive unit tests for modules/subreddit, bringing statement coverage from 0% to 98.6%.

Test Files

  • subreddit_test.go — Tests for GetLinks API function:

    • URL construction for all sort modes (hot, new, rising, top)
    • Top time period query parameters
    • User-Agent header verification
    • HTTP error handling (4xx, 5xx status codes)
    • Connection refused errors
    • Invalid JSON responses
    • Empty children ("no links" error)
    • Large response handling
  • widget_test.go — Tests for widget display logic:

    • content() rendering: title format, error display, link formatting, special character escaping
    • Refresh(): numberOfPosts truncation, fewer-than-requested, exact count, error paths
    • openLink()/openReddit(): valid selection, invalid selection, nil links, out-of-bounds
  • settings_test.go — Tests for YAML settings parsing:

    • All fields specified vs defaults applied
    • ConfigText() output

Approach

Uses net/http/httptest to mock the Reddit JSON API by overriding the package-level rootPage variable. All tests are table-driven.

Add comprehensive tests covering:
- GetLinks API: URL construction, sort modes, top time periods, User-Agent header
- HTTP error handling: 4xx/5xx status codes, connection errors, invalid JSON
- Link/RedditDocument JSON parsing with edge cases
- Widget content rendering: title format, error display, link formatting
- Widget Refresh: numberOfPosts truncation, fewer-than-requested, errors
- Widget openLink/openReddit: valid/invalid selection, nil links
- Settings: YAML parsing with defaults, all field overrides
- ConfigText output

Uses net/http/httptest with table-driven tests throughout.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant