Commit 6c5e52ee authored by Yanjun Jiang's avatar Yanjun Jiang Committed by Matthew Dawson
Browse files

Create a new TypeDeclarationGenerator.

TypeDeclarationGenerator is split out from Variables DeclarationGenerator so that
the type declaration part and variable declaration part are loosely coupled.  A
test for this interface is also created, ready for implementations of the
interface to use.
parent 1f9c1f96
Loading
Loading
Loading
Loading
+52 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2014 Yanjun Jiang <jiangy76@mcmaster.ca>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in
 *       the documentation and/or other materials provided with the distribution
 *     * Neither the name of the McMaster Centre for Software Certification nor the names
 *       of its contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package ca.mcscert.jtet.expression;

import java.util.Set;

/**
 * Generates type declarations for a language.
 * <p>
 * When generating a table, the types introduced by users often need to be declared.  This interfaces provides a common
 * method to do this, simplifying the test of jTET.
 *
 * @author      Yanjun Jiang
 */
public interface TypeDeclarationGenerator {
    /**
     * Generates the type declarations, returning a string for further processing.
     * <p>
     * The function accepts types of all the variables used in a given table, and generates a string representing them
     * in a given language.  The format of the output is left for a given language to define.
     * <p>
     * @param types  The types to generate declarations for.
     * @return       The generated type declaration string.
     */
    public String GenerateTypeDeclaration(Set<VariableType> types);
}
+74 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2014 Yanjun Jiang <jiangy76@mcmaster.ca>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in
 *       the documentation and/or other materials provided with the distribution
 *     * Neither the name of the McMaster Centre for Software Certification nor the names
 *       of its contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package ca.mcscert.jtet.expression;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;

import java.util.*;

import static org.junit.Assert.assertEquals;

/**
 * Provides a set of tests for implementations of {@link TypeDeclarationGenerator}.
 * @author      Yanjun Jiang
 */
@RunWith(Parameterized.class)
public class GenericTypeDeclarationGeneratorTest {
    @Parameters
    public static Collection<Object[]> data() {
        return Arrays.asList(new Object[][]{
        });
    }
    @Parameter(value = 0)
    public TypeDeclarationGenerator generator;

    @Parameter(value = 1)
    public String SingleEnumerationExpectedOutput;

    @Test
    /**
     * This function test the single enumeration scenario
     */
    public void SingleEnumerationTest() {
        if (SingleEnumerationExpectedOutput != null) {
            Set<VariableType> types = new LinkedHashSet<VariableType>();
            EnumerationVariableType enum1 = new EnumerationVariableType("Enum1");
            enum1.enumerationValues().add("e1");
            enum1.enumerationValues().add("e2");
            enum1.enumerationValues().add("e3");

            types.add(enum1);
            assertEquals(SingleEnumerationExpectedOutput, generator.GenerateTypeDeclaration(types));
        }
    }
}