Refactor how numbers are printed

This commit is contained in:
ConnorSkees 2020-03-17 12:47:27 -04:00
parent 1c4be96f73
commit d560f13289
4 changed files with 81 additions and 36 deletions

View File

@ -100,28 +100,47 @@ impl Display for Number {
if self.val.is_negative() { if self.val.is_negative() {
f.write_char('-')?; 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(); let mut frac = self.val.fract();
if frac != BigRational::from_integer(BigInt::from(0)) { if frac != BigRational::from_integer(BigInt::from(0)) {
f.write_char('.')?; dec.write_char('.')?;
for _ in 0..(PRECISION - 1) { for _ in 0..(PRECISION - 1) {
frac *= BigRational::from_integer(BigInt::from(10)); frac *= BigRational::from_integer(BigInt::from(10));
write!(f, "{}", frac.to_integer().abs())?; write!(dec, "{}", frac.to_integer().abs())?;
frac = frac.fract(); frac = frac.fract();
if frac == BigRational::from_integer(BigInt::from(0)) { if frac == BigRational::from_integer(BigInt::from(0)) {
break; break;
} }
} }
if frac != BigRational::from_integer(BigInt::from(0)) { if frac != BigRational::from_integer(BigInt::from(0)) {
write!( let f = (frac * BigRational::from_integer(BigInt::from(10)))
f,
"{}",
(frac * BigRational::from_integer(BigInt::from(10)))
.round() .round()
.to_integer() .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(()) Ok(())
} }
} }

View File

@ -46,15 +46,3 @@ test!(
"a {\n color: red; ;\n}\n", "a {\n color: red; ;\n}\n",
"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"
);

52
tests/number.rs Normal file
View File

@ -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");

View File

@ -321,20 +321,6 @@ test!(
"a {\n color: 1 + foo();\n}\n", "a {\n color: 1 + foo();\n}\n",
"a {\n color: 1foo();\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!( test!(
unitless_plus_null, unitless_plus_null,
"a {\n color: 1 + null;\n}\n", "a {\n color: 1 + null;\n}\n",