diagnostics_test.dart 83.7 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'dart:convert';
6
import 'dart:ui';
7

8
import 'package:flutter/foundation.dart';
9
import 'package:flutter_test/flutter_test.dart';
10

11
class TestTree extends Object with DiagnosticableTreeMixin {
12
  TestTree({
13
    this.name = '',
14
    this.style,
15 16
    this.children = const <TestTree>[],
    this.properties = const <DiagnosticsNode>[],
17 18 19 20
  });

  final String name;
  final List<TestTree> children;
21
  final List<DiagnosticsNode> properties;
22
  final DiagnosticsTreeStyle? style;
23 24

  @override
25
  List<DiagnosticsNode> debugDescribeChildren() => <DiagnosticsNode>[
26
    for (final TestTree child in children)
27
      child.toDiagnosticsNode(
28 29
        name: 'child ${child.name}',
        style: child.style,
30 31
      ),
  ];
32 33

  @override
34
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
35
    super.debugFillProperties(properties);
36
    if (style != null)
37
      properties.defaultDiagnosticsTreeStyle = style!;
38
    this.properties.forEach(properties.add);
39 40 41 42 43 44 45
  }
}

enum ExampleEnum {
  hello,
  world,
  deferToChild,
46 47
}

48 49
/// Encode and decode to JSON to make sure all objects in the JSON for the
/// [DiagnosticsNode] are valid JSON.
50 51
Map<String, Object?> simulateJsonSerialization(DiagnosticsNode node) {
  return json.decode(json.encode(node.toJsonMap(const DiagnosticsSerializationDelegate()))) as Map<String, Object?>;
52 53 54 55 56 57
}

void validateNodeJsonSerialization(DiagnosticsNode node) {
  validateNodeJsonSerializationHelper(simulateJsonSerialization(node), node);
}

58
void validateNodeJsonSerializationHelper(Map<String, Object?> json, DiagnosticsNode node) {
59
  expect(json['name'], equals(node.name));
60
  expect(json['showSeparator'] ?? true, equals(node.showSeparator));
61
  expect(json['description'], equals(node.toDescription()));
62 63
  expect(json['level'] ?? describeEnum(DiagnosticLevel.info), equals(describeEnum(node.level)));
  expect(json['showName'] ?? true, equals(node.showName));
64
  expect(json['emptyBodyDescription'], equals(node.emptyBodyDescription));
65
  expect(json['style'] ?? describeEnum(DiagnosticsTreeStyle.sparse), equals(describeEnum(node.style!)));
66
  expect(json['type'], equals(node.runtimeType.toString()));
67
  expect(json['hasChildren'] ?? false, equals(node.getChildren().isNotEmpty));
68 69
}

70
void validatePropertyJsonSerialization(DiagnosticsProperty<Object?> property) {
71 72 73 74
  validatePropertyJsonSerializationHelper(simulateJsonSerialization(property), property);
}

void validateStringPropertyJsonSerialization(StringProperty property) {
75
  final Map<String, Object?> json = simulateJsonSerialization(property);
76 77 78 79 80
  expect(json['quoted'], equals(property.quoted));
  validatePropertyJsonSerializationHelper(json, property);
}

void validateFlagPropertyJsonSerialization(FlagProperty property) {
81
  final Map<String, Object?> json = simulateJsonSerialization(property);
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
  expect(json['ifTrue'], equals(property.ifTrue));

  if (property.ifTrue != null) {
    expect(json['ifTrue'], equals(property.ifTrue));
  } else {
    expect(json.containsKey('ifTrue'), isFalse);
  }

  if (property.ifFalse != null) {
    expect(json['ifFalse'], property.ifFalse);
  } else {
    expect(json.containsKey('isFalse'), isFalse);
  }
  validatePropertyJsonSerializationHelper(json, property);
}

void validateDoublePropertyJsonSerialization(DoubleProperty property) {
99
  final Map<String, Object?> json = simulateJsonSerialization(property);
100 101 102 103 104 105 106 107 108 109 110 111
  if (property.unit != null) {
    expect(json['unit'], equals(property.unit));
  } else {
    expect(json.containsKey('unit'), isFalse);
  }

  expect(json['numberToString'], equals(property.numberToString()));

  validatePropertyJsonSerializationHelper(json, property);
}

void validateObjectFlagPropertyJsonSerialization(ObjectFlagProperty<Object> property) {
112
  final Map<String, Object?> json = simulateJsonSerialization(property);
113 114 115 116 117 118 119 120 121
  if (property.ifPresent != null) {
    expect(json['ifPresent'], equals(property.ifPresent));
  } else {
    expect(json.containsKey('ifPresent'), isFalse);
  }

  validatePropertyJsonSerializationHelper(json, property);
}

122
void validateIterableFlagsPropertyJsonSerialization(FlagsSummary<Object?> property) {
123
  final Map<String, Object?> json = simulateJsonSerialization(property);
124 125 126
  if (property.value.isNotEmpty) {
    expect(json['values'], equals(
      property.value.entries
127 128
        .where((MapEntry<String, Object?> entry) => entry.value != null)
        .map((MapEntry<String, Object?> entry) => entry.key).toList(),
129 130 131 132 133 134 135 136
    ));
  } else {
    expect(json.containsKey('values'), isFalse);
  }

  validatePropertyJsonSerializationHelper(json, property);
}

137
void validateIterablePropertyJsonSerialization(IterableProperty<Object> property) {
138
  final Map<String, Object?> json = simulateJsonSerialization(property);
139
  if (property.value != null) {
140
    final List<Object?> valuesJson = json['values']! as List<Object?>;
141
    final List<String> expectedValues = property.value!.map<String>((Object value) => value.toString()).toList();
142 143 144 145 146 147 148 149
    expect(listEquals(valuesJson, expectedValues), isTrue);
  } else {
    expect(json.containsKey('values'), isFalse);
  }

  validatePropertyJsonSerializationHelper(json, property);
}

150
void validatePropertyJsonSerializationHelper(final Map<String, Object?> json, DiagnosticsProperty<Object?> property) {
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
  if (property.defaultValue != kNoDefaultValue) {
    expect(json['defaultValue'], equals(property.defaultValue.toString()));
  } else {
    expect(json.containsKey('defaultValue'), isFalse);
  }

  if (property.ifEmpty != null) {
    expect(json['ifEmpty'], equals(property.ifEmpty));
  } else {
    expect(json.containsKey('ifEmpty'), isFalse);
  }
  if (property.ifNull != null) {
    expect(json['ifNull'], equals(property.ifNull));
  } else {
    expect(json.containsKey('ifNull'), isFalse);
  }

  if (property.tooltip != null) {
    expect(json['tooltip'], equals(property.tooltip));
  } else {
    expect(json.containsKey('tooltip'), isFalse);
  }

  expect(json['missingIfNull'], equals(property.missingIfNull));
  if (property.exception != null) {
    expect(json['exception'], equals(property.exception.toString()));
  } else {
    expect(json.containsKey('exception'), isFalse);
  }
  expect(json['propertyType'], equals(property.propertyType.toString()));
  expect(json.containsKey('defaultLevel'), isTrue);
182
  if (property.value is Diagnosticable) {
183 184 185 186 187 188 189
    expect(json['isDiagnosticableValue'], isTrue);
  } else {
    expect(json.containsKey('isDiagnosticableValue'), isFalse);
  }
  validateNodeJsonSerializationHelper(json, property);
}

