[ Index ]
 

Code source de CMS made simple 1.0.5

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/scriptaculous/ -> unittest.js (source)

   1  // Copyright (c) 2005 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us)
   2  //           (c) 2005 Jon Tirsen (http://www.tirsen.com)
   3  //           (c) 2005 Michael Schuerig (http://www.schuerig.de/michael/)
   4  //
   5  // Permission is hereby granted, free of charge, to any person obtaining
   6  // a copy of this software and associated documentation files (the
   7  // "Software"), to deal in the Software without restriction, including
   8  // without limitation the rights to use, copy, modify, merge, publish,
   9  // distribute, sublicense, and/or sell copies of the Software, and to
  10  // permit persons to whom the Software is furnished to do so, subject to
  11  // the following conditions:
  12  // 
  13  // The above copyright notice and this permission notice shall be
  14  // included in all copies or substantial portions of the Software.
  15  // 
  16  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17  // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18  // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19  // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  20  // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  21  // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  22  // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23  
  24  
  25  // experimental, Firefox-only
  26  Event.simulateMouse = function(element, eventName) {
  27    var options = Object.extend({
  28      pointerX: 0,
  29      pointerY: 0,
  30      buttons: 0
  31    }, arguments[2] || {});
  32    var oEvent = document.createEvent("MouseEvents");
  33    oEvent.initMouseEvent(eventName, true, true, document.defaultView, 
  34      options.buttons, options.pointerX, options.pointerY, options.pointerX, options.pointerY, 
  35      false, false, false, false, 0, $(element));
  36    
  37    if(this.mark) Element.remove(this.mark);
  38    this.mark = document.createElement('div');
  39    this.mark.appendChild(document.createTextNode(" "));
  40    document.body.appendChild(this.mark);
  41    this.mark.style.position = 'absolute';
  42    this.mark.style.top = options.pointerY + "px";
  43    this.mark.style.left = options.pointerX + "px";
  44    this.mark.style.width = "5px";
  45    this.mark.style.height = "5px;";
  46    this.mark.style.borderTop = "1px solid red;"
  47    this.mark.style.borderLeft = "1px solid red;"
  48    
  49    if(this.step)
  50      alert('['+new Date().getTime().toString()+'] '+eventName+'/'+Test.Unit.inspect(options));
  51    
  52    $(element).dispatchEvent(oEvent);
  53  };
  54  
  55  // Note: Due to a fix in Firefox 1.0.5/6 that probably fixed "too much", this doesn't work in 1.0.6 or DP2.
  56  // You need to downgrade to 1.0.4 for now to get this working
  57  // See https://bugzilla.mozilla.org/show_bug.cgi?id=289940 for the fix that fixed too much
  58  Event.simulateKey = function(element, eventName) {
  59    var options = Object.extend({
  60      ctrlKey: false,
  61      altKey: false,
  62      shiftKey: false,
  63      metaKey: false,
  64      keyCode: 0,
  65      charCode: 0
  66    }, arguments[2] || {});
  67  
  68    var oEvent = document.createEvent("KeyEvents");
  69    oEvent.initKeyEvent(eventName, true, true, window, 
  70      options.ctrlKey, options.altKey, options.shiftKey, options.metaKey,
  71      options.keyCode, options.charCode );
  72    $(element).dispatchEvent(oEvent);
  73  };
  74  
  75  Event.simulateKeys = function(element, command) {
  76    for(var i=0; i<command.length; i++) {
  77      Event.simulateKey(element,'keypress',{charCode:command.charCodeAt(i)});
  78    }
  79  };
  80  
  81  Test = {}
  82  Test.Unit = {};
  83  
  84  // security exception workaround
  85  Test.Unit.inspect = function(obj) {
  86    var info = [];
  87  
  88    if(typeof obj=="string" || 
  89       typeof obj=="number") {
  90      return obj;
  91    } else {
  92      for(property in obj)
  93        if(typeof obj[property]!="function")
  94          info.push(property + ' => "' + obj[property] + '"');
  95    }
  96  
  97    return ("'" + obj + "' #" + typeof obj + 
  98      ": {" + info.join(", ") + "}");
  99  }
 100  
 101  Test.Unit.Logger = Class.create();
 102  Test.Unit.Logger.prototype = {
 103    initialize: function(log) {
 104      this.log = $(log);
 105      if (this.log) {
 106        this._createLogTable();
 107      }
 108    },
 109    start: function(testName) {
 110      if (!this.log) return;
 111      this.testName = testName;
 112      this.lastLogLine = document.createElement('tr');
 113      this.statusCell = document.createElement('td');
 114      this.nameCell = document.createElement('td');
 115      this.nameCell.appendChild(document.createTextNode(testName));
 116      this.messageCell = document.createElement('td');
 117      this.lastLogLine.appendChild(this.statusCell);
 118      this.lastLogLine.appendChild(this.nameCell);
 119      this.lastLogLine.appendChild(this.messageCell);
 120      this.loglines.appendChild(this.lastLogLine);
 121    },
 122    finish: function(status, summary) {
 123      if (!this.log) return;
 124      this.lastLogLine.className = status;
 125      this.statusCell.innerHTML = status;
 126      this.messageCell.innerHTML = this._toHTML(summary);
 127    },
 128    message: function(message) {
 129      if (!this.log) return;
 130      this.messageCell.innerHTML = this._toHTML(message);
 131    },
 132    summary: function(summary) {
 133      if (!this.log) return;
 134      this.logsummary.innerHTML = this._toHTML(summary);
 135    },
 136    _createLogTable: function() {
 137      this.log.innerHTML =
 138      '<div id="logsummary"></div>' +
 139      '<table id="logtable">' +
 140      '<thead><tr><th>Status</th><th>Test</th><th>Message</th></tr></thead>' +
 141      '<tbody id="loglines"></tbody>' +
 142      '</table>';
 143      this.logsummary = $('logsummary')
 144      this.loglines = $('loglines');
 145    },
 146    _toHTML: function(txt) {
 147      return txt.escapeHTML().replace(/\n/,"<br/>");
 148    }
 149  }
 150  
 151  Test.Unit.Runner = Class.create();
 152  Test.Unit.Runner.prototype = {
 153    initialize: function(testcases) {
 154      this.options = Object.extend({
 155        testLog: 'testlog'
 156      }, arguments[1] || {});
 157      if (this.options.testLog) {
 158        this.options.testLog = $(this.options.testLog) || null;
 159      }
 160      if(this.options.tests) {
 161        this.tests = [];
 162        for(var i = 0; i < this.options.tests.length; i++) {
 163          if(/^test/.test(this.options.tests[i])) {
 164            this.tests.push(new Test.Unit.Testcase(this.options.tests[i], testcases[this.options.tests[i]], testcases["setup"], testcases["teardown"]));
 165          }
 166        }
 167      } else {
 168        if (this.options.test) {
 169          this.tests = [new Test.Unit.Testcase(this.options.test, testcases[this.options.test], testcases["setup"], testcases["teardown"])];
 170        } else {
 171          this.tests = [];
 172          for(var testcase in testcases) {
 173            if(/^test/.test(testcase)) {
 174              this.tests.push(new Test.Unit.Testcase(testcase, testcases[testcase], testcases["setup"], testcases["teardown"]));
 175            }
 176          }
 177        }
 178      }
 179      this.currentTest = 0;
 180      this.logger = new Test.Unit.Logger(this.options.testLog);
 181      setTimeout(this.runTests.bind(this), 1000);
 182    },
 183    runTests: function() {
 184      var test = this.tests[this.currentTest];
 185      if (!test) {
 186        // finished!
 187        this.logger.summary(this.summary());
 188        return;
 189      }
 190      if(!test.isWaiting) {
 191        this.logger.start(test.name);
 192      }
 193      test.run();
 194      if(test.isWaiting) {
 195        this.logger.message("Waiting for " + test.timeToWait + "ms");
 196        setTimeout(this.runTests.bind(this), test.timeToWait || 1000);
 197      } else {
 198        this.logger.finish(test.status(), test.summary());
 199        this.currentTest++;
 200        // tail recursive, hopefully the browser will skip the stackframe
 201        this.runTests();
 202      }
 203    },
 204    summary: function() {
 205      var assertions = 0;
 206      var failures = 0;
 207      var errors = 0;
 208      var messages = [];
 209      for(var i=0;i<this.tests.length;i++) {
 210        assertions +=   this.tests[i].assertions;
 211        failures   +=   this.tests[i].failures;
 212        errors     +=   this.tests[i].errors;
 213      }
 214      return (
 215        this.tests.length + " tests, " + 
 216        assertions + " assertions, " + 
 217        failures   + " failures, " +
 218        errors     + " errors");
 219    }
 220  }
 221  
 222  Test.Unit.Assertions = Class.create();
 223  Test.Unit.Assertions.prototype = {
 224    initialize: function() {
 225      this.assertions = 0;
 226      this.failures   = 0;
 227      this.errors     = 0;
 228      this.messages   = [];
 229    },
 230    summary: function() {
 231      return (
 232        this.assertions + " assertions, " + 
 233        this.failures   + " failures, " +
 234        this.errors     + " errors" + "\n" +
 235        this.messages.join("\n"));
 236    },
 237    pass: function() {
 238      this.assertions++;
 239    },
 240    fail: function(message) {
 241      this.failures++;
 242      this.messages.push("Failure: " + message);
 243    },
 244    error: function(error) {
 245      this.errors++;
 246      this.messages.push(error.name + ": "+ error.message + "(" + Test.Unit.inspect(error) +")");
 247    },
 248    status: function() {
 249      if (this.failures > 0) return 'failed';
 250      if (this.errors > 0) return 'error';
 251      return 'passed';
 252    },
 253    assert: function(expression) {
 254      var message = arguments[1] || 'assert: got "' + Test.Unit.inspect(expression) + '"';
 255      try { expression ? this.pass() : 
 256        this.fail(message); }
 257      catch(e) { this.error(e); }
 258    },
 259    assertEqual: function(expected, actual) {
 260      var message = arguments[2] || "assertEqual";
 261      try { (expected == actual) ? this.pass() :
 262        this.fail(message + ': expected "' + Test.Unit.inspect(expected) + 
 263          '", actual "' + Test.Unit.inspect(actual) + '"'); }
 264      catch(e) { this.error(e); }
 265    },
 266    assertNotEqual: function(expected, actual) {
 267      var message = arguments[2] || "assertNotEqual";
 268      try { (expected != actual) ? this.pass() : 
 269        this.fail(message + ': got "' + Test.Unit.inspect(actual) + '"'); }
 270      catch(e) { this.error(e); }
 271    },
 272    assertNull: function(obj) {
 273      var message = arguments[1] || 'assertNull'
 274      try { (obj==null) ? this.pass() : 
 275        this.fail(message + ': got "' + Test.Unit.inspect(obj) + '"'); }
 276      catch(e) { this.error(e); }
 277    },
 278    assertHidden: function(element) {
 279      var message = arguments[1] || 'assertHidden';
 280      this.assertEqual("none", element.style.display, message);
 281    },
 282    assertNotNull: function(object) {
 283      var message = arguments[1] || 'assertNotNull';
 284      this.assert(object != null);
 285    },
 286    assertInstanceOf: function(expected, actual) {
 287      var message = arguments[1] || 'assertInstanceOf';
 288      this.assert(actual instanceof expected);    
 289    },
 290    _isVisible: function(element) {
 291      element = $(element);
 292      if(element == document) return true;
 293      this.assertNotNull(element);
 294      // if it's not an element (just check parent) may be a text node for example
 295      if (element.style && Element.getStyle(element, 'display') == 'none') {
 296        return false;
 297      } else {
 298        return this._isVisible(element.parentNode);
 299      }
 300      this.assertVisible(element.parentNode);
 301    },
 302    assertNotVisible: function(element) {
 303      this.assert(!this._isVisible(element), Test.Unit.inspect(element) + " was not hidden and didn't have a hidden parent either");
 304    },
 305    assertVisible: function(element) {
 306      this.assert(this._isVisible(element), Test.Unit.inspect(element) + " was not visible");
 307    }
 308  }
 309  
 310  Test.Unit.Testcase = Class.create();
 311  Object.extend(Object.extend(Test.Unit.Testcase.prototype, Test.Unit.Assertions.prototype), {
 312    initialize: function(name, test, setup, teardown) {
 313      Test.Unit.Assertions.prototype.initialize.bind(this)();
 314      this.name           = name;
 315      this.test           = test || function() {};
 316      this.setup          = setup || function() {};
 317      this.teardown       = teardown || function() {};
 318      this.isWaiting      = false;
 319      this.timeToWait     = 1000;
 320    },
 321    wait: function(time, nextPart) {
 322      this.isWaiting = true;
 323      this.test = nextPart;
 324      this.timeToWait = time;
 325    },
 326    run: function() {
 327      try {
 328        try {
 329          if (!this.isWaiting) this.setup.bind(this)();
 330          this.isWaiting = false;
 331          this.test.bind(this)();
 332        } finally {
 333          if(!this.isWaiting) {
 334            this.teardown.bind(this)();
 335          }
 336        }
 337      }
 338      catch(e) { this.error(e); }
 339    }
 340  });


Généré le : Tue Apr 3 18:50:37 2007 par Balluche grâce à PHPXref 0.7