Replace: Unterschied zwischen den Versionen

Aus JACK Wiki
Wechseln zu: Navigation, Suche
(JUnit Tests)
Zeile 25: Zeile 25:
 
<span class="mw-customtoggle-myDivision">[Anzeigen]</span>
 
<span class="mw-customtoggle-myDivision">[Anzeigen]</span>
 
<syntaxhighlight lang="java" class="mw-collapsible mw-collapsed" id="mw-customcollapsible-myDivision">
 
<syntaxhighlight lang="java" class="mw-collapsible mw-collapsed" id="mw-customcollapsible-myDivision">
private char cMax = Character.MAX_VALUE;
+
@BeforeClass
private char cMin = Character.MIN_VALUE;
+
public static void beforeTest() {
 
 
@Before
+
fillInVariableMap.put(7, OMConverter.toObject("<OMOBJ><OMSTR>HaveQaQniceQday</OMSTR></OMOBJ>"));
public void beforeTest(){
+
fillInVariableMap.put(8, OMConverter.toObject("<OMOBJ><OMSTR>a</OMSTR></OMOBJ>"));
  placeFillInVariable(1, OMParser.parse("HaveQaQniceQday"));
+
fillInVariableMap.put(9, OMConverter.toObject("<OMOBJ><OMSTR>b</OMSTR></OMOBJ>"));
  placeFillInVariable(2, OMParser.parse("a"));
+
 
  placeFillInVariable(3, OMParser.parse("b"));
+
exerciseVariableMap.put("g", OMConverter.toObject("<OMOBJ><OMSTR>HaveQaQniceQday</OMSTR></OMOBJ>"));
  placeExerciseVariable("a", OMParser.parse("HaveQaQniceQday"));
+
exerciseVariableMap.put("h", OMConverter.toObject("<OMOBJ><OMSTR>a</OMSTR></OMOBJ>"));
  placeExerciseVariable("b", OMParser.parse("a"));
+
exerciseVariableMap.put("i", OMConverter.toObject("<OMOBJ><OMSTR>b</OMSTR></OMOBJ>"));
  placeExerciseVariable("c", OMParser.parse("b"));
+
 +
}
 +
 
 +
@Test
 +
public void testReplace1() {
 +
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("Hello$$World!$")),
 +
Evaluator.evaluate("replace('Hello  World! ', ' ', '$')", exerciseVariableMap, fillInVariableMap));
 +
}
 +
 
 +
@Test
 +
public void testReplace2() {
 +
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("Have a nice day ")),
 +
Evaluator.evaluate("replace('Have a nice day!', '!', ' ')", exerciseVariableMap, fillInVariableMap));
 +
}
 +
 
 +
@Test
 +
public void testReplace3() {
 +
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("1010101")),
 +
Evaluator.evaluate("replace('a0a0a0a', 'a', '1')", exerciseVariableMap, fillInVariableMap));
 +
}
 +
 
 +
@Test
 +
public void testReplace4() {
 +
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("`0`0`0`")),
 +
Evaluator.evaluate("replace('a0a0a0a', 'a', '`')", exerciseVariableMap, fillInVariableMap));
 +
}
 +
 
 +
@Test
 +
public void testReplaceWithInput1() {
 +
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("Have a nice day")),
 +
Evaluator.evaluate("replace('[pos=7]', 'Q', ' ')", exerciseVariableMap, fillInVariableMap));
 +
}
 +
 
 +
@Test
 +
public void testReplaceWithInput2() {
 +
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("H veQ QniceQd y")),
 +
Evaluator.evaluate("replace('[pos=7]', '[pos=8]', ' ')", exerciseVariableMap, fillInVariableMap));
 +
}
 +
 
 +
@Test
 +
public void testReplaceWithInput3() {
 +
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("HbveQbQniceQdby")),
 +
Evaluator.evaluate("replace('[pos=7]', '[pos=8]', '[pos=9]')", exerciseVariableMap, fillInVariableMap));
 +
}
 +
 
 +
@Test
 +
public void testReplaceWithVariables1() {
 +
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("Have a nice day")),
 +
Evaluator.evaluate("replace('[var=g]', 'Q', ' ')", exerciseVariableMap, fillInVariableMap));
 
}
 
}
+
 
 
@Test
 
