gecko-dev/servo/tests/unit/style/attr.rs
Bob 8f05b7fca8 servo: Merge #12632 - account for sign in double parsing (from bobthekingofegypt:signed_parse_double); r=KiChjang
<!-- Please describe your changes on the following line: -->
account for sign in double parsing inside styles attr.rs

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors

<!-- Either: -->
- [X] There are tests for these changes OR
- [ ] These changes do not require tests because _____

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

When sign was present during double parsing correctly jump forward the
extra character when parsing fraction and exponent.

Source-Repo: https://github.com/servo/servo
Source-Revision: 1f34d4f219a8d237acbf02737343ce520d73fd7a
2016-07-28 16:56:43 -05:00

76 lines
2.4 KiB
Rust

/* 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 http://mozilla.org/MPL/2.0/. */
use app_units::Au;
use style::attr::{AttrValue, LengthOrPercentageOrAuto, parse_length};
#[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: LengthOrPercentageOrAuto) {
let parsed = parse_length(input);
assert_eq!(parsed, expected);
}
check("0", LengthOrPercentageOrAuto::Length(Au::from_px(0)));
check("0.000%", LengthOrPercentageOrAuto::Percentage(0.0));
check("+5.82%", LengthOrPercentageOrAuto::Percentage(0.0582));
check("5.82", LengthOrPercentageOrAuto::Length(Au::from_f64_px(5.82)));
check("invalid", LengthOrPercentageOrAuto::Auto);
check("12 followed by invalid", LengthOrPercentageOrAuto::Length(Au::from_px(12)));
}