Home | History | Annotate | Download | only in unittest
      1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 'use strict';
      6 
      7 base.require('base.unittest.assertions');
      8 
      9 base.unittest.testSuite('base.unittest.assertions', function() {
     10   setup(function() {
     11     global.rawAssertThrows = function(fn) {
     12       try {
     13         fn();
     14       } catch (e) {
     15         return;
     16       }
     17       throw new Error('Expected <' + fn + '> to throw');
     18     };
     19 
     20     global.rawAssertNotThrows = function(fn) {
     21       try {
     22         fn();
     23       } catch (e) {
     24         throw new Error('Expected <' + fn + '> to not throw');
     25       }
     26     };
     27   });
     28 
     29   teardown(function() {
     30     global.rawAssertThrows = undefined;
     31     global.rawAssertNotThrows = undefined;
     32   });
     33 
     34   test('assertTrue', function() {
     35     rawAssertThrows(function() {
     36       assertTrue(false);
     37     });
     38     rawAssertNotThrows(function() {
     39       assertTrue(true);
     40     });
     41   });
     42 
     43   test('assertObjectEquals', function() {
     44     rawAssertThrows(function() {
     45       assertObjectEquals({a: 1}, {a: 2});
     46     });
     47     rawAssertThrows(function() {
     48       assertObjectEquals({a: 1}, []);
     49     });
     50     rawAssertThrows(function() {
     51       assertObjectEquals({a: 1, b: {}}, {a: 1, c: {}, b: {}});
     52     });
     53     rawAssertNotThrows(function() {
     54       assertObjectEquals({}, {});
     55     });
     56     rawAssertNotThrows(function() {
     57       assertObjectEquals({a: 1}, {a: 1});
     58     });
     59   });
     60 
     61   test('assertThrows', function() {
     62     rawAssertThrows(function() {
     63       assertThrows(function() {
     64       });
     65     });
     66     rawAssertNotThrows(function() {
     67       assertThrows(function() {
     68         throw new Error('expected_error');
     69       });
     70     });
     71   });
     72 });
     73