1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5 use app_units::Au;
6 use style::attr::{AttrValue, LengthPercentageOrAuto, parse_length};
7 use style::values::computed::{CalcLengthPercentage, Percentage};
8
9 #[test]
test_length_calc()10 fn test_length_calc() {
11 let calc = CalcLengthPercentage::new(Au(10).into(), Some(Percentage(0.2)));
12 assert_eq!(calc.to_used_value(Some(Au(10))), Some(Au(12)));
13 assert_eq!(calc.to_used_value(Some(Au(0))), Some(Au(10)));
14 assert_eq!(calc.to_used_value(None), None);
15
16 let calc = CalcLengthPercentage::new(Au(10).into(), None);
17 assert_eq!(calc.to_used_value(Some(Au(0))), Some(Au(10)));
18 assert_eq!(calc.to_used_value(None), Some(Au(10)));
19 }
20
21 #[test]
test_parse_double()22 fn test_parse_double() {
23 let value = String::from("432.5e2");
24 match AttrValue::from_double(value, 0.0) {
25 AttrValue::Double(_, num) => assert_eq!(num, 43250f64),
26 _ => panic!("expected a double value")
27 }
28 }
29
30 #[test]
test_parse_double_negative_prefix()31 fn test_parse_double_negative_prefix() {
32 let value = String::from("-5.6");
33 match AttrValue::from_double(value, 0.0) {
34 AttrValue::Double(_, num) => assert_eq!(num, -5.6f64),
35 _ => panic!("expected a double value")
36 }
37 }
38
39 #[test]
test_parse_double_positive_prefix()40 fn test_parse_double_positive_prefix() {
41 let value = String::from("+5.6");
42 match AttrValue::from_double(value, 0.0) {
43 AttrValue::Double(_, num) => assert_eq!(num, 5.6f64),
44 _ => panic!("expected a double value")
45 }
46 }
47
48 #[test]
test_from_limited_i32_should_be_default_when_less_than_0()49 fn test_from_limited_i32_should_be_default_when_less_than_0() {
50 let value = String::from("-1");
51 match AttrValue::from_limited_i32(value, 0) {
52 AttrValue::Int(_, 0) => (),
53 _ => panic!("expected an IndexSize error")
54 }
55 }
56
57 #[test]
test_from_limited_i32_should_parse_a_uint_when_value_is_0_or_greater()58 fn test_from_limited_i32_should_parse_a_uint_when_value_is_0_or_greater() {
59 match AttrValue::from_limited_i32(String::from("1"), 0) {
60 AttrValue::Int(_, 1) => (),
61 _ => panic!("expected an successful parsing")
62 }
63 }
64
65 #[test]
test_from_limited_i32_should_keep_parsed_value_when_not_an_int()66 fn test_from_limited_i32_should_keep_parsed_value_when_not_an_int() {
67 match AttrValue::from_limited_i32(String::from("parsed-value"), 0) {
68 AttrValue::Int(p, 0) => {
69 assert_eq!(p, String::from("parsed-value"))
70 },
71 _ => panic!("expected an successful parsing")
72 }
73 }
74
75 #[test]
test_parse_length()76 pub fn test_parse_length() {
77 fn check(input: &str, expected: LengthPercentageOrAuto) {
78 let parsed = parse_length(input);
79 assert_eq!(parsed, expected);
80 }
81
82 check("0", LengthPercentageOrAuto::Length(Au::from_px(0)));
83 check("0.000%", LengthPercentageOrAuto::Percentage(0.0));
84 check("+5.82%", LengthPercentageOrAuto::Percentage(0.0582));
85 check("5.82", LengthPercentageOrAuto::Length(Au::from_f64_px(5.82)));
86 check("invalid", LengthPercentageOrAuto::Auto);
87 check("12 followed by invalid", LengthPercentageOrAuto::Length(Au::from_px(12)));
88 }
89