| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library logging_test; | 5 library logging_test; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'package:logging/logging.dart'; | 9 import 'package:logging/logging.dart'; |
| 10 import 'package:test/test.dart'; | 10 import 'package:test/test.dart'; |
| (...skipping 326 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 337 | 337 |
| 338 root.finest('1'); | 338 root.finest('1'); |
| 339 root.finer('2'); | 339 root.finer('2'); |
| 340 root.fine('3'); | 340 root.fine('3'); |
| 341 root.config('4'); | 341 root.config('4'); |
| 342 root.info('5'); | 342 root.info('5'); |
| 343 root.warning('6'); | 343 root.warning('6'); |
| 344 root.severe('7'); | 344 root.severe('7'); |
| 345 root.shout('8'); | 345 root.shout('8'); |
| 346 | 346 |
| 347 expect(rootMessages, equals([ | 347 expect( |
| 348 'FINEST: 1', | 348 rootMessages, |
| 349 'FINER: 2', | 349 equals([ |
| 350 'FINE: 3', | 350 'FINEST: 1', |
| 351 'CONFIG: 4', | 351 'FINER: 2', |
| 352 'INFO: 5', | 352 'FINE: 3', |
| 353 'WARNING: 6', | 353 'CONFIG: 4', |
| 354 'SEVERE: 7', | 354 'INFO: 5', |
| 355 'SHOUT: 8' | 355 'WARNING: 6', |
| 356 ])); | 356 'SEVERE: 7', |
| 357 'SHOUT: 8' |
| 358 ])); |
| 357 }); | 359 }); |
| 358 | 360 |
| 359 test('logging methods store exception', () { | 361 test('logging methods store exception', () { |
| 360 root.level = Level.ALL; | 362 root.level = Level.ALL; |
| 361 var rootMessages = []; | 363 var rootMessages = []; |
| 362 root.onRecord.listen((r) { | 364 root.onRecord.listen((r) { |
| 363 rootMessages.add('${r.level}: ${r.message} ${r.error}'); | 365 rootMessages.add('${r.level}: ${r.message} ${r.error}'); |
| 364 }); | 366 }); |
| 365 | 367 |
| 366 root.finest('1'); | 368 root.finest('1'); |
| 367 root.finer('2'); | 369 root.finer('2'); |
| 368 root.fine('3'); | 370 root.fine('3'); |
| 369 root.config('4'); | 371 root.config('4'); |
| 370 root.info('5'); | 372 root.info('5'); |
| 371 root.warning('6'); | 373 root.warning('6'); |
| 372 root.severe('7'); | 374 root.severe('7'); |
| 373 root.shout('8'); | 375 root.shout('8'); |
| 374 root.finest('1', 'a'); | 376 root.finest('1', 'a'); |
| 375 root.finer('2', 'b'); | 377 root.finer('2', 'b'); |
| 376 root.fine('3', ['c']); | 378 root.fine('3', ['c']); |
| 377 root.config('4', 'd'); | 379 root.config('4', 'd'); |
| 378 root.info('5', 'e'); | 380 root.info('5', 'e'); |
| 379 root.warning('6', 'f'); | 381 root.warning('6', 'f'); |
| 380 root.severe('7', 'g'); | 382 root.severe('7', 'g'); |
| 381 root.shout('8', 'h'); | 383 root.shout('8', 'h'); |
| 382 | 384 |
| 383 expect(rootMessages, equals([ | 385 expect( |
| 384 'FINEST: 1 null', | 386 rootMessages, |
| 385 'FINER: 2 null', | 387 equals([ |
| 386 'FINE: 3 null', | 388 'FINEST: 1 null', |
| 387 'CONFIG: 4 null', | 389 'FINER: 2 null', |
| 388 'INFO: 5 null', | 390 'FINE: 3 null', |
| 389 'WARNING: 6 null', | 391 'CONFIG: 4 null', |
| 390 'SEVERE: 7 null', | 392 'INFO: 5 null', |
| 391 'SHOUT: 8 null', | 393 'WARNING: 6 null', |
| 392 'FINEST: 1 a', | 394 'SEVERE: 7 null', |
| 393 'FINER: 2 b', | 395 'SHOUT: 8 null', |
| 394 'FINE: 3 [c]', | 396 'FINEST: 1 a', |
| 395 'CONFIG: 4 d', | 397 'FINER: 2 b', |
| 396 'INFO: 5 e', | 398 'FINE: 3 [c]', |
| 397 'WARNING: 6 f', | 399 'CONFIG: 4 d', |
| 398 'SEVERE: 7 g', | 400 'INFO: 5 e', |
| 399 'SHOUT: 8 h' | 401 'WARNING: 6 f', |
| 400 ])); | 402 'SEVERE: 7 g', |
| 403 'SHOUT: 8 h' |
| 404 ])); |
| 401 }); | 405 }); |
| 402 | 406 |
| 403 test('message logging - no hierarchy', () { | 407 test('message logging - no hierarchy', () { |
| 404 root.level = Level.WARNING; | 408 root.level = Level.WARNING; |
| 405 var rootMessages = []; | 409 var rootMessages = []; |
| 406 var aMessages = []; | 410 var aMessages = []; |
| 407 var cMessages = []; | 411 var cMessages = []; |
| 408 c.onRecord.listen((record) { | 412 c.onRecord.listen((record) { |
| 409 cMessages.add('${record.level}: ${record.message}'); | 413 cMessages.add('${record.level}: ${record.message}'); |
| 410 }); | 414 }); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 421 | 425 |
| 422 b.info('4'); | 426 b.info('4'); |
| 423 b.severe('5'); | 427 b.severe('5'); |
| 424 b.warning('6'); | 428 b.warning('6'); |
| 425 b.fine('7'); | 429 b.fine('7'); |
| 426 | 430 |
| 427 c.fine('8'); | 431 c.fine('8'); |
| 428 c.warning('9'); | 432 c.warning('9'); |
| 429 c.shout('10'); | 433 c.shout('10'); |
| 430 | 434 |
| 431 expect(rootMessages, equals([ | 435 expect( |
| 432 // 'INFO: 1' is not loggable | 436 rootMessages, |
| 433 // 'FINE: 2' is not loggable | 437 equals([ |
| 434 'SHOUT: 3', | 438 // 'INFO: 1' is not loggable |
| 435 // 'INFO: 4' is not loggable | 439 // 'FINE: 2' is not loggable |
| 436 'SEVERE: 5', | 440 'SHOUT: 3', |
| 437 'WARNING: 6', | 441 // 'INFO: 4' is not loggable |
| 438 // 'FINE: 7' is not loggable | 442 'SEVERE: 5', |
| 439 // 'FINE: 8' is not loggable | 443 'WARNING: 6', |
| 440 'WARNING: 9', | 444 // 'FINE: 7' is not loggable |
| 441 'SHOUT: 10' | 445 // 'FINE: 8' is not loggable |
| 442 ])); | 446 'WARNING: 9', |
| 447 'SHOUT: 10' |
| 448 ])); |
| 443 | 449 |
| 444 // no hierarchy means we all hear the same thing. | 450 // no hierarchy means we all hear the same thing. |
| 445 expect(aMessages, equals(rootMessages)); | 451 expect(aMessages, equals(rootMessages)); |
| 446 expect(cMessages, equals(rootMessages)); | 452 expect(cMessages, equals(rootMessages)); |
| 447 }); | 453 }); |
| 448 | 454 |
| 449 test('message logging - with hierarchy', () { | 455 test('message logging - with hierarchy', () { |
| 450 hierarchicalLoggingEnabled = true; | 456 hierarchicalLoggingEnabled = true; |
| 451 | 457 |
| 452 b.level = Level.WARNING; | 458 b.level = Level.WARNING; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 470 | 476 |
| 471 b.info('4'); | 477 b.info('4'); |
| 472 b.severe('5'); | 478 b.severe('5'); |
| 473 b.warning('6'); | 479 b.warning('6'); |
| 474 b.fine('7'); | 480 b.fine('7'); |
| 475 | 481 |
| 476 c.fine('8'); | 482 c.fine('8'); |
| 477 c.warning('9'); | 483 c.warning('9'); |
| 478 c.shout('10'); | 484 c.shout('10'); |
| 479 | 485 |
| 480 expect(rootMessages, equals([ | 486 expect( |
| 481 'INFO: 1', | 487 rootMessages, |
| 482 // 'FINE: 2' is not loggable | 488 equals([ |
| 483 'SHOUT: 3', | 489 'INFO: 1', |
| 484 // 'INFO: 4' is not loggable | 490 // 'FINE: 2' is not loggable |
| 485 'SEVERE: 5', | 491 'SHOUT: 3', |
| 486 'WARNING: 6', | 492 // 'INFO: 4' is not loggable |
| 487 // 'FINE: 7' is not loggable | 493 'SEVERE: 5', |
| 488 // 'FINE: 8' is not loggable | 494 'WARNING: 6', |
| 489 'WARNING: 9', | 495 // 'FINE: 7' is not loggable |
| 490 'SHOUT: 10' | 496 // 'FINE: 8' is not loggable |
| 491 ])); | 497 'WARNING: 9', |
| 498 'SHOUT: 10' |
| 499 ])); |
| 492 | 500 |
| 493 expect(aMessages, equals([ | 501 expect( |
| 494 // 1,2 and 3 are lower in the hierarchy | 502 aMessages, |
| 495 // 'INFO: 4' is not loggable | 503 equals([ |
| 496 'SEVERE: 5', | 504 // 1,2 and 3 are lower in the hierarchy |
| 497 'WARNING: 6', | 505 // 'INFO: 4' is not loggable |
| 498 // 'FINE: 7' is not loggable | 506 'SEVERE: 5', |
| 499 // 'FINE: 8' is not loggable | 507 'WARNING: 6', |
| 500 'WARNING: 9', | 508 // 'FINE: 7' is not loggable |
| 501 'SHOUT: 10' | 509 // 'FINE: 8' is not loggable |
| 502 ])); | 510 'WARNING: 9', |
| 511 'SHOUT: 10' |
| 512 ])); |
| 503 | 513 |
| 504 expect(cMessages, equals([ | 514 expect( |
| 505 // 1 - 7 are lower in the hierarchy | 515 cMessages, |
| 506 // 'FINE: 8' is not loggable | 516 equals([ |
| 507 'WARNING: 9', | 517 // 1 - 7 are lower in the hierarchy |
| 508 'SHOUT: 10' | 518 // 'FINE: 8' is not loggable |
| 509 ])); | 519 'WARNING: 9', |
| 520 'SHOUT: 10' |
| 521 ])); |
| 510 }); | 522 }); |
| 511 | 523 |
| 512 test('message logging - lazy functions', () { | 524 test('message logging - lazy functions', () { |
| 513 root.level = Level.INFO; | 525 root.level = Level.INFO; |
| 514 var messages = []; | 526 var messages = []; |
| 515 root.onRecord.listen((record) { | 527 root.onRecord.listen((record) { |
| 516 messages.add('${record.level}: ${record.message}'); | 528 messages.add('${record.level}: ${record.message}'); |
| 517 }); | 529 }); |
| 518 | 530 |
| 519 var callCount = 0; | 531 var callCount = 0; |
| 520 var myClosure = () => "${++callCount}"; | 532 var myClosure = () => "${++callCount}"; |
| 521 | 533 |
| 522 root.info(myClosure); | 534 root.info(myClosure); |
| 523 root.finer(myClosure); // Should not get evaluated. | 535 root.finer(myClosure); // Should not get evaluated. |
| 524 root.warning(myClosure); | 536 root.warning(myClosure); |
| 525 | 537 |
| 526 expect(messages, equals(['INFO: 1', 'WARNING: 2',])); | 538 expect(messages, equals(['INFO: 1', 'WARNING: 2',])); |
| 527 }); | 539 }); |
| 528 | 540 |
| 529 test('message logging - calls toString', () { | 541 test('message logging - calls toString', () { |
| 530 root.level = Level.INFO; | 542 root.level = Level.INFO; |
| 531 var messages = []; | 543 var messages = []; |
| 544 var objects = []; |
| 545 var object = new Object(); |
| 532 root.onRecord.listen((record) { | 546 root.onRecord.listen((record) { |
| 533 messages.add('${record.level}: ${record.message}'); | 547 messages.add('${record.level}: ${record.message}'); |
| 548 objects.add(record.object); |
| 534 }); | 549 }); |
| 535 | 550 |
| 536 root.info(5); | 551 root.info(5); |
| 537 root.info(false); | 552 root.info(false); |
| 538 root.info([1, 2, 3]); | 553 root.info([1, 2, 3]); |
| 539 root.info(() => 10); | 554 root.info(() => 10); |
| 555 root.info(object); |
| 540 | 556 |
| 541 expect(messages, | 557 expect( |
| 542 equals(['INFO: 5', 'INFO: false', 'INFO: [1, 2, 3]', 'INFO: 10',])); | 558 messages, |
| 559 equals([ |
| 560 'INFO: 5', |
| 561 'INFO: false', |
| 562 'INFO: [1, 2, 3]', |
| 563 'INFO: 10', |
| 564 "INFO: Instance of 'Object'" |
| 565 ])); |
| 566 |
| 567 expect(objects, [ |
| 568 5, |
| 569 false, |
| 570 [1, 2, 3], |
| 571 10, |
| 572 object |
| 573 ]); |
| 543 }); | 574 }); |
| 544 }); | 575 }); |
| 545 | 576 |
| 546 group('recordStackTraceAtLevel', () { | 577 group('recordStackTraceAtLevel', () { |
| 547 var root = Logger.root; | 578 var root = Logger.root; |
| 548 tearDown(() { | 579 tearDown(() { |
| 549 recordStackTraceAtLevel = Level.OFF; | 580 recordStackTraceAtLevel = Level.OFF; |
| 550 root.clearListeners(); | 581 root.clearListeners(); |
| 551 }); | 582 }); |
| 552 | 583 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 572 expect(records, hasLength(3)); | 603 expect(records, hasLength(3)); |
| 573 expect(records[0].stackTrace, isNotNull); | 604 expect(records[0].stackTrace, isNotNull); |
| 574 expect(records[1].stackTrace, isNotNull); | 605 expect(records[1].stackTrace, isNotNull); |
| 575 expect(records[2].stackTrace, isNull); | 606 expect(records[2].stackTrace, isNull); |
| 576 }); | 607 }); |
| 577 | 608 |
| 578 test('provided trace is used if given', () { | 609 test('provided trace is used if given', () { |
| 579 var trace; | 610 var trace; |
| 580 try { | 611 try { |
| 581 throw 'trace'; | 612 throw 'trace'; |
| 582 } catch(e, t) { | 613 } catch (e, t) { |
| 583 trace = t; | 614 trace = t; |
| 584 } | 615 } |
| 585 var records = new List<LogRecord>(); | 616 var records = new List<LogRecord>(); |
| 586 recordStackTraceAtLevel = Level.WARNING; | 617 recordStackTraceAtLevel = Level.WARNING; |
| 587 root.onRecord.listen(records.add); | 618 root.onRecord.listen(records.add); |
| 588 root.severe('hello'); | 619 root.severe('hello'); |
| 589 root.warning('hello', 'a', trace); | 620 root.warning('hello', 'a', trace); |
| 590 expect(records, hasLength(2)); | 621 expect(records, hasLength(2)); |
| 591 expect(records[0].stackTrace, isNot(equals(trace))); | 622 expect(records[0].stackTrace, isNot(equals(trace))); |
| 592 expect(records[1].stackTrace, trace); | 623 expect(records[1].stackTrace, trace); |
| 593 }); | 624 }); |
| 594 | 625 |
| 595 test('error also generated when generating a trace', () { | 626 test('error also generated when generating a trace', () { |
| 596 var records = new List<LogRecord>(); | 627 var records = new List<LogRecord>(); |
| 597 recordStackTraceAtLevel = Level.WARNING; | 628 recordStackTraceAtLevel = Level.WARNING; |
| 598 root.onRecord.listen(records.add); | 629 root.onRecord.listen(records.add); |
| 599 root.severe('hello'); | 630 root.severe('hello'); |
| 600 root.warning('hello'); | 631 root.warning('hello'); |
| 601 root.info('hello'); | 632 root.info('hello'); |
| 602 expect(records, hasLength(3)); | 633 expect(records, hasLength(3)); |
| 603 expect(records[0].error, isNotNull); | 634 expect(records[0].error, isNotNull); |
| 604 expect(records[1].error, isNotNull); | 635 expect(records[1].error, isNotNull); |
| 605 expect(records[2].error, isNull); | 636 expect(records[2].error, isNull); |
| 606 }); | 637 }); |
| 607 }); | 638 }); |
| 608 } | 639 } |
| OLD | NEW |