Ifthenelse: Unterschied zwischen den Versionen

Aus JACK Wiki
Wechseln zu: Navigation, Suche
(JUnit Tests)
Zeile 27: Zeile 27:
 
<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(1, OMParser.parse("0"));
+
  placeFillInVariable(2, OMParser.parse("1"));
+
fillInVariableMap.put(7, OMConverter.toObject("<OMOBJ><OMSTR>test</OMSTR></OMOBJ>"));
  placeFillInVariable(3, OMParser.parse("test"));
+
 
+
exerciseVariableMap.put("g", OMConverter.toObject("<OMOBJ><OMSTR>test</OMSTR></OMOBJ>"));
  placeExerciseVariable("a", OMParser.parse("0"));
+
  placeExerciseVariable("b", OMParser.parse("1"));
+
}
  placeExerciseVariable("c", OMParser.parse("test"));
+
 
 +
@Test
 +
public void testIfthenelse1() {
 +
assertEquals(ExpressionParser.parse("'else'", null, null),
 +
Evaluator.evaluate("ifthenelse(0, 'then', 'else')", exerciseVariableMap, fillInVariableMap));
 +
}
 +
 
 +
@Test
 +
public void testIfthenelse2() {
 +
assertEquals(ExpressionParser.parse("'then'", null, null),
 +
Evaluator.evaluate("ifthenelse(1, 'then', 'else')", exerciseVariableMap, fillInVariableMap));
 +
}
 +
 
 +
@Test
 +
public void testIfthenelse3() {
 +
assertEquals(ExpressionParser.parse("'five'", null, null),
 +
Evaluator.evaluate("ifthenelse(5.7, 'five', 'notFive')", exerciseVariableMap, fillInVariableMap));
 +
}
 +
 
 +
@Test
 +
public void testIfthenelseWithInput1() {
 +
assertEquals(ExpressionParser.parse("'else'", null, null),
 +
Evaluator.evaluate("ifthenelse([pos=1], 'then', 'else')", exerciseVariableMap, fillInVariableMap));
 +
}
 +
 
 +
@Test
 +
public void testIfthenelseWithInput2() {
 +
assertEquals(ExpressionParser.parse("'test'", null, null),
 +
Evaluator.evaluate("ifthenelse(1, '[pos=7]', 'else')", exerciseVariableMap, fillInVariableMap));
 +
}
 +
 
 +
@Test
 +
public void testIfthenelseWithInput3() {
 +
assertEquals(ExpressionParser.parse("'test'", null, null), Evaluator
 +
.evaluate("ifthenelse([pos=2], '[pos=7]', 'notFive')", exerciseVariableMap, fillInVariableMap));
 +
}
 +
 
 +
@Test
 +
public void testIfthenelseWithVariables1() {
 +
assertEquals(ExpressionParser.parse("'else'", null, null),
 +
Evaluator.evaluate("ifthenelse([var=a], 'then', 'else')", exerciseVariableMap, fillInVariableMap));
 
}
 
}
  
 
@Test
 
@Test
public void testIfthenelse(){
+
public void testIfthenelseWithVariables2() {
  assertTrue("'else'".equals(getEvaluator().getResultAsEvaluatorString("ifthenelse(0, 'then', 'else')")));
+
assertEquals(ExpressionParser.parse("'test'", null, null),
  assertTrue("'then'".equals(getEvaluator().getResultAsEvaluatorString("ifthenelse(1, 'then', 'else')")));
+
Evaluator.evaluate("ifthenelse(1, '[var=g]', 'else')", exerciseVariableMap, fillInVariableMap));
  assertTrue("'five'".equals(getEvaluator().getResultAsEvaluatorString("ifthenelse(5.7, 'five', 'notFive')")));
 
 
}
 
}
  
 
@Test
 
