Substring: Unterschied zwischen den Versionen

Aus JACK Wiki
Wechseln zu: Navigation, Suche
(JUnit Tests)
Zeile 26: Zeile 26:
 
<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">
@Before
+
@BeforeClass
public void beforeTest(){
+
public static void beforeTest() {
  placeFillInVariable(10, OMParser.parse("helloworld"));
+
  placeFillInVariable(0, OMParser.parse("0"));
+
fillInVariableMap.put(7, OMConverter.toObject("<OMOBJ><OMSTR>helloworld</OMSTR></OMOBJ>"));
  placeFillInVariable(5, OMParser.parse("5"));
+
fillInVariableMap.put(8, OMConverter.toObject("<OMOBJ><OMI>0</OMI></OMOBJ>"));
 
+
fillInVariableMap.put(9, OMConverter.toObject("<OMOBJ><OMI>5</OMI></OMOBJ>"));
  placeExerciseVariable("suba", OMParser.parse("helloworld"));
+
 
  placeExerciseVariable("a", OMParser.parse("0"));
+
exerciseVariableMap.put("suba", OMConverter.toObject("<OMOBJ><OMSTR>helloworld</OMSTR></OMOBJ>"));
  placeExerciseVariable("b", OMParser.parse("5"));
+
exerciseVariableMap.put("g", OMConverter.toObject("<OMOBJ><OMI>0</OMI></OMOBJ>"));
 +
exerciseVariableMap.put("h", OMConverter.toObject("<OMOBJ><OMI>5</OMI></OMOBJ>"));
 +
 +
}
 +
 
 +
@Test
 +
public void testSubstring1() {
 +
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("hello")),
 +
Evaluator.evaluate("substring('hello world', 0, 5)", exerciseVariableMap, fillInVariableMap));
 +
}
 +
 
 +
@Test
 +
public void testSubstring2() {
 +
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("world")),
 +
Evaluator.evaluate("substring('hello world', 6, 11)", exerciseVariableMap, fillInVariableMap));
 +
}
 +
 
 +
@Test
 +
public void testSubstring3() {
 +
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR(" ")),
 +
Evaluator.evaluate("substring('hello world', 5, 6)", exerciseVariableMap, fillInVariableMap));
 +
}
 +
 
 +
@Test
 +
public void testSubstring4() {
 +
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("a")),
 +
Evaluator.evaluate("substring('abcde', 0, 1)", exerciseVariableMap, fillInVariableMap));
 +
}
 +
 
 +
@Test
 +
public void testSubstring5() {
 +
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("e")),
 +
Evaluator.evaluate("substring('abcde', 4, 5)", exerciseVariableMap, fillInVariableMap));
 +
}
 +
 
 +
@Test
 +
public void testSubstring6() {
 +
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("")),
 +
Evaluator.evaluate("substring('abcde', 4, 4)", exerciseVariableMap, fillInVariableMap));
 +
}
 +
 
 +
@Test
 +
public void testSubstring7() {
 +
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("e")),
 +
Evaluator.evaluate("substring('abcde', 4.0, 5)", exerciseVariableMap, fillInVariableMap));
 +
}
 +
 
 +
@Test
 +
public void testSubstringWithInput1() {
 +
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("orld")),
 +
Evaluator.evaluate("substring('[pos=7]', 6, 10)", exerciseVariableMap, fillInVariableMap));
 
}
 
}
+
 
@Test public void testSubstring(){
+
@Test
  assertTrue("'hello'".equals(getEvaluator().getResultAsEvaluatorString("substring('hello world', 0, 5)")));
+
public void testSubstringWithInput2() {
  assertTrue("'world'".equals(getEvaluator().getResultAsEvaluatorString("substring('hello world', 6, 11)")));
+
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("a")),
  assertTrue("' '".equals(getEvaluator().getResultAsEvaluatorString("substring('hello world', 5, 6)")));
+
Evaluator.evaluate("substring('abcde', [pos=8], 1)", exerciseVariableMap, fillInVariableMap));
  assertTrue("'a'".equals(getEvaluator().getResultAsEvaluatorString("substring('abcde', 0, 1)")));
+
}
  assertTrue("'e'".equals(getEvaluator().getResultAsEvaluatorString("substring('abcde', 4, 5)")));
