trycmd icon indicating copy to clipboard operation
trycmd copied to clipboard

Structured data (like json, ndjson, toml, yaml) asserts in snapbox

Open epage opened this issue 2 years ago • 3 comments

Like the value from special handling of text vs binary, asserting with knowledge of structured data would help gloss over irrelevant details and allow smarter wildcards

  • [x] json (#106)
  • [x] jsonlines (#277)
  • [ ] toml

epage avatar Apr 22 '22 12:04 epage

A basic way of doing this is

  • Have feature flags per file format
  • Have path asserts check for file type and deserialize the file according to any enabled formats
  • For matches, instead of equality, create structured wildcards (wildcards that match entire subsets of the data). See cargo-test-support for ideas

epage avatar Apr 22 '22 12:04 epage

Besides a {..}, should we have {VAR}s that we deserialize and substitute?

epage avatar Apr 22 '22 12:04 epage

After some discussion with @epage, we found the following blockers:

  • [x] ~~A DataFormat enum should be created to hold all supported data formats~~ (#109)

  • [x] ~~as_bytes should be renamed to to_bytes and it should output Vec<u8>~~ (#110)

  • [x] ~~A function should be created, that outputs the current DataFormat of the Data it is called on (format(&self) -> DataFormat). Usage: expected.format()~~ (#111)

  • [x] ~~A try_coerce function should be created that takes in a DataFormat that the Data will try to be turned into (try_coerce(self, format: DataFormat) -> Self). If it fails to coerce into the expected data format the old one should be returned. Usage: actual.try_coerce(expected.format())~~ (#111)

  • [x] ~~calls to try_text in assert.rs and path.rs should be updated to use try_coerce~~ (#111)

  • [X] ~~schema.rs should change calls to Data to std::fs::read_string + normalize the newlines~~ (#122)

  • [x] ~~rename map_text to normalize. It should then take in a normalization operation / algorithm (NormalizeNewlines, NormalizeMatches, etc)~~ (#122)

  • [x] ~~calls to map_text in assert.rs and path.rs should instead call normalize~~ (#122)

  • [x] ~~as_str should be renamed to render and it should output Option<String>. The original intent of as_str was for outputting Diffs. This should be changed to String since there are some formats that have borrowing issues with &str.~~ (#122)

  • [ ] A function to detect the format of the given Data should be created (detect(&self, supported_formats: Vec<DataFormat>) -> DataFormat), where supported_formats is runtime coerced formats, controlled by user). Usage: expected.detect(supported_formats)

Muscraft avatar Jul 18 '22 18:07 Muscraft