Thanks to visit codestin.com
Credit goes to github.com

Skip to content

test(position): add should_panic tests for documented panic conditions#1168

Open
MasterOfFeelingFish wants to merge 1 commit into
pest-parser:masterfrom
MasterOfFeelingFish:clawoss/test/panic-conditions
Open

test(position): add should_panic tests for documented panic conditions#1168
MasterOfFeelingFish wants to merge 1 commit into
pest-parser:masterfrom
MasterOfFeelingFish:clawoss/test/panic-conditions

Conversation

@MasterOfFeelingFish
Copy link
Copy Markdown
Contributor

@MasterOfFeelingFish MasterOfFeelingFish commented Apr 19, 2026

Three panic conditions in \Position\ were documented but had no tests:

  • \span()\ panics when positions come from different inputs
  • \line_col()\ panics when position is out of bounds
  • \line_of()\ panics when position is out of bounds

Added #[should_panic]\ tests for each. The out-of-bounds tests use
ew_internal\ to construct invalid positions since the public \Position::new()\ validates bounds.

Related to #999

Summary by CodeRabbit

  • Tests
    • Enhanced test coverage for boundary validation in position handling, ensuring robustness of error conditions.

@MasterOfFeelingFish MasterOfFeelingFish requested a review from a team as a code owner April 19, 2026 04:10
@MasterOfFeelingFish MasterOfFeelingFish requested review from tomtau and removed request for a team April 19, 2026 04:10
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 19, 2026

📝 Walkthrough

Walkthrough

Three new unit tests added to verify panic behavior in Position methods. Tests confirm that span() panics when positions derive from different inputs, while line_col() and line_of() panic for out-of-bounds positions. No production code modifications.

Changes

Cohort / File(s) Summary
Panic Behavior Tests
pest/src/position.rs
Added three unit tests: span_panic_different_inputs, line_col_panic_out_of_bounds, and line_of_panic_out_of_bounds to verify expected panic messages and behavior in boundary/error scenarios.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Poem