@Test
public void testReplace(){
+
public void testReplaceWithVariables2() {
  assertTrue("'Hello$$World!$'".equals(getEvaluator().getResultAsEvaluatorString("replace('Hello  World! ', ' ', '$')")));
+
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("H veQ QniceQd y")),
  assertTrue("'Have a nice day '".equals(getEvaluator().getResultAsEvaluatorString("replace('Have a nice day!', '!', ' ')")));
+
Evaluator.evaluate("replace('[var=g]', '[var=h]', ' ')", exerciseVariableMap, fillInVariableMap));
  assertTrue("'1010101'".equals(getEvaluator().getResultAsEvaluatorString("replace('a0a0a0a', 'a', '1'))")));
 
  assertTrue("'`0`0`0`'".equals(getEvaluator().getResultAsEvaluatorString("replace('a0a0a0a', 'a', '`'))")));
 
 
}
 
}
+
 
 
@Test
 
@Test
public void testReplaceWithInput(){
+
public void testReplaceWithVariables3() {
  assertTrue("'Have a nice day'".equals(getEvaluator().getResultAsEvaluatorString("replace('[pos=1]', 'Q', ' ')")));
+
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("HbveQbQniceQdby")),
  assertTrue("'H veQ QniceQd y'".equals(getEvaluator().getResultAsEvaluatorString("replace('[pos=1]', '[pos=2]', ' ')")));
+
Evaluator.evaluate("replace('[var=g]', '[var=h]', '[var=i]')", exerciseVariableMap, fillInVariableMap));
  assertTrue("'HbveQbQniceQdby'".equals(getEvaluator().getResultAsEvaluatorString("replace('[pos=1]', '[pos=2]', '[pos=3]')")));
 
 
}
 
}
+
 
 
@Test
 
@Test
public void testReplaceWithVariables(){
+
public void testReplaceWithCharacterMaxMinValue1() {
  assertTrue("'Have a nice day'".equals(getEvaluator().getResultAsEvaluatorString("replace('[var=a]', 'Q', ' ')")));
+
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("" + cMax + "0")),
  assertTrue("'H veQ QniceQd y'".equals(getEvaluator().getResultAsEvaluatorString("replace('[var=a]', '[var=b]', ' ')")));
+
Evaluator.evaluate("replace('a0', 'a', '" + cMax + "')", exerciseVariableMap, fillInVariableMap));
  assertTrue("'HbveQbQniceQdby'".equals(getEvaluator().getResultAsEvaluatorString("replace('[var=a]', '[var=b]', '[var=c]')")));
 
 
}
 
}
+
 
 
@Test
 
@Test
public void testReplaceWithCharacterMaxMinValue(){
+
public void testReplaceWithCharacterMaxMinValue2() {
  assertTrue(("'" + cMax + "0'").equals(getEvaluator().getResultAsEvaluatorString("replace('a0', 'a', '" + cMax + "'))")));
+
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("" + cMin + "0")),
  assertTrue(("'" + cMin + "0'").equals(getEvaluator().getResultAsEvaluatorString("replace('a0', 'a', '" + cMin + "'))")));
+
Evaluator.evaluate("replace('a0', 'a', '" + cMin + "')", exerciseVariableMap, fillInVariableMap));
 
}
 
}
+
 
@Test(expected=InvalidEvaluatorFunctionArgumentException.class)
+
@Test
public void testReplaceWithWrongInputSecondArgumentEmpty(){
+
public void testReplaceWithWrongInputSecondArgumentEmpty() {
  getEvaluator().getResultAsEvaluatorString("replace('a', '', 'a')");
+
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("aaaaa")),
 +
Evaluator.evaluate("replace('aa', '', 'a')", exerciseVariableMap, fillInVariableMap));
 
}
 
}
+
 
@Test(expected=InvalidEvaluatorFunctionArgumentException.class)
+
@Test
public void tesReplaceWithWrongInputThirdArgumentEmpty(){
+
public void testReplaceWithThirdArgumentEmpty() {
  getEvaluator().getResultAsEvaluatorString("replace('a ', ' ', '')");
+
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("a")),
 +
Evaluator.evaluate("replace('a ', ' ', '')", exerciseVariableMap, fillInVariableMap));
 
}
 
}
+
 
