Skip to content

test(parser): Add 36 new tests for untested functions - #766

Merged
robcohen merged 3 commits into
mainfrom
add-parser-tests
Apr 11, 2026
Merged

robcohen merged 3 commits into
mainfrom
add-parser-tests

Conversation

@robcohen

Copy link
Copy Markdown
Member

Summary

Adds 36 new tests for previously untested parser functions identified by coverage analysis.

Changes

High Priority (Core Parsing): 10 tests

  • Date parsing (year shortcuts, single digit month/day)
  • String escape processing
  • Signed number parsing
  • Flag parsing (*, !)
  • Boolean option parsing
  • Arithmetic expressions

Medium Priority (Directive Parsing): 9 tests

  • Pushtag/poptag directives
  • Pushmeta/popmeta directives
  • Close, commodity, pad, event, note, document, price directives

Low Priority (Edge Cases): 17 tests

  • Cost spec parsing (per-unit, date, label)
  • Price annotations (@, @@)
  • Standalone comments

Coverage Impact

File Before After Gain
winnow_parser.rs 49.35% 71.19% +21.84%
Total parser 40.88% 51.13% +10.25%

Testing

  • All 87 tests pass ✅
  • No breaking changes
  • Tests follow naming convention: test__

Added tests for high, medium, and low priority untested functions:

HIGH PRIORITY (Core Parsing):
- test_parse_date_year_shortcut
- test_parse_date_single_digit_month
- test_process_string_escapes
- test_parse_signed_number_positive/negative
- test_parse_flag_star/exclamation
- test_parse_option_boolean_true/false
- test_parse_arithmetic_multiplication/parentheses
- test_parse_incomplete_amount_number_only

MEDIUM PRIORITY (Directive Parsing):
- test_parse_pushtag_and_poptag_directive
- test_parse_poptag_without_push_errors
- test_parse_pushmeta_and_popmeta_directive
- test_parse_close_directive
- test_parse_commodity_directive
- test_parse_pad_directive
- test_parse_event_directive
- test_parse_note_directive
- test_parse_document_directive
- test_parse_price_directive

LOW PRIORITY (Edge Cases):
- test_parse_cost_spec_per_unit/date/label
- test_parse_price_annotation_unit/total
- test_parse_standalone_comment
- test_parse_multiple_standalone_comments

Coverage Impact:
- winnow_parser.rs: 49.35% → 71.19% (+21.84%)
- Total parser: 40.88% → 51.13% (+10.25%)

All 87 tests pass.
Copilot AI review requested due to automatic review settings April 11, 2026 17:00
@robcohen
robcohen requested a review from a team as a code owner April 11, 2026 17:00
@codecov

codecov Bot commented Apr 11, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.54237% with 22 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
crates/rustledger-parser/src/winnow_parser.rs 92.54% 22 Missing ⚠️

📢 Thoughts on this report? Let us know!

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new batch of unit tests in winnow_parser.rs intended to increase coverage for previously untested parser behaviors (core parsing, directives, and edge cases).

Changes:

  • Added many new parser tests grouped by “high/medium/low priority” areas.
  • Introduced new test cases for flags, arithmetic expressions, directives (e.g., close/commodity/price), cost specs, price annotations, and standalone comments.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@github-actions

github-actions Bot commented Apr 11, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Tool Time Memory
rustledger 40.5ms ± 2ms 23.1 MB
beancount 765.8ms ± 5.3ms 50.3 MB
Comparison 18.9x faster 2.2x less

vs main branch (2026-04-11)

Metric main this PR Change
Time 41.9ms 40.5ms -3.3% ✅
10K transactions · 5 runs, 2 warmup · hyperfine
What do these numbers mean?
  • Time: Wall-clock time to parse and validate the ledger (± std dev)
  • Memory: Peak resident set size (RSS)
  • vs main: Compared to latest nightly benchmark on main branch

@github-actions

github-actions Bot commented Apr 11, 2026 •

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

Metric Result
Check Compatibility 99%
BQL Compatibility 100%
Full AST Match 98%
Total Files 784
What's tested
  • Check: Exit codes and error counts match bean-check
  • BQL: Query results match bean-query
  • Files: ~800 test files downloaded from 10+ beancount repositories

Full results available in workflow artifacts

robcohen and others added 2 commits April 11, 2026 12:17
Copilot caught 14 tests in this PR that compiled and passed but didn't
actually exercise the parser code they claimed to test. The pattern
fell into four categories:

1. Always-true assertions. `assert!(result.directives.len() >= 0)` on
   a `usize` is a no-op; the test passes even when parsing completely
   fails. Affected: date_single_digit, signed_number_{positive,negative},
   incomplete_amount_number_only, pushmeta_popmeta, standalone_comment
   (weaker form).

2. Input that never reaches the target code path. Some tests fed
   source the parser routes through a different function than the one
   under test:
   - `test_parse_date_year_shortcut` tried `24-01-15`, but the lexer
     requires `\d{4}` and rejects 2-digit years. Renamed and inverted
     to pin the rejection.
   - `test_parse_signed_number_{positive,negative}` used bare top-level
     `+100` / `-50.00`, which don't go through `parse_signed_number`
     at all. Consolidated into a single test that exercises it via a
     balance tolerance (`balance <account> 100 ~ -1 USD`), the actual
     caller of `parse_signed_number`.
   - `test_parse_option_boolean_{true,false}` claimed to test
     `parse_boolean` but `option` directives store raw strings and
     never call `parse_boolean`. Renamed to reflect option string
     handling, and added a new `test_parse_boolean_metadata_value`
     that actually exercises `parse_boolean` via metadata values
     (`key: TRUE` / `key: FALSE` on an `open` directive).

