diff --git a/src/value/number.rs b/src/value/number.rs index ae8e2fd..49d0e7d 100644 --- a/src/value/number.rs +++ b/src/value/number.rs @@ -100,28 +100,47 @@ impl Display for Number { if self.val.is_negative() { f.write_char('-')?; } - write!(f, "{}", self.val.to_integer().abs())?; + + let mut whole = self.val.to_integer().abs(); + let mut dec = String::new(); + let mut frac = self.val.fract(); if frac != BigRational::from_integer(BigInt::from(0)) { - f.write_char('.')?; + dec.write_char('.')?; for _ in 0..(PRECISION - 1) { frac *= BigRational::from_integer(BigInt::from(10)); - write!(f, "{}", frac.to_integer().abs())?; + write!(dec, "{}", frac.to_integer().abs())?; frac = frac.fract(); if frac == BigRational::from_integer(BigInt::from(0)) { break; } } if frac != BigRational::from_integer(BigInt::from(0)) { - write!( - f, - "{}", - (frac * BigRational::from_integer(BigInt::from(10))) - .round() - .to_integer() - )?; + let f = (frac * BigRational::from_integer(BigInt::from(10))) + .round() + .abs() + .to_integer(); + if f == BigInt::from(10) { + loop { + match dec.pop().unwrap() { + '9' => continue, + '.' => { + whole += 1; + break; + } + c => { + dec.push_str(&(c.to_digit(10).unwrap() + 1).to_string()); + break; + } + } + } + } else { + write!(dec, "{}", f)?; + } } } + write!(f, "{}", whole)?; + write!(f, "{}", dec)?; Ok(()) } } diff --git a/tests/misc.rs b/tests/misc.rs index dfcef7f..80e0b8b 100644 --- a/tests/misc.rs +++ b/tests/misc.rs @@ -46,15 +46,3 @@ test!( "a {\n color: red; ;\n}\n", "a {\n color: red;\n}\n" ); -// these two precisiont tests only act like this in dart sass -// these values are `1` for libsass -test!( - precision_does_not_round_up, - "a {\n color: 0.99999999991;\n}\n", - "a {\n color: 0.9999999999;\n}\n" -); -test!( - precision_does_round_up, - "a {\n color: 1.00000000009;\n}\n", - "a {\n color: 1.0000000001;\n}\n" -); diff --git a/tests/number.rs b/tests/number.rs new file mode 100644 index 0000000..6460b23 --- /dev/null +++ b/tests/number.rs @@ -0,0 +1,52 @@ +#![cfg(test)] + +#[macro_use] +mod macros; + +// this is `1` for libsass +test!( + precision_does_not_round_up, + "a {\n color: 0.99999999991;\n}\n", + "a {\n color: 0.9999999999;\n}\n" +); +// this is `1` for libsass +test!( + precision_does_round_up, + "a {\n color: 1.00000000009;\n}\n", + "a {\n color: 1.0000000001;\n}\n" +); +test!( + many_nines_becomes_one, + "a {\n color: 0.9999999999999999;\n}\n", + "a {\n color: 1;\n}\n" +); +test!( + many_nines_becomes_one_neg, + "a {\n color: -0.9999999999999999;\n}\n", + "a {\n color: -1;\n}\n" +); +test!( + negative_zero, + "a {\n color: -0;\n}\n", + "a {\n color: 0;\n}\n" +); +test!( + decimal_is_zero, + "a {\n color: 1.0;\n}\n", + "a {\n color: 1;\n}\n" +); +test!(many_nines_not_rounded, "a {\n color: 0.999999;\n}\n"); +test!(positive_integer, "a {\n color: 1;\n}\n"); +test!(negative_integer, "a {\n color: -1;\n}\n"); +test!( + positive_float_no_leading_zero, + "a {\n color: .1;\n}\n", + "a {\n color: 0.1;\n}\n" +); +test!( + negative_float_no_leading_zero, + "a {\n color: -.1;\n}\n", + "a {\n color: -0.1;\n}\n" +); +test!(positive_float_leading_zero, "a {\n color: 0.1;\n}\n"); +test!(negative_float_leading_zero, "a {\n color: -0.1;\n}\n"); diff --git a/tests/values.rs b/tests/values.rs index b51c4d7..44d9728 100644 --- a/tests/values.rs +++ b/tests/values.rs @@ -321,20 +321,6 @@ test!( "a {\n color: 1 + foo();\n}\n", "a {\n color: 1foo();\n}\n" ); -test!(positive_integer, "a {\n color: 1;\n}\n"); -test!(negative_integer, "a {\n color: -1;\n}\n"); -test!( - positive_float_no_leading_zero, - "a {\n color: .1;\n}\n", - "a {\n color: 0.1;\n}\n" -); -test!( - negative_float_no_leading_zero, - "a {\n color: -.1;\n}\n", - "a {\n color: -0.1;\n}\n" -); -test!(positive_float_leading_zero, "a {\n color: 0.1;\n}\n"); -test!(negative_float_leading_zero, "a {\n color: -0.1;\n}\n"); test!( unitless_plus_null, "a {\n color: 1 + null;\n}\n",