Trim: Unterschied zwischen den Versionen

Aus JACK Wiki
Wechseln zu: Navigation, Suche
(JUnit Tests)
Zeile 24: Zeile 24:
 
<syntaxhighlight lang="java" class="mw-collapsible mw-collapsed" id="mw-customcollapsible-myDivision">
 
<syntaxhighlight lang="java" class="mw-collapsible mw-collapsed" id="mw-customcollapsible-myDivision">
 
@Before
 
@Before
public void beforeTest(){
+
public void beforeTest() {
  placeFillInVariable(1, OMParser.parse("hello"));
+
fillInVariableMap.put(7, OMConverter.toObject("<OMOBJ><OMSTR>hello</OMSTR></OMOBJ>"));
  
  placeExerciseVariable("a", OMParser.parse("hello"));
+
exerciseVariableMap.put("g", OMConverter.toObject("<OMOBJ><OMSTR>hello</OMSTR></OMOBJ>"));
 
}
 
}
+
 
@Test public void testTrim(){
+
@Test
  assertTrue("'hello'".equals(getEvaluator().getResultAsEvaluatorString("trim(' hello')")));
+
public void testTrim1() {
  assertTrue("'hello'".equals(getEvaluator().getResultAsEvaluatorString("trim('hello ')")));
+
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("hello")),
  assertTrue("'hello'".equals(getEvaluator().getResultAsEvaluatorString("trim(' hello ')")));
+
Evaluator.evaluate("trim(' hello')", exerciseVariableMap, fillInVariableMap));
  assertTrue("'hel lo'".equals(getEvaluator().getResultAsEvaluatorString("trim(' hel lo ')")));
+
}
 +
 
 +
@Test
 +
public void testTrim2() {
 +
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("hello")),
 +
Evaluator.evaluate("trim('hello ')", exerciseVariableMap, fillInVariableMap));
 +
}
 +
 
 +
@Test
 +
public void testTrim3() {
 +
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("hello")),
 +
Evaluator.evaluate("trim(' hello ')", exerciseVariableMap, fillInVariableMap));
 +
}
 +
 
 +
@Test
 +
public void testTrim4() {
 +
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("hel lo")),
 +
Evaluator.evaluate("trim(' hel lo ')", exerciseVariableMap, fillInVariableMap));
 +
}
 +
 
 +
@Test
 +
public void testTrimWithExpression1() {
 +
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("w o r k")),
 +
Evaluator.evaluate("trim(trim(' w o r k '))", exerciseVariableMap, fillInVariableMap));
 
}
 
}
+
 
 
@Test
 
@Test
public void testTrimWithExpression(){
+
public void testTrimWithExpression2() {
  assertTrue("'w o r k'".equals(getEvaluator().getResultAsEvaluatorString("trim(trim(' w o r k '))")));
+
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("w o r k")),
  assertTrue("'w o r k'".equals(getEvaluator().getResultAsEvaluatorString("trim(trim(trim(' w o r k ')))")));
+
Evaluator.evaluate("trim(trim(trim(' w o r k ')))", exerciseVariableMap, fillInVariableMap));
 
}
 
}
+
 
@Test public void testTrimWithInput(){
+
@Test
  assertTrue("'hello'".equals(getEvaluator().getResultAsEvaluatorString("trim(' [pos=1] ')")));
+
public void testTrimWithInput() {
 +
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("hello")),
 +
Evaluator.evaluate("trim(' [pos=7] ')", exerciseVariableMap, fillInVariableMap));
 
}
 
}
+
 
@Test public void testTrimWithVariables(){
+
@Test
  assertTrue("'hello'".equals(getEvaluator().getResultAsEvaluatorString("trim(' [var=a] ')")));
+
public void testTrimWithVariables() {
 +
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("hello")),
 +
Evaluator.evaluate("trim(' [var=g] ')", exerciseVariableMap, fillInVariableMap));
 
}
 
}
+
 
@Test(expected=InvalidEvaluatorFunctionArgumentException.class)
+
@Test(expected = ParserException.class)
public void testTrimWithWrongInputCharacter(){
+
public void testTrimWithWrongInputCharacter() {
  getEvaluator().getResultAsEvaluatorString("trim(a)");
+
Evaluator.evaluate("trim(ab)", exerciseVariableMap, fillInVariableMap);
 +
fail();
 
}
 
}
+
 