@Test(expected=InvalidEvaluatorFunctionArgumentException.class)
+
@Test(expected = ParserException.class)
public void testReplaceWithWrongInputCharacter(){
+
public void testReplaceWithWrongInputCharacter() {
  getEvaluator().getResultAsEvaluatorString("replace(a, a, a)");
+
Evaluator.evaluate("replace(ab, ab, ab)", exerciseVariableMap, fillInVariableMap);
 +
fail();
 
}
 
}
+
 
@Test(expected=UnsupportedEvaluatorFunctionException.class)
+
@Test(expected = FunctionInvalidNumberOfArgumentsException.class)
public void testReplaceWithOneArgument(){
+
public void testReplaceWithOneArgument() {
  getEvaluator().getResultAsEvaluatorString("replace('a')");
+
Evaluator.evaluate("replace('a')", exerciseVariableMap, fillInVariableMap);
 +
fail();
 
}
 
}
+
 
@Test(expected=UnsupportedEvaluatorFunctionException.class)
+
@Test(expected = FunctionInvalidNumberOfArgumentsException.class)
public void testReplaceWithTwoArguments(){
+
public void testReplaceWithTwoArguments() {
  getEvaluator().getResultAsEvaluatorString("replace('a', 'b')");
+
Evaluator.evaluate("replace('a', 'b')", exerciseVariableMap, fillInVariableMap);
 +
fail();
 
}
 
}
+
 
@Test(expected=UndefinedExerciseVariableException.class)
+
@Test(expected = UndefinedExerciseVariableException.class)
public void testReplaceWithMissingExerciseVariable(){
+
public void testReplaceWithMissingExerciseVariable() {
  getEvaluator().getResultAsEvaluatorString("replace('[var=j]', '[var=j]', '[var=j]')");
+
Evaluator.evaluate("replace('[var=j]', '[var=j]', '[var=j]')", exerciseVariableMap, fillInVariableMap);
 +
fail();
 
}
 
}
+
 
@Test(expected=UndefinedFillInVariableException.class)
+
@Test(expected = UndefinedFillInVariableException.class)
public void testReplaceWithMissingInput(){
+
public void testReplaceWithMissingInput() {
  getEvaluator().getResultAsEvaluatorString("replace('[pos=42]', '[pos=42]', '[pos=42]')");
+
Evaluator.evaluate("replace('[pos=42]', '[pos=42]', '[pos=42]')", exerciseVariableMap, fillInVariableMap);
 +
fail();
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 
=== Hinweise ===
 
=== Hinweise ===
  
 
[[Kategorie:Evaluatorfunktion]]
 
[[Kategorie:Evaluatorfunktion]]

Version vom 19. September 2017, 07:39 Uhr

Beschreibung

Die Funktion replace ersetzt alle entsprechenden Zeichen einer Zeichenkette durch ein angegebenes Zeichen. Die Funktion benötigt eine Zeichenkette, ein zu ersetzendes Zeichen und ein Zeichen, durch das alle zu ersetzende Zeichen in der Zeichenkette ausgetauscht werden.

Syntax

replace(String zeichenkette, Char zeichen, Char austauschZeichen)

Parameter

  • zeichenkette - die Zeichenkette, in der Zeichen ausgetausch werden sollen
  • zeichen - das Zeichen, das ausgetauscht werden soll
  • austauschZeichen - das Zeichen, durch das die anderen ausgetauscht werden.

Return Value

  • Gibt einen String zurück

Beispiele

Standardfälle

replace('Hello  World! ',' ','$')   --> returns 'Hello$$World!$' 
replace('Have a!nice day!','!',' ')   --> returns 'Have a nice day '

Benutzung mit Variablen

repalce('[pos=1]','a','t')
replace('Hallo','[var=a]','a')

JUnit Tests

[Anzeigen]

@BeforeClass
public static void beforeTest() {
	
		fillInVariableMap.put(7, OMConverter.toObject("<OMOBJ><OMSTR>HaveQaQniceQday</OMSTR></OMOBJ>"));
		fillInVariableMap.put(8, OMConverter.toObject("<OMOBJ><OMSTR>a</OMSTR></OMOBJ>"));
		fillInVariableMap.put(9, OMConverter.toObject("<OMOBJ><OMSTR>b</OMSTR></OMOBJ>"));

		exerciseVariableMap.put("g", OMConverter.toObject("<OMOBJ><OMSTR>HaveQaQniceQday</OMSTR></OMOBJ>"));
		exerciseVariableMap.put("h", OMConverter.toObject("<OMOBJ><OMSTR>a</OMSTR></OMOBJ>"));
		exerciseVariableMap.put("i", OMConverter.toObject("<OMOBJ><OMSTR>b</OMSTR></OMOBJ>"));
	
}

@Test
public void testReplace1() {
	assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("Hello$$World!$")),
			Evaluator.evaluate("replace('Hello  World! ', ' ', '$')", exerciseVariableMap, fillInVariableMap));
}