+
 
  assertTrue("''".equals(getEvaluator().getResultAsEvaluatorString("substring('abcde', 4, 4)")));
+
@Test
  assertTrue("'e'".equals(getEvaluator().getResultAsEvaluatorString("substring('abcde', 4.0, 5)")));
+
public void testSubstringWithInput3() {
 +
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("e")),
 +
Evaluator.evaluate("substring('abcde', 4, [pos=9])", exerciseVariableMap, fillInVariableMap));
 +
}
 +
 
 +
@Test
 +
public void testSubstringWithVariables1() {
 +
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("orld")),
 +
Evaluator.evaluate("substring('[var=suba]', 6, 10)", exerciseVariableMap, fillInVariableMap));
 
}
 
}
+
 
@Test public void testSubstringWithInput(){
+
@Test
  assertTrue("'orld'".equals(getEvaluator().getResultAsEvaluatorString("substring('[pos=10]', 6, 10)")));
+
public void testSubstringWithVariables2() {
  assertTrue("'a'".equals(getEvaluator().getResultAsEvaluatorString("substring('abcde', [pos=0], 1)")));
+
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("a")),
  assertTrue("'e'".equals(getEvaluator().getResultAsEvaluatorString("substring('abcde', 4, [pos=5])")));
+
Evaluator.evaluate("substring('abcde', [var=g], 1)", exerciseVariableMap, fillInVariableMap));
 
}
 
}
+
 
@Test public void testSubstringWithVariables(){
+
@Test
  assertTrue("'orld'".equals(getEvaluator().getResultAsEvaluatorString("substring('[var=suba]', 6, 10)")));
+
public void testSubstringWithVariables3() {
  assertTrue("'a'".equals(getEvaluator().getResultAsEvaluatorString("substring('abcde', [var=a], 1)")));
+
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("e")),
  assertTrue("'e'".equals(getEvaluator().getResultAsEvaluatorString("substring('abcde', 4, [var=b])")));
+
Evaluator.evaluate("substring('abcde', 4, [var=h])", exerciseVariableMap, fillInVariableMap));
 
}
 
}
+
 
@Test(expected =InvalidEvaluatorFunctionArgumentException.class)
+
@Test(expected = FunctionInvalidArgumentException.class)
public void testSubstringWithWrongInputSecondAndThirdArgument(){
+
public void testSubstringWithWrongInputSecondAndThirdArgument() {
  getEvaluator().getResultAsEvaluatorString("substring('hello', -1, 6)");
+
Evaluator.evaluate("substring('hello', -1, 6)", exerciseVariableMap, fillInVariableMap);
 +
fail();
 
}
 
}
+
 
@Test(expected =InvalidEvaluatorFunctionArgumentException.class)
+
@Test(expected = FunctionInvalidArgumentTypeException.class)
public void testSubstringWithWrongInputSecondArgumentAsPointNumber(){
+
public void testSubstringWithWrongInputSecondArgumentAsPointNumber() {
  getEvaluator().getResultAsEvaluatorString("substring('hello', 1.3, 2)");
+
OMSTR result = Evaluator.evaluate("substring('hello', 1.3, 2)", exerciseVariableMap, fillInVariableMap).getOMSTR();
 +
assertEquals("<OMSTR>e</OMSTR>",result.toString());
 +
 
 
}
 
}
+
 
@Test(expected =InvalidEvaluatorFunctionArgumentException.class)
+
@Test(expected = FunctionInvalidArgumentTypeException.class)
public void testSubstringWithWrongInputThirdArgumentAsPointNumber(){
+
public void testSubstringWithWrongInputThirdArgumentAsPointNumber() {
  getEvaluator().getResultAsEvaluatorString("substring('hello', 3, 1.5)");
+
OMSTR result = Evaluator.evaluate("substring('hello', 1, 2.9)", exerciseVariableMap, fillInVariableMap).getOMSTR();
 +
assertEquals("<OMSTR>e</OMSTR>",result.toString());
 
}
 
}
+
 
@Test(expected=InvalidEvaluatorFunctionArgumentException.class)
+
@Test(expected = ParserException.class)
public void testSubstringWithWrongInputCharacter(){
+
public void testSubstringWithWrongInputCharacter() {
  getEvaluator().getResultAsEvaluatorString("substring(a, a, a)");
+
Evaluator.evaluate("substring(ab, ab, ab)", exerciseVariableMap, fillInVariableMap);
 +
fail();
 
}
 
}
+
 