@Test(expected=UnsupportedEvaluatorFunctionException.class)
+
@Test(expected = FunctionInvalidNumberOfArgumentsException.class)
public void testTrimWithTwoArguments(){
+
public void testTrimWithTwoArguments() {
  getEvaluator().getResultAsEvaluatorString("trim(' a', 'a ')");
+
Evaluator.evaluate("trim(' a', 'a ')", exerciseVariableMap, fillInVariableMap);
 +
fail();
 
}
 
}
+
 
@Test(expected=UnsupportedEvaluatorFunctionException.class)
+
@Test(expected = FunctionInvalidNumberOfArgumentsException.class)
public void testTrimWithThreeArguments(){
+
public void testTrimWithThreeArguments() {
  getEvaluator().getResultAsEvaluatorString("trim(' a', 'a ', ' a ')");
+
Evaluator.evaluate("trim(' a', 'a ', ' a ')", exerciseVariableMap, fillInVariableMap);
 +
fail();
 
}
 
}
+
 
@Test(expected=UndefinedExerciseVariableException.class)
+
@Test(expected = UndefinedExerciseVariableException.class)
public void testTrimWithMissingExerciseVariable(){
+
public void testTrimWithMissingExerciseVariable() {
  getEvaluator().getResultAsEvaluatorString("trim('[var=j]')");
+
Evaluator.evaluate("trim('[var=j]')", exerciseVariableMap, fillInVariableMap);
 +
fail();
 
}
 
}
+
 
@Test(expected=UndefinedFillInVariableException.class)
+
@Test(expected = UndefinedFillInVariableException.class)
public void testTrimWithMissingInput(){
+
public void testTrimWithMissingInput() {
  getEvaluator().getResultAsEvaluatorString("trim('[pos=42]')");
+
Evaluator.evaluate("trim('[pos=42]')", exerciseVariableMap, fillInVariableMap);
 +
fail();
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>

Version vom 19. September 2017, 07:48 Uhr

Beschreibung

Die Funktion trim entfernt aus einer Zeichenkette alle am Anfang und am Ende stehende Leerzeichen. Die Funktion erwartet einen String und gibt eine modifizierte Zeichenkette als String zurück.

Syntax

trim(String zeichenkette)

Parameter

  • zeichenkette - die Zeichenkette bei der die am Anfang und Ende befindlichen Leerzeichen entfernt werden sollen

Return Value

  • Gibt einen Stringzurück

Beispiele

Standardfälle

trim('     Leerzeichen wurden entfernt      ')   --> returns 'Leerzeichen wurden entfernt'
trim('   hello')   --> returns 'hello'

Benutzung mit Variablen

trim('[pos=1]')
trim('[var=a]')

JUnit Tests

[Anzeigen]

@Before
public void beforeTest() {
	fillInVariableMap.put(7, OMConverter.toObject("<OMOBJ><OMSTR>hello</OMSTR></OMOBJ>"));

	exerciseVariableMap.put("g", OMConverter.toObject("<OMOBJ><OMSTR>hello</OMSTR></OMOBJ>"));
}

@Test
public void testTrim1() {
	assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("hello")),
			Evaluator.evaluate("trim(' hello')", exerciseVariableMap, fillInVariableMap));
}

@Test
public void testTrim2() {
	assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("hello")),
			Evaluator.evaluate("trim('hello ')", exerciseVariableMap, fillInVariableMap));
}

@Test
public void testTrim3() {
	assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("hello")),
			Evaluator.evaluate("trim(' hello ')", exerciseVariableMap, fillInVariableMap));
}

@Test
public void testTrim4() {
	assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("hel lo")),
			Evaluator.evaluate("trim(' hel lo ')", exerciseVariableMap, fillInVariableMap));
}

@Test
public void testTrimWithExpression1() {
	assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("w o r k")),
			Evaluator.evaluate("trim(trim(' w o r k '))", exerciseVariableMap, fillInVariableMap));
}

@Test
public void testTrimWithExpression2() {
	assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("w o r k")),
			Evaluator.evaluate("trim(trim(trim(' w o r k ')))", exerciseVariableMap, fillInVariableMap));
}

@Test
public void testTrimWithInput() {
	assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("hello")),
			Evaluator.evaluate("trim(' [pos=7] ')", exerciseVariableMap, fillInVariableMap));
}

@Test
public void testTrimWithVariables() {
	assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("hello")),
			Evaluator.evaluate("trim(' [var=g] ')", exerciseVariableMap, fillInVariableMap));
}

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

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

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

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

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

Hinweise

  • Es werden keine Leerzeichen zwischen den Wörtern der Zeichenkette entfernt.
  • Im Moment werden jedoch alle Leerzeichen entfernt!