@Test
public void testIfthenelseWithInput(){
+
public void testIfthenelseWithVariables3() {
  assertTrue("'else'".equals(getEvaluator().getResultAsEvaluatorString("ifthenelse([pos=1], 'then', 'else')")));
+
assertEquals(ExpressionParser.parse("'test'", null, null), Evaluator
  assertTrue("'test'".equals(getEvaluator().getResultAsEvaluatorString("ifthenelse(1, '[pos=3]', 'else')")));
+
.evaluate("ifthenelse([var=b], '[var=g]', 'notFive')", exerciseVariableMap, fillInVariableMap));
  assertTrue("'test'".equals(getEvaluator().getResultAsEvaluatorString("ifthenelse([pos=2], '[pos=3]', 'notFive')")));
 
 
}
 
}
  
 
@Test
 
@Test
public void testIfthenelseWithVariables(){
+
public void testIfthenelseWithExpressions1() {
  assertTrue("'else'".equals(getEvaluator().getResultAsEvaluatorString("ifthenelse([var=a], 'then', 'else')")));
+
assertEquals(ExpressionParser.parse("a", null, null),
  assertTrue("'test'".equals(getEvaluator().getResultAsEvaluatorString("ifthenelse(1, '[var=c]', 'else')")));
+
Evaluator.evaluate("ifthenelse('5*2 +5 == 5*3', 'a', 'b')", exerciseVariableMap, fillInVariableMap));
  assertTrue("'test'".equals(getEvaluator().getResultAsEvaluatorString("ifthenelse([var=b], '[var=c]', 'notFive')")));
 
 
}
 
}
  
 
@Test
 
@Test
public void testIfthenelseWithExpressions(){
+
public void testIfthenelseWithExpressions2() {
  assertTrue("'a'".equals(getEvaluator().getResultAsEvaluatorString("ifthenelse('5*2 +5 == 5*3', 'a', 'b')")));
+
assertEquals(ExpressionParser.parse("b", null, null), Evaluator
  assertTrue("'b'".equals(getEvaluator().getResultAsEvaluatorString("ifthenelse('ifthenelse(0, '1', '0')', 'a', 'b')")));
+
.evaluate("ifthenelse(ifthenelse(0, '1', '0'), 'a', 'b')", exerciseVariableMap, fillInVariableMap));
 
}
 
}
+
 
@Test(expected=InvalidEvaluatorFunctionArgumentException.class)
+
@Test(expected = ParserException.class)
public void testIfthenelseWithWrongInputCharacter(){
+
public void testIfthenelseWithWrongInputCharacter() {
  getEvaluator().getResultAsEvaluatorString("ifthenelse(a, 'a', 'b')");
+
Evaluator.evaluate("ifthenelse(ab, 'a', 'b')", exerciseVariableMap, fillInVariableMap);
 
}
 
}
  
@Test(expected=UnsupportedEvaluatorFunctionException.class)
+
@Test
public void testIfthenelseWithTwoRationalAndOneTextArgument(){
+
public void testIfthenelseWithTwoRationalAndOneTextArgument() {
  getEvaluator().getResultAsEvaluatorString("ifthenelse(1, 0, 'b')");
+
assertEquals(ExpressionParser.parse("0", null, null), Evaluator.evaluate("ifthenelse(1, 0, 'b')", exerciseVariableMap, fillInVariableMap));
 
}
 
}
  
@Test(expected=UnsupportedEvaluatorFunctionException.class)
+
@Test
public void testIfthenelseWithThreeRationalArguments(){
+
public void testIfthenelseWithThreeRationalArguments() {
  getEvaluator().getResultAsEvaluatorString("ifthenelse(1, 0, 7)");
+
assertEquals(ExpressionParser.parse("0", null, null), Evaluator.evaluate("ifthenelse(1, 0, 7)", exerciseVariableMap, fillInVariableMap));
 
}
 
}
  
@Test(expected=UnsupportedEvaluatorFunctionException.class)
+
@Test(expected = FunctionInvalidNumberOfArgumentsException.class)
public void testIfthenelseWithOneArgument(){
+
public void testIfthenelseWithOneArgument() {
  getEvaluator().getResultAsEvaluatorString("ifthenelse(1)");
+
Evaluator.evaluate("ifthenelse(1)", exerciseVariableMap, fillInVariableMap);
 +
fail();
 
}
 
}
+
 
@Test(expected=UnsupportedEvaluatorFunctionException.class)
+
@Test(expected = FunctionInvalidNumberOfArgumentsException.class)
public void testIfthenelseWithTwoArguments(){
+
public void testIfthenelseWithTwoArguments() {
  getEvaluator().getResultAsEvaluatorString("ifthenelse(0, 'this')");
+
Evaluator.evaluate("ifthenelse(0, 'this')", exerciseVariableMap, fillInVariableMap);
 +
fail();
 
}
 
}
  
@Test(expected=UndefinedExerciseVariableException.class)
+
@Test(expected = UndefinedExerciseVariableException.class)
public void testIfthenelseWithMissingExerciseVariable(){
+
public void testIfthenelseWithMissingExerciseVariable() {
  getEvaluator().getResultAsEvaluatorString("ifthenelse('[var=j]', 'a', 'b')");
+
Evaluator.evaluate("ifthenelse('[var=j]', 'a', 'b')", exerciseVariableMap, fillInVariableMap);
 +
fail();
 
}
 
}
  
@Test(expected=UndefinedFillInVariableException.class)
+
@Test(expected = UndefinedFillInVariableException.class)
public void testIfthenelseWithMissingInput(){
+
public void testIfthenelseWithMissingInput() {
  getEvaluator().getResultAsEvaluatorString("ifthenelse('[pos=42]', 'a', 'b')");
+
Evaluator.evaluate("ifthenelse('[pos=42]', 'a', 'b')", exerciseVariableMap, fillInVariableMap);
}
+
fail();
 +
}
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 
=== Hinweise ===
 
=== Hinweise ===
 
* Zahlen ungleich 0 werden zu ''wahr'' ausgewertet.
 
* Zahlen ungleich 0 werden zu ''wahr'' ausgewertet.
  
 
[[Kategorie:Evaluatorfunktion]]
 
[[Kategorie:Evaluatorfunktion]]

Version vom 18. September 2017, 13:01 Uhr

Beschreibung

Die Funktion ifthenelse wählt zwischen zwei Zeichenketten, abhängig vom logischen Wahrheitsgehalt einer gegebenen Aussage. Als Aussage sind rationale Zahlen, sowie mathematische Terme und Un-/Gleichungen zulässig. Zahlen ungleich 0 werden als wahr, 0 wird zu falsch ausgewertet.

Die Funktion erwartet eine Aussage, die sich zu wahr oder falsch auswerten lässt, einen String, der zurückgegeben wird, falls die Aussage wahr ist, und einen String, der zurückgegeben wird, falls die Aussage falsch ist. Es wird ein String zurückgegeben.

Syntax

ifthenelse(String aussage, String wahrAussage, String falschAussage)

Parameter

  • aussage - logisch auzuwertende Aussage
  • wahrAussage - String, der im Falle einer wahren Aussage zurückgegeben wird
  • falschAussage - String, der im Falle einer falschen Aussage zurückgegeben wird

Return Value

  • Gibt einen String zurück

Beispiele

Standardfälle

ifthenelse('1','wahr','falsch')   --> returns wahr
ifthenelse('5*2+5==5*3','a','b')   --> returns a

Benutzung von Variablen

ifthenelse('[pos=1]','then','else')
ifthenelse('15','[var=a]','text')

JUnit Tests

[Anzeigen]

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

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

@Test
public void testIfthenelse1() {
	assertEquals(ExpressionParser.parse("'else'", null, null),
			Evaluator.evaluate("ifthenelse(0, 'then', 'else')", exerciseVariableMap, fillInVariableMap));
}