@Test(expected=UnsupportedEvaluatorFunctionException.class)
+
@Test(expected = FunctionInvalidNumberOfArgumentsException.class)
public void testSubstringWithOneArgument(){
+
public void testSubstringWithOneArgument() {
  getEvaluator().getResultAsEvaluatorString("substring('hello')");
+
Evaluator.evaluate("substring('hello')", exerciseVariableMap, fillInVariableMap);
 +
fail();
 
}
 
}
+
 
@Test(expected=UnsupportedEvaluatorFunctionException.class)
+
@Test
public void testSubstringWithTwoArguments(){
+
public void testSubstringWithTwoArguments() {
  getEvaluator().getResultAsEvaluatorString("substring('hello', 2)");
+
assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("llo")),
 +
Evaluator.evaluate("substring('hello', 2)", exerciseVariableMap, fillInVariableMap));
 
}
 
}
+
 
@Test(expected=UndefinedExerciseVariableException.class)
+
@Test(expected = UndefinedExerciseVariableException.class)
public void testSubstringWithMissingExerciseVariable(){
+
public void testSubstringWithMissingExerciseVariable() {
  getEvaluator().getResultAsEvaluatorString("substring('[var=j]', '[var=j]', '[var=j]')");
+
Evaluator.evaluate("substring('[var=j]', '[var=j]', '[var=j]')", exerciseVariableMap, fillInVariableMap);
 +
fail();
 
}
 
}
+
 
@Test(expected=UndefinedFillInVariableException.class)
+
@Test(expected = UndefinedFillInVariableException.class)
public void testSubstringWithMissingInput(){
+
public void testSubstringWithMissingInput() {
  getEvaluator().getResultAsEvaluatorString("substring('[pos=42]', '[pos=42]', '[pos=42]')");
+
Evaluator.evaluate("substring('[pos=42]', '[pos=42]', '[pos=42]')", exerciseVariableMap, fillInVariableMap);
 +
fail();
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>

Version vom 19. September 2017, 07:43 Uhr

Beschreibung

Die Funktion Substring gibt anhand des Anfangs- und der Endposition eine Zeichenkette aus einer übergebenen Zeichenkette zurück. Die Funktion erwartet einen Zeichenkette aus der eine Zeichenkette extrahiert werden soll und jeweils eine Zahl für die Anfangs- und die Endposition. Die Werte der Positionen dürfen nicht geringer als 0 und nicht größer als die Länge der übergebenen Zeichenkette sein. Es wird die extrahierte Zeichenkette zurück gegeben.

Syntax

substring(String zeichenkette, int anfangsPosition, int endPosition)

Parameter

  • zeichenkette - die Zeichenkette aus der eine Zeichenkette extrahiert werde soll.
  • anfangsPosition - die Position des ersten Zeichens in der Zeichenkette, ab dem extrahiert werden soll.
  • endPosition - die Position+1 des letzten Zeichen, bis zu dem extrahiert werden soll.


Return Value

  • Gibt einen String zurück

Beispiele

Standardfälle

substring('hello world',0,5)   --> returns 'hello'
substring('hello world',6,11)   --> returns 'world'

Benutzung mit Variablen

substring('[pos=1]',0,5)
substring('[var=a]',6,11)

JUnit Tests

[Anzeigen]

@BeforeClass
public static void beforeTest() {
	
		fillInVariableMap.put(7, OMConverter.toObject("<OMOBJ><OMSTR>helloworld</OMSTR></OMOBJ>"));
		fillInVariableMap.put(8, OMConverter.toObject("<OMOBJ><OMI>0</OMI></OMOBJ>"));
		fillInVariableMap.put(9, OMConverter.toObject("<OMOBJ><OMI>5</OMI></OMOBJ>"));

		exerciseVariableMap.put("suba", OMConverter.toObject("<OMOBJ><OMSTR>helloworld</OMSTR></OMOBJ>"));
		exerciseVariableMap.put("g", OMConverter.toObject("<OMOBJ><OMI>0</OMI></OMOBJ>"));
		exerciseVariableMap.put("h", OMConverter.toObject("<OMOBJ><OMI>5</OMI></OMOBJ>"));
	
}

@Test
public void testSubstring1() {
	assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("hello")),
			Evaluator.evaluate("substring('hello world', 0, 5)", exerciseVariableMap, fillInVariableMap));
}

