tests.rs raw

use super::*;
use crate::sexpr;

fn parse(source: &str) -> Result<Vec<PathCmd>, Diagnostic> {
    let doc = sexpr::read(source).expect("reads");
    let form = &doc.values[0];
    let items = form.as_list().expect("list");
    parse_path_form(items, form.span()).expect("a path form")
}

#[test]
fn sexpr_path_commands() {
    let cmds = parse("(path (move 0 0) (line 1 0.5) (cubic 0.1 0.2 0.3 0.4 0.5 0.6) (close))")
        .expect("valid");
    assert_eq!(
        cmds,
        vec![
            PathCmd::Move(0.0, 0.0),
            PathCmd::Line(1.0, 0.5),
            PathCmd::Cubic(0.1, 0.2, 0.3, 0.4, 0.5, 0.6),
            PathCmd::Close,
        ]
    );
}

#[test]
fn svg_absolute_matches_sexpr() {
    let svg = parse(r##"(svg-path "M0 0 L1 0.5 Z")"##).expect("valid");
    assert_eq!(
        svg,
        vec![
            PathCmd::Move(0.0, 0.0),
            PathCmd::Line(1.0, 0.5),
            PathCmd::Close
        ]
    );
}

#[test]
fn svg_relative_and_implicit_repeat() {
    // `m` then two implicit line segments (repeated as relative L).
    let svg = parse(r##"(svg-path "m1 1 1 0 0 1")"##).expect("valid");
    assert_eq!(
        svg,
        vec![
            PathCmd::Move(1.0, 1.0),
            PathCmd::Line(2.0, 1.0),
            PathCmd::Line(2.0, 2.0),
        ]
    );
}

#[test]
fn svg_horizontal_vertical() {
    let svg = parse(r##"(svg-path "M0 0 H10 V5")"##).expect("valid");
    assert_eq!(
        svg,
        vec![
            PathCmd::Move(0.0, 0.0),
            PathCmd::Line(10.0, 0.0),
            PathCmd::Line(10.0, 5.0),
        ]
    );
}

#[test]
fn wrong_arity_is_an_error() {
    assert!(parse("(path (move 0))").is_err());
}

#[test]
fn unsupported_svg_command_is_pointed() {
    let err = parse(r##"(svg-path "M0 0 A1 1 0 0 1 2 2")"##).unwrap_err();
    assert!(err.message.contains("unsupported SVG path command `A`"));
}

#[test]
fn non_path_head_returns_none() {
    let doc = sexpr::read("(list 1 2)").expect("reads");
    let form = &doc.values[0];
    assert!(parse_path_form(form.as_list().unwrap(), form.span()).is_none());
}