1 | package com.framsticks.parsers; |
---|
2 | |
---|
3 | import org.testng.annotations.*; |
---|
4 | |
---|
5 | import com.framsticks.model.*; |
---|
6 | import com.framsticks.model.Package; |
---|
7 | import com.framsticks.model.f0.Schema; |
---|
8 | import com.framsticks.params.*; |
---|
9 | import com.framsticks.params.types.FloatParam; |
---|
10 | import com.framsticks.params.types.StringParam; |
---|
11 | import com.framsticks.test.TestConfiguration; |
---|
12 | import com.framsticks.util.math.Point3d; |
---|
13 | |
---|
14 | import java.io.IOException; |
---|
15 | import java.text.ParseException; |
---|
16 | import java.util.List; |
---|
17 | import static org.fest.assertions.Assertions.*; |
---|
18 | import static org.fest.assertions.Delta.*; |
---|
19 | |
---|
20 | /** |
---|
21 | * Author: Piotr Śniegowski |
---|
22 | */ |
---|
23 | public class F0ParserTest extends TestConfiguration { |
---|
24 | |
---|
25 | private Schema schema; |
---|
26 | private List<AccessInterface> accesses; |
---|
27 | private List<Object> objects; |
---|
28 | private Model model; |
---|
29 | |
---|
30 | @BeforeClass |
---|
31 | public void setUp() throws Exception { |
---|
32 | schema = Schema.load(Schema.getDefaultDefinitionAsStream()); |
---|
33 | Package.register(schema.getRegistry()); |
---|
34 | } |
---|
35 | |
---|
36 | @Test |
---|
37 | public void primitiveParam() { |
---|
38 | FramsClass joint = schema.getFramsClass("j"); |
---|
39 | PrimitiveParam<?> dx = joint.getParamEntry("dx", PrimitiveParam.class); |
---|
40 | assertThat(dx).isInstanceOf(FloatParam.class); |
---|
41 | assertThat(schema.getNeuroClasses().size()).isEqualTo(21); |
---|
42 | assertThat(dx.getName()).isEqualTo("delta.x"); |
---|
43 | assertThat(dx.getMin(Double.class)).isEqualTo(-2.0, delta(0.0)); |
---|
44 | |
---|
45 | assertThat(schema.getRegistry().getInfoFromCache("n").getParamEntry("d", StringParam.class).getDef(String.class)).isEqualTo("N"); |
---|
46 | } |
---|
47 | |
---|
48 | @Test |
---|
49 | public void readF0() throws IOException, ParseException { |
---|
50 | assertThat(schema.getFramsClass("p")).isInstanceOf(FramsClass.class); |
---|
51 | assertThat(schema.getRegistry().getInfoFromCache("p").getParamEntry("as", FloatParam.class).getDef(Double.class)).isEqualTo(0.25, delta(0.0)); |
---|
52 | |
---|
53 | accesses = new F0Parser(schema, F0ParserTest.class.getResourceAsStream("/parsers/f0_example.txt")).parse(); |
---|
54 | |
---|
55 | assertThat(accesses.size()).isEqualTo(12); |
---|
56 | assertThat(accesses.get(0).getSelected()).isInstanceOf(Model.class); |
---|
57 | assertThat(accesses.get(5).get("i", String.class)).isEqualTo("1,2,3,\"dsadsa,,,,"); |
---|
58 | assertThat(accesses.get(7).get("d", String.class)).isEqualTo("|:p=0.25,r=1"); |
---|
59 | assertThat(accesses.get(10).get("d", String.class)).isEqualTo("@:p=0.25"); |
---|
60 | } |
---|
61 | |
---|
62 | @Test(dependsOnMethods = {"readF0"}) |
---|
63 | public void stripAccessInterface() { |
---|
64 | objects = Util.stripAccessInterface(accesses); |
---|
65 | |
---|
66 | assertThat(objects.get(1)).isInstanceOf(Part.class); |
---|
67 | assertThat(objects.get(4)).isInstanceOf(Joint.class); |
---|
68 | assertThat(objects.get(6)).isInstanceOf(NeuroDef.class); |
---|
69 | } |
---|
70 | |
---|
71 | @Test(dependsOnMethods = {"stripAccessInterface"}) |
---|
72 | public void buildModel() { |
---|
73 | model = Model.build(objects); |
---|
74 | |
---|
75 | assertThat(model.getParts().size()).isEqualTo(3); |
---|
76 | assertThat(model.getNeuroDefs().size()).isEqualTo(6); |
---|
77 | assertThat(model.getJoints().size()).isEqualTo(2); |
---|
78 | |
---|
79 | assertThat(model.getJoints().get(0).part1).isEqualTo(0); |
---|
80 | assertThat(model.getJoints().get(0).part2).isEqualTo(1); |
---|
81 | assertThat(model.getNeuroDefs().size()).isEqualTo(6); |
---|
82 | assertThat(model.getNeuroDefs().get(0).part).isEqualTo(1); |
---|
83 | assertThat(model.getNeuroDefs().get(0).joint).isEqualTo(-1); |
---|
84 | assertThat(model.getNeuroDefs().get(1).details).isEqualTo("|:p=0.25,r=1"); |
---|
85 | assertThat(model.getNeuroDefs().get(3).details).isEqualTo("N"); |
---|
86 | assertThat(model.getNeuroDefs().get(4).part).isEqualTo(-1); |
---|
87 | |
---|
88 | assertThat(model.getParts().get(1).getPosition().x).isEqualTo(2.0, delta(0.0)); |
---|
89 | assertThat(model.getParts().get(2).getPosition().sub(new Point3d(2.27236, -0.0792596, -0.958924)).length()).isLessThan(0.0001); |
---|
90 | assertThat(model.getParts().get(2).getOrientation().y.sub(new Point3d(0.870277, -0.404792, 0.280644)).length()).isLessThan(0.0001); |
---|
91 | } |
---|
92 | |
---|
93 | @Test(dependsOnMethods = {"buildModel"}) |
---|
94 | public void print() throws Exception { |
---|
95 | ListSink sink = new ListSink(); |
---|
96 | |
---|
97 | |
---|
98 | new F0Writer(schema, model, sink).write(); |
---|
99 | |
---|
100 | assertThat(sink.getOut()).containsExactly( |
---|
101 | "p:", |
---|
102 | "p:2.0,i=,Vstyle=", |
---|
103 | "p:2.272364001928095,-0.07925961087140347,-0.9589242746631385,i=bla", |
---|
104 | "j:0,1,dx=2.0", |
---|
105 | "j:1,2,rx=8.0,5.0,6.0,dx=1.0,i=\"1,2,3,\\\"dsadsa,,,,\"", |
---|
106 | "n:p=1", |
---|
107 | "n:j=0,d=\"|:p=0.25,r=1\"", |
---|
108 | "n:j=0,d=G", |
---|
109 | "n:p=1", |
---|
110 | "n:j=0,d=@:p=0.25", |
---|
111 | "n:p=1,d=Nu", |
---|
112 | "m:" |
---|
113 | ); |
---|
114 | |
---|
115 | } |
---|
116 | } |
---|