summaryrefslogtreecommitdiffstats
path: root/servo/tests/unit/style/attr.rs
blob: b3164c4911644a0f5d94f1d29f8f98df779d432b (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use app_units::Au;
use style::attr::{AttrValue, LengthPercentageOrAuto, parse_length};
use style::values::computed::{CalcLengthPercentage, Percentage};

#[test]
fn test_length_calc() {
    let calc = CalcLengthPercentage::new(Au(10).into(), Some(Percentage(0.2)));
    assert_eq!(calc.to_used_value(Some(Au(10))), Some(Au(12)));
    assert_eq!(calc.to_used_value(Some(Au(0))), Some(Au(10)));
    assert_eq!(calc.to_used_value(None), None);

    let calc = CalcLengthPercentage::new(Au(10).into(), None);
    assert_eq!(calc.to_used_value(Some(Au(0))), Some(Au(10)));
    assert_eq!(calc.to_used_value(None), Some(Au(10)));
}

#[test]
fn test_parse_double() {
    let value = String::from("432.5e2");
    match AttrValue::from_double(value, 0.0) {
        AttrValue::Double(_, num) => assert_eq!(num, 43250f64),
        _ => panic!("expected a double value")
    }
}

#[test]
fn test_parse_double_negative_prefix() {
    let value = String::from("-5.6");
    match AttrValue::from_double(value, 0.0) {
        AttrValue::Double(_, num) => assert_eq!(num, -5.6f64),
        _ => panic!("expected a double value")
    }
}

#[test]
fn test_parse_double_positive_prefix() {
    let value = String::from("+5.6");
    match AttrValue::from_double(value, 0.0) {
        AttrValue::Double(_, num) => assert_eq!(num, 5.6f64),
        _ => panic!("expected a double value")
    }
}

#[test]
fn test_from_limited_i32_should_be_default_when_less_than_0() {
    let value = String::from("-1");
    match AttrValue::from_limited_i32(value, 0) {
        AttrValue::Int(_, 0) => (),
        _ => panic!("expected an IndexSize error")
    }
}

#[test]
fn test_from_limited_i32_should_parse_a_uint_when_value_is_0_or_greater() {
    match AttrValue::from_limited_i32(String::from("1"), 0) {
        AttrValue::Int(_, 1) => (),
        _ => panic!("expected an successful parsing")
    }
}

#[test]
fn test_from_limited_i32_should_keep_parsed_value_when_not_an_int() {
    match AttrValue::from_limited_i32(String::from("parsed-value"), 0) {
        AttrValue::Int(p, 0) => {
            assert_eq!(p, String::from("parsed-value"))
        },
        _ => panic!("expected an successful parsing")
    }
}

#[test]
pub fn test_parse_length() {
    fn check(input: &str, expected: LengthPercentageOrAuto) {
        let parsed = parse_length(input);
        assert_eq!(parsed, expected);
    }

    check("0", LengthPercentageOrAuto::Length(Au::from_px(0)));
    check("0.000%", LengthPercentageOrAuto::Percentage(0.0));
    check("+5.82%", LengthPercentageOrAuto::Percentage(0.0582));
    check("5.82", LengthPercentageOrAuto::Length(Au::from_f64_px(5.82)));
    check("invalid", LengthPercentageOrAuto::Auto);
    check("12 followed by invalid", LengthPercentageOrAuto::Length(Au::from_px(12)));
}