2018-01-13 02:08:53 +01:00
|
|
|
// Copyright 2018 Google Inc. Use of this source code is governed by an
|
|
|
|
// MIT-style license that can be found in the LICENSE file or at
|
|
|
|
// https://opensource.org/licenses/MIT.
|
|
|
|
|
|
|
|
@TestOn("vm")
|
|
|
|
|
|
|
|
import 'dart:math' as math;
|
|
|
|
|
|
|
|
import 'package:test/test.dart';
|
|
|
|
|
|
|
|
import 'package:sass/sass.dart';
|
|
|
|
|
|
|
|
import 'utils.dart';
|
|
|
|
|
2019-11-06 01:28:26 +01:00
|
|
|
void main() {
|
2018-01-13 02:08:53 +01:00
|
|
|
group("a unitless integer", () {
|
|
|
|
SassNumber value;
|
|
|
|
setUp(() => value = parseValue("123") as SassNumber);
|
|
|
|
|
|
|
|
test("has the correct value", () {
|
|
|
|
expect(value.value, equals(123));
|
2018-06-15 22:58:13 +02:00
|
|
|
expect(value.value, const TypeMatcher<int>());
|
2018-01-13 02:08:53 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
test("has no units", () {
|
|
|
|
expect(value.numeratorUnits, isEmpty);
|
|
|
|
expect(value.denominatorUnits, isEmpty);
|
|
|
|
expect(value.hasUnits, isFalse);
|
|
|
|
expect(value.hasUnit("px"), isFalse);
|
|
|
|
expect(() => value.assertUnit("px"), throwsSassScriptException);
|
|
|
|
value.assertNoUnits(); // Should not throw.
|
|
|
|
});
|
|
|
|
|
|
|
|
test("is an int", () {
|
|
|
|
expect(value.isInt, isTrue);
|
|
|
|
expect(value.asInt, equals(123));
|
|
|
|
expect(value.assertInt(), equals(123));
|
|
|
|
});
|
|
|
|
|
|
|
|
test("can be coerced to any units", () {
|
|
|
|
expect(
|
|
|
|
value.coerce(["abc"], ["def"]),
|
2018-11-16 00:16:24 +01:00
|
|
|
equals(SassNumber.withUnits(123,
|
2018-01-13 02:08:53 +01:00
|
|
|
numeratorUnits: ["abc"], denominatorUnits: ["def"])));
|
|
|
|
});
|
|
|
|
|
|
|
|
test("can return its value in any units", () {
|
|
|
|
expect(value.valueInUnits(["abc"], ["def"]), equals(123));
|
|
|
|
});
|
|
|
|
|
2018-01-15 00:18:16 +01:00
|
|
|
group("valueInRange()", () {
|
|
|
|
test("returns its value within a given range", () {
|
|
|
|
expect(value.valueInRange(0, 123), equals(123));
|
|
|
|
expect(value.valueInRange(123, 123), equals(123));
|
|
|
|
expect(value.valueInRange(123, 1000), equals(123));
|
|
|
|
});
|
|
|
|
|
|
|
|
test("rejects a value outside the range", () {
|
|
|
|
expect(() => value.valueInRange(0, 122), throwsSassScriptException);
|
|
|
|
expect(() => value.valueInRange(124, 1000), throwsSassScriptException);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-01-13 02:08:53 +01:00
|
|
|
test("equals the same number", () {
|
2018-11-16 00:16:24 +01:00
|
|
|
expect(value, equalsWithHash(SassNumber(123)));
|
2018-01-13 02:08:53 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
test("equals the same number within precision tolerance", () {
|
|
|
|
expect(
|
|
|
|
value,
|
|
|
|
equalsWithHash(
|
2018-12-14 00:27:08 +01:00
|
|
|
SassNumber(123 + math.pow(10, -SassNumber.precision - 2))));
|
2018-01-13 02:08:53 +01:00
|
|
|
expect(
|
|
|
|
value,
|
|
|
|
equalsWithHash(
|
2018-12-14 00:27:08 +01:00
|
|
|
SassNumber(123 - math.pow(10, -SassNumber.precision - 2))));
|
2018-01-13 02:08:53 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
test("doesn't equal a different number", () {
|
2018-11-16 00:16:24 +01:00
|
|
|
expect(value, isNot(equals(SassNumber(124))));
|
|
|
|
expect(value, isNot(equals(SassNumber(122))));
|
2018-12-14 00:27:08 +01:00
|
|
|
expect(
|
|
|
|
value,
|
|
|
|
isNot(equals(
|
|
|
|
SassNumber(123 + math.pow(10, -SassNumber.precision - 1)))));
|
|
|
|
expect(
|
|
|
|
value,
|
|
|
|
isNot(equals(
|
|
|
|
SassNumber(123 - math.pow(10, -SassNumber.precision - 1)))));
|
2018-01-13 02:08:53 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
test("doesn't equal a number with units", () {
|
2018-11-16 00:16:24 +01:00
|
|
|
expect(value, isNot(equals(SassNumber(123, "px"))));
|
2018-01-13 02:08:53 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
test("is a number", () {
|
|
|
|
expect(value.assertNumber(), equals(value));
|
|
|
|
});
|
|
|
|
|
|
|
|
test("isn't any other type", () {
|
|
|
|
expect(value.assertBoolean, throwsSassScriptException);
|
|
|
|
expect(value.assertColor, throwsSassScriptException);
|
|
|
|
expect(value.assertFunction, throwsSassScriptException);
|
|
|
|
expect(value.assertMap, throwsSassScriptException);
|
2020-09-16 01:23:01 +02:00
|
|
|
expect(value.tryMap(), isNull);
|
2018-01-13 02:08:53 +01:00
|
|
|
expect(value.assertString, throwsSassScriptException);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
group("a unitless double", () {
|
|
|
|
SassNumber value;
|
|
|
|
setUp(() => value = parseValue("123.456") as SassNumber);
|
|
|
|
|
|
|
|
test("has the correct value", () {
|
|
|
|
expect(value.value, equals(123.456));
|
|
|
|
});
|
|
|
|
|
|
|
|
test("is not an int", () {
|
|
|
|
expect(value.isInt, isFalse);
|
|
|
|
expect(value.asInt, isNull);
|
|
|
|
expect(value.assertInt, throwsSassScriptException);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
group("a unitless fuzzy integer", () {
|
|
|
|
SassNumber value;
|
2018-12-14 00:27:08 +01:00
|
|
|
setUp(() => value = parseValue("123.000000000001") as SassNumber);
|
2018-01-13 02:08:53 +01:00
|
|
|
|
|
|
|
test("has the correct value", () {
|
2018-12-14 00:27:08 +01:00
|
|
|
expect(value.value, equals(123.000000000001));
|
2018-01-13 02:08:53 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
test("is an int", () {
|
|
|
|
expect(value.isInt, isTrue);
|
|
|
|
expect(value.asInt, equals(123));
|
|
|
|
expect(value.assertInt(), equals(123));
|
|
|
|
});
|
|
|
|
|
|
|
|
test("equals the same number", () {
|
|
|
|
expect(
|
|
|
|
value,
|
|
|
|
equalsWithHash(
|
2018-12-14 00:27:08 +01:00
|
|
|
SassNumber(123 + math.pow(10, -SassNumber.precision - 2))));
|
2018-01-13 02:08:53 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
test("equals the same number within precision tolerance", () {
|
2018-11-16 00:16:24 +01:00
|
|
|
expect(value, equalsWithHash(SassNumber(123)));
|
2018-01-13 02:08:53 +01:00
|
|
|
expect(
|
|
|
|
value,
|
|
|
|
equalsWithHash(
|
2018-12-14 00:27:08 +01:00
|
|
|
SassNumber(123 - math.pow(10, -SassNumber.precision - 2))));
|
2018-01-13 02:08:53 +01:00
|
|
|
});
|
2018-01-15 00:18:16 +01:00
|
|
|
|
|
|
|
group("valueInRange()", () {
|
|
|
|
test("clamps within the given range", () {
|
|
|
|
expect(value.valueInRange(0, 123), equals(123));
|
|
|
|
expect(value.valueInRange(123, 123), equals(123));
|
|
|
|
expect(value.valueInRange(123, 1000), equals(123));
|
|
|
|
});
|
|
|
|
|
|
|
|
test("rejects a value outside the range", () {
|
|
|
|
expect(() => value.valueInRange(0, 122), throwsSassScriptException);
|
|
|
|
expect(() => value.valueInRange(124, 1000), throwsSassScriptException);
|
|
|
|
});
|
|
|
|
});
|
2018-01-13 02:08:53 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
group("an integer with a single numerator unit", () {
|
|
|
|
SassNumber value;
|
|
|
|
setUp(() => value = parseValue("123px") as SassNumber);
|
|
|
|
|
|
|
|
test("has that unit", () {
|
|
|
|
expect(value.numeratorUnits, equals(["px"]));
|
|
|
|
expect(value.hasUnits, isTrue);
|
|
|
|
expect(value.hasUnit("px"), isTrue);
|
|
|
|
value.assertUnit("px"); // Should not throw.
|
|
|
|
expect(value.assertNoUnits, throwsSassScriptException);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("has no other units", () {
|
|
|
|
expect(value.denominatorUnits, isEmpty);
|
|
|
|
expect(value.hasUnit("in"), isFalse);
|
|
|
|
expect(() => value.assertUnit("in"), throwsSassScriptException);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("can be coerced to compatible units", () {
|
|
|
|
expect(value.coerce(["px"], []), equals(value));
|
2018-11-16 00:16:24 +01:00
|
|
|
expect(value.coerce(["in"], []), equals(SassNumber(1.28125, "in")));
|
2018-01-13 02:08:53 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
test("can return its value in compatible units", () {
|
|
|
|
expect(value.valueInUnits(["px"], []), equals(123));
|
|
|
|
expect(value.valueInUnits(["in"], []), equals(1.28125));
|
|
|
|
});
|
|
|
|
|
|
|
|
test("equals the same number", () {
|
2018-11-16 00:16:24 +01:00
|
|
|
expect(value, equalsWithHash(SassNumber(123, "px")));
|
2018-01-13 02:08:53 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
test("equals an equivalent number", () {
|
2018-11-16 00:16:24 +01:00
|
|
|
expect(value.hashCode, equals(SassNumber(1.28125, "in").hashCode));
|
|
|
|
expect(value, equalsWithHash(SassNumber(1.28125, "in")));
|
2018-01-13 02:08:53 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
test("doesn't equal a unitless number", () {
|
2018-11-16 00:16:24 +01:00
|
|
|
expect(value, isNot(equals(SassNumber(123))));
|
2018-01-13 02:08:53 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
test("doesn't equal a number with different units", () {
|
2018-11-16 00:16:24 +01:00
|
|
|
expect(value, isNot(equals(SassNumber(123, "abc"))));
|
2018-01-13 02:08:53 +01:00
|
|
|
expect(
|
|
|
|
value,
|
2018-11-16 00:16:24 +01:00
|
|
|
isNot(
|
|
|
|
equals(SassNumber.withUnits(123, numeratorUnits: ["px", "px"]))));
|
2018-01-13 02:08:53 +01:00
|
|
|
expect(
|
|
|
|
value,
|
2018-11-16 00:16:24 +01:00
|
|
|
isNot(equals(SassNumber.withUnits(123,
|
2018-01-13 02:08:53 +01:00
|
|
|
numeratorUnits: ["px"], denominatorUnits: ["abc"]))));
|
2018-11-16 00:16:24 +01:00
|
|
|
expect(value,
|
|
|
|
isNot(equals(SassNumber.withUnits(123, denominatorUnits: ["px"]))));
|
2018-01-13 02:08:53 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
group("an integer with numerator and denominator units", () {
|
|
|
|
SassNumber value;
|
|
|
|
setUp(() => value = parseValue("123px / 5ms") as SassNumber);
|
|
|
|
|
|
|
|
test("has those units", () {
|
|
|
|
expect(value.numeratorUnits, equals(["px"]));
|
|
|
|
expect(value.denominatorUnits, equals(["ms"]));
|
|
|
|
expect(value.hasUnits, isTrue);
|
|
|
|
expect(value.assertNoUnits, throwsSassScriptException);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("reports false for hasUnit()", () {
|
|
|
|
// [hasUnit] and [assertUnit] only allow a single numerator unit.
|
|
|
|
expect(value.hasUnit("px"), isFalse);
|
|
|
|
expect(() => value.assertUnit("px"), throwsSassScriptException);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("can be coerced to compatible units", () {
|
|
|
|
expect(value.coerce(["px"], ["ms"]), equals(value));
|
|
|
|
expect(
|
|
|
|
value.coerce(["in"], ["s"]),
|
2018-11-16 00:16:24 +01:00
|
|
|
equals(SassNumber.withUnits(256.25,
|
2018-01-13 02:08:53 +01:00
|
|
|
numeratorUnits: ["in"], denominatorUnits: ["s"])));
|
|
|
|
});
|
|
|
|
|
|
|
|
test("can return its value in compatible units", () {
|
|
|
|
expect(value.valueInUnits(["px"], ["ms"]), equals(24.6));
|
|
|
|
expect(value.valueInUnits(["in"], ["s"]), equals(256.25));
|
|
|
|
});
|
|
|
|
|
|
|
|
test("equals the same number", () {
|
|
|
|
expect(
|
|
|
|
value,
|
2018-11-16 00:16:24 +01:00
|
|
|
equalsWithHash(SassNumber.withUnits(24.6,
|
2018-01-13 02:08:53 +01:00
|
|
|
numeratorUnits: ["px"], denominatorUnits: ["ms"])));
|
|
|
|
});
|
|
|
|
|
|
|
|
test("equals an equivalent number", () {
|
|
|
|
expect(
|
|
|
|
value,
|
2018-11-16 00:16:24 +01:00
|
|
|
equalsWithHash(SassNumber.withUnits(256.25,
|
2018-01-13 02:08:53 +01:00
|
|
|
numeratorUnits: ["in"], denominatorUnits: ["s"])));
|
|
|
|
});
|
|
|
|
|
|
|
|
test("doesn't equal a unitless number", () {
|
2018-11-16 00:16:24 +01:00
|
|
|
expect(value, isNot(equals(SassNumber(24.6))));
|
2018-01-13 02:08:53 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
test("doesn't equal a number with different units", () {
|
2018-11-16 00:16:24 +01:00
|
|
|
expect(value, isNot(equals(SassNumber(24.6, "px"))));
|
|
|
|
expect(value,
|
|
|
|
isNot(equals(SassNumber.withUnits(24.6, denominatorUnits: ["ms"]))));
|
2018-01-13 02:08:53 +01:00
|
|
|
expect(
|
|
|
|
value,
|
2018-11-16 00:16:24 +01:00
|
|
|
isNot(equals(SassNumber.withUnits(24.6,
|
2018-01-13 02:08:53 +01:00
|
|
|
numeratorUnits: ["ms"], denominatorUnits: ["px"]))));
|
|
|
|
expect(
|
|
|
|
value,
|
2018-11-16 00:16:24 +01:00
|
|
|
isNot(equals(SassNumber.withUnits(24.6,
|
2018-01-13 02:08:53 +01:00
|
|
|
numeratorUnits: ["in"], denominatorUnits: ["s"]))));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
group("new SassNumber()", () {
|
|
|
|
test("can create a unitless number", () {
|
2018-11-16 00:16:24 +01:00
|
|
|
var number = SassNumber(123.456);
|
2018-01-13 02:08:53 +01:00
|
|
|
expect(number.value, equals(123.456));
|
|
|
|
expect(number.hasUnits, isFalse);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("can create a number with a numerator unit", () {
|
2018-11-16 00:16:24 +01:00
|
|
|
var number = SassNumber(123.456, "px");
|
2018-01-13 02:08:53 +01:00
|
|
|
expect(number.value, equals(123.456));
|
|
|
|
expect(number.hasUnit('px'), isTrue);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
group("new SassNumber.withUnits()", () {
|
|
|
|
test("can create a unitless number", () {
|
2018-11-16 00:16:24 +01:00
|
|
|
var number = SassNumber.withUnits(123.456);
|
2018-01-13 02:08:53 +01:00
|
|
|
expect(number.value, equals(123.456));
|
|
|
|
expect(number.hasUnits, isFalse);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("can create a number with units", () {
|
2018-11-16 00:16:24 +01:00
|
|
|
var number = SassNumber.withUnits(123.456,
|
2018-01-13 02:08:53 +01:00
|
|
|
numeratorUnits: ["px", "em"], denominatorUnits: ["ms", "kHz"]);
|
|
|
|
expect(number.value, equals(123.456));
|
|
|
|
expect(number.numeratorUnits, equals(["px", "em"]));
|
|
|
|
expect(number.denominatorUnits, equals(["ms", "kHz"]));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|