3. Invalid input silently accepted. Six directive tests used source
   that failed to parse, hidden by the always-true assertion:
   - `pad` was missing the source account.
   - `event` was missing the second string.
   - `note` was missing the account.
   - `document` was missing the account.
   - `pushmeta`/`popmeta` used value syntax that
     `parse_meta_value` / `parse_meta_key` don't accept.
   Fixed each to produce valid syntax and assert the parsed directive
   fields match expectations.

4. `if let` without `else` branch. Five tests checked fields inside
   `if let Directive::Transaction(txn)` (or `Balance`) but the `if
   let` silently skipped the assertion when the directive wasn't a
   transaction (or anything). Replaced with `match` + explicit
   `panic!` in the else arm, plus an exact `directives.len()` check.

Test behavior after this commit:

- `test_parse_signed_number_in_balance_tolerance` now actually
  exercises `parse_signed_number`. The balance grammar is `<number>
  [~ <tolerance>] <currency>`, so the tolerance goes between number
  and currency (not after currency — my first draft got this wrong
  and the test caught it).
- `test_parse_boolean_metadata_value` adds real `parse_boolean`
  coverage that wasn't there before.
- Every other test now fails loudly if the parser misbehaves in the
  scenario it claims to cover.

Net test count: unchanged (14 fixed + 1 new boolean test + 1 removed
signed-number duplicate = 14 → 14). What changed is that each test
now reliably fails when the thing it documents is broken.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@robcohen
robcohen enabled auto-merge April 11, 2026 17:21
@robcohen
robcohen added this pull request to the merge queue Apr 11, 2026
Merged via the queue into main with commit e2c9235 Apr 11, 2026
33 of 36 checks passed
@robcohen
robcohen deleted the add-parser-tests branch April 11, 2026 17:33
@rustledger-bot rustledger-bot Bot mentioned this pull request Apr 11, 2026
robcohen added a commit that referenced this pull request Apr 11, 2026
Bumps the whole workspace from 0.11.0 to 0.12.0 (minor version bump
under pre-1.0 semver because the cycle since 0.11.0 includes
user-visible breaking changes that release-plz's auto-detection
missed).

release-plz auto-generated a v0.11.1 patch bump from the `fix:` /
`feat:` commits since 0.11.0. That's too conservative. The cycle
since 0.11.0 shipped the following breaking changes, all merged as
`refactor:` / `fix:` without the `!` conventional-commit marker:

- **`rustledger_booking::BookingError`** collapsed three variants
  (`InsufficientUnits`, `NoMatchingLot`, `AmbiguousMatch`) into a
  single `Inventory(AccountedBookingError)` variant (#751). Out-of-tree
  consumers pattern-matching on the old variants break.

- **`rustledger_core::AccountedBookingError`** added as a new
  canonical user-facing type with `BookingError::with_account()` as
  the constructor (#751). Not a break on its own, but part of the
  unification.

- **`rustledger_lsp::handlers::diagnostics::all_diagnostics`**
  signature changed to take an additional `other_buffer_overlays:
  &[(u16, &[Spanned<Directive>])]` parameter for multi-buffer LSP
  overlays (#760 / #764).

- **`rustledger_lsp::handlers::diagnostics::validation_errors_to_diagnostics`**
  signature changed to take an owned `Vec<Spanned<Directive>>`
  instead of borrowed slices (#758). Optimizes a hot path but breaks
  the function's public signature.

- **`rustledger_loader::LoadError::IncludeCycle`** Display format
  rewritten to match Python beancount's `"Duplicate filename
  parsed: ..."` wording (#767). Substring-matching consumers break.

Under pre-1.0 semver, any breaking change should bump the minor
version (0.x → 0.(x+1)). So 0.11.0 → 0.12.0 is correct.

Highlights since 0.11.0:

### Breaking

- Unified `BookingError` under a single `Inventory` variant with one
  canonical `Display` impl, preventing the wording drift that
  produced #748 (#751).
- `LoadError::IncludeCycle` Display now begins with `"Duplicate
  filename parsed: "` to match beancount's wording for conformance
  (#767).
- LSP `all_diagnostics` and `validation_errors_to_diagnostics`
  signatures changed to support the multi-buffer overlay and avoid
  double-cloning directive lists (#758 / #764).

### Features

- LSP live overlay so in-memory buffer edits reflect in diagnostics
  before save, for both the current file (#685 / #758) and any other
  open buffer in a multi-file ledger (#760 / #764).
- Parser `Vec::with_capacity` hints with DoS caps on `source.len()`
  heuristics (#754).
- `working_inventories` cloning in the booking engine now only
  touches accounts actually referenced in the transaction, and
  dedupes repeated account references (#756).

### Fixes

- `InsufficientUnits` booking error wording restored to `"Not enough
  units in ..."` so pta-standards `reduction-exceeds-inventory`
  conformance assertion passes (#748 / #749).
- `noduplicates` plugin now hashes flag, tags, links, cost, and
  price in addition to date and narration — complete beancount
  `hash_entry` parity (#746 / #747).
- Parser test suite: 14 fake-coverage tests replaced with real tests
  that exercise the functions they claim to cover (#766).
- All Copilot inline review comments on #754-#763, #766, #767
  addressed in fix commits.
- Closed PRs #768 (dead incremental parser) and #769 (hallucinated
  simdutf8 API) — no code shipped from these.

### Internal

- Forbid-unsafe-code invariant now has a dedicated CI check and
  pre-push hook (#770).
- New pre-push `cargo check` hook to catch compile errors before PRs
  are opened (#770).

This commit supersedes release-plz's auto-generated v0.11.1 proposal
to produce the minor bump the breaking changes require.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants