X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.databoard%2Ftestcases%2Forg%2Fsimantics%2Fdataboard%2Ftests%2FParserSuccessTests.java;fp=bundles%2Forg.simantics.databoard%2Ftestcases%2Forg%2Fsimantics%2Fdataboard%2Ftests%2FParserSuccessTests.java;h=2af48dbb048ca78c99b41cf4697fa8edc485a3a5;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.databoard/testcases/org/simantics/databoard/tests/ParserSuccessTests.java b/bundles/org.simantics.databoard/testcases/org/simantics/databoard/tests/ParserSuccessTests.java new file mode 100644 index 000000000..2af48dbb0 --- /dev/null +++ b/bundles/org.simantics.databoard/testcases/org/simantics/databoard/tests/ParserSuccessTests.java @@ -0,0 +1,250 @@ +/******************************************************************************* + * Copyright (c) 2010 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.databoard.tests; + +import java.util.HashMap; +import java.util.Map; + +import junit.framework.TestCase; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.simantics.databoard.Datatypes; +import org.simantics.databoard.annotations.Length; +import org.simantics.databoard.annotations.MIMEType; +import org.simantics.databoard.annotations.Optional; +import org.simantics.databoard.annotations.Pattern; +import org.simantics.databoard.annotations.Range; +import org.simantics.databoard.annotations.Union; +import org.simantics.databoard.annotations.Unit; +import org.simantics.databoard.parser.repository.DataTypeRepository; +import org.simantics.databoard.parser.repository.DataTypeSyntaxError; +import org.simantics.databoard.type.Component; +import org.simantics.databoard.type.Datatype; +import org.simantics.databoard.type.RecordType; + + +public class ParserSuccessTests extends TestCase { + + DataTypeRepository repository; + Map reference; + + @Before + public void setUp() { + repository = new DataTypeRepository(); + reference = new HashMap(); + } + + public void printRepository() { + for(String name : repository.getTypeNames()) { + Datatype type = repository.get(name); + System.out.println(name + " = " + type + " (" + + type.getClass() + ")"); + } + } + + @After + public void compare() { + for(String name : reference.keySet()) + if(!repository.getTypeNames().contains(name)) + fail("Type " + name + " is not in repository."); + for(String name : repository.getTypeNames()) + if(!reference.containsKey(name)) + fail("Type " + name + " should not be in the repository."); + for(String name : repository.getTypeNames()) + assertEquals("Problem with type " + name + ".", + reference.get(name), repository.get(name)); + } + + @Test + public void testBuiltins() throws DataTypeSyntaxError { + repository.addDefinitions( + "type MyBoolean = Boolean " + + "type MyByte = Byte " + + "type MyInteger = Integer " + + "type MyLong = Long " + + "type MyFloat = Float " + + "type MyDouble = Double " + + "type MyString = String " + ); + reference.put("MyBoolean", Datatypes.getDatatypeUnchecked(Boolean.class)); + reference.put("MyByte", Datatypes.getDatatypeUnchecked(Byte.class)); + reference.put("MyInteger", Datatypes.getDatatypeUnchecked(Integer.class)); + reference.put("MyFloat", Datatypes.getDatatypeUnchecked(Float.class)); + reference.put("MyDouble", Datatypes.getDatatypeUnchecked(Double.class)); + reference.put("MyLong", Datatypes.getDatatypeUnchecked(Long.class)); + reference.put("MyString", Datatypes.getDatatypeUnchecked(String.class)); + } + + @Test + public void testArray() throws DataTypeSyntaxError { + repository.addDefinitions( + "type Array1 = Integer[] " + + "type Array2 = String[][] " + ); + reference.put("Array1", Datatypes.getDatatypeUnchecked(int[].class)); + reference.put("Array2", Datatypes.getDatatypeUnchecked(String[][].class)); + } + + static class Optional1 { + @Optional Integer a; + @Optional float[] b; + } + + @Test + public void testOptional() throws DataTypeSyntaxError { + repository.addDefinitions( + "type Optional1 = {" + + "a : Optional(Integer)," + + "b : Optional(Float[])" + + "}" + ); + reference.put("Optional1", Datatypes.getDatatypeUnchecked(Optional1.class)); + } + + static class Record1 { + } + + static class Record2 { + String a; + Integer b; + } + + @Test + public void testRecord() throws DataTypeSyntaxError { + repository.addDefinitions( + "type Record1 = {} " + + "type Record2 = { a : String, b : Integer } " + ); + reference.put("Record1", Datatypes.getDatatypeUnchecked(Record1.class)); + reference.put("Record2", Datatypes.getDatatypeUnchecked(Record2.class)); + } + + public Datatype tuple(Datatype ... types) { + RecordType tuple = new RecordType(); + Component[] components = new Component[types.length]; + for(int i=0;i