]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/testcases/org/simantics/databoard/tests/ValueParserSuccessTests.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / testcases / org / simantics / databoard / tests / ValueParserSuccessTests.java
1 /*******************************************************************************
2  *  Copyright (c) 2010 Association for Decentralized Information Management in
3  *  Industry THTH ry.
4  *  All rights reserved. This program and the accompanying materials
5  *  are made available under the terms of the Eclipse Public License v1.0
6  *  which accompanies this distribution, and is available at
7  *  http://www.eclipse.org/legal/epl-v10.html
8  *
9  *  Contributors:
10  *      VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.databoard.tests;
13
14 import java.util.Arrays;
15 import java.util.HashMap;
16
17 import junit.framework.TestCase;
18
19 import org.junit.Test;
20 import org.simantics.databoard.Bindings;
21 import org.simantics.databoard.annotations.Union;
22 import org.simantics.databoard.binding.Binding;
23 import org.simantics.databoard.binding.impl.HashMapBinding;
24 import org.simantics.databoard.binding.impl.OptionalBindingDefault;
25 import org.simantics.databoard.parser.repository.DataValueRepository;
26
27 public class ValueParserSuccessTests extends TestCase { 
28         
29         public Object parse(Binding binding, String text) throws Exception {
30                 return new DataValueRepository().translate(text, binding);
31         }
32         
33         @Test
34         public void testPrimitives() throws Exception {
35                 assertEquals(Boolean.TRUE, parse(Bindings.BOOLEAN, "true"));
36                 assertEquals(Boolean.FALSE, parse(Bindings.BOOLEAN, "false"));
37                 
38                 assertEquals(123, parse(Bindings.INTEGER, "123"));
39                 assertEquals(-234234, parse(Bindings.INTEGER, "-234234"));
40                 
41                 assertEquals(12345678912345L, parse(Bindings.LONG, "12345678912345"));
42                 
43                 assertEquals(1.2345, parse(Bindings.DOUBLE, "1.2345"));         
44                 assertEquals(-3.0, parse(Bindings.DOUBLE, "-3"));
45                 assertEquals(1e-10, parse(Bindings.DOUBLE, "1e-10"));
46                 
47                 assertEquals(2.3f, parse(Bindings.FLOAT, "2.3"));
48                 
49                 assertEquals("Hello World!", parse(Bindings.STRING, "\"Hello World!\""));
50                 assertEquals("\n\t\r\\\"", parse(Bindings.STRING, "\"\\n\\t\\r\\\\\\\"\""));
51                 assertEquals("This\ntext\nspans\nmultiple\nlines.", 
52                                 parse(Bindings.STRING, "\"\"\"This\ntext\nspans\nmultiple\nlines.\"\"\""));
53                 assertEquals("\u0008", parse(Bindings.STRING, "\"\b\""));
54         }
55         
56         public void assertArraysEqual(int[] expected, int[] actual) {
57                 if(!Arrays.equals(expected, actual))            
58                         failNotEquals(null, expected, actual);
59         }
60         
61         public void testArray() throws Exception {
62                 assertArraysEqual(new int[] {1,2,3}, (int[])parse(Bindings.INT_ARRAY, "[1,2,3]"));
63         }
64         
65         public void testOptional() throws Exception {
66                 assertEquals(null, 
67                                 (Integer)parse(new OptionalBindingDefault(Bindings.INTEGER), "null"));
68                 assertEquals((Integer)1, 
69                                 (Integer)parse(new OptionalBindingDefault(Bindings.INTEGER), "1"));
70         }
71         
72         static public class MyRecord {
73                 public int x;
74                 public String y;
75                 
76                 public MyRecord(int x, String y) {
77                         this.x = x;
78                         this.y = y;
79                 }                       
80         }
81         
82         public void testRecord() throws Exception {
83                 MyRecord record = (MyRecord)parse(Bindings.getBinding(MyRecord.class), "{ y = \"asd\", x = 5 }");
84                 assertEquals(5, record.x);
85                 assertEquals("asd", record.y);
86         }
87         
88         public static class MyRecord2 {
89                 public int x;
90                 public String y;
91         }
92         
93         public void testRecord2() throws Exception {
94                 MyRecord2 record = (MyRecord2)parse(Bindings.getBinding(MyRecord2.class), "{ y = \"asd\", x = 5 }");
95                 assertEquals(5, record.x);
96                 assertEquals("asd", record.y);
97         }
98         
99         public void testTuple() throws Exception {
100                 MyRecord record = (MyRecord)parse(Bindings.getBinding(MyRecord.class), "( 5, \"asd\")");
101                 assertEquals(5, record.x);
102                 assertEquals("asd", record.y);
103         }
104         
105         @Union({A.class, C.class})
106         interface Union1 {              
107         }
108         
109         static public class A implements Union1 {}
110         static public class C implements Union1 { 
111                 public float x; 
112                 public int y;
113                 public C(float x, int y) {
114                         this.x = x;
115                         this.y = y;
116                 }               
117         }
118         
119         public void testUnion() throws Exception {
120                 Union1 union = (Union1)parse(Bindings.getBinding(Union1.class), "C (1.2, 3)");
121                 assertTrue(union instanceof C);
122                 assertEquals(1.2f, ((C)union).x);
123                 assertEquals(3, ((C)union).y);
124         }
125         
126         @SuppressWarnings("rawtypes")
127     public void testMap() throws Exception {
128                 HashMap map = (HashMap)parse(
129                                 new HashMapBinding(
130                                                 Bindings.getBinding(String.class),
131                                                 Bindings.getBinding(Integer.class)
132                                                 ),                               
133                                 "map { \"1\" = 1, \"2\" = 2 }");
134                 assertEquals(2, map.size());
135                 assertEquals(1, map.get("1"));
136                 assertEquals(2, map.get("2"));
137         }
138         
139 }