@Test
public void testReplace2() {
	assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("Have a nice day ")),
			Evaluator.evaluate("replace('Have a nice day!', '!', ' ')", exerciseVariableMap, fillInVariableMap));
}

@Test
public void testReplace3() {
	assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("1010101")),
			Evaluator.evaluate("replace('a0a0a0a', 'a', '1')", exerciseVariableMap, fillInVariableMap));
}

@Test
public void testReplace4() {
	assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("`0`0`0`")),
			Evaluator.evaluate("replace('a0a0a0a', 'a', '`')", exerciseVariableMap, fillInVariableMap));
}

@Test
public void testReplaceWithInput1() {
	assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("Have a nice day")),
			Evaluator.evaluate("replace('[pos=7]', 'Q', ' ')", exerciseVariableMap, fillInVariableMap));
}

@Test
public void testReplaceWithInput2() {
	assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("H veQ QniceQd y")),
			Evaluator.evaluate("replace('[pos=7]', '[pos=8]', ' ')", exerciseVariableMap, fillInVariableMap));
}

@Test
public void testReplaceWithInput3() {
	assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("HbveQbQniceQdby")),
			Evaluator.evaluate("replace('[pos=7]', '[pos=8]', '[pos=9]')", exerciseVariableMap, fillInVariableMap));
}

@Test
public void testReplaceWithVariables1() {
	assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("Have a nice day")),
			Evaluator.evaluate("replace('[var=g]', 'Q', ' ')", exerciseVariableMap, fillInVariableMap));
}

@Test
public void testReplaceWithVariables2() {
	assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("H veQ QniceQd y")),
			Evaluator.evaluate("replace('[var=g]', '[var=h]', ' ')", exerciseVariableMap, fillInVariableMap));
}

@Test
public void testReplaceWithVariables3() {
	assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("HbveQbQniceQdby")),
			Evaluator.evaluate("replace('[var=g]', '[var=h]', '[var=i]')", exerciseVariableMap, fillInVariableMap));
}

@Test
public void testReplaceWithCharacterMaxMinValue1() {
	assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("" + cMax + "0")),
			Evaluator.evaluate("replace('a0', 'a', '" + cMax + "')", exerciseVariableMap, fillInVariableMap));
}

@Test
public void testReplaceWithCharacterMaxMinValue2() {
	assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("" + cMin + "0")),
			Evaluator.evaluate("replace('a0', 'a', '" + cMin + "')", exerciseVariableMap, fillInVariableMap));
}

@Test
public void testReplaceWithWrongInputSecondArgumentEmpty() {
	assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("aaaaa")),
			Evaluator.evaluate("replace('aa', '', 'a')", exerciseVariableMap, fillInVariableMap));
}

@Test
public void testReplaceWithThirdArgumentEmpty() {
	assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("a")),
			Evaluator.evaluate("replace('a ', ' ', '')", exerciseVariableMap, fillInVariableMap));
}

@Test(expected = ParserException.class)
public void testReplaceWithWrongInputCharacter() {
	Evaluator.evaluate("replace(ab, ab, ab)", exerciseVariableMap, fillInVariableMap);
	fail();
}

@Test(expected = FunctionInvalidNumberOfArgumentsException.class)
public void testReplaceWithOneArgument() {
	Evaluator.evaluate("replace('a')", exerciseVariableMap, fillInVariableMap);
	fail();
}

@Test(expected = FunctionInvalidNumberOfArgumentsException.class)
public void testReplaceWithTwoArguments() {
	Evaluator.evaluate("replace('a', 'b')", exerciseVariableMap, fillInVariableMap);
	fail();
}

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

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

Hinweise