Commit 25e839da authored by Matthew Dawson's avatar Matthew Dawson
Browse files

Make PartialVariableCollecction attempt to pull enumeration types out.

When the single argument constructor was used, PartialVariableCollection
made the assumption that no enumeration types were used, which made the
unit tests harder to write as they had to explicitly deal with the extraction.
Since the two argument constructor is an optimization, make the single
argument constructor attempt to pull out the enumeration types from the given
variables, making unit tests easier.
parent 775ee0c7
Loading
Loading
Loading
Loading
+10 −5
Original line number Diff line number Diff line
@@ -28,10 +28,7 @@
 */
package ca.mcscert.jtet.expression;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.*;

/**
 * @author Matthew Dawson <matthew@mjdsystems.ca>
@@ -46,7 +43,15 @@ final public class PartialVariableCollection {
    }

    public PartialVariableCollection(Map<String, Variable> variables) {
        this(variables, null);
        m_variables = Collections.unmodifiableMap(variables);

        final Set<EnumerationVariableType> enumerationTypes = new LinkedHashSet<EnumerationVariableType>();
        for (Variable var : variables.values()) {
            if (var.type() instanceof EnumerationVariableType) {
                enumerationTypes.add((EnumerationVariableType) var.type());
            }
        }
        m_enumerationTypes = Collections.unmodifiableSet(enumerationTypes);
    }

    public final Map<String,Variable> m_variables;
+18 −1
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@

package ca.mcscert.jtet.expression.test;

import ca.mcscert.jtet.expression.EnumerationVariableType;
import ca.mcscert.jtet.expression.PartialVariableCollection;
import ca.mcscert.jtet.expression.RealVariableType;
import ca.mcscert.jtet.expression.Variable;
@@ -61,9 +62,25 @@ public class PartialVariableCollectionTest {
        final Map<String, Variable> variables = new HashMap<String, Variable>();
        variables.put("test", new Variable("test", RealVariableType.Type));

        PartialVariableCollection vars = new PartialVariableCollection(variables, null);
        PartialVariableCollection vars = new PartialVariableCollection(variables);

        assertThat(vars.m_variables, is(variables));
        assertThat(vars.m_enumerationTypes, is(notNullValue()));
        assertThat(vars.m_enumerationTypes.size(), is(0));
    }

    @Test
    public void singleArgumentConstructorEnumerations() {
        final EnumerationVariableType enumerationType = new EnumerationVariableType("testtype");
        enumerationType.enumerationValues().add("testconst ");
        final Map<String, Variable> variables = new HashMap<String, Variable>();
        variables.put("test", new Variable("test", enumerationType));

        PartialVariableCollection vars = new PartialVariableCollection(variables);

        assertThat(vars.m_variables, is(variables));
        assertThat(vars.m_enumerationTypes, is(notNullValue()));
        assertThat(vars.m_enumerationTypes.size(), is(1));
        assertThat(vars.m_enumerationTypes.contains(enumerationType), is(true));
    }
}