Skip to content

Instantly share code, notes, and snippets.

@supersha
Last active December 19, 2015 09:19
Show Gist options
  • Save supersha/5932377 to your computer and use it in GitHub Desktop.
Save supersha/5932377 to your computer and use it in GitHub Desktop.
//should介词
should("xxx").be()
should("xxx").not()
should("xxx").and()
should("xxx").have()
should(xxx).has()
//should(xxx).have().property
should({name : 1}).have().property("name").fail(function(){
monitor.sendError("have not property");
}).success(function(){
cnosole.log("have property");
});
//should(xxx).have().attr
should({name : 1}).not().have().property("name").fail(function(){
monitor.sendError("it have property");
});
//should(xxx).be().a
should({name : 1}).be().a("object").fail(function(){
monitor.sendError("not a object");
});
should([1,2,4]).be().a("array").fail(function(){
monitor.sendError("not a array");
});
should(document.getElementById("test")).be().a("dom").faild(function(){
monitor.sendError("not a dom element");
});
//should链式调用
should({name : 1}).not().be().empty().fail(function(){
monitor.sendError("it is empty");
}).and().have().property("name").fail(function(){
monitor.sendError("have no name property");
});
//上面使用到的fail和success需要在回调函数中去发送错误日志,也可以直接用下面的方法的
should("").not().be().empty().error("指定的参数不为空");
should("").not().be().empty().warn("指定的参数不为空");
should("").not().be().empty().log("指定的参数不为空");
should("").not().be().empty().sms("指定的参数不为空");
should("").not().be().empty().mail("指定的参数不为空");
//should(xxx).be().visible()/should(xxx).be().hide()
should($("#container")).be().visible().error("没有正常显示");
should($("#container")).be().hide().error("没有正常关闭");
var _should = function(val,monitor){
this.correct = -1;
this.negate = false;
this.monitor = monitor
//zepto or jquery
if(val && ((val.wrapAll && val.wrapInner && val.wrap && val.unwrap) || (val.each && val.addClass && val.css))){
this.value = [];
var that = this;
val.each(function(index,item){
that.value.push(item);
});
}else if(Util.type(val,"object") || Util.type(val,"string") || Util.type(val,"number") || Util.type(val,"dom") || !val){
this.value = [val];
}else{
//NodeList,Array...
this.value = val;
}
}
_should.prototype = {
be: function(){
return this;
},
have: function(){
return this;
},
has: function(){
return this;
},
not: function(){
this.negate = true;
return this;
},
and: function(){
return this;
},
to : function(){
return this;
},
assert : function(bool){
var flag = bool;
this.correct = -1;
if(this.negate){flag = !bool;}
if(flag){
this.correct = 1;
}else{
this.correct = 2;
}
this.negate = false;
},
success : function(success){
(this.correct == 1) && success && success(this);
return this;
},
fail : function(fail){
(this.correct === 2) && fail && fail(this);
return this;
},
error : function(content){
(this.correct === 2) && this.monitor.error(content);
},
warn : function(content){
(this.correct === 2) && this.monitor.warn(content);
},
log : function(content){
(this.correct === 2) && this.monitor.log(content);
},
sms : function(content){
(this.correct === 2) && this.monitor.sms(content);
},
mail : function(content){
(this.correct === 2) && this.monitor.mail(content);
},
property : function(prop){
var flag = true;
for(var i=0;i<this.value.length;i++){
if(!this.value[i][prop]){
flag = false;
break;
}
}
this.assert(flag);
return this;
},
beTrue:function(){
var flag = true;
for(var i=0;i<this.value.length;i++){
if(!this.value[i]){
flag = false;
break;
}
}
this.assert(flag);
return this;
},
beFalse:function(){
var flag = true;
for(var i=0;i<this.value.length;i++){
if(this.value[i]){
flag = false;
break;
}
}
this.assert(flag);
return this;
},
attr : function(at){
var flag = true;
for(var i=0;i<this.value.length;i++){
if(!this.value[i].getAttribute(at)){
flag = false;
break;
}
}
this.assert(flag);
return this;
},
ownProperty : function(prop){
var flag = true;
for(var i=0;i<this.value.length;i++){
if(!Util.ownProperty(this.value[i],prop)){
flag = false;
break;
}
}
this.assert(flag);
return this;
},
lengthOf : function(num){
var flag = true;
for(var i=0;i<this.value.length;i++){
if(!(this.value.length === num)){
flag = false;
break;
}
}
this.assert(flag);
return this;
},
keys : function(arr){
var bool = true;
for(var i =0,len = arr.length;i<len;i++){
for(var j=0;j<this.value.length;j++){
if(this.value[j].indexOf(arr[i]) === -1){
bool = false;
break;
}
}
if(!bool){break;}
}
this.assert(bool);
return this;
},
include : function(str){
var flag = true;
for(var i=0;i<this.value.length;i++){
if(Util.type(this.value[i],"object")){
if(!this.value[str]){
flag = false;
break;
}
}else{
if(this.value[i].indexOf(str) === -1){
flag = false;
break;
}
}
}
this.assert(flag);
return this;
},
empty : function(){
var isEmpty = true;
for(var i=0;i<this.value.length;i++){
if(Util.type(this.value[i],"object")){
for(var key in this.value[i]){
if(Util.ownProperty(this.value[i],key)){
isEmpty = false;
break;
}
}
}else if(Util.type(this.value[i],"array")){
isEmpty = this.value[i].length === 0;
}else if(Util.type(this.value[i],"dom")){
isEmpty = this.value[i].innerHTML === "";
}else{
isEmpty = this.value[i] === "";
}
if(!isEmpty){break;}
}
this.assert(isEmpty);
return this;
},
a : function(string){
var flag = true;
for(var i=0;i<this.value.length;i++){
if(!Util.type(this.value[i],string)){
flag = false;
break;
}
}
this.assert(flag);
return this;
},
above : function(num){
var flag = true;
for(var i=0;i<this.value.length;i++){
if(this.value[i] < num){
flag = false;
break;
}
}
this.assert(flag);
return this;
},
below : function(num){
var flag = true;
for(var i=0;i<this.value.length;i++){
if(this.value[i] >= num){
flag = false;
break;
}
}
this.assert(flag);
return this;
},
equal : function(obj){
var flag = true;
for(var i=0;i < this.value.length;i++){
if(this.value[i] !== obj){
flag = false;
break;
}
}
this.assert(flag);
return this;
},
exist : function(){
var flag = true;
for(var i=0;i<this.value.length;i++){
if(!this.value[i]){
flag = false;
break;
}
}
this.assert(flag);
return this;
},
match : function(reg){
var flag = true;
for(var i=0;i<this.value.length;i++){
if(!reg.test(this.value[i])){
flag = false;
break;
}
}
this.assert(flag);
return this;
},
child : function(elem){
this.assert(elem && this.value[0] && elem.parentNode == this.value[0]);
return this;
},
parent : function(elem){
this.assert(elem && this.value[0] && this.value[0].parentNode == elem);
return this;
},
next : function(elem){
this.assert(elem && this.value[0] && this.value[0].nextSibling == elem);
return this;
},
prev : function(elem){
this.assert(elem && this.value[0] && this[0].value.previousSibling == elem);
return this;
},
visible : function(){
var flag = true;
for(var i=0;i<this.value.length;i++){
if($(this.value[i]).css("display") == "none"){
flag = false;
break;
}
}
this.assert(flag);
return this;
},
hide : function(){
var flag = true;
for(var i=0;i<this.value.length;i++){
if($(this.value[i]).css("display") != "none"){
flag = false;
break;
}
}
this.assert(flag);
return this;
},
haveHrefKeys : function(keys){
var flag = true;
for(var i=0;i<this.value.length;i++){
for(var key in keys){
if(keys.hasOwnProperty(key)){
var reg = new RegExp(key + "=([^&]+)");
if(!reg.test($(this.value[i]).attr("href"))){
flag = false;
break;
}
if(keys[key] && (keys[key] != decodeURIComponent(RegExp.$1))){
flag = false;
break;
}
}
}
if(!flag){ break; }
}
this.assert(flag);
return this;
},
haveHrefKey : function(key,value){
var flag = true;
var reg = new RegExp(key + "=([^&]+)");
for(var i=0;i<this.value.length;i++){
if(!reg.test($(this.value[i]).attr("href"))){
flag = false;
break;
}
if(value && (decodeURIComponent(RegExp.$1) != value)){
flag = false;
break;
}
}
this.assert(flag);
return this;
},
haveFormKeys : function(keys){
var flag = true,$elem;
for(var i=0;i<this.value.length;i++){
for(var key in keys){
if(keys.hasOwnProperty(key)){
$elem = $(this.value[i]).find("[name=" + key + "]");
if($elem.size()){
if($elem.val() != keys[key]){
flag = false;
break;
}
}else{
flag = false;
break;
}
}
}
if(!flag){ break; }
}
this.assert(flag);
return this;
},
haveFormKey : function(key,value){
var flag = true,$elem;
for(var i=0;i<this.value.length;i++){
$elem = $(this.value[i]).find("[name=" + key + "]");
if($elem.size()){
if($elem.val() != value){
flag = false;
break;
}
}else{
flag = false;
break;
}
}
this.assert(flag);
return this;
}
}
_should.prototype.hasOwnProperty = _should.prototype.ownProperty;
_should.prototype.beLengthOf = _should.prototype.lengthOf;
_should.prototype.haveKeys = _should.prototype.keys;
_should.prototype.hasKeys = _should.prototype.keys;
_should.prototype.beEmpty = _should.prototype.empty;
_should.prototype.haveAttr = _should.prototype.attr;
_should.prototype.hasAttr = _should.prototype.attr;
_should.prototype.haveProperty = _should.prototype.property;
_should.prototype.hasProperty = _should.prototype.property;
_should.prototype.beA = _should.prototype.a;
_should.prototype.beAbove = _should.prototype.above;
_should.prototype.beBelow = _should.prototype.below;
_should.prototype.beEqual = _should.prototype.equal;
_should.prototype.beMatch = _should.prototype.match;
_should.prototype.haveChild = _should.prototype.child;
_should.prototype.hasChild = _should.prototype.child;
_should.prototype.haveParent = _should.prototype.parent;
_should.prototype.hasParent = _should.prototype.parent;
_should.prototype.haveNext = _should.prototype.next;
_should.prototype.hasNext = _should.prototype.next;
_should.prototype.havePrev = _should.prototype.prev;
_should.prototype.hasPrev = _should.prototype.prev;
_should.prototype.beTrues = _should.prototype.beTrue;
_should.prototype.beFalses = _should.prototype.beFalse;
_should.prototype.beVisible = _should.prototype.visible;
_should.prototype.beHide = _should.prototype.hide;
_should.prototype.hasHrefKeys = _should.prototype.haveHrefKeys;
_should.prototype.hasHrefKey = _should.prototype.haveHrefKey;
_should.prototype.hasFormKeys = _should.prototype.haveFormKeys;
_should.prototype.hasFormKey = _should.prototype.haveFormKey;
win.__Case.should = _should;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment