Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Strengthen test_add_duplicate_student to detect overwrites in student-grades-tracker#186

Open
HenriTEL wants to merge 1 commit into
rustfinity:mainrustfinity/rustfinity:mainfrom
HenriTEL:mainHenriTEL/rustfinity:mainCopy head branch name to clipboard
Open

Strengthen test_add_duplicate_student to detect overwrites in student-grades-tracker#186
HenriTEL wants to merge 1 commit into
rustfinity:mainrustfinity/rustfinity:mainfrom
HenriTEL:mainHenriTEL/rustfinity:mainCopy head branch name to clipboard

Conversation

@HenriTEL
Copy link
Copy Markdown

Fix test_add_duplicate_student so that it properly checks for overwrites.

A solution like the following should not pass:

use std::collections::HashMap;

pub struct Student {
    pub name: String,
    pub grades: Vec<u8>,
}

pub struct StudentGrades {
    pub students: HashMap<String, Student>,
}

impl StudentGrades {
    pub fn new() -> Self {
        Self {
            students: HashMap::new(),
        }
    }

    pub fn add_student(&mut self, name: &str) {
        self.students.insert(
            name.to_string(),
            Student {
                name: name.to_string(),
                grades: vec![]
            }
        );
    }

    pub fn add_grade(&mut self, name: &str, grade: u8) {
        if let Some(student) = self.students.get_mut(name) {
            student.grades.push(grade);
        }
    }

    pub fn get_grades(&self, name: &str) -> &[u8] {
        if let Some(student) = self.students.get(name) {
            student.grades.as_slice()
        } else {
            &[]
        }
    }
}

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 30, 2026

Review Change Stack

Walkthrough

The test now mutates the existing "Alice" entry (sets name to "Original Alice"), calls add_student("Alice") again, and then asserts the map size and key presence are unchanged and that the stored "Alice" entry’s name remains "Original Alice".

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description check ✅ Passed The description clearly relates to the changeset by explaining the test fix, the problem being addressed (detecting overwrites), and providing context for why the change is necessary.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title directly and specifically describes the main change: strengthening the test_add_duplicate_student test to detect overwrites, which matches the core objective of the PR.

✏️ 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

@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 current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@challenges/student-grades-tracker/tests/tests.rs`:
- Around line 14-19: The test should not rely on pointer identity (which HashMap
does not guarantee); instead, ensure duplicate insertion preserves the student's
observable state: before calling tracker.add_student("Alice") a second time,
mutate Alice (e.g., call tracker.add_grade("Alice", grade) or push into the
Student.grades via tracker.students.get_mut("Alice")), then call
tracker.add_student("Alice") and assert the student's data is unchanged by
comparing the Student record or its grades vector (use
tracker.students.get("Alice").unwrap() and check fields like grades) and also
assert len stayed 1 and contains_key("Alice"); update the test to remove the
pointer comparisons (before/after pointers) and replace them with these
value-based assertions referencing tracker, add_student, Student, and the grades
field or add_grade method.
🪄 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: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 426f603b-9c4e-4f92-a0d6-5f90c20b3d61

📥 Commits

Reviewing files that changed from the base of the PR and between a2179b0 and 9e0af51.

📒 Files selected for processing (1)
  • challenges/student-grades-tracker/tests/tests.rs

Comment thread challenges/student-grades-tracker/tests/tests.rs Outdated
@HenriTEL HenriTEL marked this pull request as draft May 30, 2026 07:19
Fix `test_add_duplicate_student` so that it properly check for overwrites.

A solution like the following should not pass:

```rust
use std::collections::HashMap;

pub struct Student {
    pub name: String,
    pub grades: Vec<u8>,
}

pub struct StudentGrades {
    pub students: HashMap<String, Student>,
}

impl StudentGrades {
    pub fn new() -> Self {
        Self {
            students: HashMap::new(),
        }
    }

    pub fn add_student(&mut self, name: &str) {
        self.students.insert(
            name.to_string(),
            Student {
                name: name.to_string(),
                grades: vec![]
            }
        );
    }

    pub fn add_grade(&mut self, name: &str, grade: u8) {
        if let Some(student) = self.students.get_mut(name) {
            student.grades.push(grade);
        }
    }

    pub fn get_grades(&self, name: &str) -> &[u8] {
        if let Some(student) = self.students.get(name) {
            student.grades.as_slice()
        } else {
            &[]
        }
    }
}
```
@HenriTEL HenriTEL marked this pull request as ready for review May 30, 2026 08:15
Copy link
Copy Markdown

@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 current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@challenges/student-grades-tracker/tests/tests.rs`:
- Around line 14-18: The test currently mutates the internal tracker.students
map and Student.name directly; instead, remove any direct access to
tracker.students and Student.name and verify duplicate handling via the public
API: create the student with tracker.add_student("Alice"), use the public method
to add a grade (e.g., tracker.add_grade("Alice", ...)), call
tracker.add_student("Alice") again, then assert the student still exists and
that the previously added grades are preserved by querying through the public
getter (e.g., tracker.get_grades("Alice") or the equivalent public accessor).
Ensure all assertions use add_student, add_grade, and the public grade retrieval
methods rather than inspecting internal fields.
🪄 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: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 0a0fdabc-56aa-4606-a02e-48e3530beffc

📥 Commits

Reviewing files that changed from the base of the PR and between 9e0af51 and d44c423.

📒 Files selected for processing (1)
  • challenges/student-grades-tracker/tests/tests.rs

Comment thread challenges/student-grades-tracker/tests/tests.rs
@HenriTEL HenriTEL changed the title Update tests.rs in student-grades-tracker Strengthen test_add_duplicate_student to detect overwrites in student-grades-tracker May 30, 2026
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.

1 participant

Morty Proxy This is a proxified and sanitized view of the page, visit original site.