@Test
public void testSubstring2() {
	assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("world")),
			Evaluator.evaluate("substring('hello world', 6, 11)", exerciseVariableMap, fillInVariableMap));
}

@Test
public void testSubstring3() {
	assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR(" ")),
			Evaluator.evaluate("substring('hello world', 5, 6)", exerciseVariableMap, fillInVariableMap));
}

@Test
public void testSubstring4() {
	assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("a")),
			Evaluator.evaluate("substring('abcde', 0, 1)", exerciseVariableMap, fillInVariableMap));
}

@Test
public void testSubstring5() {
	assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("e")),
			Evaluator.evaluate("substring('abcde', 4, 5)", exerciseVariableMap, fillInVariableMap));
}

@Test
public void testSubstring6() {
	assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("")),
			Evaluator.evaluate("substring('abcde', 4, 4)", exerciseVariableMap, fillInVariableMap));
}

@Test
public void testSubstring7() {
	assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("e")),
			Evaluator.evaluate("substring('abcde', 4.0, 5)", exerciseVariableMap, fillInVariableMap));
}

@Test
public void testSubstringWithInput1() {
	assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("orld")),
			Evaluator.evaluate("substring('[pos=7]', 6, 10)", exerciseVariableMap, fillInVariableMap));
}

@Test
public void testSubstringWithInput2() {
	assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("a")),
			Evaluator.evaluate("substring('abcde', [pos=8], 1)", exerciseVariableMap, fillInVariableMap));
}

@Test
public void testSubstringWithInput3() {
	assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("e")),
			Evaluator.evaluate("substring('abcde', 4, [pos=9])", exerciseVariableMap, fillInVariableMap));
}

@Test
public void testSubstringWithVariables1() {
	assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("orld")),
			Evaluator.evaluate("substring('[var=suba]', 6, 10)", exerciseVariableMap, fillInVariableMap));
}

@Test
public void testSubstringWithVariables2() {
	assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("a")),
			Evaluator.evaluate("substring('abcde', [var=g], 1)", exerciseVariableMap, fillInVariableMap));
}

@Test
public void testSubstringWithVariables3() {
	assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("e")),
			Evaluator.evaluate("substring('abcde', 4, [var=h])", exerciseVariableMap, fillInVariableMap));
}

@Test(expected = FunctionInvalidArgumentException.class)
public void testSubstringWithWrongInputSecondAndThirdArgument() {
	Evaluator.evaluate("substring('hello', -1, 6)", exerciseVariableMap, fillInVariableMap);
	fail();
}

@Test(expected = FunctionInvalidArgumentTypeException.class)
public void testSubstringWithWrongInputSecondArgumentAsPointNumber() {
	OMSTR result = Evaluator.evaluate("substring('hello', 1.3, 2)", exerciseVariableMap, fillInVariableMap).getOMSTR();
	assertEquals("<OMSTR>e</OMSTR>",result.toString());

}

@Test(expected = FunctionInvalidArgumentTypeException.class)
public void testSubstringWithWrongInputThirdArgumentAsPointNumber() {
	OMSTR result = Evaluator.evaluate("substring('hello', 1, 2.9)", exerciseVariableMap, fillInVariableMap).getOMSTR();
	assertEquals("<OMSTR>e</OMSTR>",result.toString());
}

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

@Test(expected = FunctionInvalidNumberOfArgumentsException.class)
public void testSubstringWithOneArgument() {
	Evaluator.evaluate("substring('hello')", exerciseVariableMap, fillInVariableMap);
	fail();
}

@Test
public void testSubstringWithTwoArguments() {
	assertEquals(OMCreator.createOMOBJ(OMCreator.createOMSTR("llo")),
			Evaluator.evaluate("substring('hello', 2)", exerciseVariableMap, fillInVariableMap));
}

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

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

Hinweise

  • Die Position 0 ist das erste Zeichen, nicht Position 1