test(visualize): add 39 tests covering full public API surface#433
test(visualize): add 39 tests covering full public API surface#433thepastaclaw wants to merge 1 commit into
Conversation
…rate Add 39 tests covering all public API surface: - to_hex: empty, short, medium, long (truncation), boundary cases - Drawer: write at various indent levels, down/up, flush, multiline handling - Visualize for [u8]: valid/invalid UTF-8, truncation at STR_LEN boundary - Visualize for Vec<u8>, &T, Option<T> (Some and None) - DebugBytes and DebugByteVectors: Debug formatting, Ord, Eq, Hash - visualize_to_vec, visualize_stderr, visualize_stdout - Integration tests combining Drawer indentation with Visualize trait The crate previously had 0 tests (61.70% coverage from downstream usage).
📝 WalkthroughWalkthroughA comprehensive test suite was added to Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (2)
visualize/src/lib.rs (2)
305-316: This test codifies newline stripping at indentation level 0.
drawer_writebehaving like a write proxy should generally preserve\nat level 0; asserting"line1line2"bakes in a surprising behavior and makes later correction harder.Suggested assertion if preserving newlines is the intended contract
- // At level 0, newlines are replaced with empty separator - assert_eq!(output, "line1line2"); + // At level 0, newlines should be preserved + assert_eq!(output, "line1\nline2");🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@visualize/src/lib.rs` around lines 305 - 316, The test drawer_down_up_returns_to_zero asserts that newlines are stripped at indentation level 0, but as a write proxy Drawer::write should preserve '\n' when indentation level is zero; update the test for Drawer (the drawer_down_up_returns_to_zero function) to expect retained newlines (e.g. "line1\nline2") after calling drawer.write(...) instead of "line1line2" so the behavior matches a no-op at level 0 and doesn't lock in the surprising newline-stripping behavior.
241-249:to_hextruncation assertion is too permissive.
starts_with+ends_with("0c")can pass with malformed middle/tail. This case is deterministic; assert the exact output.Stronger assertion
let result = to_hex(&bytes); -assert!(result.starts_with("01020304..")); -assert!(result.ends_with("0c")); +assert_eq!(result, "01020304..090a0b0c");🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@visualize/src/lib.rs` around lines 241 - 249, The test to_hex_long_bytes_truncated uses starts_with/ends_with which is too permissive; update the test to assert the exact deterministic output from to_hex for the given 12-byte input (replace the loose assertions in to_hex_long_bytes_truncated with a single equality assertion comparing result to the expected string "01020304..090a0b0c" so the middle/trailing content is checked exactly).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@visualize/src/lib.rs`:
- Around line 405-413: The test visualize_bytes_str_len_plus_one currently uses
a 33-byte input which locks in an off-by-one inclusive-slice behavior; update
the test to use exactly STR_LEN bytes (32) so it asserts the intended
non-truncation behavior, i.e., change the test input to a 32-byte string and
keep the assertion that output contains the full string produced by
visualize_to_vec; alternatively, if the intended contract is that STR_LEN+1
should be truncated, update the assertion to check that output contains only the
STR_LEN-length prefix plus the truncation marker used by the visualization code
instead of the full 33-char string.
---
Nitpick comments:
In `@visualize/src/lib.rs`:
- Around line 305-316: The test drawer_down_up_returns_to_zero asserts that
newlines are stripped at indentation level 0, but as a write proxy Drawer::write
should preserve '\n' when indentation level is zero; update the test for Drawer
(the drawer_down_up_returns_to_zero function) to expect retained newlines (e.g.
"line1\nline2") after calling drawer.write(...) instead of "line1line2" so the
behavior matches a no-op at level 0 and doesn't lock in the surprising
newline-stripping behavior.
- Around line 241-249: The test to_hex_long_bytes_truncated uses
starts_with/ends_with which is too permissive; update the test to assert the
exact deterministic output from to_hex for the given 12-byte input (replace the
loose assertions in to_hex_long_bytes_truncated with a single equality assertion
comparing result to the expected string "01020304..090a0b0c" so the
middle/trailing content is checked exactly).
| fn visualize_bytes_str_len_plus_one() { | ||
| let data = b"abcdefghijklmnopqrstuvwxyz1234567"; // 33 bytes | ||
| assert_eq!(data.len(), 33); | ||
| let mut buf = Vec::new(); | ||
| visualize_to_vec(&mut buf, data.as_slice()); | ||
| let output = String::from_utf8(buf).unwrap(); | ||
| // [..=STR_LEN] is inclusive, so 33 chars from a 33-char string = full | ||
| assert!(output.contains("str: abcdefghijklmnopqrstuvwxyz1234567")); | ||
| } |
There was a problem hiding this comment.
STR_LEN + 1 test is locking in an off-by-one boundary behavior.
This test currently expects no truncation for 33 chars even though STR_LEN is 32. It cements the inclusive-slice quirk instead of the intended boundary contract.
Suggested test expectation update
fn visualize_bytes_str_len_plus_one() {
let data = b"abcdefghijklmnopqrstuvwxyz1234567"; // 33 bytes
assert_eq!(data.len(), 33);
let mut buf = Vec::new();
visualize_to_vec(&mut buf, data.as_slice());
let output = String::from_utf8(buf).unwrap();
- // [..=STR_LEN] is inclusive, so 33 chars from a 33-char string = full
- assert!(output.contains("str: abcdefghijklmnopqrstuvwxyz1234567"));
+ // > STR_LEN should be truncated to STR_LEN chars
+ assert!(output.contains("str: abcdefghijklmnopqrstuvwxyz123456"));
+ assert!(!output.contains("str: abcdefghijklmnopqrstuvwxyz1234567"));
}📝 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.
| fn visualize_bytes_str_len_plus_one() { | |
| let data = b"abcdefghijklmnopqrstuvwxyz1234567"; // 33 bytes | |
| assert_eq!(data.len(), 33); | |
| let mut buf = Vec::new(); | |
| visualize_to_vec(&mut buf, data.as_slice()); | |
| let output = String::from_utf8(buf).unwrap(); | |
| // [..=STR_LEN] is inclusive, so 33 chars from a 33-char string = full | |
| assert!(output.contains("str: abcdefghijklmnopqrstuvwxyz1234567")); | |
| } | |
| fn visualize_bytes_str_len_plus_one() { | |
| let data = b"abcdefghijklmnopqrstuvwxyz1234567"; // 33 bytes | |
| assert_eq!(data.len(), 33); | |
| let mut buf = Vec::new(); | |
| visualize_to_vec(&mut buf, data.as_slice()); | |
| let output = String::from_utf8(buf).unwrap(); | |
| // > STR_LEN should be truncated to STR_LEN chars | |
| assert!(output.contains("str: abcdefghijklmnopqrstuvwxyz123456")); | |
| assert!(!output.contains("str: abcdefghijklmnopqrstuvwxyz1234567")); | |
| } |
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@visualize/src/lib.rs` around lines 405 - 413, The test
visualize_bytes_str_len_plus_one currently uses a 33-byte input which locks in
an off-by-one inclusive-slice behavior; update the test to use exactly STR_LEN
bytes (32) so it asserts the intended non-truncation behavior, i.e., change the
test input to a 32-byte string and keep the assertion that output contains the
full string produced by visualize_to_vec; alternatively, if the intended
contract is that STR_LEN+1 should be truncated, update the assertion to check
that output contains only the STR_LEN-length prefix plus the truncation marker
used by the visualization code instead of the full 33-char string.
|
Closing duplicate — superseded by #435 |
Summary
Adds 39 tests to the
grovedb-visualizecrate, which previously had zero tests (61.70% coverage came entirely from downstream usage).Part of the GroveDB code coverage improvement initiative (tracker #364).
Tests Added (39 total)
to_hexDrawerVisualizefor[u8]VisualizeforVec<u8>Visualizefor&TVisualizeforOption<T>DebugBytesDebugByteVectorsvisualize_to_vec,visualize_stderr,visualize_stdoutValidation
cargo test -p grovedb-visualize→ 39 passed, 0 failedcargo clippy -p grovedb-visualize --tests→ cleancargo fmt -p grovedb-visualize -- --check→ cleanSummary by CodeRabbit