Changeset 19:ab5f65372038 in pyenvjasmine


Ignore:
Timestamp:
Apr 24, 2015, 1:03:31 PM (9 years ago)
Author:
Borja Lopez <borja@…>
Branch:
default
Phase:
public
Message:

Imported latest envjasmine version, the initial import from darcs had the wrong version

Location:
pyenvjasmine/envjasmine
Files:
17 added
1 deleted
8 edited

Legend:

Unmodified
Added
Removed
  • pyenvjasmine/envjasmine/README.textile

    r0 r19  
    156156bc. ./tests/EnvJasmine/bin/run_test.sh --configFile=</absolute/path/to/demo/tests/EnvJasmine/etc/conf/demo.conf.js> specs/add-numbers.spec.js
    157157
     158
     159h2. Code Coverage
     160
     161See @lib/jscover/README.textile@
     162
     163
     164h2. EnvJasmine as a Ruby gem
     165
     166EnvJasmine can also be compiled to a Ruby gem.
     167
     168h3. How to build and install the EnvJasmine gem
     169
     170    git clone git://github.com/trevmex/EnvJasmine.git
     171    cd EnvJasmine
     172    gem build EnvJasmine.gemspec
     173    gem install EnvJasmine-1.7.1.gem
     174
     175h3. Using the EnvJasmine Ruby gem
     176
     177Note that, when used as a Ruby gem, EnvJasmine requires a few command line arguments be specified.
     178
     179Here is a Ruby usage example:
     180
     181    envjs_run_test --configFile='some_spec_helper.js' --testDir='specs' --rootDir='some_project'
     182
    158183That's it! For more help on Jasmine docs at https://github.com/pivotal/jasmine/wiki
    159184
    160185Please contact Trevor Lalish-Menagh through github (https://github.com/trevmex) with any defects or feature requests!
     186
  • pyenvjasmine/envjasmine/include/dependencies.js

    r0 r19  
    22//
    33// NOTE: Load order does matter.
    4 
    54// Load the envjasmine environment
    65EnvJasmine.loadGlobal(EnvJasmine.libDir + "envjs/env.rhino.1.2.js");
     
    1211
    1312// This is your main JavaScript directory in your project.
    14 EnvJasmine.jsDir = EnvJasmine.rootDir + "/samples/"; // TODO: Change this to your project's main js directory.
     13EnvJasmine.jsDir = EnvJasmine.jsDir || EnvJasmine.rootDir + "/samples/"; // TODO: Change this to your project's main js directory.
    1514
    1615EnvJasmine.loadGlobal(EnvJasmine.includeDir + "jquery-1.4.4.js"); // for example, load jquery.
    1716// TODO: Add your own
     17
     18
     19// this will include the code coverage plugin
     20//EnvJasmine.loadGlobal(EnvJasmine.libDir + "/jscover/envjasmine-sonar-coverage-properties.js"); // TODO: Uncomment and update if you want code coverage
     21//EnvJasmine.loadGlobal(EnvJasmine.coverage.envjasmine_coverage_js); // TODO: Uncomment if you want code coverage
  • pyenvjasmine/envjasmine/lib/envjasmine.js

    r0 r19  
    4040};
    4141
     42EnvJasmine.printDebug = function(str) {
     43    if (EnvJasmine.debug) {
     44        print(str);
     45    }
     46}
     47
    4248EnvJasmine.normalizePath = function(path) {
    4349    var endsInSlash = (path.slice(-1) == "/");
     
    5965            EnvJasmine.cx.evaluateReader(scope, fileIn, normalizedPath, 0, null);
    6066        } catch (e) {
     67            print (e);
    6168            print('Could not read file: ' + normalizedPath );
    6269        } finally {
     
    8895};
    8996
     97EnvJasmine.setJsDir = function(jsDir) {
     98    EnvJasmine.jsDir = jsDir;
     99};
     100
    90101// Process command line options
    91 
    92102(function(argumentList) {
    93103    var arg, nameValue, spec = "", specLoc;
     
    97107    EnvJasmine.failedCount = 0;
    98108    EnvJasmine.totalCount = 0;
    99 
    100109    for (var i = 0; i < argumentList.length; i++) {
    101110        arg = argumentList[i];
    102 
    103111        if (arg.slice(0, 2) == "--") {
    104112            nameValue = arg.slice(2).split('=');
    105 
    106113            switch(nameValue[0]) {
    107114                case "testDir":
     
    112119                    EnvJasmine.setTestDir(nameValue[1]); // Set the root as the default testDir.
    113120                    break;
     121                case "jsDir":
     122                    EnvJasmine.setJsDir(nameValue[1]);
     123                    break;
    114124                case "environment":
    115125                    EnvJasmine.environment = nameValue[1];
     
    129139                case "customSpecsDir":
    130140                    EnvJasmine.customSpecsDir = nameValue[1];
     141                    break;
     142                case "plugin":
     143                    EnvJasmine.addPlugin(nameValue[1]);
     144                    break;
     145                case "debug":
     146                    EnvJasmine.debug = true;
    131147                    break;
    132148                case "help":
     
    196212EnvJasmine.results = [];
    197213
     214EnvJasmine.addFinallyFunction = function(f) {
     215    EnvJasmine.finallyFunctions.push(f);
     216};
     217EnvJasmine.finallyFunctions = [];
     218
     219
    198220EnvJasmine.loadConfig = function () {
    199221    EnvJasmine.loadGlobal(EnvJasmine.configFile);
    200222};
    201223
     224EnvJasmine.plugins = [];
     225EnvJasmine.addPlugin = function(path) {
     226    EnvJasmine.plugins.push(path);
     227};
     228
    202229(function() {
    203230    var i, fileIn, len;
    204231
    205232    EnvJasmine.loadConfig();
    206    
     233
     234    for (i = 0; i < EnvJasmine.plugins.length; i++) {
     235        EnvJasmine.loadGlobal(EnvJasmine.plugins[i]);
     236    }
     237
    207238    if (typeof EnvJasmine.reporterClass === "undefined") {
    208239        // Use the standard reporter
    209240        EnvJasmine.reporterClass = RhinoReporter;
    210241    }
    211    
     242
    212243
    213244    jasmine.getEnv().addReporter(new EnvJasmine.reporterClass());
     
    242273            EnvJasmine.cx.evaluateString(EnvJasmine.currentScope, 'window.location.assign(["file://", EnvJasmine.libDir, "envjasmine.html"].join(EnvJasmine.SEPARATOR));', 'Executing '+EnvJasmine.specs[i], 0, null);
    243274        }
     275        catch (e) {
     276            print("Problem opening " + EnvJasmine.specFile + ": " + e);
     277        }
    244278        finally {
    245             fileIn.close();
    246         }
     279            if(fileIn) {
     280                fileIn.close();
     281            }
     282        }
     283    }
     284
     285    for (i = 0; i < EnvJasmine.finallyFunctions.length; i++) {
     286        EnvJasmine.finallyFunctions[i]();
    247287    }
    248288
  • pyenvjasmine/envjasmine/lib/jasmine-jquery/jasmine-jquery.js

    r0 r19  
    11var readFixtures = function() {
    2   return jasmine.getFixtures().proxyCallTo_('read', arguments);
    3 };
     2  return jasmine.getFixtures().proxyCallTo_('read', arguments)
     3}
    44
    55var preloadFixtures = function() {
    6   jasmine.getFixtures().proxyCallTo_('preload', arguments);
    7 };
     6  jasmine.getFixtures().proxyCallTo_('preload', arguments)
     7}
    88
    99var loadFixtures = function() {
    10   jasmine.getFixtures().proxyCallTo_('load', arguments);
    11 };
     10  jasmine.getFixtures().proxyCallTo_('load', arguments)
     11}
     12
     13var appendLoadFixtures = function() {
     14  jasmine.getFixtures().proxyCallTo_('appendLoad', arguments)
     15}
    1216
    1317var setFixtures = function(html) {
    14   jasmine.getFixtures().set(html);
    15 };
     18  jasmine.getFixtures().proxyCallTo_('set', arguments)
     19}
     20
     21var appendSetFixtures = function() {
     22  jasmine.getFixtures().proxyCallTo_('appendSet', arguments)
     23}
    1624
    1725var sandbox = function(attributes) {
    18   return jasmine.getFixtures().sandbox(attributes);
    19 };
     26  return jasmine.getFixtures().sandbox(attributes)
     27}
    2028
    2129var spyOnEvent = function(selector, eventName) {
    22   jasmine.JQuery.events.spyOn(selector, eventName);
     30  return jasmine.JQuery.events.spyOn(selector, eventName)
     31}
     32
     33var preloadStyleFixtures = function() {
     34  jasmine.getStyleFixtures().proxyCallTo_('preload', arguments)
     35}
     36
     37var loadStyleFixtures = function() {
     38  jasmine.getStyleFixtures().proxyCallTo_('load', arguments)
     39}
     40
     41var appendLoadStyleFixtures = function() {
     42  jasmine.getStyleFixtures().proxyCallTo_('appendLoad', arguments)
     43}
     44
     45var setStyleFixtures = function(html) {
     46  jasmine.getStyleFixtures().proxyCallTo_('set', arguments)
     47}
     48
     49var appendSetStyleFixtures = function(html) {
     50  jasmine.getStyleFixtures().proxyCallTo_('appendSet', arguments)
     51}
     52
     53var loadJSONFixtures = function() {
     54  return jasmine.getJSONFixtures().proxyCallTo_('load', arguments)
     55}
     56
     57var getJSONFixture = function(url) {
     58  return jasmine.getJSONFixtures().proxyCallTo_('read', arguments)[url]
     59}
     60
     61jasmine.spiedEventsKey = function (selector, eventName) {
     62  return [$(selector).selector, eventName].toString()
    2363}
    2464
    2565jasmine.getFixtures = function() {
    26   return jasmine.currentFixtures_ = jasmine.currentFixtures_ || new jasmine.Fixtures();
    27 };
     66  return jasmine.currentFixtures_ = jasmine.currentFixtures_ || new jasmine.Fixtures()
     67}
     68
     69jasmine.getStyleFixtures = function() {
     70  return jasmine.currentStyleFixtures_ = jasmine.currentStyleFixtures_ || new jasmine.StyleFixtures()
     71}
    2872
    2973jasmine.Fixtures = function() {
    30   this.containerId = 'jasmine-fixtures';
    31   this.fixturesCache_ = {};
    32   this.fixturesPath = 'spec/javascripts/fixtures';
    33 };
     74  this.containerId = 'jasmine-fixtures'
     75  this.fixturesCache_ = {}
     76  this.fixturesPath = 'spec/javascripts/fixtures'
     77}
    3478
    3579jasmine.Fixtures.prototype.set = function(html) {
    36   this.cleanUp();
    37   this.createContainer_(html);
    38 };
     80  this.cleanUp()
     81  this.createContainer_(html)
     82}
     83
     84jasmine.Fixtures.prototype.appendSet= function(html) {
     85  this.addToContainer_(html)
     86}
    3987
    4088jasmine.Fixtures.prototype.preload = function() {
    41   this.read.apply(this, arguments);
    42 };
     89  this.read.apply(this, arguments)
     90}
    4391
    4492jasmine.Fixtures.prototype.load = function() {
    45   this.cleanUp();
    46   this.createContainer_(this.read.apply(this, arguments));
    47 };
     93  this.cleanUp()
     94  this.createContainer_(this.read.apply(this, arguments))
     95}
     96
     97jasmine.Fixtures.prototype.appendLoad = function() {
     98  this.addToContainer_(this.read.apply(this, arguments))
     99}
    48100
    49101jasmine.Fixtures.prototype.read = function() {
    50   var htmlChunks = [];
    51 
    52   var fixtureUrls = arguments;
     102  var htmlChunks = []
     103
     104  var fixtureUrls = arguments
    53105  for(var urlCount = fixtureUrls.length, urlIndex = 0; urlIndex < urlCount; urlIndex++) {
    54     htmlChunks.push(this.getFixtureHtml_(fixtureUrls[urlIndex]));
    55   }
    56 
    57   return htmlChunks.join('');
    58 };
     106    htmlChunks.push(this.getFixtureHtml_(fixtureUrls[urlIndex]))
     107  }
     108
     109  return htmlChunks.join('')
     110}
    59111
    60112jasmine.Fixtures.prototype.clearCache = function() {
    61   this.fixturesCache_ = {};
    62 };
     113  this.fixturesCache_ = {}
     114}
    63115
    64116jasmine.Fixtures.prototype.cleanUp = function() {
    65   jQuery('#' + this.containerId).remove();
    66 };
     117  $('#' + this.containerId).remove()
     118}
    67119
    68120jasmine.Fixtures.prototype.sandbox = function(attributes) {
    69   var attributesToSet = attributes || {};
    70   return jQuery('<div id="sandbox" />').attr(attributesToSet);
    71 };
     121  var attributesToSet = attributes || {}
     122  return $('<div id="sandbox" />').attr(attributesToSet)
     123}
    72124
    73125jasmine.Fixtures.prototype.createContainer_ = function(html) {
    74   var container;
    75   if(html instanceof jQuery) {
    76     container = jQuery('<div id="' + this.containerId + '" />');
    77     container.html(html);
     126  var container
     127  if(html instanceof $) {
     128    container = $('<div id="' + this.containerId + '" />')
     129    container.html(html)
    78130  } else {
    79131    container = '<div id="' + this.containerId + '">' + html + '</div>'
    80132  }
    81   jQuery('body').append(container);
    82 };
    83 
    84 jasmine.Fixtures.prototype.getFixtureHtml_ = function(url) { 
    85   if (typeof this.fixturesCache_[url] == 'undefined') {
    86     this.loadFixtureIntoCache_(url);
    87   }
    88   return this.fixturesCache_[url];
    89 };
     133  $(document.body).append(container)
     134}
     135
     136jasmine.Fixtures.prototype.addToContainer_ = function(html){
     137  var container = $(document.body).find('#'+this.containerId).append(html)
     138  if(!container.length){
     139    this.createContainer_(html)
     140  }
     141}
     142
     143jasmine.Fixtures.prototype.getFixtureHtml_ = function(url) {
     144  if (typeof this.fixturesCache_[url] === 'undefined') {
     145    this.loadFixtureIntoCache_(url)
     146  }
     147  return this.fixturesCache_[url]
     148}
    90149
    91150jasmine.Fixtures.prototype.loadFixtureIntoCache_ = function(relativeUrl) {
    92   var self = this;
    93   var url = this.fixturesPath.match('/$') ? this.fixturesPath + relativeUrl : this.fixturesPath + '/' + relativeUrl;
    94   jQuery.ajax({
     151  var url = this.makeFixtureUrl_(relativeUrl)
     152  var request = $.ajax({
     153    type: "GET",
     154    url: url + "?" + new Date().getTime(),
     155    async: false
     156  })
     157  this.fixturesCache_[relativeUrl] = request.responseText
     158}
     159
     160jasmine.Fixtures.prototype.makeFixtureUrl_ = function(relativeUrl){
     161  return this.fixturesPath.match('/$') ? this.fixturesPath + relativeUrl : this.fixturesPath + '/' + relativeUrl
     162}
     163
     164jasmine.Fixtures.prototype.proxyCallTo_ = function(methodName, passedArguments) {
     165  return this[methodName].apply(this, passedArguments)
     166}
     167
     168
     169jasmine.StyleFixtures = function() {
     170  this.fixturesCache_ = {}
     171  this.fixturesNodes_ = []
     172  this.fixturesPath = 'spec/javascripts/fixtures'
     173}
     174
     175jasmine.StyleFixtures.prototype.set = function(css) {
     176  this.cleanUp()
     177  this.createStyle_(css)
     178}
     179
     180jasmine.StyleFixtures.prototype.appendSet = function(css) {
     181  this.createStyle_(css)
     182}
     183
     184jasmine.StyleFixtures.prototype.preload = function() {
     185  this.read_.apply(this, arguments)
     186}
     187
     188jasmine.StyleFixtures.prototype.load = function() {
     189  this.cleanUp()
     190  this.createStyle_(this.read_.apply(this, arguments))
     191}
     192
     193jasmine.StyleFixtures.prototype.appendLoad = function() {
     194  this.createStyle_(this.read_.apply(this, arguments))
     195}
     196
     197jasmine.StyleFixtures.prototype.cleanUp = function() {
     198  while(this.fixturesNodes_.length) {
     199    this.fixturesNodes_.pop().remove()
     200  }
     201}
     202
     203jasmine.StyleFixtures.prototype.createStyle_ = function(html) {
     204  var styleText = $('<div></div>').html(html).text(),
     205    style = $('<style>' + styleText + '</style>')
     206
     207  this.fixturesNodes_.push(style)
     208
     209  $('head').append(style)
     210}
     211
     212jasmine.StyleFixtures.prototype.clearCache = jasmine.Fixtures.prototype.clearCache
     213
     214jasmine.StyleFixtures.prototype.read_ = jasmine.Fixtures.prototype.read
     215
     216jasmine.StyleFixtures.prototype.getFixtureHtml_ = jasmine.Fixtures.prototype.getFixtureHtml_
     217
     218jasmine.StyleFixtures.prototype.loadFixtureIntoCache_ = jasmine.Fixtures.prototype.loadFixtureIntoCache_
     219
     220jasmine.StyleFixtures.prototype.makeFixtureUrl_ = jasmine.Fixtures.prototype.makeFixtureUrl_
     221
     222jasmine.StyleFixtures.prototype.proxyCallTo_ = jasmine.Fixtures.prototype.proxyCallTo_
     223
     224jasmine.getJSONFixtures = function() {
     225  return jasmine.currentJSONFixtures_ = jasmine.currentJSONFixtures_ || new jasmine.JSONFixtures()
     226}
     227
     228jasmine.JSONFixtures = function() {
     229  this.fixturesCache_ = {}
     230  this.fixturesPath = 'spec/javascripts/fixtures/json'
     231}
     232
     233jasmine.JSONFixtures.prototype.load = function() {
     234  this.read.apply(this, arguments)
     235  return this.fixturesCache_
     236}
     237
     238jasmine.JSONFixtures.prototype.read = function() {
     239  var fixtureUrls = arguments
     240  for(var urlCount = fixtureUrls.length, urlIndex = 0; urlIndex < urlCount; urlIndex++) {
     241    this.getFixtureData_(fixtureUrls[urlIndex])
     242  }
     243  return this.fixturesCache_
     244}
     245
     246jasmine.JSONFixtures.prototype.clearCache = function() {
     247  this.fixturesCache_ = {}
     248}
     249
     250jasmine.JSONFixtures.prototype.getFixtureData_ = function(url) {
     251  this.loadFixtureIntoCache_(url)
     252  return this.fixturesCache_[url]
     253}
     254
     255jasmine.JSONFixtures.prototype.loadFixtureIntoCache_ = function(relativeUrl) {
     256  var self = this
     257  var url = this.fixturesPath.match('/$') ? this.fixturesPath + relativeUrl : this.fixturesPath + '/' + relativeUrl
     258  $.ajax({
    95259    async: false, // must be synchronous to guarantee that no tests are run before fixture is loaded
    96260    cache: false,
    97     dataType: 'html',
     261    dataType: 'json',
    98262    url: url,
    99263    success: function(data) {
    100       self.fixturesCache_[relativeUrl] = data;
    101     },
    102     error: function(jqXHR, status, errorThrown) {
    103         throw Error('Fixture could not be loaded: ' + url + ' (status: ' + status + ', message: ' + errorThrown.message + ')');
    104     }
    105   });
    106 };
    107 
    108 jasmine.Fixtures.prototype.proxyCallTo_ = function(methodName, passedArguments) {
    109   return this[methodName].apply(this, passedArguments);
    110 };
    111 
    112 
    113 jasmine.JQuery = function() {};
     264      self.fixturesCache_[relativeUrl] = data
     265    },
     266    fail: function(jqXHR, status, errorThrown) {
     267        throw Error('JSONFixture could not be loaded: ' + url + ' (status: ' + status + ', message: ' + errorThrown.message + ')')
     268    }
     269  })
     270}
     271
     272jasmine.JSONFixtures.prototype.proxyCallTo_ = function(methodName, passedArguments) {
     273  return this[methodName].apply(this, passedArguments)
     274}
     275
     276jasmine.JQuery = function() {}
    114277
    115278jasmine.JQuery.browserTagCaseIndependentHtml = function(html) {
    116   return jQuery('<div/>').append(html).html();
    117 };
     279  return $('<div/>').append(html).html()
     280}
    118281
    119282jasmine.JQuery.elementToString = function(element) {
    120   return jQuery('<div />').append(element.clone()).html();
    121 };
    122 
    123 jasmine.JQuery.matchersClass = {};
    124 
    125 (function(namespace) {
     283  var domEl = $(element).get(0)
     284  if (domEl == undefined || domEl.cloneNode)
     285    return $('<div />').append($(element).clone()).html()
     286  else
     287    return element.toString()
     288}
     289
     290jasmine.JQuery.matchersClass = {}
     291
     292!function(namespace) {
    126293  var data = {
    127294    spiedEvents: {},
    128295    handlers:    []
    129   };
     296  }
    130297
    131298  namespace.events = {
    132299    spyOn: function(selector, eventName) {
    133300      var handler = function(e) {
    134         data.spiedEvents[[selector, eventName]] = e;
    135       };
    136       jQuery(selector).bind(eventName, handler);
    137       data.handlers.push(handler);
     301        data.spiedEvents[jasmine.spiedEventsKey(selector, eventName)] = e
     302      }
     303      $(selector).bind(eventName, handler)
     304      data.handlers.push(handler)
     305      return {
     306        selector: selector,
     307        eventName: eventName,
     308        handler: handler,
     309        reset: function(){
     310          delete data.spiedEvents[jasmine.spiedEventsKey(selector, eventName)]
     311        }
     312      }
    138313    },
    139314
    140315    wasTriggered: function(selector, eventName) {
    141       return !!(data.spiedEvents[[selector, eventName]]);
     316      return !!(data.spiedEvents[jasmine.spiedEventsKey(selector, eventName)])
     317    },
     318
     319    wasPrevented: function(selector, eventName) {
     320      var e;
     321      return (e = data.spiedEvents[jasmine.spiedEventsKey(selector, eventName)]) && e.isDefaultPrevented()
    142322    },
    143323
    144324    cleanUp: function() {
    145       data.spiedEvents = {};
    146       data.handlers    = [];
    147     }
    148   }
    149 })(jasmine.JQuery);
    150 
    151 (function(){
     325      data.spiedEvents = {}
     326      data.handlers    = []
     327    }
     328  }
     329}(jasmine.JQuery)
     330
     331!function(){
    152332  var jQueryMatchers = {
    153333    toHaveClass: function(className) {
    154       return this.actual.hasClass(className);
     334      return this.actual.hasClass(className)
     335    },
     336
     337    toHaveCss: function(css){
     338      for (var prop in css){
     339        if (this.actual.css(prop) !== css[prop]) return false
     340      }
     341      return true
    155342    },
    156343
    157344    toBeVisible: function() {
    158       return this.actual.is(':visible');
     345      return this.actual.is(':visible')
    159346    },
    160347
    161348    toBeHidden: function() {
    162       return this.actual.is(':hidden');
     349      return this.actual.is(':hidden')
    163350    },
    164351
    165352    toBeSelected: function() {
    166       return this.actual.is(':selected');
     353      return this.actual.is(':selected')
    167354    },
    168355
    169356    toBeChecked: function() {
    170       return this.actual.is(':checked');
     357      return this.actual.is(':checked')
    171358    },
    172359
    173360    toBeEmpty: function() {
    174       return this.actual.is(':empty');
     361      return this.actual.is(':empty')
    175362    },
    176363
    177364    toExist: function() {
    178       return this.actual.size() > 0;
     365      return $(document).find(this.actual).length
     366    },
     367
     368    toHaveLength: function(length) {
     369      return this.actual.length === length
    179370    },
    180371
    181372    toHaveAttr: function(attributeName, expectedAttributeValue) {
    182       return hasProperty(this.actual.attr(attributeName), expectedAttributeValue);
     373      return hasProperty(this.actual.attr(attributeName), expectedAttributeValue)
     374    },
     375
     376    toHaveProp: function(propertyName, expectedPropertyValue) {
     377      return hasProperty(this.actual.prop(propertyName), expectedPropertyValue)
    183378    },
    184379
    185380    toHaveId: function(id) {
    186       return this.actual.attr('id') == id;
     381      return this.actual.attr('id') == id
    187382    },
    188383
    189384    toHaveHtml: function(html) {
    190       return this.actual.html() == jasmine.JQuery.browserTagCaseIndependentHtml(html);
     385      return this.actual.html() == jasmine.JQuery.browserTagCaseIndependentHtml(html)
     386    },
     387
     388    toContainHtml: function(html){
     389      var actualHtml = this.actual.html()
     390      var expectedHtml = jasmine.JQuery.browserTagCaseIndependentHtml(html)
     391      return (actualHtml.indexOf(expectedHtml) >= 0)
    191392    },
    192393
    193394    toHaveText: function(text) {
    194       if (text && jQuery.isFunction(text.test)) {
    195         return text.test(this.actual.text());
     395      var trimmedText = $.trim(this.actual.text())
     396      if (text && $.isFunction(text.test)) {
     397        return text.test(trimmedText)
    196398      } else {
    197         return this.actual.text() == text;
     399        return trimmedText == text
    198400      }
    199401    },
    200402
    201403    toHaveValue: function(value) {
    202       return this.actual.val() == value;
     404      return this.actual.val() === value
    203405    },
    204406
    205407    toHaveData: function(key, expectedValue) {
    206       return hasProperty(this.actual.data(key), expectedValue);
     408      return hasProperty(this.actual.data(key), expectedValue)
    207409    },
    208410
    209411    toBe: function(selector) {
    210       return this.actual.is(selector);
     412      return this.actual.is(selector)
    211413    },
    212414
    213415    toContain: function(selector) {
    214       return this.actual.find(selector).size() > 0;
     416      return this.actual.find(selector).length
    215417    },
    216418
    217419    toBeDisabled: function(selector){
    218       return this.actual.is(':disabled');
    219     },
    220 
    221     // tests the existence of a specific event binding
    222     toHandle: function(eventName) {
    223       var events = this.actual.data("events");
    224       return events && events[eventName].length > 0;
    225     },
    226    
     420      return this.actual.is(':disabled')
     421    },
     422
     423    toBeFocused: function(selector) {
     424      return this.actual[0] === this.actual[0].ownerDocument.activeElement
     425    },
     426
     427    toHandle: function(event) {
     428
     429      var events = $._data(this.actual.get(0), "events")
     430
     431      if(!events || !event || typeof event !== "string") {
     432        return false
     433      }
     434
     435      var namespaces = event.split(".")
     436      var eventType = namespaces.shift()
     437      var sortedNamespaces = namespaces.slice(0).sort()
     438      var namespaceRegExp = new RegExp("(^|\\.)" + sortedNamespaces.join("\\.(?:.*\\.)?") + "(\\.|$)")
     439
     440      if(events[eventType] && namespaces.length) {
     441        for(var i = 0; i < events[eventType].length; i++) {
     442          var namespace = events[eventType][i].namespace
     443          if(namespaceRegExp.test(namespace)) {
     444            return true
     445          }
     446        }
     447      } else {
     448        return events[eventType] && events[eventType].length > 0
     449      }
     450    },
     451
    227452    // tests the existence of a specific event binding + handler
    228453    toHandleWith: function(eventName, eventHandler) {
    229       var stack = this.actual.data("events")[eventName];
    230       var i;
    231       for (i = 0; i < stack.length; i++) {
    232         if (stack[i].handler == eventHandler) {
    233           return true;
    234         }
    235       }
    236       return false;
    237     }
    238   };
     454      var stack = $._data(this.actual.get(0), "events")[eventName]
     455      for (var i = 0; i < stack.length; i++) {
     456        if (stack[i].handler == eventHandler) return true
     457      }
     458      return false
     459    }
     460  }
    239461
    240462  var hasProperty = function(actualValue, expectedValue) {
    241     if (expectedValue === undefined) {
    242       return actualValue !== undefined;
    243     }
    244     return actualValue == expectedValue;
    245   };
     463    if (expectedValue === undefined) return actualValue !== undefined
     464    return actualValue == expectedValue
     465  }
    246466
    247467  var bindMatcher = function(methodName) {
    248     var builtInMatcher = jasmine.Matchers.prototype[methodName];
     468    var builtInMatcher = jasmine.Matchers.prototype[methodName]
    249469
    250470    jasmine.JQuery.matchersClass[methodName] = function() {
    251       if (this.actual instanceof jQuery) {
    252         var result = jQueryMatchers[methodName].apply(this, arguments);
    253         this.actual = jasmine.JQuery.elementToString(this.actual);
    254         return result;
    255       }
    256 
    257       if (builtInMatcher) {
    258         return builtInMatcher.apply(this, arguments);
    259       }
    260 
    261       return false;
    262     };
    263   };
     471      if (this.actual
     472        && (this.actual instanceof $
     473          || jasmine.isDomNode(this.actual))) {
     474            this.actual = $(this.actual)
     475            var result = jQueryMatchers[methodName].apply(this, arguments)
     476            var element
     477            if (this.actual.get && (element = this.actual.get()[0]) && !$.isWindow(element) && element.tagName !== "HTML")
     478              this.actual = jasmine.JQuery.elementToString(this.actual)
     479            return result
     480          }
     481
     482          if (builtInMatcher) {
     483            return builtInMatcher.apply(this, arguments)
     484          }
     485
     486          return false
     487    }
     488  }
    264489
    265490  for(var methodName in jQueryMatchers) {
    266     bindMatcher(methodName);
    267   }
    268 })();
     491    bindMatcher(methodName)
     492  }
     493}()
    269494
    270495beforeEach(function() {
    271   this.addMatchers(jasmine.JQuery.matchersClass);
     496  this.addMatchers(jasmine.JQuery.matchersClass)
    272497  this.addMatchers({
    273498    toHaveBeenTriggeredOn: function(selector) {
    274499      this.message = function() {
    275500        return [
    276           "Expected event " + this.actual + " to have been triggered on" + selector,
    277           "Expected event " + this.actual + " not to have been triggered on" + selector
    278         ];
    279       };
    280       return jasmine.JQuery.events.wasTriggered(selector, this.actual);
     501          "Expected event " + this.actual + " to have been triggered on " + selector,
     502          "Expected event " + this.actual + " not to have been triggered on " + selector
     503        ]
     504      }
     505      return jasmine.JQuery.events.wasTriggered(selector, this.actual)
    281506    }
    282507  })
    283 });
     508  this.addMatchers({
     509    toHaveBeenTriggered: function(){
     510      var eventName = this.actual.eventName,
     511          selector = this.actual.selector
     512      this.message = function() {
     513        return [
     514          "Expected event " + eventName + " to have been triggered on " + selector,
     515          "Expected event " + eventName + " not to have been triggered on " + selector
     516        ]
     517      }
     518      return jasmine.JQuery.events.wasTriggered(selector, eventName)
     519     }
     520  })
     521  this.addMatchers({
     522    toHaveBeenPreventedOn: function(selector) {
     523      this.message = function() {
     524        return [
     525          "Expected event " + this.actual + " to have been prevented on " + selector,
     526          "Expected event " + this.actual + " not to have been prevented on " + selector
     527        ]
     528      }
     529      return jasmine.JQuery.events.wasPrevented(selector, this.actual)
     530    }
     531  })
     532  this.addMatchers({
     533    toHaveBeenPrevented: function() {
     534      var eventName = this.actual.eventName,
     535          selector = this.actual.selector
     536      this.message = function() {
     537        return [
     538          "Expected event " + eventName + " to have been prevented on " + selector,
     539          "Expected event " + eventName + " not to have been prevented on " + selector
     540        ]
     541      }
     542      return jasmine.JQuery.events.wasPrevented(selector, eventName)
     543    }
     544  })
     545})
    284546
    285547afterEach(function() {
    286   jasmine.getFixtures().cleanUp();
    287   jasmine.JQuery.events.cleanUp();
    288 });
     548  jasmine.getFixtures().cleanUp()
     549  jasmine.getStyleFixtures().cleanUp()
     550  jasmine.JQuery.events.cleanUp()
     551})
  • pyenvjasmine/envjasmine/samples/ajaxDemo.js

    r0 r19  
    11// This is the contents of ajaxDemo.js, the file to test.
    2 var TwitterWidget = {
    3     makeRequest: function() {
    4         var self = this;
    5        
    6         $.ajax({
    7             method: "GET",
    8             url: "http://api.twitter.com/1/statuses/show/trevmex.json",
    9             datatype: "json",
    10             success: function (data) {
    11                 self.addDataToDOM(data);
    12             }
    13         });
    14     },
    152
    16     addDataToDOM: function(data) {
    17         // does something
    18         // We will mock this behavior with a spy.
    19        
    20         return data;
    21     }
     3if (typeof(NS) === 'undefined' || !NS) {
     4    var NS = {};
     5}
     6
     7NS.greeter = function (name) {
     8    return 'Hello ' + name + '!';
    229};
     10
     11NS.greetUser = function (id) {
     12    return $.ajax({
     13        data: id,
     14        url: 'name.html',
     15        success: function (data) {
     16            NS.greeter(data.name);
     17        }
     18    });
     19};
     20
     21
  • pyenvjasmine/envjasmine/samples/demo.js

    r0 r19  
    11// Sample demo
    22
    3 if (!this.Demo) {
    4     Demo = {};
     3if (typeof(NS) === 'undefined' || !NS) {
     4    var NS = {};
    55}
    66
    7 Demo.checkBirthdate = function (user) {
    8     return !user.birthdate ? false : true;
    9 }
     7NS.greeter = function (name) {
     8    return 'Hello ' + name + '!';
     9};
  • pyenvjasmine/envjasmine/specs/ajaxDemo.spec.js

    r0 r19  
    1 // Load the file to test here.
    2 //
    3 // Example:
    41EnvJasmine.load(EnvJasmine.jsDir + "ajaxDemo.js");
    52
    6 // This is the test code.
    7 describe("AjaxDemo", function () {
    8     it("calls the addDataToDOM function on success", function () {
    9         TwitterWidget.makeRequest(); // Make the AJAX call
    10 
    11         spyOn(TwitterWidget, "addDataToDOM"); // Add a spy to the callback
    12 
    13         mostRecentAjaxRequest().response({status: 200, responseText: "foo"}); // Mock the response
    14 
    15         expect(TwitterWidget.addDataToDOM).toHaveBeenCalledWith("foo");
     3describe('greetUser', function () {
     4    it('calls greeter on success', function () {
     5        NS.greetUser(1);
     6        spyOn(NS, 'greeter');
     7        mostRecentAjaxRequest().response({
     8            status: 200,
     9            responseText: {"name":"Trevor"}
     10        });
     11        expect(NS.greeter).toHaveBeenCalledWith('Trevor');
    1612    });
    1713});
  • pyenvjasmine/envjasmine/specs/demo.spec.js

    r0 r19  
    1 // Load the file to test here.
    2 //
    3 // Example:
    41EnvJasmine.load(EnvJasmine.jsDir + "demo.js");
    52
    6 // Load mocks for this spec
    7 EnvJasmine.load(EnvJasmine.mocksDir + "demo.mock.js");
    8 
    9 describe("Demo", function () {
    10     it("asserts that one plus one equals two", function () {
    11         expect(1 + 1 == 2).toEqual(true);
    12     });
    13 
    14     it("asserts that 1 + 1 does not equal 3", function () {
    15         expect(1 + 1 == 3).toEqual(false);
    16     });
    17 
    18     it("asserts that a user has a birthdate", function () {
    19         expect(Demo.checkBirthdate(demoUser)).toEqual(true);
    20     });
    21 
    22     it("asserts that an ill-formed user has no birthdate", function () {
    23         expect(Demo.checkBirthdate(badUser)).toEqual(false);
     3describe('greeter', function () {
     4    it('greets me', function () {
     5        expect(NS.greeter('Trevor')).toEqual('Hello Trevor!');
    246    });
    257});
Note: See TracChangeset for help on using the changeset viewer.