Junit-Quickcheck:生成与模式匹配的字符串 [英] Junit-Quickcheck: Generate String matching a pattern

查看:228
本文介绍了Junit-Quickcheck:生成与模式匹配的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using pholser's port. I have to generate strings matching a given pattern like \[a-zA-Z0-9\\.\\-\\\\;\\:\\_\\@\\[\\]\\^/\\|\\}\\{]* Length 40.

我将Generator类扩展为:

I extend the Generator class as:

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

重载函数:

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

现在,random具有 nextDouble(),nextInt()等功能,但没有字符串!如何生成与上述模式匹配的随机字符串?

Now, random has functions like nextDouble(), nextInt() but there is nothing for strings! How can I generate random strings matching the above pattern?

推荐答案

查找实现<$ c $的自定义生成器的以下片段c> generate(..)返回与您发布的模式匹配的随机字符串的方法。

Find below snippet for a custom generator which implement the generate(..) method to return a random string matching your posted pattern.

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();
    }
}

编辑一个简单的单位测试以演示 MyCharacterGenerator 类的用法。

edit A simple unit test to demonstrate the usage of the 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\
...

这篇关于Junit-Quickcheck:生成与模式匹配的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