用javascript模拟C#的[Attribute]用法

<!--用js模拟c#的attribute-->
执行结果:<br>
<textarea rows = "10" cols = "100" id = "output"></textarea><br>
<br>
调试信息:<br>
<textarea rows = "10" cols = "100" id = "debug"></textarea><br>

<script language="javascript">
/* 特性(attributes)是一种崭新的声明性信息。
我们不仅可以通过特性来定义设计层面的信息
(例如help file, url for documentation)
以及运行时(run-time)信息(例如使xml与class相联系),
而且我们还可以利用特性建立自描述(self-describing)组件。
*/

function attribute()  /attribute 基类,可以自行定义其中的接口以扩充功能,这里只是一个简单的演示,因此留空
{

}

function testmethod() /定义一个新的attribute类 testmethod,用它来给需要进行单元测试的方法提供额外信息
{
 this.name = "testmethod";
}testmethod.prototype = new attribute();
function testmethodattribute() /必需的执行方法
{
 return new testmethod();
}

function debugoutput(boutput) /定义一个新的attribute类 debugoutput,用它来指示是否在测试中输出额外的调试信息
{
 this.name = "debugoutput";
 this.isallowdebugoutput = boutput;
}debugoutput.prototype = new attribute();
function debugoutputattribute(boutput)  /必需的执行方法
{
 return new debugoutput(boutput);
}

function.__captureattributes = function(obj) 
{
 var attributedef = //[/w+/].*/n.*(?=/=[/s]*function)/g;
 var matches = obj.constructor.tostring().match(attributedef);
 if(matches != null)
 {
  for (var i = 0; i < matches.length; i++)
  {
   var part = matches[i].split(/[/s/n]/);
   var attrlists = part[0].split(",");
   var methodobj = eval(part[part.length-1]);
   methodobj.__attributes = new array();
   methodobj.__attributes.__all = new array();

   for (var j = 0; j < attrlists.length; j++)
   {
    if(!/^.+/(.*/)$/.test(attrlists[j].slice(1,-1)))
    {
     attrlists[j] = "[" + attrlists[j].slice(1,-1) + "()" + "]"; /处理省略括号的情况
    }
    if(!/^.+attribute$/.test(attrlists[j].split("(")[0]))
    {
     attrlists[j] = attrlists[j].split("(")[0] + "attribute" + "(" + attrlists[j].split("(")[1];
    }

    var attrobj = eval(eval(attrlists[j])[0]);
    methodobj.__attributes.__all.push(attrobj);
    methodobj.__attributes[attrlists[j].split("(")[0].replace(/[/[/]]/g,"").replace(/attribute$/g,"")] = attrobj;
    methodobj.__attributes[attrlists[j].split("(")[0].replace(/[/[/]]/g,"")] = attrobj;
   }
  }
 }
}

function unittest()  /单元测试框架,被赋予[testmethod]特性的方法会被作为case执行测试
{
 this.errors = 0;
 this.passed = 0;
 /声明testmethod特性,teststring方法将被runcase方法执行,同时声明了debugoutput特性,将返回的信息输出到调试窗口
 /特性的声明必须放在被指定特性的方法之前,而且要独占一行,如果有多个特性可以以逗号分隔
 /包含特性声明的函数要以";"结尾,不可省略
 [testmethod],[debugoutput(true)]
 unittest.prototype.teststring = function()  /测试字符串方法,这里假设自己实现了一个string类然后来测试
 {
  var testcase = new string();
  testcase = "abc";
  this.test(testcase == "abc");  /测试赋值操作
  testcase += "def";
  this.test(testcase == "abcdef"); /测试连接操作
  this.test(testcase.length == 6); /测试长度属性

  self.output.value += "/n";
  var result = "debug - teststring finished with " + this.passed + " cases passed and " + this.errors + " cases failed!/n";
  this.passed = 0;
  this.errors = 0;
  return result;
 };
 /只测试不输出调试信息的方法
 [testmethod]
 unittest.prototype.testregexp = function()
 {
  var errors = 0;
  var passed = 0;
  if(/abc/.test("abc"))
  {
   self.output.value += ".";
   passed ++;
  }
  else
  {
   self.output.value += "e";
   errors ++;
  }
  if(/abc/.test("aababcd"))
  {
   self.output.value += ".";
   passed ++;
  }
 };
 
 /不被测试的方法
 unittest.prototype.foo = function()
 {
  alert('foo not being tested!');
 };
 unittest.prototype.runcases = function()
 {
  for (each in this)
  {
   if(this[each].__attributes != null && this[each].__attributes["debugoutput"] != null)
   {
    var result = this[each].call(this);
    if(this[each].__attributes["debugoutput"].isallowdebugoutput)
    {
     self.debug.value = result;
    }
   }
   else if(this[each].__attributes != null && this[each].__attributes["testmethod"] != null)
   {
    this[each].call(this);
   }
  }
 };
 unittest.prototype.test = function(cond)
 {
  if(cond)
  {
   self.output.value += ".";
   this.passed ++;
  }
  else
  {
   self.output.value += ".";
   this.errors ++;
  }
 };
 /在类内部捕获attribute对象,必须在使用特性的对象内部声明,这一点同c#还是有区别的
 function.__captureattributes(this);
}

var test = new unittest();
test.runcases();
/或许一些人不太习惯上面的这种做法,但是它有一个显而易见的好处就是我如果希望添加更多的单元测试用例,只需要增加新的标记为[testmethod]的方法,而不用修改runcases方法的任何代码!这样我就可以将整个单元测试框架“封装起来”而依然允许使用者从“外部”添加自己的测试方法!

/除此以外,我们可以用“特性”相当便利地用来实现许多模式,这方面的具体深入用法这里不再详述了,有兴趣的朋友可以自行尝试^^,不过现在这个模拟的“特性”还有一些不足之处,例如只能将特性声明到对象“方法”而不能声明给对象本身,这样要实现一些像serializable之类的对象特性就不太方便了=.=
</script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值