datta 0.1.1

Rust implementation of RFC6570 - URI Template that can processURI Templates up and to including ones designated Level 4.
Documentation
// File autogenerated with /scripts/generate_tests.py

use datta::UriTemplate;

// Additional Examples 1
#[test]
fn test_additional_examples_1() {
    let mut templates = [
        UriTemplate::new("{/id*}"),
        UriTemplate::new("{/id*}{?fields,first_name,last.name,token}"),
        UriTemplate::new("/search.{format}{?q,geocode,lang,locale,page,result_type}"),
        UriTemplate::new("/test{/Some%20Thing}"),
        UriTemplate::new("/set{?number}"),
        UriTemplate::new("/loc{?long,lat}"),
        UriTemplate::new("/base{/group_id,first_name}/pages{/page,lang}{?format,q}"),
        UriTemplate::new("/sparql{?query}"),
        UriTemplate::new("/go{?uri}"),
        UriTemplate::new("/service{?word}"),
        UriTemplate::new("/lookup{?Stra%C3%9Fe}"),
        UriTemplate::new("{random}"),
        UriTemplate::new("{?assoc_special_chars*}"),
    ];

    for template in &mut templates {
        template.set("long", "37.76");
        template.set("lat", "-122.427");
        template.set("last.name", "Doe");
        template.set("token", "12345");
        template.set("uri", "http://example.org/?uri=http%3A%2F%2Fexample.org%2F");
        template.set("Stra%C3%9Fe", "Grüner Weg");
        template.set("Some%20Thing", "foo");
        template.set("first_name", "John");
        template.set("random", "šö䟜ñꀣ¥‡ÑÒÓÔÕÖרÙÚàáâãäåæçÿ");
        template.set("geocode", &["37.76", "-122.427"] as &[&str]);
        template.set(
            "assoc_special_chars",
            &[("šö䟜ñꀣ¥‡ÑÒÓÔÕ", "ÖרÙÚàáâãäåæçÿ")] as &[(&str, &str)],
        );
        template.set("id", "person");
        template.set("number", "6");
        template.set("q", "URI Templates");
        template.set("query", "PREFIX dc: <http://purl.org/dc/elements/1.1/> SELECT ?book ?who WHERE { ?book dc:creator ?who }");
        template.set("word", "drücken");
        template.set("format", "json");
        template.set("group_id", "12345");
        template.set("fields", &["id", "name", "picture"] as &[&str]);
        template.set("page", "5");
        template.set("lang", "en");
    }

    assert_eq!(templates[0].build(), "/person");
    let template_1_answers = [
        "/person?fields=id,name,picture&first_name=John&last.name=Doe&token=12345",
        "/person?fields=id,picture,name&first_name=John&last.name=Doe&token=12345",
        "/person?fields=picture,name,id&first_name=John&last.name=Doe&token=12345",
        "/person?fields=picture,id,name&first_name=John&last.name=Doe&token=12345",
        "/person?fields=name,picture,id&first_name=John&last.name=Doe&token=12345",
        "/person?fields=name,id,picture&first_name=John&last.name=Doe&token=12345",
    ];
    assert!(template_1_answers.contains(&templates[1].build().as_ref()));
    let template_2_answers = [
        "/search.json?q=URI%20Templates&geocode=37.76,-122.427&lang=en&page=5",
        "/search.json?q=URI%20Templates&geocode=-122.427,37.76&lang=en&page=5",
    ];
    assert!(template_2_answers.contains(&templates[2].build().as_ref()));
    assert_eq!(templates[3].build(), "/test/foo");
    assert_eq!(templates[4].build(), "/set?number=6");
    assert_eq!(templates[5].build(), "/loc?long=37.76&lat=-122.427");
    assert_eq!(
        templates[6].build(),
        "/base/12345/John/pages/5/en?format=json&q=URI%20Templates"
    );
    assert_eq!(templates[7].build(), "/sparql?query=PREFIX%20dc%3A%20%3Chttp%3A%2F%2Fpurl.org%2Fdc%2Felements%2F1.1%2F%3E%20SELECT%20%3Fbook%20%3Fwho%20WHERE%20%7B%20%3Fbook%20dc%3Acreator%20%3Fwho%20%7D");
    assert_eq!(
        templates[8].build(),
        "/go?uri=http%3A%2F%2Fexample.org%2F%3Furi%3Dhttp%253A%252F%252Fexample.org%252F"
    );
    assert_eq!(templates[9].build(), "/service?word=dr%C3%BCcken");
    assert_eq!(
        templates[10].build(),
        "/lookup?Stra%C3%9Fe=Gr%C3%BCner%20Weg"
    );
    assert_eq!(templates[11].build(), "%C5%A1%C3%B6%C3%A4%C5%B8%C5%93%C3%B1%C3%AA%E2%82%AC%C2%A3%C2%A5%E2%80%A1%C3%91%C3%92%C3%93%C3%94%C3%95%C3%96%C3%97%C3%98%C3%99%C3%9A%C3%A0%C3%A1%C3%A2%C3%A3%C3%A4%C3%A5%C3%A6%C3%A7%C3%BF");
    assert_eq!(templates[12].build(), "?%C5%A1%C3%B6%C3%A4%C5%B8%C5%93%C3%B1%C3%AA%E2%82%AC%C2%A3%C2%A5%E2%80%A1%C3%91%C3%92%C3%93%C3%94%C3%95=%C3%96%C3%97%C3%98%C3%99%C3%9A%C3%A0%C3%A1%C3%A2%C3%A3%C3%A4%C3%A5%C3%A6%C3%A7%C3%BF");
}

// Additional Examples 2
#[test]
fn test_additional_examples_2() {
    let mut templates = [
        UriTemplate::new("{/id*}"),
        UriTemplate::new("{/id*}{?fields,token}"),
    ];

    for template in &mut templates {
        template.set("q", "URI Templates");
        template.set("lang", "en");
        template.set("format", "atom");
        template.set("token", "12345");
        template.set("geocode", &["37.76", "-122.427"] as &[&str]);
        template.set("fields", &["id", "name", "picture"] as &[&str]);
        template.set("page", "10");
        template.set("start", "5");
        template.set("id", &["person", "albums"] as &[&str]);
    }

    let template_0_answers = ["/person/albums", "/albums/person"];
    assert!(template_0_answers.contains(&templates[0].build().as_ref()));
    let template_1_answers = [
        "/person/albums?fields=id,name,picture&token=12345",
        "/person/albums?fields=id,picture,name&token=12345",
        "/person/albums?fields=picture,name,id&token=12345",
        "/person/albums?fields=picture,id,name&token=12345",
        "/person/albums?fields=name,picture,id&token=12345",
        "/person/albums?fields=name,id,picture&token=12345",
        "/albums/person?fields=id,name,picture&token=12345",
        "/albums/person?fields=id,picture,name&token=12345",
        "/albums/person?fields=picture,name,id&token=12345",
        "/albums/person?fields=picture,id,name&token=12345",
        "/albums/person?fields=name,picture,id&token=12345",
        "/albums/person?fields=name,id,picture&token=12345",
    ];
    assert!(template_1_answers.contains(&templates[1].build().as_ref()));
}

// Additional Examples 3: Empty Variables
#[test]
fn test_additional_examples_3() {
    let mut templates = [
        UriTemplate::new("{/empty_list}"),
        UriTemplate::new("{/empty_list*}"),
        UriTemplate::new("{?empty_list}"),
        UriTemplate::new("{?empty_list*}"),
        UriTemplate::new("{?empty_assoc}"),
        UriTemplate::new("{?empty_assoc*}"),
    ];

    for template in &mut templates {
        template.set("empty_assoc", &[] as &[(&str, &str)]);
        template.set("empty_list", &[] as &[&str]);
    }

    let template_0_answers = [""];
    assert!(template_0_answers.contains(&templates[0].build().as_ref()));
    let template_1_answers = [""];
    assert!(template_1_answers.contains(&templates[1].build().as_ref()));
    let template_2_answers = [""];
    assert!(template_2_answers.contains(&templates[2].build().as_ref()));
    let template_3_answers = [""];
    assert!(template_3_answers.contains(&templates[3].build().as_ref()));
    let template_4_answers = [""];
    assert!(template_4_answers.contains(&templates[4].build().as_ref()));
    let template_5_answers = [""];
    assert!(template_5_answers.contains(&templates[5].build().as_ref()));
}

// Additional Examples 4: Numeric Keys
#[test]
fn test_additional_examples_4() {
    let mut templates = [
        UriTemplate::new("{42}"),
        UriTemplate::new("{?42}"),
        UriTemplate::new("{1337}"),
        UriTemplate::new("{?1337*}"),
        UriTemplate::new("{?german*}"),
    ];

    for template in &mut templates {
        template.set(
            "german",
            &[("12", "zwölf"), ("11", "elf")] as &[(&str, &str)],
        );
        template.set(
            "42",
            "The Answer to the Ultimate Question of Life, the Universe, and Everything",
        );
        template.set("1337", &["leet", "as", "it", "can", "be"] as &[&str]);
    }

    assert_eq!(templates[0].build(), "The%20Answer%20to%20the%20Ultimate%20Question%20of%20Life%2C%20the%20Universe%2C%20and%20Everything");
    assert_eq!(templates[1].build(), "?42=The%20Answer%20to%20the%20Ultimate%20Question%20of%20Life%2C%20the%20Universe%2C%20and%20Everything");
    assert_eq!(templates[2].build(), "leet,as,it,can,be");
    assert_eq!(
        templates[3].build(),
        "?1337=leet&1337=as&1337=it&1337=can&1337=be"
    );
    let template_4_answers = ["?11=elf&12=zw%C3%B6lf", "?12=zw%C3%B6lf&11=elf"];
    assert!(template_4_answers.contains(&templates[4].build().as_ref()));
}