1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/devtools/tests/unit/test_safeErrorString.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,57 @@ 1.4 +/* -*- Mode: js; js-indent-level: 2; -*- */ 1.5 +/* Any copyright is dedicated to the Public Domain. 1.6 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.7 + 1.8 +// Test DevToolsUtils.safeErrorString 1.9 + 1.10 +function run_test() { 1.11 + test_with_error(); 1.12 + test_with_tricky_error(); 1.13 + test_with_string(); 1.14 + test_with_thrower(); 1.15 + test_with_psychotic(); 1.16 +} 1.17 + 1.18 +function test_with_error() { 1.19 + let s = DevToolsUtils.safeErrorString(new Error("foo bar")); 1.20 + // Got the message. 1.21 + do_check_true(s.contains("foo bar")); 1.22 + // Got the stack. 1.23 + do_check_true(s.contains("test_with_error")) 1.24 + do_check_true(s.contains("test_safeErrorString.js")); 1.25 + // Got the lineNumber and columnNumber. 1.26 + do_check_true(s.contains("Line")); 1.27 + do_check_true(s.contains("column")); 1.28 +} 1.29 + 1.30 +function test_with_tricky_error() { 1.31 + let e = new Error("batman"); 1.32 + e.stack = { toString: Object.create(null) }; 1.33 + let s = DevToolsUtils.safeErrorString(e); 1.34 + // Still got the message, despite a bad stack property. 1.35 + do_check_true(s.contains("batman")); 1.36 +} 1.37 + 1.38 +function test_with_string() { 1.39 + let s = DevToolsUtils.safeErrorString("not really an error"); 1.40 + // Still get the message. 1.41 + do_check_true(s.contains("not really an error")); 1.42 +} 1.43 + 1.44 +function test_with_thrower() { 1.45 + let s = DevToolsUtils.safeErrorString({ 1.46 + toString: () => { 1.47 + throw new Error("Muahahaha"); 1.48 + } 1.49 + }); 1.50 + // Still don't fail, get string back. 1.51 + do_check_eq(typeof s, "string"); 1.52 +} 1.53 + 1.54 +function test_with_psychotic() { 1.55 + let s = DevToolsUtils.safeErrorString({ 1.56 + toString: () => Object.create(null) 1.57 + }); 1.58 + // Still get a string out, and no exceptions thrown 1.59 + do_check_eq(typeof s, "string"); 1.60 +}