### Question:
VariableFieldExpression implements Expression<String, Record> { @Override public String evaluate(final Record target) { final List<VariableField> fields = target.getVariableFields(fieldNumber); if (fields == null || fields.isEmpty()) { return null; } for (final VariableField field : fields) { final DataField dataField = (DataField) field; if (hasIndicatorPattern) { if (!(matches(firstIndicatorPattern, dataField.getIndicator1()) && matches(secondIndicatorPattern, dataField.getIndicator2()))) { continue; } } final List<Subfield> subfields = dataField.getSubfields(subfieldCode); if (subfields == null || subfields.isEmpty()) { continue; } for (final Subfield subfield : subfields) { final String data = subfield.getData(); if (data != null) { return data; } } } return null; } VariableFieldExpression(final String specs); @Override String evaluate(final Record target); @Override String specs();  }### Answer:
@Test public void noTargetField() { final Record record = newRecord(A_VARIABLE_FIELD_NAME, '#', '#', 'a'); final VariableFieldExpression expression = new VariableFieldExpression(A_VARIABLE_FIELD_NAME + "a"); assertNull(expression.evaluate(record)); }
@Test public void noTargetSubField() { final Record record = newRecord(A_VARIABLE_FIELD_NAME, '#', '#', 'a'); final VariableFieldExpression expression = new VariableFieldExpression(A_VARIABLE_FIELD_NAME); assertNull(expression.evaluate(record)); }
@Test public void selectorWithoutIndicatorsPattern() { final String expectedValue = "This is the expected value"; final Record record = newRecord(A_VARIABLE_FIELD_NAME, '#', '#', 'a', expectedValue); final VariableFieldExpression expression = new VariableFieldExpression(A_VARIABLE_FIELD_NAME + "a"); assertEquals(expectedValue, expression.evaluate(record)); }
@Test public void selectorWithoutIndicatorsPatternAndMultipleMatchingValues() { final String expectedValue = "This is the expected value"; final Record record = newRecord(A_VARIABLE_FIELD_NAME, '#', '#', 'a', expectedValue, "another value", "still another value"); final VariableFieldExpression expression = new VariableFieldExpression(A_VARIABLE_FIELD_NAME + "a"); assertEquals(expectedValue, expression.evaluate(record)); }
@Test public void indicatorsPatternWithLiterals() { final String expectedValue = "This is the expected value"; final Record record = newRecord(A_VARIABLE_FIELD_NAME, '1', '2', 'a', expectedValue); final VariableFieldExpression expression = new VariableFieldExpression(A_VARIABLE_FIELD_NAME + "(12)a"); assertEquals(expectedValue, expression.evaluate(record)); }
@Test public void indicatorsPatternWithGlob() { final String expectedValue = "This is the expected value"; final Record record = newRecord(A_VARIABLE_FIELD_NAME, '1', '2', 'a', expectedValue); final String [] matchingPatterns = {"??", "1?", "?2"}; for (final String matchingPattern : matchingPatterns) { final VariableFieldExpression expression = new VariableFieldExpression(A_VARIABLE_FIELD_NAME + "(" + matchingPattern + ")a"); assertEquals(expectedValue, expression.evaluate(record)); } }
@Test public void indicatorsPatternDoesntMatch() { final Record record = newRecord(A_VARIABLE_FIELD_NAME, '1', '2', 'a', "A value"); final String [] unmatchingPatterns = {"?3", "#?", "##", "44", "21"}; for (final String unmatchingPattern : unmatchingPatterns) { final VariableFieldExpression expression = new VariableFieldExpression(A_VARIABLE_FIELD_NAME + "(" + unmatchingPattern + ")a"); assertNull(expression.evaluate(record)); } }