safer version comparisons
Showing
4 changed files
with
63 additions
and
3 deletions
... | @@ -45,7 +45,7 @@ var Mouse = function Mouse(casper) { | ... | @@ -45,7 +45,7 @@ var Mouse = function Mouse(casper) { |
45 | 45 | ||
46 | var slice = Array.prototype.slice, | 46 | var slice = Array.prototype.slice, |
47 | nativeEvents = ['mouseup', 'mousedown', 'click', 'mousemove']; | 47 | nativeEvents = ['mouseup', 'mousedown', 'click', 'mousemove']; |
48 | if (phantom.version.major >= 1 && phantom.version.minor >= 8) { | 48 | if (utils.gteVersion(phantom.version, '1.8.0')) { |
49 | nativeEvents.push('doubleclick'); | 49 | nativeEvents.push('doubleclick'); |
50 | } | 50 | } |
51 | var emulatedEvents = ['mouseover', 'mouseout'], | 51 | var emulatedEvents = ['mouseover', 'mouseout'], | ... | ... |
... | @@ -579,3 +579,59 @@ function unique(array) { | ... | @@ -579,3 +579,59 @@ function unique(array) { |
579 | return r; | 579 | return r; |
580 | } | 580 | } |
581 | exports.unique = unique; | 581 | exports.unique = unique; |
582 | |||
583 | /** | ||
584 | * Compare two version numbers represented as strings. | ||
585 | * | ||
586 | * @param String a Version a | ||
587 | * @param String b Version b | ||
588 | * @return Number | ||
589 | */ | ||
590 | function cmpVersion(a, b) { | ||
591 | var i, cmp, len, re = /(\.0)+[^\.]*$/; | ||
592 | function versionToString(version) { | ||
593 | if (isObject(version)) { | ||
594 | try { | ||
595 | return [version.major, version.minor, version.patch].join('.'); | ||
596 | } catch (e) {} | ||
597 | } | ||
598 | return version; | ||
599 | } | ||
600 | a = versionToString(a); | ||
601 | b = versionToString(b); | ||
602 | a = (a + '').replace(re, '').split('.'); | ||
603 | b = (b + '').replace(re, '').split('.'); | ||
604 | len = Math.min(a.length, b.length); | ||
605 | for (i = 0; i < len; i++) { | ||
606 | cmp = parseInt(a[i], 10) - parseInt(b[i], 10); | ||
607 | if (cmp !== 0) { | ||
608 | return cmp; | ||
609 | } | ||
610 | } | ||
611 | return a.length - b.length; | ||
612 | } | ||
613 | exports.cmpVersion = cmpVersion; | ||
614 | |||
615 | /** | ||
616 | * Checks if a version number string is greater or equals another. | ||
617 | * | ||
618 | * @param String a Version a | ||
619 | * @param String b Version b | ||
620 | * @return Boolean | ||
621 | */ | ||
622 | function gteVersion(a, b) { | ||
623 | return cmpVersion(a, b) >= 0; | ||
624 | } | ||
625 | exports.gteVersion = gteVersion; | ||
626 | |||
627 | /** | ||
628 | * Checks if a version number string is less than another. | ||
629 | * | ||
630 | * @param String a Version a | ||
631 | * @param String b Version b | ||
632 | * @return Boolean | ||
633 | */ | ||
634 | function ltVersion(a, b) { | ||
635 | return cmpVersion(a, b) < 0; | ||
636 | } | ||
637 | exports.ltVersion = ltVersion; | ... | ... |
1 | /*global casper*/ | 1 | /*global casper*/ |
2 | /*jshint strict:false maxstatements: 99*/ | 2 | /*jshint strict:false maxstatements: 99*/ |
3 | var utils = require('utils'); | ||
4 | |||
3 | casper.start('tests/site/index.html', function() { | 5 | casper.start('tests/site/index.html', function() { |
4 | this.click('a[href="test.html"]'); | 6 | this.click('a[href="test.html"]'); |
5 | }); | 7 | }); |
... | @@ -57,7 +59,7 @@ casper.then(function() { | ... | @@ -57,7 +59,7 @@ casper.then(function() { |
57 | results = this.getGlobal('results'); | 59 | results = this.getGlobal('results'); |
58 | this.test.assertEquals(results.testmove, [200, 100], 'Mouse.move() has moved to the specified position'); | 60 | this.test.assertEquals(results.testmove, [200, 100], 'Mouse.move() has moved to the specified position'); |
59 | 61 | ||
60 | if (phantom.version.major >= 1 && phantom.version.minor >= 8) { | 62 | if (utils.gteVersion(phantom.version, '1.8.0')) { |
61 | this.test.comment('Mouse.doubleclick()'); | 63 | this.test.comment('Mouse.doubleclick()'); |
62 | this.mouse.doubleclick(200, 100); | 64 | this.mouse.doubleclick(200, 100); |
63 | results = this.getGlobal('results'); | 65 | results = this.getGlobal('results'); | ... | ... |
1 | /*jshint strict:false*/ | 1 | /*jshint strict:false*/ |
2 | /*global CasperError casper console phantom require*/ | 2 | /*global CasperError casper console phantom require*/ |
3 | if (phantom.version.major === 1 && phantom.version.minor < 8) { | 3 | var utils = require('utils') |
4 | |||
5 | if (utils.ltVersion(phantom.version, '1.8.0')) { | ||
4 | // https://github.com/n1k0/casperjs/issues/101 | 6 | // https://github.com/n1k0/casperjs/issues/101 |
5 | casper.warn('document.location is broken under phantomjs < 1.8'); | 7 | casper.warn('document.location is broken under phantomjs < 1.8'); |
6 | casper.test.done(); | 8 | casper.test.done(); | ... | ... |
-
Please register or sign in to post a comment