ज्यूनिट में असगे रेगेक्स से मेल खाता है


84

रूबी की Test::Unitएक अच्छी assert_matchesविधि है जिसे इकाई परीक्षणों में इस्तेमाल किया जा सकता है ताकि यह पता लगाया जा सके कि एक रेक्सक्स एक स्ट्रिंग से मेल खाता है।

क्या JUnit में ऐसा कुछ है? वर्तमान में, मैं यह करता हूं:

assertEquals(true, actual.matches(expectedRegex));

जवाबों:


100

यदि आप assertThat()एक हैमरेस्ट मैचर के साथ प्रयोग करते हैं जो रेगेक्स मैचों के लिए परीक्षण करता है, तो यदि दावा विफल हो जाता है तो आपको एक अच्छा संदेश मिलेगा जो अपेक्षित पैटर्न और वास्तविक पाठ को इंगित करता है। अभिकथन अधिक धाराप्रवाह भी पढ़ेगा, उदा

assertThat("FooBarBaz", matchesPattern("^Foo"));

Hamcrest 2 के साथ आप एक matchesPatternविधि पा सकते हैं MatchesPattern.matchesPattern


21
Hamcrest 2.0 में Matchers.matchesPattern(String)अब बिल्ट-इन है: github.com/hamcrest/Java
Hamcrest

5
@HinneLinks को संदर्भित करने के लिए Permalink: github.com/hamcrest/Java
Hamcrest

54

कोई अन्य विकल्प नहीं है जो मुझे पता है। बस सुनिश्चित करने के लिए जोर javadoc की जाँच की । बस एक छोटा सा परिवर्तन, हालांकि:

assertTrue(actual.matches(expectedRegex));

संपादित करें: मैं हैल्क्रेस्ट मैचर्स का उपयोग फोलसर के उत्तर के बाद से कर रहा हूं, यह भी देखें!


1
आह हाँ, assertTrue()निश्चित रूप से अच्छा है। मैं उस बारे में न जानने के लिए एक्लिप्स के ऑटो-कम्प्लीट को दोष देता हूं। ;)
जोश ग्लोवर

4
assertTrueआप के रूप में ज्यादा विस्तार के रूप में नहीं दे सकता assertEqualsया assertThatजब एक परीक्षण में विफल रहता है
माइक Valenty

1
@ मिचेल यकीन है कि यह कर सकते हैं। assertTrue("Expected string matching '" +expectedRegex+ "'. Got: "+actual, actual.matches(expectedRegex));। यह Hamcrest के रूप में हालांकि अच्छा नहीं है।
10

@ मायकेवैलेंटी यदि आप सिर्फ एक मूल्य की तुलना कर रहे हैं is(true), तो assertThatआपको इससे अधिक विवरण नहीं देता assertTrueहै। उचित त्रुटि संदेश प्राप्त करने के लिए, आपको एक अलग मिलानकर्ता की आवश्यकता होती है (या आप संदेश का निर्माण मैन्युअल रूप से @MikeFHay के रूप में करते हैं)।
थ्रॉन्का

20

आप Hamcrest का उपयोग कर सकते हैं, लेकिन आपको अपना खुद का मिलान लिखना होगा:

public class RegexMatcher extends TypeSafeMatcher<String> {

    private final String regex;

    public RegexMatcher(final String regex) {
        this.regex = regex;
    }

    @Override
    public void describeTo(final Description description) {
        description.appendText("matches regex=`" + regex + "`");
    }

    @Override
    public boolean matchesSafely(final String string) {
        return string.matches(regex);
    }


    public static RegexMatcher matchesRegex(final String regex) {
        return new RegexMatcher(regex);
    }
}

प्रयोग

import org.junit.Assert;


Assert.assertThat("test", RegexMatcher.matchesRegex(".*est");

18

आप हैमरेस्ट और जेकाबी-मैचर्स का उपयोग कर सकते हैं :

import static com.jcabi.matchers.RegexMatchers.matchesPattern;
import static org.junit.Assert.assertThat;
assertThat("test", matchesPattern("[a-z]+"));

यहाँ अधिक विवरण: नियमित अभिव्यक्ति हैमरेस्ट मैचर्स

आपको क्लासपाथ में इन दो निर्भरताओं की आवश्यकता होगी:

<dependency>
  <groupId>org.hamcrest</groupId>
  <artifactId>hamcrest-core</artifactId>
  <version>1.3</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>com.jcabi</groupId>
  <artifactId>jcabi-matchers</artifactId>
  <version>1.3</version>
  <scope>test</scope>
</dependency>

मैंने पाया है कि हैमरेस्ट-कोर निर्भरता की आवश्यकता नहीं है
जॉनपी 2

4

क्योंकि मैं भी इस कार्यशीलता की तलाश में था, इसलिए मैंने GitHub पर एक प्रोजेक्ट शुरू किया है जिसे regex-tester कहा जाता है । यह एक पुस्तकालय है जो जावा में नियमित अभिव्यक्ति का परीक्षण करने में आसानी करता है (केवल वर्तमान में JUnit के साथ काम करता है)।

लाइब्रेरी अभी बहुत सीमित है, लेकिन इसमें एक हैमरेस्ट मैचर है जो इस तरह से काम करता है

assertThat("test", doesMatchRegex("tes.+"));
assertThat("test", doesNotMatchRegex("tex.+"));

रेगेक्स-टेस्टर का उपयोग करने के बारे में अधिक जानकारी यहाँ है


4

राल्फ के कार्यान्वयन के समान एक मिलान आधिकारिक जावा हैमरेस्ट मैचर्स लाइब्रेरी में जोड़ा गया है । दुर्भाग्य से, यह अभी तक रिलीज़ पैकेज में उपलब्ध नहीं है। वर्ग GitHub पर है यदि आप एक नज़र चाहते हैं।


3

हैमरेस्ट में संगत मिलानकर्ता है: org.hamcrest.Matchers.matchesPattern (स्ट्रिंग regex)

Hamcrest के विकास के कारण आप नवीनतम उपलब्ध v1.3 का उपयोग नहीं कर सकते हैं:

testCompile("org.hamcrest:hamcrest-library:1.3")

इसके बजाय आपको नई देव श्रृंखला का उपयोग करने की आवश्यकता है (लेकिन जनवरी 2015 तक दिनांकित ):

testCompile("org.hamcrest:java-hamcrest:2.0.0.0")

या इससे भी बेहतर:

configurations {
    testCompile.exclude group: "org.hamcrest", module: "hamcrest-core"
    testCompile.exclude group: "org.hamcrest", module: "hamcrest-library"
}
dependencies {
    testCompile("org.hamcrest:hamcrest-junit:2.0.0.0")
}

परीक्षण में:

Assert.assertThat("123456", Matchers.matchesPattern("^[0-9]+$"));

मुझे त्रुटि मिलती है: डुप्लिकेट क्लास org.hamcrest.BaseDescription मॉड्यूल में पाया जाता है जो जेट-हैमरेस्ट-कोर-1.3.jar (org.hamcrest: हैमरेस्ट-कोर: 1.3) और जेटी-जावा-हैमरेस्ट-2.0.0.0.jar (org.hamcrest) : जावा-हैमरेस्ट: 2.0.0.0)
a_subscriber

2

एक अन्य विकल्प का उपयोग करते हुए। यह दृष्टिकोण अच्छा है क्योंकि यह आपको पैटर्न ऑब्जेक्ट को सीधे पास करने की अनुमति देता है।

import static org.assertj.core.api.Assertions.assertThat;
assertThat("my\nmultiline\nstring").matches(Pattern.compile("(?s)my.*string", Pattern.MULTILINE));

1


यह JUnit नहीं है, लेकिन यहाँ उत्सव-मुखर के साथ एक और तरीका है:

assertThat(myTestedValue).as("your value is so so bad").matches(expectedRegex);
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.