@Test
public void testIfthenelse2() {
	assertEquals(ExpressionParser.parse("'then'", null, null),
			Evaluator.evaluate("ifthenelse(1, 'then', 'else')", exerciseVariableMap, fillInVariableMap));
}

@Test
public void testIfthenelse3() {
	assertEquals(ExpressionParser.parse("'five'", null, null),
			Evaluator.evaluate("ifthenelse(5.7, 'five', 'notFive')", exerciseVariableMap, fillInVariableMap));
}

@Test
public void testIfthenelseWithInput1() {
	assertEquals(ExpressionParser.parse("'else'", null, null),
			Evaluator.evaluate("ifthenelse([pos=1], 'then', 'else')", exerciseVariableMap, fillInVariableMap));
}

@Test
public void testIfthenelseWithInput2() {
	assertEquals(ExpressionParser.parse("'test'", null, null),
			Evaluator.evaluate("ifthenelse(1, '[pos=7]', 'else')", exerciseVariableMap, fillInVariableMap));
}

@Test
public void testIfthenelseWithInput3() {
	assertEquals(ExpressionParser.parse("'test'", null, null), Evaluator
			.evaluate("ifthenelse([pos=2], '[pos=7]', 'notFive')", exerciseVariableMap, fillInVariableMap));
}

@Test
public void testIfthenelseWithVariables1() {
	assertEquals(ExpressionParser.parse("'else'", null, null),
			Evaluator.evaluate("ifthenelse([var=a], 'then', 'else')", exerciseVariableMap, fillInVariableMap));
}

@Test
public void testIfthenelseWithVariables2() {
	assertEquals(ExpressionParser.parse("'test'", null, null),
			Evaluator.evaluate("ifthenelse(1, '[var=g]', 'else')", exerciseVariableMap, fillInVariableMap));
}

@Test
public void testIfthenelseWithVariables3() {
	assertEquals(ExpressionParser.parse("'test'", null, null), Evaluator
			.evaluate("ifthenelse([var=b], '[var=g]', 'notFive')", exerciseVariableMap, fillInVariableMap));
}

@Test
public void testIfthenelseWithExpressions1() {
	assertEquals(ExpressionParser.parse("a", null, null),
			Evaluator.evaluate("ifthenelse('5*2 +5 == 5*3', 'a', 'b')", exerciseVariableMap, fillInVariableMap));
}

@Test
public void testIfthenelseWithExpressions2() {
	assertEquals(ExpressionParser.parse("b", null, null), Evaluator
			.evaluate("ifthenelse(ifthenelse(0, '1', '0'), 'a', 'b')", exerciseVariableMap, fillInVariableMap));
}

@Test(expected = ParserException.class)
public void testIfthenelseWithWrongInputCharacter() {
	Evaluator.evaluate("ifthenelse(ab, 'a', 'b')", exerciseVariableMap, fillInVariableMap);
}

@Test
public void testIfthenelseWithTwoRationalAndOneTextArgument() {
	assertEquals(ExpressionParser.parse("0", null, null), Evaluator.evaluate("ifthenelse(1, 0, 'b')", exerciseVariableMap, fillInVariableMap));
}

@Test
public void testIfthenelseWithThreeRationalArguments() {
	assertEquals(ExpressionParser.parse("0", null, null), Evaluator.evaluate("ifthenelse(1, 0, 7)", exerciseVariableMap, fillInVariableMap));
}

@Test(expected = FunctionInvalidNumberOfArgumentsException.class)
public void testIfthenelseWithOneArgument() {
	Evaluator.evaluate("ifthenelse(1)", exerciseVariableMap, fillInVariableMap);
	fail();
}

@Test(expected = FunctionInvalidNumberOfArgumentsException.class)
public void testIfthenelseWithTwoArguments() {
	Evaluator.evaluate("ifthenelse(0, 'this')", exerciseVariableMap, fillInVariableMap);
	fail();
}

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

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

Hinweise

  • Zahlen ungleich 0 werden zu wahr ausgewertet.