0

In my unit test, I need to test order of elements as that :

test("Check map order", () {
  LinkedHashMap<String, String> actualMap = LinkedHashMap();
  actualMap["0"] = "firstValue";
  actualMap["1"] = "secondValue";
  LinkedHashMap<String, String> expectedMap = LinkedHashMap();
  expectedMap["1"] = "secondValue";
  expectedMap["0"] = "firstValue";
  print("actual: $actualMap");
  print("expected: $expectedMap");
  expect(actualMap, isNot(expectedMap));
});

This test fails, event if order is respected:

actual: {0: firstValue, 1: secondValue}
expected: {1: secondValue, 0: firstValue}
package:test_api                                                      expect
test/services/game/negotiation/NegotiationGameService_test.dart 33:7  main.<fn>.<fn>

Expected: not {'1': 'secondValue', '0': 'firstValue'}
  Actual: {'0': 'firstValue', '1': 'secondValue'}

1 Answers1

0

First idea that works but is a bit boring:

test("Check map order", () {
  LinkedHashMap<String, String> actualMap = LinkedHashMap();
  actualMap["0"] = "firstValue";
  actualMap["1"] = "secondValue";
  LinkedHashMap<String, String> expectedMapSameOrder = LinkedHashMap();
  expectedMap["0"] = "firstValue";
  expectedMap["1"] = "secondValue";
  LinkedHashMap<String, String> expectedMapDifferentOrder = LinkedHashMap();
  expectedMap["1"] = "secondValue";
  expectedMap["0"] = "firstValue";
  print("actual: $actualMap");
  print("expected: $expectedMapSameOrder");
  print("expected: $expectedMapDifferentOrder");
  expect(actualMap, expectedMapSameOrder);
  expect(actualMap.keys, isNot(orderedEquals(expectedMapSameOrder.keys)));
  expect(actualMap, expectedMapDifferentOrder);
  expect(actualMap.keys, orderedEquals(expectedMapDifferentOrder.keys));
});