Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JUnit中文化的原型验证 #10

Closed
nobodxbodon opened this issue Aug 1, 2017 · 13 comments
Closed

JUnit中文化的原型验证 #10

nobodxbodon opened this issue Aug 1, 2017 · 13 comments
Assignees

Comments

@nobodxbodon
Copy link
Member

#9 提到的JUnit中文化, 下面是个pretotype:

  @Test
  public void test() {
    相等("1=1", 1, 1);
    不等("1!=2", 1, 2);
  }

与中文化前对比:

  @Test
  public void test() {
    assertEquals("1=1", 1, 1);
    assertNotEquals("1!=2", 1, 2);
  }

实现如下:

package com.github.programinchinese;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;

public class 断言 {
  public static void 相等(String 不等反馈信息, Object 期望, Object 实际) {
    assertEquals(不等反馈信息, 期望, 实际);
  }

  public static void 不等(String 不等反馈信息, Object 期望, Object 实际) {
    assertNotEquals(不等反馈信息, 期望, 实际);
  }
}

个人感觉用相对简单的实现, 达到了一定的可读性改进, 还是有意义的. 如果没有意见, 就打算照此思路开始项目了.

@nobodxbodon nobodxbodon self-assigned this Aug 1, 2017
@ice1000
Copy link

ice1000 commented Aug 1, 2017

你为啥不把@Test改成中文

@nobodxbodon
Copy link
Member Author

还没来得及看怎么改. 请问你有思路吗?

@ice1000
Copy link

ice1000 commented Aug 1, 2017

In Kotlin we have

typealias CESHI = @org.junit.Test

or something else. I'm no so sure.

I don't know if there's something that can do the same job in Java as the Kotlin code above.

@nobodxbodon
Copy link
Member Author

能提供一个kotlin里重命名annotation的实例吗? 刚搜了一下没找到.
这里是在Java里定义annotation和使用的例子, 粗看一下觉得如果要实现 @Test->@测试, 而且JUnit框架还要能识别的话(比如说Eclipse里的JUnit相关功能都不影响), 好像需要修改JUnit的源代码或者JUnit插件的源码. 还没有想出单纯靠第三方库实现的方法.

@nobodxbodon
Copy link
Member Author

第三方库的一个缺点, Trace信息里会多一层:
screen shot 2017-08-01 at 10 36 50 pm

打算先把Assert方法汉化之后先在汇编器项目里实验一下.

@ice1000
Copy link

ice1000 commented Aug 2, 2017

I've already mentioned the way that can add an alias to an existing name.

@ice1000
Copy link

ice1000 commented Aug 2, 2017

But as far as I'm concerned, all people here know how to declare an annotation. It's commonsense IMO

@nobodxbodon
Copy link
Member Author

暂时没发现Java的类似方法.

@ice1000
Copy link

ice1000 commented Aug 2, 2017

我刚提交了个 commit ,那个 Kotlin 的,我已经告诉你咋整了。另外那个测试方法在 IntelliJ IDEA 中被成功识别。

@nobodxbodon
Copy link
Member Author

我刚提交了个 commit ,那个 Kotlin 的,我已经告诉你咋整了。另外那个测试方法在 IntelliJ IDEA 中被成功识别。

看到了, 多谢. 可惜Java没有这个原生特性. 你有兴趣实现一个中文化的Kotlin测试框架吗?

@azige
Copy link

azige commented Aug 7, 2017

关于 @Test -> @测试 的问题,扩展一下Runner如何,像这样

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface 测试{

    Class 期待异常() default org.junit.Test.None.class;

    long 超时() default 0L;
}

public class CustomRunner extends org.junit.runners.BlockJUnit4ClassRunner{

    public CustomRunner(Class klass) throws InitializationError{
        super(klass);
    }

    @Override
    protected List computeTestMethods(){
        return getTestClass().getAnnotatedMethods(测试.class);
    }

    @Override
    protected void validateTestMethods(List errors){
        validatePublicVoidNoArgMethods(测试.class, false, errors);
    }

    @Override
    protected Statement possiblyExpectingExceptions(FrameworkMethod method, Object test, Statement next){
        测试 annotation = method.getAnnotation(测试.class);
        return expectsException(annotation) ? new ExpectException(next,
            getExpectedException(annotation)) : next;
    }

    @Override
    protected Statement withPotentialTimeout(FrameworkMethod method,
        Object test, Statement next){
        long timeout = getTimeout(method.getAnnotation(测试.class));
        return timeout > 0 ? new FailOnTimeout(next, timeout) : next;
    }

    private Class getExpectedException(测试 annotation){
        if (annotation == null || annotation.期待异常() == None.class){
            return null;
        }else{
            return annotation.期待异常();
        }
    }

    private boolean expectsException(测试 annotation){
        return getExpectedException(annotation) != null;
    }

    private long getTimeout(测试 annotation){
        if (annotation == null){
            return 0;
        }
        return annotation.超时();
    }
}

然后在测试类前加@RunWith(CustomRunner.class)就能用了(感觉问题又变成如何汉化这部分了)
NB里收集测试信息是正常的,别的IDE不知道会不会有影响

@nobodxbodon
Copy link
Member Author

多谢! 一会细看. 感觉可以"立项"了, 新开一个repo继续 :)

@nobodxbodon
Copy link
Member Author

多谢各位提点. 已开https://github.com/program-in-chinese/junit4_in_chinese, 欢迎在那里继续探讨.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants