Matches

Aus JACK Wiki
Zur Navigation springen Zur Suche springen

Beschreibung

Die Funktion matches prüft, ob ein String einem gegebenen regulären Ausdruck genügt. Die Funktion erwartet zwei String, die Zeichenkette, die geprüft werden soll, und den regulärer Ausdruck, der als Grundlage zum Prüfen gilt. Es wird ein Boolean zurückgegeben.

Syntax

matches(String zeichenkette, String regExp)

Parameter

  • zeichenkette - zu prüfende Zeichenkette
  • regExp - regulärer Ausdruck

Return Value

  • Gibt einen Boolean zurück

Beispiele

Standardfälle

matches('Abcd0','[A-Za-z0-9]*')   --> returns true

Benutzung von Variablen

   matches('abababa','[var=a]')
   matches('[var=c]','\\d')

JUnit Tests

[Anzeigen]

@BeforeClass
public static void beforeTest() {
	fillInVariableMap.put(7, OMConverter.toObject("<OMOBJ><OMSTR>abababa</OMSTR></OMOBJ>"));
	fillInVariableMap.put(8, OMConverter.toObject("<OMOBJ><OMI>6</OMI></OMOBJ>"));
	fillInVariableMap.put(9, OMConverter.toObject("<OMOBJ><OMI>2</OMI></OMOBJ>"));
		
	exerciseVariableMap.put("g", OMConverter.toObject("<OMOBJ><OMSTR>abababa</OMSTR></OMOBJ>"));
	exerciseVariableMap.put("h", OMConverter.toObject("<OMOBJ><OMI>6</OMI></OMOBJ>"));
	exerciseVariableMap.put("i", OMConverter.toObject("<OMOBJ><OMI>2</OMI></OMOBJ>"));
}

@Test
public void testMatches1() {
	assertTrue(Evaluator.getBooleanResult("matches('abababa','abababa')", exerciseVariableMap, fillInVariableMap));
}

@Test
public void testMatches2() {
	assertTrue(Evaluator.getBooleanResult("matches('abcdefghijklmnopqrstuvwxyz','abcdefghijklmnopqrstuvwxyz')", exerciseVariableMap, fillInVariableMap));
}

@Test
public void testMatches3() {
	assertTrue(Evaluator.getBooleanResult("matches('5', '[0-6]+')", exerciseVariableMap, fillInVariableMap));
}

@Test
public void testMatches4() {
	assertTrue(Evaluator.getBooleanResult("matches('Ab3', '[A-Za-z0-9]+')", exerciseVariableMap, fillInVariableMap));
}

@Test
public void testMatches5() {
	assertTrue(Evaluator.getBooleanResult("matches('2', '\\d')", exerciseVariableMap, fillInVariableMap));
}

@Test
public void testMatches6() {
	assertTrue(!Evaluator.getBooleanResult("matches('abc', '\\d')", exerciseVariableMap, fillInVariableMap));
}

@Test
public void testMatchesAndInput1() {
	assertTrue(Evaluator.getBooleanResult("matches('abababa','[pos=7]')", exerciseVariableMap, fillInVariableMap));
}

@Test
public void testMatchesAndInput2() {
	assertTrue(Evaluator.getBooleanResult("matches('5','[0-[pos=8]]+')", exerciseVariableMap, fillInVariableMap));
}

@Test
public void testMatchesAndInput3() {
	assertTrue(Evaluator.getBooleanResult("matches('[pos=9]', '\\d')", exerciseVariableMap, fillInVariableMap));
}

@Test
public void testMatchesWithVariables1() {
	assertTrue(Evaluator.getBooleanResult("matches('abababa','[var=g]')", exerciseVariableMap, fillInVariableMap));
}

@Test
public void testMatchesWithVariables2() {
	assertTrue(Evaluator.getBooleanResult("matches('5', '[0-[var=h]]+')", exerciseVariableMap, fillInVariableMap));
}

@Test
public void testMatchesWithVariables3() {
	assertTrue(Evaluator.getBooleanResult("matches('[var=i]', '\\d')", exerciseVariableMap, fillInVariableMap));
}

@Test(expected=ParserException.class)
public void testMatchesWithWrongInputCharacter() {
	Evaluator.getNumberResult("matches(ab, ab)", exerciseVariableMap, fillInVariableMap);
	fail();
}

@Test(expected=FunctionInvalidNumberOfArgumentsException.class)
public void testMatchesWithOneArgument() {
	Evaluator.getNumberResult("matches('abc')", exerciseVariableMap, fillInVariableMap);
	fail();
}

@Test(expected=FunctionInvalidNumberOfArgumentsException.class)
public void testmatchesWithThreeArguments() {
	Evaluator.getNumberResult("matches('str', '[i]+', 'string')", exerciseVariableMap, fillInVariableMap);
	fail();
}

@Test(expected=UndefinedExerciseVariableException.class)
public void testMatchesWithMissingExerciseVariable() {
	Evaluator.getNumberResult("matches('[var=j]', '[var=j]')", exerciseVariableMap, fillInVariableMap);
	fail();
}

@Test(expected=UndefinedFillInVariableException.class)
public void testMatchesWithMissingInput() {
	Evaluator.getNumberResult("matches('[pos=42]', '[pos=42]')", exerciseVariableMap, fillInVariableMap);
	fail();
}

Hinweise