Skip to content

An idea for tests #174

Closed
Closed
@johnlepikhin

Description

@johnlepikhin

As a consequence of issue #169, I just wanted to suggest a technique of testing parsers and printers. Parsing and printing (serializers and deserializers) are mutually inverse functions: the result of print(parse(some_text)) should be some_text itself – in case if some_text is well-formatted. Thus, it is really easy to write parser+printer tests literally in declarative style:

let cases = [
  ("**some bold text**", "should support bold text"),
  "plain text\n\nwith newlines", "should support text with newlines"),
  ...
  ...
];

#[test]
fn roundtrip() {
  for (orig_md_text, error_message) in cases {
    let ast = markdown::to_mdast(&orig_md_text, &markdown::ParseOptions::default()).unwrap();
    let formatted_md_text = to(&ast).unwrap();
    assert_eq!(formatted_md_text, orig_md_text, error_message)
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions