/* ************************************************************************

   qooxdoo - the new era of web development

   http://qooxdoo.org

   Copyright:
     2007-2010 1&1 Internet AG, Germany, http://www.1und1.de

   License:
     LGPL: http://www.gnu.org/licenses/lgpl.html
     EPL: http://www.eclipse.org/org/documents/epl-v10.php
     See the LICENSE file in the project's top-level directory for details.

************************************************************************ */

qx.Class.define("qx.test.dev.StackTrace",
{
  extend : qx.dev.unit.TestCase,

  members :
  {
    tearDown : function()
    {
      qx.dev.StackTrace.FILENAME_TO_CLASSNAME = null;
      qx.dev.StackTrace.FORMAT_STACKTRACE = null;
    },


    testGetStackTraceFromError : function()
    {
      var trace = [];
      try {
        throw new Error("Expected exception");
      } catch(ex) {
        trace = qx.dev.StackTrace.getStackTraceFromError(ex);
      }
      if (qx.core.Environment.get("engine.name") == "mshtml") {
        // Can't get stack trace for IE
        qx.core.Assert.assertEquals(0, trace.length, "Got stack trace information for IE!");
      } else {
        qx.core.Assert.assertNotEquals(0, trace.length, "No stack trace information returned!");
      }
    },


    testFilenameConverterDefault : function()
    {
      var ex = new Error("Just a test");
      var stack = qx.dev.StackTrace.getStackTraceFromError(ex);
      for (var i=0, l=stack.length; i<l; i++) {
        this.assertMatch(stack[i], /((?:test\.dev\.StackTrace)|(?:dev\.unit)|(?:testrunner\.js)|(?:tests\.js))/);
      }
    },


    testFilenameConverterCustom : function()
    {
      var converter = function(fileName) {
        this.assertString(fileName);
        return "FOO";
      }

      qx.dev.StackTrace.FILENAME_TO_CLASSNAME = qx.lang.Function.bind(converter, this);
      var ex = new Error("Just a test");
      var stack = qx.dev.StackTrace.getStackTraceFromError(ex);
      for (var i=0, l=stack.length; i<l; i++) {
        this.assertMatch(stack[i], /FOO/);
      }
    },


    testFormatStackTrace : function()
    {
      var formatter = function(trace) {
        this.assertArray(trace);
        for (var i=0, l=trace.length; i<l; i++) {
          trace[i] = "BAR " + trace[i];
        }
        return trace;
      }

      qx.dev.StackTrace.FORMAT_STACKTRACE = qx.lang.Function.bind(formatter, this);
      var ex = new Error("Just a test");
      var stack = qx.dev.StackTrace.getStackTraceFromError(ex);
      for (var i=0, l=stack.length; i<l; i++) {
        this.assertMatch(stack[i], /^BAR/);
      }
    }
  }
});