aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/tests/dependency_resolver/request_test.rs
blob: 969cd4d0a68ed13dc273020e8e922ab585e14407 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//! ref: composer/tests/Composer/Test/DependencyResolver/RequestTest.php

use indexmap::IndexMap;
use shirabe::dependency_resolver::request::Request;
use shirabe::package::version::version_parser::VersionParser;
use shirabe_semver::constraint::{AnyConstraint, MatchAllConstraint, SimpleConstraint};

// AnyConstraint does not implement PartialEq (it lives in another crate), so
// the original assertEquals() on the requires map is reproduced by comparing
// the Debug representations.
//
// The original tests also build ArrayRepositories and add packages, but those
// are never read by the assertions (the Request does not consult them), so the
// repository setup is omitted here.

fn get_version_constraint(operator: &str, version: &str) -> AnyConstraint {
    let normalized = VersionParser::new().normalize(version, None).unwrap();
    AnyConstraint::Simple(SimpleConstraint::new(
        operator.to_string(),
        normalized,
        Some(format!("{} {}", operator, version)),
    ))
}

#[test]
fn test_request_install() {
    let mut request = Request::new(None);
    request.require_name("foo", None).unwrap();

    let mut expected: IndexMap<String, AnyConstraint> = IndexMap::new();
    expected.insert("foo".to_string(), MatchAllConstraint::new(None).into());

    assert_eq!(
        format!("{:?}", expected),
        format!("{:?}", request.get_requires())
    );
}

#[test]
fn test_request_install_same_package_from_different_repositories() {
    let constraint = get_version_constraint("=", "1");

    let mut request = Request::new(None);
    request
        .require_name("foo", Some(constraint.clone()))
        .unwrap();

    let mut expected: IndexMap<String, AnyConstraint> = IndexMap::new();
    expected.insert("foo".to_string(), constraint);

    assert_eq!(
        format!("{:?}", expected),
        format!("{:?}", request.get_requires())
    );
}