0
public boolean accept(File directory, String fileName) {
    boolean fileOK = true;

    if (name != null) {
        fileOK &= fileName.startsWith(name);
    }

    if (pattern != null) {
        fileOK &= Pattern.matches(regex, fileName);
    }

    if (extension != null) {
        fileOK &= fileName.endsWith('.' + extension);
    }
    return fileOK;
}
4

2 回答 2

5

下面是另一种写法。我使用了数据驱动的方法,因为您必须测试多个场景(方法中有多个 if)

def "should accept valid filenames"() {
    expect:
    foobar.accept(new File("/tmp"), fileName)

    where:
    fileName << ["valid_filename_1", "valid_filename_2", "valid_filename_n"]
}
于 2012-05-14T20:44:38.710 回答
1

是的!

def "file should be valid"() {
    setup:
        def dir = new File("/tmp")
        def fileName = "foo.bar"

    when:
        boolean valid = foobar.accept(dir, fileName)

    then:
        valid
} 
于 2012-05-14T20:24:45.007 回答