andrewjc2000 commented on a change in pull request #394:
URL: https://github.com/apache/incubator-daffodil/pull/394#discussion_r455887835



##########
File path: 
daffodil-core/src/test/scala/org/apache/daffodil/dsom/walker/TestDSOMWalker.scala
##########
@@ -0,0 +1,229 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.daffodil.dsom.walker
+
+import org.apache.daffodil.util._
+import org.apache.daffodil.compiler.{ Compiler, ProcessorFactory }
+import org.junit.Test
+import org.junit.Assert._
+
+class TestDSOMWalker {
+
+  @Test def testComplexTypes(): Unit = {
+    val testSchema = SchemaUtils.dfdlTestSchema(
+      <xs:include 
schemaLocation="org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd"/>,
+      <dfdl:format ref="ex:GeneralFormat"
+                   alignment="implicit" alignmentUnits="bits" 
occursCountKind="implicit"
+                   lengthKind="delimited" encoding="ASCII"/>,
+      <xs:element name="PersonData">
+        <xs:complexType>
+          <xs:sequence>
+            <xs:choice>
+              <xs:element name="age" type="xs:int" minOccurs="1" 
maxOccurs="1"/>
+            </xs:choice>
+            <xs:group ref="testGroup" />
+          </xs:sequence>
+        </xs:complexType>
+      </xs:element>
+      <xs:group name="testGroup">
+        <xs:sequence />
+      </xs:group>
+    )
+    val pf: ProcessorFactory = Compiler().compileNode(testSchema)
+    assertEquals(s"This basic Schema: $testSchema should compile; here are 
some diagnostics: ${pf.getDiagnostics}", false, pf.isError)
+    val walker: BasicWalker = new BasicWalker()
+    walker.walkFromRoot(pf.rootView)
+    val nodeStack: List[Either[TermView, TypeView]] = walker.nodeArr.toList
+    assertEquals(s"Node Stack $nodeStack did not have the expected number of 
elements", 7, nodeStack.size)
+    nodeStack.head match {
+      case Left(personElement: RootView) =>
+        assertEquals("The root element should be named 'PersonData'", 
"PersonData", personElement.name)
+      case Left(someClass) => fail(s"The Root element was not of type 
RootView, it was a ${someClass.getClass}!")
+      case Right(someClass) => fail(s"The Root element was not of type 
RootView, it was a ${someClass.getClass}!")
+    }
+    nodeStack(1) match {
+      case Right(_: ComplexTypeView) =>
+      case Left(someClass) => fail(s"The Root element did not contain a 
complexType wrapper child, it was a ${someClass.getClass}!")
+      case Right(someClass) => fail(s"The Root element element did not contain 
a complexType wrapper child, it was a ${someClass.getClass}!")
+    }
+    nodeStack(2) match {
+      case Left(_: SequenceView) =>
+      case Left(someClass) => fail(s"The complexType element did not contain a 
Sequence child, it was a ${someClass.getClass}!")
+      case Right(someClass) => fail(s"The complexType element did not contain 
a Sequence child, it was a ${someClass.getClass}!")
+    }
+    nodeStack(3) match {
+      case Left(_: ChoiceView) =>
+      case Left(someClass) => fail(s"The Sequence element did not contain a 
Choice child, it was a ${someClass.getClass}!")
+      case Right(someClass) => fail(s"The Sequence element did not contain a 
Choice child, it was a ${someClass.getClass}!")
+    }
+    nodeStack(4) match {
+      case Left(nameElement: ElementBaseView) =>
+        assertEquals("The second child element should be named 'age'", "age", 
nameElement.name)
+        assertEquals("The second child element should be a simple type", true, 
nameElement.isSimpleType)
+      case Left(someClass) => fail(s"The Sequence element did not contain a 
second Element child, it was a ${someClass.getClass}!")
+      case Right(someClass) => fail(s"The Sequence element did not contain a 
second Element child, it was a ${someClass.getClass}!")
+    }
+    nodeStack(5) match {
+      case Right(simpleType: SimpleTypeView) =>
+        simpleType.primType match {
+          case _:IntView =>
+          case _ => fail(s"The 'age' element should be of simple type Int")
+        }
+      case Left(someClass) => fail(s"The 'age' element did not have a 
SimpleTypeView, it was a ${someClass.getClass}!")
+      case Right(someClass) => fail(s"The 'age' element did not have a 
SimpleTypeView, it was a ${someClass.getClass}!")
+    }
+    nodeStack(6) match {
+      case Left(_: GroupRefView) =>
+      case Left(someClass) => fail(s"The Sequence element did not contain a 
GroupRef child, it was a ${someClass.getClass}!")
+      case Right(someClass) => fail(s"The Sequence element did not contain a 
GroupRef child, it was a ${someClass.getClass}!")
+    }
+  }

Review comment:
       I agree that it is robust, and I had it this way mainly for the sake of 
pattern matching and being able to figure out types of specific View elements 
on the stack when tests were failing.  That being said, for simplicity sake, I 
think the series of asserts based on `isInstanceOf` and `asInstanceOf` would be 
best.  I tried implementing a case class with an equals method that takes in an 
`AnyRef` as an argument as you suggested above, and it turned out to be an even 
longer solution.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to