LocalVariableScope extends AbstractVariableScope { @Override public @Nullable MplVariable<?> findVariable(String identifier) { MplVariable<?> result = super.findVariable(identifier); if (result != null) { return result; } if (parent != null) { return parent.findVariable(identifier); } return null; } LocalVariableScope(VariableScope parent); @Override VariableScope getParent(); @Override boolean mayChildDeclareLocalVariable(String identifier); @Override boolean mayDeclareVariable(String identifier); @Override @Nullable MplVariable<?> findVariable(String identifier);  }
@Test public void test_findVariable__Returns_correct_Variable_in_the_Scope() throws Exception { GlobalVariableScope root = new GlobalVariableScope(); LocalVariableScope underTest = new LocalVariableScope(root); MplVariable<?> variable = some($MplVariable()); underTest.declareVariable(variable); underTest.declareVariable(some($MplVariable())); MplVariable<?> actual = underTest.findVariable(variable.getIdentifier()); assertThat(actual).isSameAs(variable); }
@Test public void test_findVariable__Returns_null_if_there_is_no_Variable_with_the_specified_Identifier() throws Exception { GlobalVariableScope root = new GlobalVariableScope(); LocalVariableScope underTest = new LocalVariableScope(root); underTest.declareVariable(some($MplVariable())); MplVariable<?> actual = underTest.findVariable(some($Identifier())); assertThat(actual).isNull(); }
@Test public void test_findVariable__Returns_correct_Variable_from_Parent_LocalVariableScope() throws Exception { GlobalVariableScope root = new GlobalVariableScope(); LocalVariableScope parent = new LocalVariableScope(root); LocalVariableScope underTest = new LocalVariableScope(parent); MplVariable<?> variable = some($MplVariable()); parent.declareVariable(variable); parent.declareVariable(some($MplVariable())); underTest.declareVariable(some($MplVariable())); MplVariable<?> actual = underTest.findVariable(variable.getIdentifier()); assertThat(actual).isSameAs(variable); }
@Test public void test_findVariable__Returns_correct_Variable_from_Parent_GlobalVariableScope() throws Exception { GlobalVariableScope parent = new GlobalVariableScope(); LocalVariableScope underTest = new LocalVariableScope(parent); MplVariable<?> variable = some($MplVariable()); parent.declareVariable(variable); parent.declareVariable(some($MplVariable())); underTest.declareVariable(some($MplVariable())); MplVariable<?> actual = underTest.findVariable(variable.getIdentifier()); assertThat(actual).isSameAs(variable); }
@Test public void test_findVariable__Returns_local_Variable_over_global_Variable() throws Exception { GlobalVariableScope parent = new GlobalVariableScope(); LocalVariableScope underTest = new LocalVariableScope(parent); MplVariable<?> variable = some($MplVariable()); String identifier = variable.getIdentifier(); parent.declareVariable(some($MplVariable().withIdentifier(identifier))); underTest.declareVariable(variable); MplVariable<?> actual = underTest.findVariable(identifier); assertThat(actual).isSameAs(variable); }