🐰 Three panic tests now hop and play,
Catching bounds that go astray,
Span and lines in harmony,
Validation sets our code free! 🌿

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: adding should_panic tests for documented panic conditions in the Position module.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@pest/src/position.rs`:
- Around line 674-688: The tests currently call Position::new_internal(input, 5)
which triggers its debug_assert before reaching line_col()/line_of(); instead
construct a Position that bypasses new_internal so the methods themselves
perform the bounds check and panic with "position out of bounds". Replace the
calls to Position::new_internal with a direct construction that sets the
internal input and offset (e.g., create the same backing string/arc/rc used by
Position and instantiate a Position with pos/offset = 5), or use any existing
unsafe/unchecked constructor if provided, then call pos.line_col() and
pos.line_of() so the expected panic message is produced (refer to
Position::new_internal, line_col, line_of to locate the code).
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 21760a66-242d-4b03-be15-ca367b5f21af

📥 Commits

Reviewing files that changed from the base of the PR and between 2a69a98 and 18b9cf6.

📒 Files selected for processing (1)
  • pest/src/position.rs

Comment thread pest/src/position.rs
Comment on lines +674 to +688
#[test]
#[should_panic(expected = "position out of bounds")]
fn line_col_panic_out_of_bounds() {
let input = "ab";
let pos = Position::new_internal(input, 5);
pos.line_col();
}

#[test]
#[should_panic(expected = "position out of bounds")]
fn line_of_panic_out_of_bounds() {
let input = "ab";
let pos = Position::new_internal(input, 5);
pos.line_of();
}
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.

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Description: Confirm the invalid-position tests call new_internal despite its debug_assert invariant check.
rg -n -C2 'fn new_internal|debug_assert!\(input\.get\(pos\.\.\)\.is_some\(\)\)|Position::new_internal\(input, 5\)' pest/src/position.rs

Repository: pest-parser/pest

Length of output: 700


🏁 Script executed:

rg -n "fn line_col|fn line_of" pest/src/position.rs | head -20

Repository: pest-parser/pest

Length of output: 388


🏁 Script executed:

rg -n -A 20 "fn line_col\(" pest/src/position.rs | grep -E "panic|unwrap"

Repository: pest-parser/pest

Length of output: 957


🏁 Script executed:

rg -n -A 20 "fn line_of\(" pest/src/position.rs | grep -E "panic|unwrap"

Repository: pest-parser/pest

Length of output: 947


🏁 Script executed:

rg -n -B 2 "struct Position" pest/src/position.rs | head -20

Repository: pest-parser/pest

Length of output: 212


🏁 Script executed:

rg -n -A 5 "pub struct Position" pest/src/position.rs

Repository: pest-parser/pest

Length of output: 166


🏁 Script executed:

rg -n "mod tests|#\[cfg\(test\)\]" pest/src/position.rs

Repository: pest-parser/pest

Length of output: 92


🏁 Script executed:

rg -n -A 10 "pub struct Position" pest/src/position.rs | head -15

Repository: pest-parser/pest

Length of output: 440


Bypass new_internal to reach the intended panic message in these tests.

The tests on lines 678 and 686 call Position::new_internal(input, 5) for input "ab", but that constructor has a debug_assert!(input.get(pos..).is_some()) that will panic before line_col() or line_of() execute. The debug assertion will panic with an assertion failure message, not the expected "position out of bounds", causing the #[should_panic] check to fail.

Fix
     fn line_col_panic_out_of_bounds() {
         let input = "ab";
-        let pos = Position::new_internal(input, 5);
+        let pos = Position { input, pos: 5 };
         pos.line_col();
     }
@@
     fn line_of_panic_out_of_bounds() {
         let input = "ab";
-        let pos = Position::new_internal(input, 5);
+        let pos = Position { input, pos: 5 };
         pos.line_of();
     }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
#[test]
#[should_panic(expected = "position out of bounds")]
fn line_col_panic_out_of_bounds() {
let input = "ab";
let pos = Position::new_internal(input, 5);
pos.line_col();
}
#[test]
#[should_panic(expected = "position out of bounds")]
fn line_of_panic_out_of_bounds() {
let input = "ab";
let pos = Position::new_internal(input, 5);
pos.line_of();
}
#[test]
#[should_panic(expected = "position out of bounds")]
fn line_col_panic_out_of_bounds() {
let input = "ab";
let pos = Position { input, pos: 5 };
pos.line_col();
}
#[test]
#[should_panic(expected = "position out of bounds")]
fn line_of_panic_out_of_bounds() {
let input = "ab";
let pos = Position { input, pos: 5 };
pos.line_of();
}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@pest/src/position.rs` around lines 674 - 688, The tests currently call
Position::new_internal(input, 5) which triggers its debug_assert before reaching
line_col()/line_of(); instead construct a Position that bypasses new_internal so
the methods themselves perform the bounds check and panic with "position out of
bounds". Replace the calls to Position::new_internal with a direct construction
that sets the internal input and offset (e.g., create the same backing
string/arc/rc used by Position and instantiate a Position with pos/offset = 5),
or use any existing unsafe/unchecked constructor if provided, then call
pos.line_col() and pos.line_of() so the expected panic message is produced
(refer to Position::new_internal, line_col, line_of to locate the code).

Copy link
Copy Markdown
Contributor

@tomtau tomtau left a comment

Choose a reason for hiding this comment

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

looks like the expected panic message may be different?

Comment thread pest/src/position.rs
Comment on lines +674 to +688
#[test]
#[should_panic(expected = "position out of bounds")]
fn line_col_panic_out_of_bounds() {
let input = "ab";
let pos = Position::new_internal(input, 5);
pos.line_col();
}

#[test]
#[should_panic(expected = "position out of bounds")]
fn line_of_panic_out_of_bounds() {
let input = "ab";
let pos = Position::new_internal(input, 5);
pos.line_of();
}
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.

these two tests don't seem to pass

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