dart lang中的注释/元数据

哈桑·优素福(Hasan A Yousef)

任何人都可以帮助将下面的JAVA转换为DART ...

我试图理解注释,在DART中找不到足够的文档,并在这里找到了这个JAVA示例尝试将其转换为DART,但失败了:(

Test.java

  package com.mkyong.test.core; 
  import java.lang.annotation.ElementType;
  import java.lang.annotation.Retention;
  import java.lang.annotation.RetentionPolicy;
  import java.lang.annotation.Target;

  @Retention(RetentionPolicy.RUNTIME)
  @Target(ElementType.METHOD) //can use in method only.
  public @interface Test {

      //should ignore this test?
      public boolean enabled() default true;
  }

TesterInfo.java

package com.mkyong.test.core;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE) //on class level
public @interface TesterInfo {

 public enum Priority {
   LOW, MEDIUM, HIGH
}

Priority priority() default Priority.MEDIUM;

String[] tags() default "";

    String createdBy() default "Mkyong";
   String lastModified() default "03/01/2014";

 }

TestExample.java

package com.mkyong.test;

import com.mkyong.test.core.Test;
import com.mkyong.test.core.TesterInfo;
import com.mkyong.test.core.TesterInfo.Priority;

@TesterInfo(
 priority = Priority.HIGH, 
 createdBy = "mkyong.com",  
 tags = {"sales","test" }
 )
 public class TestExample {

@Test
void testA() {
  if (true)
    throw new RuntimeException("This test always failed");
}

@Test(enabled = false)
void testB() {
  if (false)
    throw new RuntimeException("This test always passed");
}

@Test(enabled = true)
void testC() {
  if (10 > 1) {
    // do nothing, this test always passed.
  }
}

}

RunTest.java

package com.mkyong.test;

 import java.lang.annotation.Annotation;
 import java.lang.reflect.Method;

  import com.mkyong.test.core.Test;
  import com.mkyong.test.core.TesterInfo;

  public class RunTest {

  public static void main(String[] args) throws Exception {

  System.out.println("Testing...");

  int passed = 0, failed = 0, count = 0, ignore = 0;

   Class<TestExample> obj = TestExample.class;

   // Process @TesterInfo
   if (obj.isAnnotationPresent(TesterInfo.class)) {

    Annotation annotation = obj.getAnnotation(TesterInfo.class);
    TesterInfo testerInfo = (TesterInfo) annotation;

    System.out.printf("%nPriority :%s", testerInfo.priority());
    System.out.printf("%nCreatedBy :%s", testerInfo.createdBy());
    System.out.printf("%nTags :");

    int tagLength = testerInfo.tags().length;
    for (String tag : testerInfo.tags()) {
        if (tagLength > 1) {
            System.out.print(tag + ", ");
        } else {
            System.out.print(tag);
        }
        tagLength--;
    }

    System.out.printf("%nLastModified :%s%n%n", testerInfo.lastModified());

    }

    // Process @Test
    for (Method method : obj.getDeclaredMethods()) {

    // if method is annotated with @Test
    if (method.isAnnotationPresent(Test.class)) {

        Annotation annotation = method.getAnnotation(Test.class);
        Test test = (Test) annotation;

        // if enabled = true (default)
        if (test.enabled()) {

          try {
            method.invoke(obj.newInstance());
            System.out.printf("%s - Test '%s' - passed %n", ++count, method.getName());
            passed++;
          } catch (Throwable ex) {
            System.out.printf("%s - Test '%s' - failed: %s %n", ++count, method.getName(), ex.getCause());
            failed++;
          }

        } else {
            System.out.printf("%s - Test '%s' - ignored%n", ++count, method.getName());
            ignore++;
        }

    }

   }
    System.out.printf("%nResult : Total : %d, Passed: %d, Failed %d, Ignore %d%n", count, passed, failed, ignore);

    }
 }

上面的输出应该是:

测试中...

优先级:HIGH CreatedBy:mkyong.com标签:销售,测试最后修改:03/01/2014

1-测试'testA'-失败:java.lang.RuntimeException:此测试始终失败2-测试'testC'-通过了3-测试'testB'-被忽略

结果:总数:3,通过:1,失败1,忽略1

贡特·佐赫鲍尔(GünterZöchbauer)

我没有仔细看过代码及其作用,只是注解

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD) //can use in method only.

在Dart中没有等效功能。在dart中,每个常量值都可以在允许注释的每个位置用作注释。

有关在Dart中使用注释的一些问题/答案

查看标签dart-mirrors以了解更多信息。

有关在Dart中编写单元测试的信息https://www.dartlang.org/articles/dart-unit-tests/

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章