Skip to content

Duplicated formula validation type removal - #419

Merged
MathNya merged 3 commits into
MathNya:masterfrom
c-git:duplicated-formula-type-removal
Nov 27, 2026
Merged

MathNya merged 3 commits into
MathNya:masterfrom
c-git:duplicated-formula-type-removal

Conversation

@c-git

@c-git c-git commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Not ready for review. Will turn it to ready after #418 lands an I rebase this PR to clean up the diff. Landed.

Created this PR to demonstrate what I meant. It's not complete for two reasons.

  1. There is currently no test coverage for the TAGS. There is one test that calls set_attribute and write_to but it doesn't seem like it validates any expected value so it passes even if I use the wrong tag.
  2. I branched from another PR that renames the files in this PR. So I'd need to wait for that one to land before I can land this one as I'd need to rebase it to clean up this PR.

Closes: #416

I guess the code won't be worse that it was before without the tests but it feels weird to add code with no tests. I'm not quite sure how to test it though. Maybe we could look for the value in the existing test? I don't know enough to know if that makes sense.

My naive idea is to use insta to save a snapshot of the value read so if the tag were to be wrong it wouldn't get the value. I would use insta because it would make it easier if the expected value changes later.

@c-git
c-git force-pushed the duplicated-formula-type-removal branch from bc6e08d to d51201f Compare November 25, 2026 04:17
@c-git
c-git marked this pull request as ready for review November 25, 2026 05:10
@MathNya

MathNya commented Nov 25, 2026

Copy link
Copy Markdown
Owner

@c-git
Thanks for the cleanup, the macro looks good to me.

On the testing question: I'd rather not add insta just for this. A plain assertion on the written XML is enough to catch a wrong tag, and it matches what we already do in src/structs/data_validation.rs (write_keeps_both_formulas).

One thing I noticed while trying it: checking the value after reading does not catch a wrong tag. DataValidation::set_attributes picks DataValidationFormula1/2 by matching the hard-coded x14:formula1/x14:formula2 names, and the generated set_attributes returns as soon as it sees <xm:f>. So TAG is only really used on the write side. With the two tags swapped in the macro calls, a read-based test still passes. The write-side test below fails.

Could you add something like this to src/structs/office2010/excel/data_validation.rs?

#[cfg(test)]
mod tests {
    use super::*;
    use crate::structs::{
        Address,
        office::excel::Formula,
    };

    fn formula(address: &str) -> Formula {
        let mut a = Address::default();
        a.set_address(address);
        let mut f = Formula::default();
        f.set_value(a);
        f
    }

    #[test]
    fn write_uses_correct_formula_tags() {
        let mut dv = DataValidation::default();
        let mut f1 = DataValidationFormula1::default();
        f1.set_value(formula("Sheet1!$A$1"));
        let mut f2 = DataValidationFormula2::default();
        f2.set_value(formula("Sheet1!$B$1"));
        dv.set_formula1(f1).set_formula2(f2);

        let mut writer = Writer::new(Cursor::new(Vec::new()));
        dv.write_to(&mut writer);
        let xml = String::from_utf8(writer.into_inner().into_inner()).unwrap();
        assert!(
            xml.contains(
                "<x14:formula1><xm:f>Sheet1!$A$1</xm:f></x14:formula1>\
                 <x14:formula2><xm:f>Sheet1!$B$1</xm:f></x14:formula2>"
            ),
            "{xml}"
        );
    }
}

If we want snapshot testing for the XML writers more broadly, I'm open to discussing insta in a separate issue.

@c-git

c-git commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor Author

I agree, unless it is going to be used broadly it wouldn't make sense just for this one test. (It's just my go to anytime I'm going to assert a specific value that I might want to update in the future). Will test the code you provided thank you so much for your help.

PS. Always feel free to edit any of my PRs as you see fit. I always enable maintainer edits.

@c-git

c-git commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Added. Thank you very much I'm much more comfortable with the test in place.

@c-git c-git mentioned this pull request Nov 25, 2026
@MathNya

MathNya commented Nov 27, 2026

Copy link
Copy Markdown
Owner

@c-git
Thank you for your assistance.
I’ll go ahead and merge it.

@MathNya
MathNya merged commit cfc8349 into MathNya:master Nov 27, 2026
6 checks passed
@c-git
c-git deleted the duplicated-formula-type-removal branch November 27, 2026 11:33
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.

Duplicate Formula code

2 participants