dart-sass/test/dart_api/value/map_test.dart

185 lines
5.7 KiB
Dart
Raw Normal View History

// 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 'package:test/test.dart';
import 'package:sass/sass.dart';
import 'utils.dart';
main() {
group("a map with contents", () {
SassMap value;
setUp(() => value = parseValue("(a: b, c: d)") as SassMap);
test("is comma-separated", () {
expect(value.separator, equals(ListSeparator.comma));
});
test("returns its contents as a map", () {
expect(
value.contents,
equals({
2018-11-16 00:16:24 +01:00
SassString("a", quotes: false): SassString("b", quotes: false),
SassString("c", quotes: false): SassString("d", quotes: false)
}));
});
test("returns its contents as a list", () {
expect(
value.asList,
equals([
2018-11-16 00:16:24 +01:00
SassList([
SassString("a", quotes: false),
SassString("b", quotes: false)
], ListSeparator.space),
2018-11-16 00:16:24 +01:00
SassList([
SassString("c", quotes: false),
SassString("d", quotes: false)
], ListSeparator.space)
]));
});
group("sassIndexToListIndex()", () {
test("converts a positive index to a Dart index", () {
2018-11-16 00:16:24 +01:00
expect(value.sassIndexToListIndex(SassNumber(1)), equals(0));
expect(value.sassIndexToListIndex(SassNumber(2)), equals(1));
});
test("converts a negative index to a Dart index", () {
2018-11-16 00:16:24 +01:00
expect(value.sassIndexToListIndex(SassNumber(-1)), equals(1));
expect(value.sassIndexToListIndex(SassNumber(-2)), equals(0));
});
test("rejects invalid indices", () {
2018-11-16 00:16:24 +01:00
expect(() => value.sassIndexToListIndex(SassNumber(0)),
throwsSassScriptException);
2018-11-16 00:16:24 +01:00
expect(() => value.sassIndexToListIndex(SassNumber(3)),
throwsSassScriptException);
2018-11-16 00:16:24 +01:00
expect(() => value.sassIndexToListIndex(SassNumber(-3)),
throwsSassScriptException);
});
});
test("equals the same map", () {
expect(
value,
2018-11-16 00:16:24 +01:00
equalsWithHash(SassMap({
SassString("a", quotes: false): SassString("b", quotes: false),
SassString("c", quotes: false): SassString("d", quotes: false)
})));
});
test("doesn't equal the equivalent list", () {
expect(
value,
2018-11-16 00:16:24 +01:00
isNot(equals(SassList([
SassList([
SassString("a", quotes: false),
SassString("b", quotes: false)
], ListSeparator.space),
2018-11-16 00:16:24 +01:00
SassList([
SassString("c", quotes: false),
SassString("d", quotes: false)
], ListSeparator.space)
], ListSeparator.comma))));
});
group("doesn't equal a map with", () {
test("a different value", () {
expect(
value,
2018-11-16 00:16:24 +01:00
isNot(equals(SassMap({
SassString("a", quotes: false): SassString("x", quotes: false),
SassString("c", quotes: false): SassString("d", quotes: false)
}))));
});
test("a different key", () {
expect(
value,
2018-11-16 00:16:24 +01:00
isNot(equals(SassMap({
SassString("a", quotes: false): SassString("b", quotes: false),
SassString("x", quotes: false): SassString("d", quotes: false)
}))));
});
test("a missing pair", () {
expect(
value,
2018-11-16 00:16:24 +01:00
isNot(equals(SassMap({
SassString("a", quotes: false): SassString("b", quotes: false)
}))));
});
test("an additional pair", () {
expect(
value,
2018-11-16 00:16:24 +01:00
isNot(equals(SassMap({
SassString("a", quotes: false): SassString("b", quotes: false),
SassString("c", quotes: false): SassString("d", quotes: false),
SassString("e", quotes: false): SassString("f", quotes: false)
}))));
});
});
test("is a map", () {
expect(value.assertMap(), equals(value));
});
test("isn't any other type", () {
expect(value.assertBoolean, throwsSassScriptException);
expect(value.assertColor, throwsSassScriptException);
expect(value.assertFunction, throwsSassScriptException);
expect(value.assertNumber, throwsSassScriptException);
expect(value.assertString, throwsSassScriptException);
});
});
group("an empty map", () {
SassMap value;
setUp(() => value = parseValue("map-remove((a: b), a)") as SassMap);
test("is comma-separated", () {
expect(value.separator, equals(ListSeparator.comma));
});
test("returns its contents as a map", () {
expect(value.contents, isEmpty);
});
test("returns its contents as a list", () {
expect(value.asList, isEmpty);
});
test("equals an empty list", () {
2018-11-16 00:16:24 +01:00
expect(value, equalsWithHash(SassList.empty()));
});
test("sassIndexToListIndex() rejects invalid indices", () {
2018-11-16 00:16:24 +01:00
expect(() => value.sassIndexToListIndex(SassNumber(0)),
throwsSassScriptException);
2018-11-16 00:16:24 +01:00
expect(() => value.sassIndexToListIndex(SassNumber(1)),
throwsSassScriptException);
2018-11-16 00:16:24 +01:00
expect(() => value.sassIndexToListIndex(SassNumber(-1)),
throwsSassScriptException);
});
});
test("new SassMap.empty() creates an empty map with default metadata", () {
2018-11-16 00:16:24 +01:00
expect(SassMap.empty().contents, isEmpty);
});
test("new SassMap() creates a map with the given contents", () {
2018-11-16 00:16:24 +01:00
var list = SassMap(
{SassString("a", quotes: false): SassString("b", quotes: false)});
expect(
list.contents,
2018-11-16 00:16:24 +01:00
equals(
{SassString("a", quotes: false): SassString("b", quotes: false)}));
});
}