Substring

Aus JACK Wiki
Zur Navigation springen Zur Suche springen

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(Zeichenkette zeichenkette, Ganzzahl anfangsPosition, Ganzzahl 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

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