190 191
void main() {
  test('TreeDiagnosticsMixin control test', () async {
192 193
    void goldenStyleTest(
      String description, {
194 195
      DiagnosticsTreeStyle? style,
      DiagnosticsTreeStyle? lastChildStyle,
196 197
      String golden = '',
    }) {
198 199 200
      final TestTree tree = TestTree(children: <TestTree>[
        TestTree(name: 'node A', style: style),
        TestTree(
201 202
          name: 'node B',
          children: <TestTree>[
203 204 205
            TestTree(name: 'node B1', style: style),
            TestTree(name: 'node B2', style: style),
            TestTree(name: 'node B3', style: lastChildStyle ?? style),
206 207 208
          ],
          style: style,
        ),
209
        TestTree(name: 'node C', style: lastChildStyle ?? style),
210 211 212 213 214 215 216 217
      ], style: lastChildStyle);

      expect(tree, hasAGoodToStringDeep);
      expect(
        tree.toDiagnosticsNode(style: style).toStringDeep(),
        equalsIgnoringHashCodes(golden),
        reason: description,
      );
218
      validateNodeJsonSerialization(tree.toDiagnosticsNode());
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
    }

    goldenStyleTest(
      'dense',
      style: DiagnosticsTreeStyle.dense,
      golden:
      'TestTree#00000\n'
      '├child node A: TestTree#00000\n'
      '├child node B: TestTree#00000\n'
      '│├child node B1: TestTree#00000\n'
      '│├child node B2: TestTree#00000\n'
      '│└child node B3: TestTree#00000\n'
      '└child node C: TestTree#00000\n',
    );

    goldenStyleTest(
      'sparse',
      style: DiagnosticsTreeStyle.sparse,
      golden:
      'TestTree#00000\n'
      ' ├─child node A: TestTree#00000\n'
      ' ├─child node B: TestTree#00000\n'
      ' │ ├─child node B1: TestTree#00000\n'
      ' │ ├─child node B2: TestTree#00000\n'
      ' │ └─child node B3: TestTree#00000\n'
      ' └─child node C: TestTree#00000\n',
    );

    goldenStyleTest(
      'dashed',
      style: DiagnosticsTreeStyle.offstage,
      golden:
      'TestTree#00000\n'
      ' ╎╌child node A: TestTree#00000\n'
      ' ╎╌child node B: TestTree#00000\n'
      ' ╎ ╎╌child node B1: TestTree#00000\n'
      ' ╎ ╎╌child node B2: TestTree#00000\n'
      ' ╎ └╌child node B3: TestTree#00000\n'
      ' └╌child node C: TestTree#00000\n',
    );

    goldenStyleTest(
      'leaf children',
      style: DiagnosticsTreeStyle.sparse,
      lastChildStyle: DiagnosticsTreeStyle.transition,
      golden:
      'TestTree#00000\n'
      ' ├─child node A: TestTree#00000\n'
      ' ├─child node B: TestTree#00000\n'
      ' │ ├─child node B1: TestTree#00000\n'
      ' │ ├─child node B2: TestTree#00000\n'
      ' │ ╘═╦══ child node B3 ═══\n'
      ' │   ║ TestTree#00000\n'
      ' │   ╚═══════════\n'
      ' ╘═╦══ child node C ═══\n'
      '   ║ TestTree#00000\n'
      '   ╚═══════════\n',
    );

278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
    goldenStyleTest(
      'error children',
      style: DiagnosticsTreeStyle.sparse,
      lastChildStyle: DiagnosticsTreeStyle.error,
      golden:
      'TestTree#00000\n'
      ' ├─child node A: TestTree#00000\n'
      ' ├─child node B: TestTree#00000\n'
      ' │ ├─child node B1: TestTree#00000\n'
      ' │ ├─child node B2: TestTree#00000\n'
      ' │ ╘═╦══╡ CHILD NODE B3: TESTTREE#00000 ╞═══════════════════════════════\n'
      ' │   ╚══════════════════════════════════════════════════════════════════\n'
      ' ╘═╦══╡ CHILD NODE C: TESTTREE#00000 ╞════════════════════════════════\n'
      '   ╚══════════════════════════════════════════════════════════════════\n',
    );

    // You would never really want to make everything a transition child like
    // this but you can and still get a readable tree.
296 297 298
    // The joint between single and double lines here is a bit clunky
    // but we could correct that if there is any real use for this style.
    goldenStyleTest(
299
      'transition',
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
      style: DiagnosticsTreeStyle.transition,
      golden:
      'TestTree#00000:\n'
      '  ╞═╦══ child node A ═══\n'
      '  │ ║ TestTree#00000\n'
      '  │ ╚═══════════\n'
      '  ╞═╦══ child node B ═══\n'
      '  │ ║ TestTree#00000:\n'
      '  │ ║   ╞═╦══ child node B1 ═══\n'
      '  │ ║   │ ║ TestTree#00000\n'
      '  │ ║   │ ╚═══════════\n'
      '  │ ║   ╞═╦══ child node B2 ═══\n'
      '  │ ║   │ ║ TestTree#00000\n'
      '  │ ║   │ ╚═══════════\n'
      '  │ ║   ╘═╦══ child node B3 ═══\n'
      '  │ ║     ║ TestTree#00000\n'
      '  │ ║     ╚═══════════\n'
      '  │ ╚═══════════\n'
      '  ╘═╦══ child node C ═══\n'
      '    ║ TestTree#00000\n'
      '    ╚═══════════\n',
    );

323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
    goldenStyleTest(
      'error',
      style: DiagnosticsTreeStyle.error,
      golden:
      '══╡ TESTTREE#00000 ╞═════════════════════════════════════════════\n'
      '╞═╦══╡ CHILD NODE A: TESTTREE#00000 ╞════════════════════════════════\n'
      '│ ╚══════════════════════════════════════════════════════════════════\n'
      '╞═╦══╡ CHILD NODE B: TESTTREE#00000 ╞════════════════════════════════\n'
      '│ ║ ╞═╦══╡ CHILD NODE B1: TESTTREE#00000 ╞═══════════════════════════════\n'
      '│ ║ │ ╚══════════════════════════════════════════════════════════════════\n'
      '│ ║ ╞═╦══╡ CHILD NODE B2: TESTTREE#00000 ╞═══════════════════════════════\n'
      '│ ║ │ ╚══════════════════════════════════════════════════════════════════\n'
      '│ ║ ╘═╦══╡ CHILD NODE B3: TESTTREE#00000 ╞═══════════════════════════════\n'
      '│ ║   ╚══════════════════════════════════════════════════════════════════\n'
      '│ ╚══════════════════════════════════════════════════════════════════\n'
      '╘═╦══╡ CHILD NODE C: TESTTREE#00000 ╞════════════════════════════════\n'
      '  ╚══════════════════════════════════════════════════════════════════\n'
      '═════════════════════════════════════════════════════════════════\n',
    );

343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
    goldenStyleTest(
      'whitespace',
      style: DiagnosticsTreeStyle.whitespace,
      golden:
      'TestTree#00000:\n'
      '  child node A: TestTree#00000\n'
      '  child node B: TestTree#00000:\n'
      '    child node B1: TestTree#00000\n'
      '    child node B2: TestTree#00000\n'
      '    child node B3: TestTree#00000\n'
      '  child node C: TestTree#00000\n',
    );

    // Single line mode does not display children.
    goldenStyleTest(
      'single line',
      style: DiagnosticsTreeStyle.singleLine,
      golden: 'TestTree#00000',
    );
  });

  test('TreeDiagnosticsMixin tree with properties test', () async {
365 366
    void goldenStyleTest(
      String description, {
367 368 369
      String? name,
      DiagnosticsTreeStyle? style,
      DiagnosticsTreeStyle? lastChildStyle,
370
      DiagnosticsTreeStyle propertyStyle = DiagnosticsTreeStyle.singleLine,
371
      required String golden,
372
    }) {
373
      final TestTree tree = TestTree(
374
        properties: <DiagnosticsNode>[
375 376 377 378 379 380 381
          StringProperty('stringProperty1', 'value1', quoted: false, style: propertyStyle),
          DoubleProperty('doubleProperty1', 42.5, style: propertyStyle),
          DoubleProperty('roundedProperty', 1.0 / 3.0, style: propertyStyle),
          StringProperty('DO_NOT_SHOW', 'DO_NOT_SHOW', level: DiagnosticLevel.hidden, quoted: false, style: propertyStyle),
          DiagnosticsProperty<Object>('DO_NOT_SHOW_NULL', null, defaultValue: null, style: propertyStyle),
          DiagnosticsProperty<Object>('nullProperty', null, style: propertyStyle),
          StringProperty('node_type', '<root node>', showName: false, quoted: false, style: propertyStyle),
382 383
        ],
        children: <TestTree>[
384 385
          TestTree(name: 'node A', style: style),
          TestTree(
386 387
            name: 'node B',
            properties: <DiagnosticsNode>[
388 389
              StringProperty('p1', 'v1', quoted: false, style: propertyStyle),
              StringProperty('p2', 'v2', quoted: false, style: propertyStyle),
390 391
            ],
            children: <TestTree>[
392 393
              TestTree(name: 'node B1', style: style),
              TestTree(
394
                name: 'node B2',
395
                properties: <DiagnosticsNode>[StringProperty('property1', 'value1', quoted: false, style: propertyStyle)],
396 397
                style: style,
              ),
398
              TestTree(
399 400
                name: 'node B3',
                properties: <DiagnosticsNode>[
401 402
                  StringProperty('node_type', '<leaf node>', showName: false, quoted: false, style: propertyStyle),
                  IntProperty('foo', 42, style: propertyStyle),
403 404 405 406 407 408
                ],
                style: lastChildStyle ?? style,
              ),
            ],
            style: style,
          ),
409
          TestTree(
410 411
            name: 'node C',
            properties: <DiagnosticsNode>[
412
              StringProperty('foo', 'multi\nline\nvalue!', quoted: false, style: propertyStyle),
413 414 415 416 417 418 419
            ],
            style: lastChildStyle ?? style,
          ),
        ],
        style: lastChildStyle,
      );

420 421
      if (tree.style != DiagnosticsTreeStyle.singleLine &&
          tree.style != DiagnosticsTreeStyle.errorProperty) {
422
        expect(tree, hasAGoodToStringDeep);
423
      }
424

425
      expect(
426
        tree.toDiagnosticsNode(name: name, style: style).toStringDeep(),
427 428 429
        equalsIgnoringHashCodes(golden),
        reason: description,
      );
430
      validateNodeJsonSerialization(tree.toDiagnosticsNode());
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
    }

    goldenStyleTest(
      'sparse',
      style: DiagnosticsTreeStyle.sparse,
      golden:
      'TestTree#00000\n'
      ' │ stringProperty1: value1\n'
      ' │ doubleProperty1: 42.5\n'
      ' │ roundedProperty: 0.3\n'
      ' │ nullProperty: null\n'
      ' │ <root node>\n'
      ' │\n'
      ' ├─child node A: TestTree#00000\n'
      ' ├─child node B: TestTree#00000\n'
      ' │ │ p1: v1\n'
      ' │ │ p2: v2\n'
      ' │ │\n'
      ' │ ├─child node B1: TestTree#00000\n'
      ' │ ├─child node B2: TestTree#00000\n'
      ' │ │   property1: value1\n'
      ' │ │\n'
      ' │ └─child node B3: TestTree#00000\n'
      ' │     <leaf node>\n'
      ' │     foo: 42\n'
      ' │\n'
      ' └─child node C: TestTree#00000\n'
      '     foo:\n'
459 460 461
      '       multi\n'
      '       line\n'
      '       value!\n',
462 463
    );

464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
    goldenStyleTest(
      'sparse with indented single line properties',
      style: DiagnosticsTreeStyle.sparse,
      propertyStyle: DiagnosticsTreeStyle.errorProperty,
      golden:
      'TestTree#00000\n'
      ' │ stringProperty1:\n'
      ' │   value1\n'
      ' │ doubleProperty1:\n'
      ' │   42.5\n'
      ' │ roundedProperty:\n'
      ' │   0.3\n'
      ' │ nullProperty:\n'
      ' │   null\n'
      ' │ <root node>\n'
      ' │\n'
      ' ├─child node A: TestTree#00000\n'
      ' ├─child node B: TestTree#00000\n'
      ' │ │ p1:\n'
      ' │ │   v1\n'
      ' │ │ p2:\n'
      ' │ │   v2\n'
      ' │ │\n'
      ' │ ├─child node B1: TestTree#00000\n'
      ' │ ├─child node B2: TestTree#00000\n'
      ' │ │   property1:\n'
      ' │ │     value1\n'
      ' │ │\n'
      ' │ └─child node B3: TestTree#00000\n'
      ' │     <leaf node>\n'
      ' │     foo: 42\n'
      ' │\n'
      ' └─child node C: TestTree#00000\n'
      '     foo:\n'
      '       multi\n'
      '       line\n'
      '       value!\n',
    );

503 504 505 506
    goldenStyleTest(
      'dense',
      style: DiagnosticsTreeStyle.dense,
      golden:
507 508 509 510 511 512 513
        'TestTree#00000(stringProperty1: value1, doubleProperty1: 42.5, roundedProperty: 0.3, nullProperty: null, <root node>)\n'
        '├child node A: TestTree#00000\n'
        '├child node B: TestTree#00000(p1: v1, p2: v2)\n'
        '│├child node B1: TestTree#00000\n'
        '│├child node B2: TestTree#00000(property1: value1)\n'
        '│└child node B3: TestTree#00000(<leaf node>, foo: 42)\n'
        '└child node C: TestTree#00000(foo: multi\\nline\\nvalue!)\n',
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
    );

    goldenStyleTest(
      'dashed',
      style: DiagnosticsTreeStyle.offstage,
      golden:
      'TestTree#00000\n'
      ' │ stringProperty1: value1\n'
      ' │ doubleProperty1: 42.5\n'
      ' │ roundedProperty: 0.3\n'
      ' │ nullProperty: null\n'
      ' │ <root node>\n'
      ' │\n'
      ' ╎╌child node A: TestTree#00000\n'
      ' ╎╌child node B: TestTree#00000\n'
      ' ╎ │ p1: v1\n'
      ' ╎ │ p2: v2\n'
      ' ╎ │\n'
      ' ╎ ╎╌child node B1: TestTree#00000\n'
      ' ╎ ╎╌child node B2: TestTree#00000\n'
      ' ╎ ╎   property1: value1\n'
      ' ╎ ╎\n'
      ' ╎ └╌child node B3: TestTree#00000\n'
      ' ╎     <leaf node>\n'
      ' ╎     foo: 42\n'
      ' ╎\n'
      ' └╌child node C: TestTree#00000\n'
      '     foo:\n'
542 543 544
      '       multi\n'
      '       line\n'
      '       value!\n',
545 546 547
    );

    goldenStyleTest(
548
      'transition children',
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
      style: DiagnosticsTreeStyle.sparse,
      lastChildStyle: DiagnosticsTreeStyle.transition,
      golden:
      'TestTree#00000\n'
      ' │ stringProperty1: value1\n'
      ' │ doubleProperty1: 42.5\n'
      ' │ roundedProperty: 0.3\n'
      ' │ nullProperty: null\n'
      ' │ <root node>\n'
      ' │\n'
      ' ├─child node A: TestTree#00000\n'
      ' ├─child node B: TestTree#00000\n'
      ' │ │ p1: v1\n'
      ' │ │ p2: v2\n'
      ' │ │\n'
      ' │ ├─child node B1: TestTree#00000\n'
      ' │ ├─child node B2: TestTree#00000\n'
      ' │ │   property1: value1\n'
      ' │ │\n'
      ' │ ╘═╦══ child node B3 ═══\n'
      ' │   ║ TestTree#00000:\n'
      ' │   ║   <leaf node>\n'
      ' │   ║   foo: 42\n'
      ' │   ╚═══════════\n'
      ' ╘═╦══ child node C ═══\n'
      '   ║ TestTree#00000:\n'
      '   ║   foo:\n'
576 577 578
      '   ║     multi\n'
      '   ║     line\n'
      '   ║     value!\n'
579 580 581
      '   ╚═══════════\n',
    );

582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
    goldenStyleTest(
      'error children',
      style: DiagnosticsTreeStyle.sparse,
      lastChildStyle: DiagnosticsTreeStyle.error,
      golden:
      'TestTree#00000\n'
      ' │ stringProperty1: value1\n'
      ' │ doubleProperty1: 42.5\n'
      ' │ roundedProperty: 0.3\n'
      ' │ nullProperty: null\n'
      ' │ <root node>\n'
      ' │\n'
      ' ├─child node A: TestTree#00000\n'
      ' ├─child node B: TestTree#00000\n'
      ' │ │ p1: v1\n'
      ' │ │ p2: v2\n'
      ' │ │\n'
      ' │ ├─child node B1: TestTree#00000\n'
      ' │ ├─child node B2: TestTree#00000\n'
      ' │ │   property1: value1\n'
      ' │ │\n'
      ' │ ╘═╦══╡ CHILD NODE B3: TESTTREE#00000 ╞═══════════════════════════════\n'
      ' │   ║ <leaf node>\n'
      ' │   ║ foo: 42\n'
      ' │   ╚══════════════════════════════════════════════════════════════════\n'
      ' ╘═╦══╡ CHILD NODE C: TESTTREE#00000 ╞════════════════════════════════\n'
      '   ║ foo:\n'
      '   ║   multi\n'
      '   ║   line\n'
      '   ║   value!\n'
      '   ╚══════════════════════════════════════════════════════════════════\n',
    );


616 617
    // You would never really want to make everything a transition child like
    // this but you can and still get a readable tree.
618
    goldenStyleTest(
619
      'transition',
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
      style: DiagnosticsTreeStyle.transition,
      golden:
      'TestTree#00000:\n'
      '  stringProperty1: value1\n'
      '  doubleProperty1: 42.5\n'
      '  roundedProperty: 0.3\n'
      '  nullProperty: null\n'
      '  <root node>\n'
      '  ╞═╦══ child node A ═══\n'
      '  │ ║ TestTree#00000\n'
      '  │ ╚═══════════\n'
      '  ╞═╦══ child node B ═══\n'
      '  │ ║ TestTree#00000:\n'
      '  │ ║   p1: v1\n'
      '  │ ║   p2: v2\n'
      '  │ ║   ╞═╦══ child node B1 ═══\n'
      '  │ ║   │ ║ TestTree#00000\n'
      '  │ ║   │ ╚═══════════\n'
      '  │ ║   ╞═╦══ child node B2 ═══\n'
      '  │ ║   │ ║ TestTree#00000:\n'
      '  │ ║   │ ║   property1: value1\n'
      '  │ ║   │ ╚═══════════\n'
      '  │ ║   ╘═╦══ child node B3 ═══\n'
      '  │ ║     ║ TestTree#00000:\n'
      '  │ ║     ║   <leaf node>\n'
      '  │ ║     ║   foo: 42\n'
      '  │ ║     ╚═══════════\n'
      '  │ ╚═══════════\n'
      '  ╘═╦══ child node C ═══\n'
      '    ║ TestTree#00000:\n'
      '    ║   foo:\n'
651 652 653
      '    ║     multi\n'
      '    ║     line\n'
      '    ║     value!\n'
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
      '    ╚═══════════\n',
    );

    goldenStyleTest(
      'whitespace',
      style: DiagnosticsTreeStyle.whitespace,
      golden:
        'TestTree#00000:\n'
        '  stringProperty1: value1\n'
        '  doubleProperty1: 42.5\n'
        '  roundedProperty: 0.3\n'
        '  nullProperty: null\n'
        '  <root node>\n'
        '  child node A: TestTree#00000\n'
        '  child node B: TestTree#00000:\n'
        '    p1: v1\n'
        '    p2: v2\n'
        '    child node B1: TestTree#00000\n'
        '    child node B2: TestTree#00000:\n'
        '      property1: value1\n'
        '    child node B3: TestTree#00000:\n'
        '      <leaf node>\n'
        '      foo: 42\n'
        '  child node C: TestTree#00000:\n'
        '    foo:\n'
679 680 681
        '      multi\n'
        '      line\n'
        '      value!\n',
682 683
    );

684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
    goldenStyleTest(
      'flat',
      style: DiagnosticsTreeStyle.flat,
      golden:
      'TestTree#00000:\n'
      'stringProperty1: value1\n'
      'doubleProperty1: 42.5\n'
      'roundedProperty: 0.3\n'
      'nullProperty: null\n'
      '<root node>\n'
      'child node A: TestTree#00000\n'
      'child node B: TestTree#00000:\n'
      'p1: v1\n'
      'p2: v2\n'
      'child node B1: TestTree#00000\n'
      'child node B2: TestTree#00000:\n'
      'property1: value1\n'
      'child node B3: TestTree#00000:\n'
      '<leaf node>\n'
      'foo: 42\n'
      'child node C: TestTree#00000:\n'
      'foo:\n'
      '  multi\n'
      '  line\n'
      '  value!\n',
    );
710 711 712 713 714 715 716
    // Single line mode does not display children.
    goldenStyleTest(
      'single line',
      style: DiagnosticsTreeStyle.singleLine,
      golden: 'TestTree#00000(stringProperty1: value1, doubleProperty1: 42.5, roundedProperty: 0.3, nullProperty: null, <root node>)',
    );

717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
    goldenStyleTest(
      'single line',
      name: 'some name',
      style: DiagnosticsTreeStyle.singleLine,
      golden: 'some name: TestTree#00000(stringProperty1: value1, doubleProperty1: 42.5, roundedProperty: 0.3, nullProperty: null, <root node>)',
    );

    // No name so we don't indent.
    goldenStyleTest(
      'indented single line',
      style: DiagnosticsTreeStyle.errorProperty,
      golden: 'TestTree#00000(stringProperty1: value1, doubleProperty1: 42.5, roundedProperty: 0.3, nullProperty: null, <root node>)\n',
    );

    goldenStyleTest(
      'indented single line',
      name: 'some name',
      style: DiagnosticsTreeStyle.errorProperty,
      golden:
      'some name:\n'
      '  TestTree#00000(stringProperty1: value1, doubleProperty1: 42.5, roundedProperty: 0.3, nullProperty: null, <root node>)\n',
    );

740
    // TODO(jacobr): this is an ugly test case.
741
    // There isn't anything interesting for this case as the children look the
742 743
    // same with and without children. Only difference is the odd and
    // undesirable density of B3 being right next to node C.
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
    goldenStyleTest(
      'single line last child',
      style: DiagnosticsTreeStyle.sparse,
      lastChildStyle: DiagnosticsTreeStyle.singleLine,
      golden:
      'TestTree#00000\n'
      ' │ stringProperty1: value1\n'
      ' │ doubleProperty1: 42.5\n'
      ' │ roundedProperty: 0.3\n'
      ' │ nullProperty: null\n'
      ' │ <root node>\n'
      ' │\n'
      ' ├─child node A: TestTree#00000\n'
      ' ├─child node B: TestTree#00000\n'
      ' │ │ p1: v1\n'
      ' │ │ p2: v2\n'
      ' │ │\n'
      ' │ ├─child node B1: TestTree#00000\n'
      ' │ ├─child node B2: TestTree#00000\n'
      ' │ │   property1: value1\n'
      ' │ │\n'
      ' │ └─child node B3: TestTree#00000(<leaf node>, foo: 42)\n'
766
      ' └─child node C: TestTree#00000(foo: multi\\nline\\nvalue!)\n',
767
    );
768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801

    // TODO(jacobr): this is an ugly test case.
    // There isn't anything interesting for this case as the children look the
    // same with and without children. Only difference is the odd and
    // undesirable density of B3 being right next to node C.
    // Typically header lines should not be places as leaves in the tree and
    // should instead be places in front of other nodes that they
    // function as a header for.
    goldenStyleTest(
      'indented single line last child',
      style: DiagnosticsTreeStyle.sparse,
      lastChildStyle: DiagnosticsTreeStyle.errorProperty,
      golden:
      'TestTree#00000\n'
      ' │ stringProperty1: value1\n'
      ' │ doubleProperty1: 42.5\n'
      ' │ roundedProperty: 0.3\n'
      ' │ nullProperty: null\n'
      ' │ <root node>\n'
      ' │\n'
      ' ├─child node A: TestTree#00000\n'
      ' ├─child node B: TestTree#00000\n'
      ' │ │ p1: v1\n'
      ' │ │ p2: v2\n'
      ' │ │\n'
      ' │ ├─child node B1: TestTree#00000\n'
      ' │ ├─child node B2: TestTree#00000\n'
      ' │ │   property1: value1\n'
      ' │ │\n'
      ' │ └─child node B3:\n'
      ' │     TestTree#00000(<leaf node>, foo: 42)\n'
      ' └─child node C:\n'
      '     TestTree#00000(foo: multi\\nline\\nvalue!)\n',
    );
802 803
  });

804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
  test('toString test', () {
    final TestTree tree = TestTree(
      properties: <DiagnosticsNode>[
        StringProperty('stringProperty1', 'value1', quoted: false),
        DoubleProperty('doubleProperty1', 42.5),
        DoubleProperty('roundedProperty', 1.0 / 3.0),
        StringProperty('DO_NOT_SHOW', 'DO_NOT_SHOW', level: DiagnosticLevel.hidden, quoted: false),
        StringProperty('DEBUG_ONLY', 'DEBUG_ONLY', level: DiagnosticLevel.debug, quoted: false),
      ],
      // child to verify that children are not included in the toString.
      children: <TestTree>[TestTree(name: 'node A')],
    );

    expect(
      tree.toString(),
      equalsIgnoringHashCodes('TestTree#00000(stringProperty1: value1, doubleProperty1: 42.5, roundedProperty: 0.3)'),
    );

    expect(
      tree.toString(minLevel: DiagnosticLevel.debug),
      equalsIgnoringHashCodes('TestTree#00000(stringProperty1: value1, doubleProperty1: 42.5, roundedProperty: 0.3, DEBUG_ONLY: DEBUG_ONLY)'),
    );
  });

828 829 830
  test('transition test', () {
    // Test multiple styles integrating together in the same tree due to using
    // transition to go between styles that would otherwise be incompatible.
831
    final TestTree tree = TestTree(
832 833
      style: DiagnosticsTreeStyle.sparse,
      properties: <DiagnosticsNode>[
834
        StringProperty('stringProperty1', 'value1'),
835
      ],
836
      children: <TestTree>[
837
        TestTree(
838 839 840
          style: DiagnosticsTreeStyle.transition,
          name: 'node transition',
          properties: <DiagnosticsNode>[
841 842
            StringProperty('p1', 'v1'),
            TestTree(
843
              properties: <DiagnosticsNode>[
844
                DiagnosticsProperty<bool>('survived', true),
845 846 847
              ],
            ).toDiagnosticsNode(name: 'tree property', style: DiagnosticsTreeStyle.whitespace),
          ],
848
          children: <TestTree>[
849 850
            TestTree(name: 'dense child', style: DiagnosticsTreeStyle.dense),
            TestTree(
851
              name: 'dense',
852
              properties: <DiagnosticsNode>[StringProperty('property1', 'value1')],
853 854
              style: DiagnosticsTreeStyle.dense,
            ),
855
            TestTree(
856 857
              name: 'node B3',
              properties: <DiagnosticsNode>[
858 859
                StringProperty('node_type', '<leaf node>', showName: false, quoted: false),
                IntProperty('foo', 42),
860
              ],
861
              style: DiagnosticsTreeStyle.dense,
862
            ),
863 864
          ],
        ),
865
        TestTree(
866 867
          name: 'node C',
          properties: <DiagnosticsNode>[
868
            StringProperty('foo', 'multi\nline\nvalue!', quoted: false),
869 870 871
          ],
          style: DiagnosticsTreeStyle.sparse,
        ),
872 873 874
      ],
    );

875
    expect(tree, hasAGoodToStringDeep);
876
    expect(
877
      tree.toDiagnosticsNode().toStringDeep(),
878
      equalsIgnoringHashCodes(
879 880 881 882 883 884 885 886
        'TestTree#00000\n'
        ' │ stringProperty1: "value1"\n'
        ' ╞═╦══ child node transition ═══\n'
        ' │ ║ TestTree#00000:\n'
        ' │ ║   p1: "v1"\n'
        ' │ ║   tree property: TestTree#00000:\n'
        ' │ ║     survived: true\n'
        ' │ ║   ├child dense child: TestTree#00000\n'
887 888
        ' │ ║   ├child dense: TestTree#00000(property1: "value1")\n'
        ' │ ║   └child node B3: TestTree#00000(<leaf node>, foo: 42)\n'
889 890 891
        ' │ ╚═══════════\n'
        ' └─child node C: TestTree#00000\n'
        '     foo:\n'
892 893 894
        '       multi\n'
        '       line\n'
        '       value!\n',
895 896
      ),
    );
897
  });
898 899 900 901 902

  test('describeEnum test', () {
    expect(describeEnum(ExampleEnum.hello), equals('hello'));
    expect(describeEnum(ExampleEnum.world), equals('world'));
    expect(describeEnum(ExampleEnum.deferToChild), equals('deferToChild'));
903 904 905 906 907 908 909 910
    expect(
      () => describeEnum('Hello World'),
      throwsA(isAssertionError.having(
        (AssertionError e) => e.message,
        'message',
        'The provided object "Hello World" is not an enum.'),
      ),
    );
911 912 913 914
  });

  test('string property test', () {
    expect(
915
      StringProperty('name', 'value', quoted: false).toString(),
916 917 918
      equals('name: value'),
    );

919
    final StringProperty stringProperty = StringProperty(
920 921 922 923 924
      'name',
      'value',
      description: 'VALUE',
      ifEmpty: '<hidden>',
      quoted: false,
925
    );
926 927
    expect(stringProperty.toString(), equals('name: VALUE'));
    validateStringPropertyJsonSerialization(stringProperty);
928 929

    expect(
930
      StringProperty(
931 932 933 934 935 936 937 938 939 940
        'name',
        'value',
        showName: false,
        ifEmpty: '<hidden>',
        quoted: false,
      ).toString(),
      equals('value'),
    );

    expect(
941
      StringProperty('name', '', ifEmpty: '<hidden>').toString(),
942 943 944 945
      equals('name: <hidden>'),
    );

    expect(
946
      StringProperty(
947 948 949 950 951 952 953 954
        'name',
        '',
        ifEmpty: '<hidden>',
        showName: false,
      ).toString(),
      equals('<hidden>'),
    );

955 956 957 958
    expect(StringProperty('name', null).isFiltered(DiagnosticLevel.info), isFalse);
    expect(StringProperty('name', 'value', level: DiagnosticLevel.hidden).isFiltered(DiagnosticLevel.info), isTrue);
    expect(StringProperty('name', null, defaultValue: null).isFiltered(DiagnosticLevel.info), isTrue);
    final StringProperty quoted = StringProperty(
959 960 961
      'name',
      'value',
      quoted: true,
962
    );
963 964
    expect(quoted.toString(), equals('name: "value"'));
    validateStringPropertyJsonSerialization(quoted);
965 966

    expect(
967
      StringProperty('name', 'value', showName: false).toString(),
968 969 970 971
      equals('"value"'),
    );

    expect(
972
      StringProperty(
973 974 975 976 977 978 979 980 981 982
        'name',
        null,
        showName: false,
        quoted: true,
      ).toString(),
      equals('null'),
    );
  });

  test('bool property test', () {
983 984
    final DiagnosticsProperty<bool> trueProperty = DiagnosticsProperty<bool>('name', true);
    final DiagnosticsProperty<bool> falseProperty = DiagnosticsProperty<bool>('name', false);
985
    expect(trueProperty.toString(), equals('name: true'));
986
    expect(trueProperty.isFiltered(DiagnosticLevel.info), isFalse);
987
    expect(trueProperty.value, isTrue);
988
    expect(falseProperty.toString(), equals('name: false'));
989
    expect(falseProperty.value, isFalse);
990
    expect(falseProperty.isFiltered(DiagnosticLevel.info), isFalse);
991 992
    validatePropertyJsonSerialization(trueProperty);
    validatePropertyJsonSerialization(falseProperty);
993
    final DiagnosticsProperty<bool> truthyProperty = DiagnosticsProperty<bool>(
994 995 996 997
      'name',
      true,
      description: 'truthy',
    );
998
    expect(
999
      truthyProperty.toString(),
1000 1001
      equals('name: truthy'),
    );
1002
    validatePropertyJsonSerialization(truthyProperty);
1003
    expect(
1004
      DiagnosticsProperty<bool>('name', true, showName: false).toString(),
1005 1006 1007
      equals('true'),
    );

1008 1009 1010 1011
    expect(DiagnosticsProperty<bool>('name', null).isFiltered(DiagnosticLevel.info), isFalse);
    expect(DiagnosticsProperty<bool>('name', true, level: DiagnosticLevel.hidden).isFiltered(DiagnosticLevel.info), isTrue);
    expect(DiagnosticsProperty<bool>('name', null, defaultValue: null).isFiltered(DiagnosticLevel.info), isTrue);
    final DiagnosticsProperty<bool> missingBool = DiagnosticsProperty<bool>('name', null, ifNull: 'missing');
1012
    expect(
1013
      missingBool.toString(),
1014 1015
      equals('name: missing'),
    );
1016
    validatePropertyJsonSerialization(missingBool);
1017 1018 1019
  });

  test('flag property test', () {
1020
    final FlagProperty trueFlag = FlagProperty(
1021 1022 1023 1024
      'myFlag',
      value: true,
      ifTrue: 'myFlag',
    );
1025
    final FlagProperty falseFlag = FlagProperty(
1026 1027 1028 1029 1030
      'myFlag',
      value: false,
      ifTrue: 'myFlag',
    );
    expect(trueFlag.toString(), equals('myFlag'));
1031 1032
    validateFlagPropertyJsonSerialization(trueFlag);
    validateFlagPropertyJsonSerialization(falseFlag);
1033

1034 1035
    expect(trueFlag.value, isTrue);
    expect(falseFlag.value, isFalse);
1036

1037 1038
    expect(trueFlag.isFiltered(DiagnosticLevel.fine), isFalse);
    expect(falseFlag.isFiltered(DiagnosticLevel.fine), isTrue);
1039 1040 1041
  });

  test('property with tooltip test', () {
1042
    final DiagnosticsProperty<String> withTooltip = DiagnosticsProperty<String>(
1043 1044 1045 1046 1047 1048 1049 1050
      'name',
      'value',
      tooltip: 'tooltip',
    );
    expect(
     withTooltip.toString(),
      equals('name: value (tooltip)'),
    );
1051
    expect(withTooltip.value, equals('value'));
1052
    expect(withTooltip.isFiltered(DiagnosticLevel.fine), isFalse);
1053
    validatePropertyJsonSerialization(withTooltip);
1054 1055 1056
  });

  test('double property test', () {
1057
    final DoubleProperty doubleProperty = DoubleProperty(
1058 1059 1060 1061
      'name',
      42.0,
    );
    expect(doubleProperty.toString(), equals('name: 42.0'));
1062
    expect(doubleProperty.isFiltered(DiagnosticLevel.info), isFalse);
1063
    expect(doubleProperty.value, equals(42.0));
1064
    validateDoublePropertyJsonSerialization(doubleProperty);
1065

1066
    expect(DoubleProperty('name', 1.3333).toString(), equals('name: 1.3'));
1067

1068 1069
    expect(DoubleProperty('name', null).toString(), equals('name: null'));
    expect(DoubleProperty('name', null).isFiltered(DiagnosticLevel.info), equals(false));
1070 1071

    expect(
1072
      DoubleProperty('name', null, ifNull: 'missing').toString(),
1073 1074 1075
      equals('name: missing'),
    );

1076
    final DoubleProperty doubleWithUnit = DoubleProperty('name', 42.0, unit: 'px');
1077 1078
    expect(doubleWithUnit.toString(), equals('name: 42.0px'));
    validateDoublePropertyJsonSerialization(doubleWithUnit);
1079 1080
  });

1081 1082 1083 1084 1085 1086 1087 1088 1089
  test('double.infinity serialization test', () {
    final DoubleProperty infProperty1 = DoubleProperty('double1', double.infinity);
    validateDoublePropertyJsonSerialization(infProperty1);
    expect(infProperty1.toString(), equals('double1: Infinity'));

    final DoubleProperty infProperty2 = DoubleProperty('double2', double.negativeInfinity);
    validateDoublePropertyJsonSerialization(infProperty2);
    expect(infProperty2.toString(), equals('double2: -Infinity'));
  });
1090 1091

  test('unsafe double property test', () {
1092
    final DoubleProperty safe = DoubleProperty.lazy(
1093 1094 1095 1096
      'name',
        () => 42.0,
    );
    expect(safe.toString(), equals('name: 42.0'));
1097
    expect(safe.isFiltered(DiagnosticLevel.info), isFalse);
1098
    expect(safe.value, equals(42.0));
1099
    validateDoublePropertyJsonSerialization(safe);
1100
    expect(
1101
      DoubleProperty.lazy('name', () => 1.3333).toString(),
1102 1103 1104 1105
      equals('name: 1.3'),
    );

    expect(
1106
      DoubleProperty.lazy('name', () => null).toString(),
1107 1108 1109
      equals('name: null'),
    );
    expect(
1110
      DoubleProperty.lazy('name', () => null).isFiltered(DiagnosticLevel.info),
1111 1112 1113
      equals(false),
    );

1114
    final DoubleProperty throwingProperty = DoubleProperty.lazy(
1115
      'name',
1116
      () => throw FlutterError.fromParts(<DiagnosticsNode>[ErrorSummary('Invalid constraints')]),
1117 1118 1119
    );
    // TODO(jacobr): it would be better if throwingProperty.object threw an
    // exception.
1120
    expect(throwingProperty.value, isNull);
1121
    expect(throwingProperty.isFiltered(DiagnosticLevel.info), isFalse);
1122 1123 1124 1125
    expect(
      throwingProperty.toString(),
      equals('name: EXCEPTION (FlutterError)'),
    );
1126
    expect(throwingProperty.level, equals(DiagnosticLevel.error));
1127
    validateDoublePropertyJsonSerialization(throwingProperty);
1128 1129 1130 1131
  });

  test('percent property', () {
    expect(
1132
      PercentProperty('name', 0.4).toString(),
1133 1134 1135
      equals('name: 40.0%'),
    );

1136
    final PercentProperty complexPercentProperty = PercentProperty('name', 0.99, unit: 'invisible', tooltip: 'almost transparent');
1137
    expect(
1138
      complexPercentProperty.toString(),
1139 1140
      equals('name: 99.0% invisible (almost transparent)'),
    );
1141
    validateDoublePropertyJsonSerialization(complexPercentProperty);
1142 1143

    expect(
1144
      PercentProperty('name', null, unit: 'invisible', tooltip: '!').toString(),
1145 1146 1147 1148
      equals('name: null (!)'),
    );

    expect(
1149
      PercentProperty('name', 0.4).value,
1150 1151 1152
      0.4,
    );
    expect(
1153
      PercentProperty('name', 0.0).toString(),
1154 1155 1156
      equals('name: 0.0%'),
    );
    expect(
1157
      PercentProperty('name', -10.0).toString(),
1158 1159 1160
      equals('name: 0.0%'),
    );
    expect(
1161
      PercentProperty('name', 1.0).toString(),
1162 1163 1164
      equals('name: 100.0%'),
    );
    expect(
1165
      PercentProperty('name', 3.0).toString(),
1166 1167 1168
      equals('name: 100.0%'),
    );
    expect(
1169
      PercentProperty('name', null).toString(),
1170 1171 1172
      equals('name: null'),
    );
    expect(
1173
      PercentProperty(
1174 1175 1176 1177 1178 1179 1180
        'name',
        null,
        ifNull: 'missing',
      ).toString(),
      equals('name: missing'),
    );
    expect(
1181
      PercentProperty(
1182 1183 1184 1185 1186 1187 1188 1189
        'name',
        null,
        ifNull: 'missing',
        showName: false,
      ).toString(),
      equals('missing'),
    );
    expect(
1190
      PercentProperty(
1191 1192 1193 1194 1195 1196 1197 1198 1199
        'name',
        0.5,
        showName: false,
      ).toString(),
      equals('50.0%'),
    );
  });

  test('callback property test', () {
1200
    final Function onClick = () { };
1201
    final ObjectFlagProperty<Function> present = ObjectFlagProperty<Function>(
1202 1203 1204 1205
      'onClick',
      onClick,
      ifPresent: 'clickable',
    );
1206
    final ObjectFlagProperty<Function> missing = ObjectFlagProperty<Function>(
1207 1208 1209 1210 1211 1212
      'onClick',
      null,
      ifPresent: 'clickable',
    );

    expect(present.toString(), equals('clickable'));
1213
    expect(present.isFiltered(DiagnosticLevel.info), isFalse);
1214
    expect(present.value, equals(onClick));
1215
    validateObjectFlagPropertyJsonSerialization(present);
1216 1217
    expect(missing.toString(), equals('onClick: null'));
    expect(missing.isFiltered(DiagnosticLevel.fine), isTrue);
1218
    validateObjectFlagPropertyJsonSerialization(missing);
1219 1220 1221
  });

  test('missing callback property test', () {
1222 1223
    void onClick() { }

1224
    final ObjectFlagProperty<Function> present = ObjectFlagProperty<Function>(
1225 1226 1227 1228
      'onClick',
      onClick,
      ifNull: 'disabled',
    );
1229
    final ObjectFlagProperty<Function> missing = ObjectFlagProperty<Function>(
1230 1231 1232 1233 1234
      'onClick',
      null,
      ifNull: 'disabled',
    );

1235
    expect(present.toString(), equals('onClick: Closure: () => void'));
1236
    expect(present.isFiltered(DiagnosticLevel.fine), isTrue);
1237
    expect(present.value, equals(onClick));
1238
    expect(missing.toString(), equals('disabled'));
1239
    expect(missing.isFiltered(DiagnosticLevel.info), isFalse);
1240 1241
    validateObjectFlagPropertyJsonSerialization(present);
    validateObjectFlagPropertyJsonSerialization(missing);
Kate Lovett's avatar
Kate Lovett committed
1242
  }, skip: isBrowser); // https://github.com/flutter/flutter/issues/54221
1243 1244

  test('describe bool property', () {
1245
    final FlagProperty yes = FlagProperty(
1246 1247 1248 1249 1250 1251
      'name',
      value: true,
      ifTrue: 'YES',
      ifFalse: 'NO',
      showName: true,
    );
1252
    final FlagProperty no = FlagProperty(
1253 1254 1255 1256 1257 1258 1259
      'name',
      value: false,
      ifTrue: 'YES',
      ifFalse: 'NO',
      showName: true,
    );
    expect(yes.toString(), equals('name: YES'));
1260
    expect(yes.level, equals(DiagnosticLevel.info));
1261
    expect(yes.value, isTrue);
1262
    validateFlagPropertyJsonSerialization(yes);
1263
    expect(no.toString(), equals('name: NO'));
1264
    expect(no.level, equals(DiagnosticLevel.info));
1265
    expect(no.value, isFalse);
1266
    validateFlagPropertyJsonSerialization(no);
1267 1268

    expect(
1269
      FlagProperty(
1270 1271 1272 1273 1274 1275 1276 1277 1278
        'name',
        value: true,
        ifTrue: 'YES',
        ifFalse: 'NO',
      ).toString(),
      equals('YES'),
    );

    expect(
1279
      FlagProperty(
1280 1281 1282 1283 1284 1285 1286 1287 1288
        'name',
        value: false,
        ifTrue: 'YES',
        ifFalse: 'NO',
      ).toString(),
      equals('NO'),
    );

    expect(
1289
      FlagProperty(
1290 1291 1292 1293
        'name',
        value: true,
        ifTrue: 'YES',
        ifFalse: 'NO',
1294
        level: DiagnosticLevel.hidden,
1295
        showName: true,
1296 1297
      ).level,
      equals(DiagnosticLevel.hidden),
1298 1299 1300 1301
    );
  });

  test('enum property test', () {
1302
    final EnumProperty<ExampleEnum> hello = EnumProperty<ExampleEnum>(
1303 1304 1305
      'name',
      ExampleEnum.hello,
    );
1306
    final EnumProperty<ExampleEnum> world = EnumProperty<ExampleEnum>(
1307 1308 1309
      'name',
      ExampleEnum.world,
    );
1310
    final EnumProperty<ExampleEnum> deferToChild = EnumProperty<ExampleEnum>(
1311 1312 1313
      'name',
      ExampleEnum.deferToChild,
    );
1314
    final EnumProperty<ExampleEnum> nullEnum = EnumProperty<ExampleEnum>(
1315 1316 1317
      'name',
      null,
    );
1318
    expect(hello.level, equals(DiagnosticLevel.info));
1319
    expect(hello.value, equals(ExampleEnum.hello));
1320
    expect(hello.toString(), equals('name: hello'));
1321
    validatePropertyJsonSerialization(hello);
1322

1323
    expect(world.level, equals(DiagnosticLevel.info));
1324
    expect(world.value, equals(ExampleEnum.world));
1325
    expect(world.toString(), equals('name: world'));
1326
    validatePropertyJsonSerialization(world);
1327

1328
    expect(deferToChild.level, equals(DiagnosticLevel.info));
1329
    expect(deferToChild.value, equals(ExampleEnum.deferToChild));
1330
    expect(deferToChild.toString(), equals('name: deferToChild'));
1331
    validatePropertyJsonSerialization(deferToChild);
1332

1333
    expect(nullEnum.level, equals(DiagnosticLevel.info));
1334
    expect(nullEnum.value, isNull);
1335
    expect(nullEnum.toString(), equals('name: null'));
1336
    validatePropertyJsonSerialization(nullEnum);
1337

1338
    final EnumProperty<ExampleEnum> matchesDefault = EnumProperty<ExampleEnum>(
1339 1340 1341 1342 1343
      'name',
      ExampleEnum.hello,
      defaultValue: ExampleEnum.hello,
    );
    expect(matchesDefault.toString(), equals('name: hello'));
1344
    expect(matchesDefault.value, equals(ExampleEnum.hello));
1345
    expect(matchesDefault.isFiltered(DiagnosticLevel.info), isTrue);
1346
    validatePropertyJsonSerialization(matchesDefault);
1347 1348

    expect(
1349
      EnumProperty<ExampleEnum>(
1350 1351
        'name',
        ExampleEnum.hello,
1352 1353 1354
        level: DiagnosticLevel.hidden,
      ).level,
      equals(DiagnosticLevel.hidden),
1355 1356 1357 1358
    );
  });

  test('int property test', () {
1359
    final IntProperty regular = IntProperty(
1360 1361 1362 1363
      'name',
      42,
    );
    expect(regular.toString(), equals('name: 42'));
1364
    expect(regular.value, equals(42));
1365
    expect(regular.level, equals(DiagnosticLevel.info));
1366

1367
    final IntProperty nullValue = IntProperty(
1368 1369 1370 1371
      'name',
      null,
    );
    expect(nullValue.toString(), equals('name: null'));
1372
    expect(nullValue.value, isNull);
1373
    expect(nullValue.level, equals(DiagnosticLevel.info));
1374

1375
    final IntProperty hideNull = IntProperty(
1376 1377
      'name',
      null,
1378
      defaultValue: null,
1379 1380
    );
    expect(hideNull.toString(), equals('name: null'));
1381
    expect(hideNull.value, isNull);
1382
    expect(hideNull.isFiltered(DiagnosticLevel.info), isTrue);
1383

1384
    final IntProperty nullDescription = IntProperty(
1385 1386 1387 1388 1389
      'name',
      null,
      ifNull: 'missing',
    );
    expect(nullDescription.toString(), equals('name: missing'));
1390
    expect(nullDescription.value, isNull);
1391
    expect(nullDescription.level, equals(DiagnosticLevel.info));
1392

1393
    final IntProperty hideName = IntProperty(
1394 1395 1396 1397 1398
      'name',
      42,
      showName: false,
    );
    expect(hideName.toString(), equals('42'));
1399
    expect(hideName.value, equals(42));
1400
    expect(hideName.level, equals(DiagnosticLevel.info));
1401

1402
    final IntProperty withUnit = IntProperty(
1403 1404 1405 1406 1407
      'name',
      42,
      unit: 'pt',
    );
    expect(withUnit.toString(), equals('name: 42pt'));
1408
    expect(withUnit.value, equals(42));
1409
    expect(withUnit.level, equals(DiagnosticLevel.info));
1410

1411
    final IntProperty defaultValue = IntProperty(
1412 1413 1414 1415 1416
      'name',
      42,
      defaultValue: 42,
    );
    expect(defaultValue.toString(), equals('name: 42'));
1417
    expect(defaultValue.value, equals(42));
1418
    expect(defaultValue.isFiltered(DiagnosticLevel.info), isTrue);
1419

1420
    final IntProperty notDefaultValue = IntProperty(
1421 1422 1423 1424 1425
      'name',
      43,
      defaultValue: 42,
    );
    expect(notDefaultValue.toString(), equals('name: 43'));
1426
    expect(notDefaultValue.value, equals(43));
1427
    expect(notDefaultValue.level, equals(DiagnosticLevel.info));
1428

1429
    final IntProperty hidden = IntProperty(
1430 1431
      'name',
      42,
1432
      level: DiagnosticLevel.hidden,
1433 1434
    );
    expect(hidden.toString(), equals('name: 42'));
1435
    expect(hidden.value, equals(42));
1436
    expect(hidden.level, equals(DiagnosticLevel.hidden));
1437 1438 1439
  });

  test('object property test', () {
Dan Field's avatar
Dan Field committed
1440
    const Rect rect = Rect.fromLTRB(0.0, 0.0, 20.0, 20.0);
1441
    final DiagnosticsProperty<Rect> simple = DiagnosticsProperty<Rect>(
1442 1443 1444
      'name',
      rect,
    );
1445
    expect(simple.value, equals(rect));
1446
    expect(simple.level, equals(DiagnosticLevel.info));
1447
    expect(simple.toString(), equals('name: Rect.fromLTRB(0.0, 0.0, 20.0, 20.0)'));
1448
    validatePropertyJsonSerialization(simple);
1449

1450
    final DiagnosticsProperty<Rect> withDescription = DiagnosticsProperty<Rect>(
1451 1452 1453 1454
      'name',
      rect,
      description: 'small rect',
    );
1455
    expect(withDescription.value, equals(rect));
1456
    expect(withDescription.level, equals(DiagnosticLevel.info));
1457
    expect(withDescription.toString(), equals('name: small rect'));
1458
    validatePropertyJsonSerialization(withDescription);
1459

1460
    final DiagnosticsProperty<Object> nullProperty = DiagnosticsProperty<Object>(
1461 1462 1463
      'name',
      null,
    );
1464
    expect(nullProperty.value, isNull);
1465
    expect(nullProperty.level, equals(DiagnosticLevel.info));
1466
    expect(nullProperty.toString(), equals('name: null'));
1467
    validatePropertyJsonSerialization(nullProperty);
1468

1469
    final DiagnosticsProperty<Object> hideNullProperty = DiagnosticsProperty<Object>(
1470 1471 1472 1473
      'name',
      null,
      defaultValue: null,
    );
1474
    expect(hideNullProperty.value, isNull);
1475
    expect(hideNullProperty.isFiltered(DiagnosticLevel.info), isTrue);
1476
    expect(hideNullProperty.toString(), equals('name: null'));
1477
    validatePropertyJsonSerialization(hideNullProperty);
1478

1479
    final DiagnosticsProperty<Object> nullDescription = DiagnosticsProperty<Object>(
1480 1481 1482 1483
      'name',
      null,
      ifNull: 'missing',
    );
1484
    expect(nullDescription.value, isNull);
1485
    expect(nullDescription.level, equals(DiagnosticLevel.info));
1486
    expect(nullDescription.toString(), equals('name: missing'));
1487
    validatePropertyJsonSerialization(nullDescription);
1488

1489
    final DiagnosticsProperty<Rect> hideName = DiagnosticsProperty<Rect>(
1490 1491 1492
      'name',
      rect,
      showName: false,
1493
      level: DiagnosticLevel.warning,
1494
    );
1495
    expect(hideName.value, equals(rect));
1496
    expect(hideName.level, equals(DiagnosticLevel.warning));
1497
    expect(hideName.toString(), equals('Rect.fromLTRB(0.0, 0.0, 20.0, 20.0)'));
1498
    validatePropertyJsonSerialization(hideName);
1499

1500
    final DiagnosticsProperty<Rect> hideSeparator = DiagnosticsProperty<Rect>(
1501 1502 1503 1504
      'Creator',
      rect,
      showSeparator: false,
    );
1505
    expect(hideSeparator.value, equals(rect));
1506
    expect(hideSeparator.level, equals(DiagnosticLevel.info));
1507 1508 1509 1510
    expect(
      hideSeparator.toString(),
      equals('Creator Rect.fromLTRB(0.0, 0.0, 20.0, 20.0)'),
    );
1511
    validatePropertyJsonSerialization(hideSeparator);
1512 1513 1514
  });

  test('lazy object property test', () {
Dan Field's avatar
Dan Field committed
1515
    const Rect rect = Rect.fromLTRB(0.0, 0.0, 20.0, 20.0);
1516
    final DiagnosticsProperty<Rect> simple = DiagnosticsProperty<Rect>.lazy(
1517 1518 1519 1520
      'name',
      () => rect,
      description: 'small rect',
    );
1521
    expect(simple.value, equals(rect));
1522
    expect(simple.level, equals(DiagnosticLevel.info));
1523
    expect(simple.toString(), equals('name: small rect'));
1524
    validatePropertyJsonSerialization(simple);
1525

1526
    final DiagnosticsProperty<Object> nullProperty = DiagnosticsProperty<Object>.lazy(
1527 1528 1529 1530
      'name',
      () => null,
      description: 'missing',
    );
1531
    expect(nullProperty.value, isNull);
1532
    expect(nullProperty.isFiltered(DiagnosticLevel.info), isFalse);
1533
    expect(nullProperty.toString(), equals('name: missing'));
1534
    validatePropertyJsonSerialization(nullProperty);
1535

1536
    final DiagnosticsProperty<Object> hideNullProperty = DiagnosticsProperty<Object>.lazy(
1537 1538 1539 1540 1541
      'name',
      () => null,
      description: 'missing',
      defaultValue: null,
    );
1542
    expect(hideNullProperty.value, isNull);
1543
    expect(hideNullProperty.isFiltered(DiagnosticLevel.info), isTrue);
1544
    expect(hideNullProperty.toString(), equals('name: missing'));
1545
    validatePropertyJsonSerialization(hideNullProperty);
1546

1547
    final DiagnosticsProperty<Rect> hideName = DiagnosticsProperty<Rect>.lazy(
1548 1549 1550 1551 1552
      'name',
      () => rect,
      description: 'small rect',
      showName: false,
    );
1553
    expect(hideName.value, equals(rect));
1554
    expect(hideName.isFiltered(DiagnosticLevel.info), isFalse);
1555
    expect(hideName.toString(), equals('small rect'));
1556
    validatePropertyJsonSerialization(hideName);
1557

1558
    final DiagnosticsProperty<Object> throwingWithDescription = DiagnosticsProperty<Object>.lazy(
1559
      'name',
1560
      () => throw FlutterError.fromParts(<DiagnosticsNode>[ErrorSummary('Property not available')]),
1561 1562 1563
      description: 'missing',
      defaultValue: null,
    );
1564
    expect(throwingWithDescription.value, isNull);
1565
    expect(throwingWithDescription.exception, isFlutterError);
1566
    expect(throwingWithDescription.isFiltered(DiagnosticLevel.info), false);
1567
    expect(throwingWithDescription.toString(), equals('name: missing'));
1568
    validatePropertyJsonSerialization(throwingWithDescription);
1569

1570
    final DiagnosticsProperty<Object> throwingProperty = DiagnosticsProperty<Object>.lazy(
1571
      'name',
1572
      () => throw FlutterError.fromParts(<DiagnosticsNode>[ErrorSummary('Property not available')]),
1573 1574
      defaultValue: null,
    );
1575
    expect(throwingProperty.value, isNull);
1576
    expect(throwingProperty.exception, isFlutterError);
1577
    expect(throwingProperty.isFiltered(DiagnosticLevel.info), false);
1578
    expect(throwingProperty.toString(), equals('name: EXCEPTION (FlutterError)'));
1579
    validatePropertyJsonSerialization(throwingProperty);
1580 1581 1582 1583 1584
  });

  test('color property test', () {
    // Add more tests if colorProperty becomes more than a wrapper around
    // objectProperty.
1585
    const Color color = Color.fromARGB(255, 255, 255, 255);
1586
    final DiagnosticsProperty<Color> simple = DiagnosticsProperty<Color>(
1587 1588 1589
      'name',
      color,
    );
1590
    validatePropertyJsonSerialization(simple);
1591
    expect(simple.isFiltered(DiagnosticLevel.info), isFalse);
1592
    expect(simple.value, equals(color));
1593
    expect(simple.propertyType, equals(Color));
1594
    expect(simple.toString(), equals('name: Color(0xffffffff)'));
1595
    validatePropertyJsonSerialization(simple);
1596 1597 1598
  });

  test('flag property test', () {
1599
    final FlagProperty show = FlagProperty(
1600 1601 1602 1603 1604
      'wasLayout',
      value: true,
      ifTrue: 'layout computed',
    );
    expect(show.name, equals('wasLayout'));
1605
    expect(show.value, isTrue);
1606
    expect(show.isFiltered(DiagnosticLevel.info), isFalse);
1607
    expect(show.toString(), equals('layout computed'));
1608
    validateFlagPropertyJsonSerialization(show);
1609

1610
    final FlagProperty hide = FlagProperty(
1611 1612 1613 1614 1615
      'wasLayout',
      value: false,
      ifTrue: 'layout computed',
    );
    expect(hide.name, equals('wasLayout'));
1616
    expect(hide.value, isFalse);
1617 1618
    expect(hide.level, equals(DiagnosticLevel.hidden));
    expect(hide.toString(), equals('wasLayout: false'));
1619
    validateFlagPropertyJsonSerialization(hide);
1620

1621
    final FlagProperty hideTrue = FlagProperty(
1622 1623 1624 1625 1626 1627 1628 1629
      'wasLayout',
      value: true,
      ifFalse: 'no layout computed',
    );
    expect(hideTrue.name, equals('wasLayout'));
    expect(hideTrue.value, isTrue);
    expect(hideTrue.level, equals(DiagnosticLevel.hidden));
    expect(hideTrue.toString(), equals('wasLayout: true'));
1630
    validateFlagPropertyJsonSerialization(hideTrue);
1631 1632 1633
  });

  test('has property test', () {
1634
    final Function onClick = () { };
1635
    final ObjectFlagProperty<Function> has = ObjectFlagProperty<Function>.has(
1636 1637 1638 1639
      'onClick',
      onClick,
    );
    expect(has.name, equals('onClick'));
1640
    expect(has.value, equals(onClick));
1641
    expect(has.isFiltered(DiagnosticLevel.info), isFalse);
1642
    expect(has.toString(), equals('has onClick'));
1643
    validateObjectFlagPropertyJsonSerialization(has);
1644

1645
    final ObjectFlagProperty<Function> missing = ObjectFlagProperty<Function>.has(
1646 1647 1648 1649
      'onClick',
      null,
    );
    expect(missing.name, equals('onClick'));
1650
    expect(missing.value, isNull);
1651 1652
    expect(missing.isFiltered(DiagnosticLevel.info), isTrue);
    expect(missing.toString(), equals('onClick: null'));
1653
    validateObjectFlagPropertyJsonSerialization(missing);
1654 1655
  });

1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695
  test('iterable flags property test', () {
    // Normal property
    {
      final Function onClick = () { };
      final Function onMove = () { };
      final Map<String, Function> value = <String, Function>{
        'click': onClick,
        'move': onMove,
      };
      final FlagsSummary<Function> flags = FlagsSummary<Function>(
        'listeners',
        value,
      );
      expect(flags.name, equals('listeners'));
      expect(flags.value, equals(value));
      expect(flags.isFiltered(DiagnosticLevel.info), isFalse);
      expect(flags.toString(), equals('listeners: click, move'));
      validateIterableFlagsPropertyJsonSerialization(flags);
    }

    // Reversed-order property
    {
      final Function onClick = () { };
      final Function onMove = () { };
      final Map<String, Function> value = <String, Function>{
        'move': onMove,
        'click': onClick,
      };
      final FlagsSummary<Function> flags = FlagsSummary<Function>(
        'listeners',
        value,
      );
      expect(flags.toString(), equals('listeners: move, click'));
      expect(flags.isFiltered(DiagnosticLevel.info), isFalse);
      validateIterableFlagsPropertyJsonSerialization(flags);
    }

    // Partially empty property
    {
      final Function onClick = () { };
1696
      final Map<String, Function?> value = <String, Function?>{
1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710
        'move': null,
        'click': onClick,
      };
      final FlagsSummary<Function> flags = FlagsSummary<Function>(
        'listeners',
        value,
      );
      expect(flags.toString(), equals('listeners: click'));
      expect(flags.isFiltered(DiagnosticLevel.info), isFalse);
      validateIterableFlagsPropertyJsonSerialization(flags);
    }

    // Empty property (without ifEmpty)
    {
1711
      final Map<String, Function?> value = <String, Function?>{
1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723
        'enter': null,
      };
      final FlagsSummary<Function> flags = FlagsSummary<Function>(
        'listeners',
        value,
      );
      expect(flags.isFiltered(DiagnosticLevel.info), isTrue);
      validateIterableFlagsPropertyJsonSerialization(flags);
    }

    // Empty property (without ifEmpty)
    {
1724
      final Map<String, Function?> value = <String, Function?>{
1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737
        'enter': null,
      };
      final FlagsSummary<Function> flags = FlagsSummary<Function>(
        'listeners',
        value,
        ifEmpty: '<none>',
      );
      expect(flags.toString(), equals('listeners: <none>'));
      expect(flags.isFiltered(DiagnosticLevel.info), isFalse);
      validateIterableFlagsPropertyJsonSerialization(flags);
    }
  });

1738 1739
  test('iterable property test', () {
    final List<int> ints = <int>[1,2,3];
1740
    final IterableProperty<int> intsProperty = IterableProperty<int>(
1741 1742 1743
      'ints',
      ints,
    );
1744
    expect(intsProperty.value, equals(ints));
1745
    expect(intsProperty.isFiltered(DiagnosticLevel.info), isFalse);
1746 1747
    expect(intsProperty.toString(), equals('ints: 1, 2, 3'));

1748 1749 1750 1751 1752 1753 1754 1755 1756
    final List<double> doubles = <double>[1,2,3];
    final IterableProperty<double> doublesProperty = IterableProperty<double>(
      'doubles',
      doubles,
    );
    expect(doublesProperty.value, equals(doubles));
    expect(doublesProperty.isFiltered(DiagnosticLevel.info), isFalse);
    expect(doublesProperty.toString(), equals('doubles: 1.0, 2.0, 3.0'));

1757
    final IterableProperty<Object> emptyProperty = IterableProperty<Object>(
1758 1759 1760
      'name',
      <Object>[],
    );
1761
    expect(emptyProperty.value, isEmpty);
1762
    expect(emptyProperty.isFiltered(DiagnosticLevel.info), isFalse);
1763
    expect(emptyProperty.toString(), equals('name: []'));
1764
    validateIterablePropertyJsonSerialization(emptyProperty);
1765

1766
    final IterableProperty<Object> nullProperty = IterableProperty<Object>(
1767 1768 1769
      'list',
      null,
    );
1770
    expect(nullProperty.value, isNull);
1771
    expect(nullProperty.isFiltered(DiagnosticLevel.info), isFalse);
1772
    expect(nullProperty.toString(), equals('list: null'));
1773
    validateIterablePropertyJsonSerialization(nullProperty);
1774

1775
    final IterableProperty<Object> hideNullProperty = IterableProperty<Object>(
1776 1777 1778 1779
      'list',
      null,
      defaultValue: null,
    );
1780
    expect(hideNullProperty.value, isNull);
1781 1782
    expect(hideNullProperty.isFiltered(DiagnosticLevel.info), isTrue);
    expect(hideNullProperty.level, equals(DiagnosticLevel.fine));
1783
    expect(hideNullProperty.toString(), equals('list: null'));
1784
    validateIterablePropertyJsonSerialization(hideNullProperty);
1785 1786

    final List<Object> objects = <Object>[
Dan Field's avatar
Dan Field committed
1787
      const Rect.fromLTRB(0.0, 0.0, 20.0, 20.0),
1788 1789
      const Color.fromARGB(255, 255, 255, 255),
    ];
1790
    final IterableProperty<Object> objectsProperty = IterableProperty<Object>(
1791 1792 1793
      'objects',
      objects,
    );
1794
    expect(objectsProperty.value, equals(objects));
1795
    expect(objectsProperty.isFiltered(DiagnosticLevel.info), isFalse);
1796 1797 1798 1799
    expect(
      objectsProperty.toString(),
      equals('objects: Rect.fromLTRB(0.0, 0.0, 20.0, 20.0), Color(0xffffffff)'),
    );
1800
    validateIterablePropertyJsonSerialization(objectsProperty);
1801

1802
    final IterableProperty<Object> multiLineProperty = IterableProperty<Object>(
1803 1804 1805 1806
      'objects',
      objects,
      style: DiagnosticsTreeStyle.whitespace,
    );
1807
    expect(multiLineProperty.value, equals(objects));
1808
    expect(multiLineProperty.isFiltered(DiagnosticLevel.info), isFalse);
1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824
    expect(
      multiLineProperty.toString(),
      equals(
        'objects:\n'
        'Rect.fromLTRB(0.0, 0.0, 20.0, 20.0)\n'
        'Color(0xffffffff)',
      ),
    );
    expect(
      multiLineProperty.toStringDeep(),
      equals(
        'objects:\n'
        '  Rect.fromLTRB(0.0, 0.0, 20.0, 20.0)\n'
        '  Color(0xffffffff)\n',
      ),
    );
1825
    validateIterablePropertyJsonSerialization(multiLineProperty);
1826 1827

    expect(
1828
      TestTree(
1829 1830 1831 1832 1833 1834 1835 1836 1837 1838
        properties: <DiagnosticsNode>[multiLineProperty],
      ).toStringDeep(),
      equalsIgnoringHashCodes(
        'TestTree#00000\n'
        '   objects:\n'
        '     Rect.fromLTRB(0.0, 0.0, 20.0, 20.0)\n'
        '     Color(0xffffffff)\n',
      ),
    );

1839
    expect(
1840 1841
      TestTree(
        properties: <DiagnosticsNode>[objectsProperty, IntProperty('foo', 42)],
1842 1843 1844 1845 1846 1847 1848
        style: DiagnosticsTreeStyle.singleLine,
      ).toStringDeep(),
      equalsIgnoringHashCodes(
        'TestTree#00000(objects: [Rect.fromLTRB(0.0, 0.0, 20.0, 20.0), Color(0xffffffff)], foo: 42)',
      ),
    );

1849 1850 1851 1852
    // Iterable with a single entry. Verify that rendering is sensible and that
    // multi line rendering isn't used even though it is not helpful.
    final List<Object> singleElementList = <Object>[const Color.fromARGB(255, 255, 255, 255)];

1853
    final IterableProperty<Object> objectProperty = IterableProperty<Object>(
1854 1855 1856 1857
      'object',
      singleElementList,
      style: DiagnosticsTreeStyle.whitespace,
    );
1858
    expect(objectProperty.value, equals(singleElementList));
1859
    expect(objectProperty.isFiltered(DiagnosticLevel.info), isFalse);
1860 1861 1862 1863 1864 1865 1866 1867
    expect(
      objectProperty.toString(),
      equals('object: Color(0xffffffff)'),
    );
    expect(
      objectProperty.toStringDeep(),
      equals('object: Color(0xffffffff)\n'),
    );
1868
    validateIterablePropertyJsonSerialization(objectProperty);
1869
    expect(
1870
      TestTree(
1871 1872 1873 1874 1875 1876 1877 1878 1879 1880
        name: 'root',
        properties: <DiagnosticsNode>[objectProperty],
      ).toStringDeep(),
      equalsIgnoringHashCodes(
        'TestTree#00000\n'
        '   object: Color(0xffffffff)\n',
      ),
    );
  });

1881 1882
  test('Stack trace test', () {
    final StackTrace stack = StackTrace.fromString(
1883 1884 1885
      '#0      someMethod  (file:///diagnostics_test.dart:42:19)\n'
      '#1      someMethod2  (file:///diagnostics_test.dart:12:3)\n'
      '#2      someMethod3  (file:///foo.dart:4:1)\n'
1886 1887 1888 1889 1890 1891
    );

    expect(
      DiagnosticsStackTrace('Stack trace', stack).toStringDeep(),
      equalsIgnoringHashCodes(
        'Stack trace:\n'
1892 1893 1894
        '#0      someMethod  (file:///diagnostics_test.dart:42:19)\n'
        '#1      someMethod2  (file:///diagnostics_test.dart:12:3)\n'
        '#2      someMethod3  (file:///foo.dart:4:1)\n'
1895
      ),
1896 1897 1898 1899 1900 1901
    );

    expect(
      DiagnosticsStackTrace('-- callback 2 --', stack, showSeparator: false).toStringDeep(),
      equalsIgnoringHashCodes(
        '-- callback 2 --\n'
1902 1903 1904
        '#0      someMethod  (file:///diagnostics_test.dart:42:19)\n'
        '#1      someMethod2  (file:///diagnostics_test.dart:12:3)\n'
        '#2      someMethod3  (file:///foo.dart:4:1)\n'
1905
      ),
1906 1907 1908
    );
  });

1909
  test('message test', () {
1910
    final DiagnosticsNode message = DiagnosticsNode.message('hello world');
1911 1912
    expect(message.toString(), equals('hello world'));
    expect(message.name, isEmpty);
1913
    expect(message.value, isNull);
1914
    expect(message.showName, isFalse);
1915
    validateNodeJsonSerialization(message);
1916

1917
    final DiagnosticsNode messageProperty = MessageProperty('diagnostics', 'hello world');
1918 1919
    expect(messageProperty.toString(), equals('diagnostics: hello world'));
    expect(messageProperty.name, equals('diagnostics'));
1920
    expect(messageProperty.value, isNull);
1921
    expect(messageProperty.showName, isTrue);
1922
    validatePropertyJsonSerialization(messageProperty as DiagnosticsProperty<Object?>);
1923
  });
1924 1925 1926 1927

  test('error message style wrap test', () {
    // This tests wrapping of properties with styles typical for error messages.
    DiagnosticsNode createTreeWithWrappingNodes({
1928 1929
      DiagnosticsTreeStyle? rootStyle,
      required DiagnosticsTreeStyle propertyStyle,
1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947
    }) {
      return TestTree(
        name: 'Test tree',
        properties: <DiagnosticsNode>[
          DiagnosticsNode.message(
          '--- example property at max length --',
            style: propertyStyle,
          ),
          DiagnosticsNode.message(
            'This is a very long message that must wrap as it cannot fit on one line. '
            'This is a very long message that must wrap as it cannot fit on one line. '
            'This is a very long message that must wrap as it cannot fit on one line.',
            style: propertyStyle,
          ),
          DiagnosticsNode.message(
            '--- example property at max length --',
            style: propertyStyle,
          ),
1948 1949 1950 1951 1952
          DiagnosticsProperty<String>(
            null,
            'Message that is not allowed to wrap even though it is very long. Message that is not allowed to wrap even though it is very long. Message that is not allowed to wrap even though it is very long. Message that is not allowed to wrap.',
            allowWrap: false,
          ),
1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094
          DiagnosticsNode.message(
            '--- example property at max length --',
            style: propertyStyle,
          ),
          DiagnosticsNode.message(
            'This property has a very long property name that will be allowed to wrap unlike most property names. This property has a very long property name that will be allowed to wrap unlike most property names:\n'
            '  http://someverylongurl.com/that-might-be-tempting-to-wrap-even-though-it-is-a-url-so-should-not-wrap.html',
            style: propertyStyle,
          ),
          DiagnosticsNode.message(
            'This property has a very long property name that will be allowed to wrap unlike most property names. This property has a very long property name that will be allowed to wrap unlike most property names:\n'
            '  https://goo.gl/',
            style: propertyStyle,
          ),
          DiagnosticsNode.message(
            'Click on the following url:\n'
            '  http://someverylongurl.com/that-might-be-tempting-to-wrap-even-though-it-is-a-url-so-should-not-wrap.html',
            style: propertyStyle,
          ),
          DiagnosticsNode.message(
            'Click on the following url\n'
            '  https://goo.gl/',
            style: propertyStyle,
          ),
          DiagnosticsNode.message(
            '--- example property at max length --',
            style: propertyStyle,
          ),
          DiagnosticsProperty<String>(
            'multi-line value',
            '[1.0, 0.0, 0.0, 0.0]\n'
            '[1.0, 1.0, 0.0, 0.0]\n'
            '[1.0, 0.0, 1.0, 0.0]\n'
            '[1.0, 0.0, 0.0, 1.0]\n',
            style: propertyStyle,
          ),
          DiagnosticsNode.message(
            '--- example property at max length --',
            style: propertyStyle,
          ),
          DiagnosticsProperty<String>(
            'This property has a very long property name that will be allowed to wrap unlike most property names. This property has a very long property name that will be allowed to wrap unlike most property names',
            'This is a very long message that must wrap as it cannot fit on one line. '
            'This is a very long message that must wrap as it cannot fit on one line. '
            'This is a very long message that must wrap as it cannot fit on one line.',
            style: propertyStyle,
          ),
          DiagnosticsNode.message(
            '--- example property at max length --',
            style: propertyStyle,
          ),
          DiagnosticsProperty<String>(
            'This property has a very long property name that will be allowed to wrap unlike most property names. This property has a very long property name that will be allowed to wrap unlike most property names',
            '[1.0, 0.0, 0.0, 0.0]\n'
            '[1.0, 1.0, 0.0, 0.0]\n'
            '[1.0, 0.0, 1.0, 0.0]\n'
            '[1.0, 0.0, 0.0, 1.0]\n',
            style: propertyStyle,
          ),
          DiagnosticsNode.message(
            '--- example property at max length --',
            style: propertyStyle,
          ),
          MessageProperty(
            'diagnosis',
            'insufficient data to draw conclusion (less than five repaints)',
            style: propertyStyle,
          ),
        ],
      ).toDiagnosticsNode(style: rootStyle);
    }

    final TextTreeRenderer renderer = TextTreeRenderer(wrapWidth: 40, wrapWidthProperties: 40);
    expect(
      renderer.render(createTreeWithWrappingNodes(
        rootStyle: DiagnosticsTreeStyle.error,
        propertyStyle: DiagnosticsTreeStyle.singleLine,
      )),
      equalsIgnoringHashCodes(
        '══╡ TESTTREE#00000 ╞════════════════════\n'
        '--- example property at max length --\n'
        'This is a very long message that must\n'
        'wrap as it cannot fit on one line. This\n'
        'is a very long message that must wrap as\n'
        'it cannot fit on one line. This is a\n'
        'very long message that must wrap as it\n'
        'cannot fit on one line.\n'
        '--- example property at max length --\n'
        'Message that is not allowed to wrap even though it is very long. Message that is not allowed to wrap even though it is very long. Message that is not allowed to wrap even though it is very long. Message that is not allowed to wrap.\n'
        '--- example property at max length --\n'
        'This property has a very long property\n'
        'name that will be allowed to wrap unlike\n'
        'most property names. This property has a\n'
        'very long property name that will be\n'
        'allowed to wrap unlike most property\n'
        'names:\n'
        '  http://someverylongurl.com/that-might-be-tempting-to-wrap-even-though-it-is-a-url-so-should-not-wrap.html\n'
        'This property has a very long property\n'
        'name that will be allowed to wrap unlike\n'
        'most property names. This property has a\n'
        'very long property name that will be\n'
        'allowed to wrap unlike most property\n'
        'names:\n'
        '  https://goo.gl/\n'
        'Click on the following url:\n'
        '  http://someverylongurl.com/that-might-be-tempting-to-wrap-even-though-it-is-a-url-so-should-not-wrap.html\n'
        'Click on the following url\n'
        '  https://goo.gl/\n'
        '--- example property at max length --\n'
        'multi-line value:\n'
        '  [1.0, 0.0, 0.0, 0.0]\n'
        '  [1.0, 1.0, 0.0, 0.0]\n'
        '  [1.0, 0.0, 1.0, 0.0]\n'
        '  [1.0, 0.0, 0.0, 1.0]\n'
        '--- example property at max length --\n'
        'This property has a very long property\n'
        'name that will be allowed to wrap unlike\n'
        'most property names. This property has a\n'
        'very long property name that will be\n'
        'allowed to wrap unlike most property\n'
        'names:\n'
        '  This is a very long message that must\n'
        '  wrap as it cannot fit on one line.\n'
        '  This is a very long message that must\n'
        '  wrap as it cannot fit on one line.\n'
        '  This is a very long message that must\n'
        '  wrap as it cannot fit on one line.\n'
        '--- example property at max length --\n'
        'This property has a very long property\n'
        'name that will be allowed to wrap unlike\n'
        'most property names. This property has a\n'
        'very long property name that will be\n'
        'allowed to wrap unlike most property\n'
        'names:\n'
        '  [1.0, 0.0, 0.0, 0.0]\n'
        '  [1.0, 1.0, 0.0, 0.0]\n'
        '  [1.0, 0.0, 1.0, 0.0]\n'
        '  [1.0, 0.0, 0.0, 1.0]\n'
        '--- example property at max length --\n'
        'diagnosis: insufficient data to draw\n'
        '  conclusion (less than five repaints)\n'
        '════════════════════════════════════════\n',
2095
      ),
2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167
    );

    // This output looks ugly but verifies that no indentation on word wrap
    // leaks in if the style is flat.
    expect(
      renderer.render(createTreeWithWrappingNodes(
        rootStyle: DiagnosticsTreeStyle.sparse,
        propertyStyle: DiagnosticsTreeStyle.flat,
      )),
      equalsIgnoringHashCodes(
        'TestTree#00000\n'
        '   --- example property at max length --\n'
        '   This is a very long message that must\n'
        '   wrap as it cannot fit on one line. This\n'
        '   is a very long message that must wrap as\n'
        '   it cannot fit on one line. This is a\n'
        '   very long message that must wrap as it\n'
        '   cannot fit on one line.\n'
        '   --- example property at max length --\n'
        '   Message that is not allowed to wrap even though it is very long. Message that is not allowed to wrap even though it is very long. Message that is not allowed to wrap even though it is very long. Message that is not allowed to wrap.\n'
        '   --- example property at max length --\n'
        '   This property has a very long property\n'
        '   name that will be allowed to wrap unlike\n'
        '   most property names. This property has a\n'
        '   very long property name that will be\n'
        '   allowed to wrap unlike most property\n'
        '   names:\n'
        '     http://someverylongurl.com/that-might-be-tempting-to-wrap-even-though-it-is-a-url-so-should-not-wrap.html\n'
        '   This property has a very long property\n'
        '   name that will be allowed to wrap unlike\n'
        '   most property names. This property has a\n'
        '   very long property name that will be\n'
        '   allowed to wrap unlike most property\n'
        '   names:\n'
        '     https://goo.gl/\n'
        '   Click on the following url:\n'
        '     http://someverylongurl.com/that-might-be-tempting-to-wrap-even-though-it-is-a-url-so-should-not-wrap.html\n'
        '   Click on the following url\n'
        '     https://goo.gl/\n'
        '   --- example property at max length --\n'
        '   multi-line value:\n'
        '   [1.0, 0.0, 0.0, 0.0]\n'
        '   [1.0, 1.0, 0.0, 0.0]\n'
        '   [1.0, 0.0, 1.0, 0.0]\n'
        '   [1.0, 0.0, 0.0, 1.0]\n'
        '   --- example property at max length --\n'
        '   This property has a very long property\n'
        '   name that will be allowed to wrap unlike\n'
        '   most property names. This property has a\n'
        '   very long property name that will be\n'
        '   allowed to wrap unlike most property\n'
        '   names:\n'
        '   This is a very long message that must\n'
        '   wrap as it cannot fit on one line. This\n'
        '   is a very long message that must wrap as\n'
        '   it cannot fit on one line. This is a\n'
        '   very long message that must wrap as it\n'
        '   cannot fit on one line.\n'
        '   --- example property at max length --\n'
        '   This property has a very long property\n'
        '   name that will be allowed to wrap unlike\n'
        '   most property names. This property has a\n'
        '   very long property name that will be\n'
        '   allowed to wrap unlike most property\n'
        '   names:\n'
        '   [1.0, 0.0, 0.0, 0.0]\n'
        '   [1.0, 1.0, 0.0, 0.0]\n'
        '   [1.0, 0.0, 1.0, 0.0]\n'
        '   [1.0, 0.0, 0.0, 1.0]\n'
        '   --- example property at max length --\n'
        '   diagnosis: insufficient data to draw\n'
        '   conclusion (less than five repaints)\n'
2168
      ),
2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242
    );

    // This case matches the styles that should generally be used for error
    // messages
    expect(
        renderer.render(createTreeWithWrappingNodes(
          rootStyle: DiagnosticsTreeStyle.error,
          propertyStyle: DiagnosticsTreeStyle.errorProperty,
        )),
        equalsIgnoringHashCodes(
          '══╡ TESTTREE#00000 ╞════════════════════\n'
          '--- example property at max length --\n'
          'This is a very long message that must\n'
          'wrap as it cannot fit on one line. This\n'
          'is a very long message that must wrap as\n'
          'it cannot fit on one line. This is a\n'
          'very long message that must wrap as it\n'
          'cannot fit on one line.\n'
          '--- example property at max length --\n'
          'Message that is not allowed to wrap even though it is very long. Message that is not allowed to wrap even though it is very long. Message that is not allowed to wrap even though it is very long. Message that is not allowed to wrap.\n'
          '--- example property at max length --\n'
          'This property has a very long property\n'
          'name that will be allowed to wrap unlike\n'
          'most property names. This property has a\n'
          'very long property name that will be\n'
          'allowed to wrap unlike most property\n'
          'names:\n'
          '  http://someverylongurl.com/that-might-be-tempting-to-wrap-even-though-it-is-a-url-so-should-not-wrap.html\n'
          'This property has a very long property\n'
          'name that will be allowed to wrap unlike\n'
          'most property names. This property has a\n'
          'very long property name that will be\n'
          'allowed to wrap unlike most property\n'
          'names:\n'
          '  https://goo.gl/\n'
          'Click on the following url:\n'
          '  http://someverylongurl.com/that-might-be-tempting-to-wrap-even-though-it-is-a-url-so-should-not-wrap.html\n'
          'Click on the following url\n'
          '  https://goo.gl/\n'
          '--- example property at max length --\n'
          'multi-line value:\n'
          '  [1.0, 0.0, 0.0, 0.0]\n'
          '  [1.0, 1.0, 0.0, 0.0]\n'
          '  [1.0, 0.0, 1.0, 0.0]\n'
          '  [1.0, 0.0, 0.0, 1.0]\n'
          '--- example property at max length --\n'
          'This property has a very long property\n'
          'name that will be allowed to wrap unlike\n'
          'most property names. This property has a\n'
          'very long property name that will be\n'
          'allowed to wrap unlike most property\n'
          'names:\n'
          '  This is a very long message that must\n'
          '  wrap as it cannot fit on one line.\n'
          '  This is a very long message that must\n'
          '  wrap as it cannot fit on one line.\n'
          '  This is a very long message that must\n'
          '  wrap as it cannot fit on one line.\n'
          '--- example property at max length --\n'
          'This property has a very long property\n'
          'name that will be allowed to wrap unlike\n'
          'most property names. This property has a\n'
          'very long property name that will be\n'
          'allowed to wrap unlike most property\n'
          'names:\n'
          '  [1.0, 0.0, 0.0, 0.0]\n'
          '  [1.0, 1.0, 0.0, 0.0]\n'
          '  [1.0, 0.0, 1.0, 0.0]\n'
          '  [1.0, 0.0, 0.0, 1.0]\n'
          '--- example property at max length --\n'
          'diagnosis:\n'
          '  insufficient data to draw conclusion\n'
          '  (less than five repaints)\n'
          '════════════════════════════════════════\n'
2243
        ),
2244 2245
    );
  });
2246 2247 2248

  test('DiagnosticsProperty for basic types has value in json', () {
    DiagnosticsProperty<int> intProperty = DiagnosticsProperty<int>('int1', 10);
2249
    Map<String, Object?> json = simulateJsonSerialization(intProperty);
2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282
    expect(json['name'], 'int1');
    expect(json['value'], 10);

    intProperty = IntProperty('int2', 20);
    json = simulateJsonSerialization(intProperty);
    expect(json['name'], 'int2');
    expect(json['value'], 20);

    DiagnosticsProperty<double> doubleProperty = DiagnosticsProperty<double>('double', 33.3);
    json = simulateJsonSerialization(doubleProperty);
    expect(json['name'], 'double');
    expect(json['value'], 33.3);

    doubleProperty = DoubleProperty('double2', 33.3);
    json = simulateJsonSerialization(doubleProperty);
    expect(json['name'], 'double2');
    expect(json['value'], 33.3);

    final DiagnosticsProperty<bool> boolProperty = DiagnosticsProperty<bool>('bool', true);
    json = simulateJsonSerialization(boolProperty);
    expect(json['name'], 'bool');
    expect(json['value'], true);

    DiagnosticsProperty<String> stringProperty = DiagnosticsProperty<String>('string1', 'hello');
    json = simulateJsonSerialization(stringProperty);
    expect(json['name'], 'string1');
    expect(json['value'], 'hello');

    stringProperty = StringProperty('string2', 'world');
    json = simulateJsonSerialization(stringProperty);
    expect(json['name'], 'string2');
    expect(json['value'], 'world');
  });
2283
}