// License: see LICENSE file at root directory of `master` branch

extern crate dia_semver;

use {
    core::str::FromStr,

    dia_semver::{Range, Result, Semver},
};

#[test]
fn ranges() -> Result<()> {
    // Single semver
    {
        let semver = Semver::new(0, 1, 2);
        let range = Range::from(&semver);

        assert_eq!(range.start(), &semver);
        assert_eq!(range.end(), &semver);

        for (s, expected) in &[
            ("0.1.2", true), ("0.1.2-apha", false), ("0.1.1", false), ("0.1.3", false), ("0.1.3+beta", false),
        ] {
            assert_eq!(range.contains(&Semver::from_str(s)?), *expected);
        }
    }

    // Empty ranges
    for s in &[
        "[0.1.2, 0.1.1]", "[0.1.2, 0.1.1)", "[0.1.2, 0.1.2-alpha]", "[0.1.2, 0.1.2-alpha)", "(0.1.2, 0.1.2)", "(0.1.2, 0.1.2]",
        "(0.1.2, 0.1.2+beta)", "(0.1.2, 0.1.2-beta)", "(0.1.2, 0.1.2-beta]",
    ] {
        let range = Range::from_str(s)?;
        assert!(range.is_empty());
        assert_eq!(Range::from_str(&range.to_string())?, range);
    }

    // Inclusive/exclusive
    for (range, semver, expected) in &[
        ("[0.1.2, 0.1.2]", "0.1.2", true),
        ("[0.1.2, 0.1.2]", "0.1.2+beta", true),
        ("[0.1.2, 0.1.2+beta]", "0.1.2+some", true),
        ("[0.1.2, 0.1.2]", "0.1.2-alpha", false),
        ("[0.1.2, 0.1.2+some]", "0.1.2-alpha", false),
        ("[0.1.2, 0.1.3-alpha)", "0.1.2-alpha", false),
        ("[0.1.2, 0.1.3-alpha)", "0.1.2+beta", true),
        ("(0.1.2, 0.1.3-alpha)", "0.1.2+beta", false),
        ("(0.1.2, 0.1.3-beta)", "0.1.3-alpha", true),
    ] {
        let range = Range::from_str(range)?;
        assert!(range.start() <= range.end());
        assert_eq!(range.contains(&Semver::from_str(semver)?), *expected);
        assert_eq!(Range::from_str(&range.to_string())?, range);
    }

    Ok(())
}
