Junit-Quickcheck:生成与模式匹配的字符串

Junit-Quickcheck: Generate String matching a pattern

我正在使用 pholser 的端口。我必须生成与给定模式匹配的字符串,例如 \[a-zA-Z0-9\.\-\\;\:\_\@\[\]\^/\|\}\{]* Length 40.

我将生成器 class 扩展为:

public class InputGenerator extends Generator<TestData> {...}

它重载了一个函数:

publicTestData generate(SourceOfRandomness random, GenerationStatus status) {...}

现在,random 有像 nextDouble()、nextInt() 这样的函数,但没有字符串!如何生成与上述模式匹配的随机字符串?

在下面找到自定义生成器的代码段,该生成器将 generate(..) 方法实现为 return 匹配您发布的模式的随机字符串。

public class MyCharacterGenerator extends Generator<String> {

    private static final String LOWERCASE_CHARS = "abcdefghijklmnopqrstuvwxyz";
    private static final String UPPERCASE_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private static final String NUMBERS = "0123456789";
    private static final String SPECIAL_CHARS = ".-\;:_@[]^/|}{";
    private static final String ALL_MY_CHARS = LOWERCASE_CHARS
            + UPPERCASE_CHARS + NUMBERS + SPECIAL_CHARS;
    public static final int CAPACITY = 40;

    public MyCharacterGenerator () {
        super(String.class);
    }

    @Override
    public String generate(SourceOfRandomness random, GenerationStatus status) {
        StringBuilder sb = new StringBuilder(CAPACITY);
        for (int i = 0; i < CAPACITY; i++) {
            int randomIndex = random.nextInt(ALL_MY_CHARS.length());
            sb.append(ALL_MY_CHARS.charAt(randomIndex));
        }
        return sb.toString();
    }
}

edit 演示 MyCharacterGenerator class.

用法的简单单元测试
import com.pholser.junit.quickcheck.ForAll;
import com.pholser.junit.quickcheck.From;
import static org.junit.Assert.assertTrue;
import org.junit.contrib.theories.Theories;
import org.junit.contrib.theories.Theory;
import org.junit.runner.RunWith;

@RunWith(Theories.class)
public class MyCharacterGeneratorTest {

    @Theory
    public void shouldHold(@ForAll @From(MyCharacterGenerator.class) String s) {
        // here you should add your unit test which uses the generated output
        // 
        // assertTrue(doMyUnitTest(s) == expectedResult);

        // the below lines only for demonstration and currently
        // check that the generated random has the expected
        // length and matches the expected pattern
        System.out.println("shouldHold(): " + s);
        assertTrue(s.length() == MyCharacterGenerator.CAPACITY);
        assertTrue(s.matches("[a-zA-Z0-9.\-\\;:_@\[\]^/|}{]*"));
    }
}

示例输出shouldHold

生成
shouldHold(): MD}o/LAkW/hbJVWPGdI;:RHpwo_T.lGs^DOFwu2.
shouldHold(): IT_O{8Umhkz{@PY:pmK6}Cb[Wc19GqGZjWVa@4li
shouldHold(): KQwpEz.CW28vy_/WJR3Lx2.tRC6uLIjOTQtYP/VR
shouldHold(): pc2_T4hLdZpK78UfcVmU\RTe9WaJBSGJ}5v@z[Z\
...

没有random.nextString(),但是在junit-quickcheck-generators 库中有一种生成随机字符串的方法。您可以在使用 gen().type(String.class) 创建新生成器时访问它。但是,我们似乎没有太多控制权。

这是一个 StringBuilder 生成器的愚蠢示例,用于演示如何使用 String 生成器:

import com.pholser.junit.quickcheck.generator.GenerationStatus;
import com.pholser.junit.quickcheck.generator.Generator;
import com.pholser.junit.quickcheck.random.SourceOfRandomness;

public class StringBuilderGenerator extends Generator<StringBuilder> {

    public StringBuilderGenerator() {
        super(StringBuilder.class);
    }

    @Override
    public StringBuilder generate(SourceOfRandomness random, GenerationStatus status) {
        String s = gen().type(String.class).generate(random, status);
        return new StringBuilder(s);
    }
}