Skip to content

Commit

Permalink
#405 Fix the way layout is drawn - fix parser unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yruslan committed Aug 2, 2021
1 parent 486dac8 commit 9fef3c5
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -251,19 +251,9 @@ class Copybook(val ast: CopybookAST) extends Serializable {
fieldStrings.mkString("\n")
}

val strings = for (grp <- ast.children) yield {
val start = grp.binaryProperties.offset + 1
val length = grp.binaryProperties.actualSize
val end = start + length - 1
val groupStr = generateGroupLayoutPositions(grp.asInstanceOf[Group])
val namePart = alignLeft(s"${grp.name}", 55)
val fieldStartPart = alignRight(s"$start", 7)
val fieldEndPart = alignRight(s"$end", 7)
val fieldLengthPart = alignRight(s"$length", 7)
s"$namePart$fieldStartPart$fieldEndPart$fieldLengthPart\n$groupStr"
}
val layout = generateGroupLayoutPositions(ast)
val header = "-------- FIELD LEVEL/NAME --------- --ATTRIBS-- FLD START END LENGTH\n\n"
header + strings.mkString("\n")
header + layout
}

def dropRoot(): Copybook = {
Expand Down Expand Up @@ -313,10 +303,14 @@ class Copybook(val ast: CopybookAST) extends Serializable {


object Copybook {
def merge(copybooks: Iterable[Copybook]): Copybook = {
def merge(copybooks: Seq[Copybook]): Copybook = {
if (copybooks.isEmpty)
throw new RuntimeException("Cannot merge an empty iterable of copybooks.")

if (copybooks.size == 1) {
return copybooks.head
}

// make sure all segments are the same level
val rootLevels: Set[Int] = copybooks.flatMap(cb => cb.ast.children.map({
case x: Group => x.level
Expand Down Expand Up @@ -354,6 +348,7 @@ object Copybook {
case x: Group => x.copy(redefines = None, isRedefined = true)(Some(newRoot))
case x: Primitive => x.copy(redefines = None, isRedefined = true)(Some(newRoot))
})

newRoot.children ++= copybooks.head.ast.children.tail.map({
case x: Group => x.copy(redefines = Option(targetName), isRedefined = false)(Some(newRoot))
case x: Primitive => x.copy(redefines = Option(targetName), isRedefined = false)(Some(newRoot))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,24 @@ class CopybooksOperationsSpec extends FunSuite {
assert(copybook1.generateRecordLayoutPositions ==
"""-------- FIELD LEVEL/NAME --------- --ATTRIBS-- FLD START END LENGTH
|
|RECORD_COPYBOOK_1 1 30 30
| 1 RECORD_COPYBOOK_1 7 1 30 30
| 5 GROUP_1 7 1 30 30
| 6 FIELD_1 3 1 10 10
| 6 FILLER 4 11 15 5
| 6 GROUP_2 7 16 30 15
| 10 NESTED_FIELD_1 6 16 25 10
| 10 FILLER 7 26 30 5"""
.stripMargin.replace("\r\n", "\n"))
assert(copybookDR1.generateRecordLayoutPositions ==
"""-------- FIELD LEVEL/NAME --------- --ATTRIBS-- FLD START END LENGTH
|
| 5 GROUP_1 6 1 30 30
| 6 FIELD_1 2 1 10 10
| 6 FILLER 3 11 15 5
| 6 GROUP_2 6 16 30 15
| 10 NESTED_FIELD_1 5 16 25 10
| 10 FILLER 6 26 30 5"""
.stripMargin.replace("\r\n", "\n"))
assert(copybookDR1.generateRecordLayoutPositions ==
"""-------- FIELD LEVEL/NAME --------- --ATTRIBS-- FLD START END LENGTH
|
|GROUP_1 1 30 30
| 6 FIELD_1 1 1 10 10
| 6 FILLER 2 11 15 5
| 6 GROUP_2 5 16 30 15
| 10 NESTED_FIELD_1 4 16 25 10
| 10 FILLER 5 26 30 5"""
.stripMargin.replace("\r\n", "\n"))

val exception = intercept[RuntimeException] {
copybookDR1.dropRoot()
Expand Down Expand Up @@ -126,22 +126,22 @@ class CopybooksOperationsSpec extends FunSuite {
assert(copybookR1.generateRecordLayoutPositions ==
"""-------- FIELD LEVEL/NAME --------- --ATTRIBS-- FLD START END LENGTH
|
|GROUP_1A 1 30 30
| 6 FIELD_1 1 1 10 10
| 6 FILLER 2 11 15 5
| 6 GROUP_2A 5 16 30 15
| 10 NESTED_FIELD_1 4 16 25 10
| 10 FILLER 5 26 30 5"""
| 5 GROUP_1A 6 1 30 30
| 6 FIELD_1 2 1 10 10
| 6 FILLER 3 11 15 5
| 6 GROUP_2A 6 16 30 15
| 10 NESTED_FIELD_1 5 16 25 10
| 10 FILLER 6 26 30 5"""
.stripMargin.replace("\r\n", "\n"))
assert(copybookR2.generateRecordLayoutPositions ==
"""-------- FIELD LEVEL/NAME --------- --ATTRIBS-- FLD START END LENGTH
|
|GROUP_1B 1 60 60
| 6 FIELD_1 1 1 20 20
| 6 FILLER 2 21 30 10
| 6 GROUP_2B 5 31 60 30
| 10 NESTED_FIELD_1 4 31 50 20
| 10 FILLER 5 51 60 10"""
| 5 GROUP_1B 6 1 60 60
| 6 FIELD_1 2 1 20 20
| 6 FILLER 3 21 30 10
| 6 GROUP_2B 6 31 60 30
| 10 NESTED_FIELD_1 5 31 50 20
| 10 FILLER 6 51 60 10"""
.stripMargin.replace("\r\n", "\n"))

val exception1 = intercept[RuntimeException] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,34 +76,34 @@ class MergeCopybooksSpec extends FunSuite {
assert(copybook123.generateRecordLayoutPositions ==
"""-------- FIELD LEVEL/NAME --------- --ATTRIBS-- FLD START END LENGTH
|
|RECORD_COPYBOOK_1 1 90 90
| 5 GROUP_1 6 1 30 30
| 6 FIELD_1 2 1 10 10
| 6 FILLER 3 11 15 5
| 6 GROUP_2 6 16 30 15
| 10 NESTED_FIELD_1 5 16 25 10
| 10 FILLER 6 26 30 5
|RECORD_COPYBOOK_2A 1 90 90
| 5 GROUP_1 12 1 60 60
| 6 FIELD_1 8 1 20 20
| 6 FILLER 9 21 30 10
| 6 GROUP_2 12 31 60 30
| 10 NESTED_FIELD_1 11 31 50 20
| 10 FILLER 12 51 60 10
|RECORD_COPYBOOK_2B 1 90 90
| 5 GROUP_1 18 1 60 60
| 6 FIELD_1 14 1 20 20
| 6 FILLER 15 21 30 10
| 6 GROUP_2 18 31 60 30
| 10 NESTED_FIELD_1 17 31 50 20
| 10 FILLER 18 51 60 10
|RECORD_COPYBOOK_3 1 90 90
| 5 GROUP_1 24 1 90 90
| 6 FIELD_1 20 1 30 30
| 6 FILLER 21 31 45 15
| 6 GROUP_2 24 46 90 45
| 10 NESTED_FIELD_1 23 46 75 30
| 10 FILLER 24 76 90 15"""
| 1 RECORD_COPYBOOK_1 r 7 1 90 90
| 5 GROUP_1 7 1 30 30
| 6 FIELD_1 3 1 10 10
| 6 FILLER 4 11 15 5
| 6 GROUP_2 7 16 30 15
| 10 NESTED_FIELD_1 6 16 25 10
| 10 FILLER 7 26 30 5
| 1 RECORD_COPYBOOK_2A rR 14 1 90 90
| 5 GROUP_1 14 1 60 60
| 6 FIELD_1 10 1 20 20
| 6 FILLER 11 21 30 10
| 6 GROUP_2 14 31 60 30
| 10 NESTED_FIELD_1 13 31 50 20
| 10 FILLER 14 51 60 10
| 1 RECORD_COPYBOOK_2B rR 21 1 90 90
| 5 GROUP_1 21 1 60 60
| 6 FIELD_1 17 1 20 20
| 6 FILLER 18 21 30 10
| 6 GROUP_2 21 31 60 30
| 10 NESTED_FIELD_1 20 31 50 20
| 10 FILLER 21 51 60 10
| 1 RECORD_COPYBOOK_3 R 28 1 90 90
| 5 GROUP_1 28 1 90 90
| 6 FIELD_1 24 1 30 30
| 6 FILLER 25 31 45 15
| 6 GROUP_2 28 46 90 45
| 10 NESTED_FIELD_1 27 46 75 30
| 10 FILLER 28 76 90 15"""
.stripMargin.replace("\r\n", "\n"))
}

Expand All @@ -126,16 +126,17 @@ class MergeCopybooksSpec extends FunSuite {
assert(copybook1M.getRecordSize == 30)

assert(copybook1M.generateRecordLayoutPositions == copybook1.generateRecordLayoutPositions)

assert(copybook1M.generateRecordLayoutPositions ==
"""-------- FIELD LEVEL/NAME --------- --ATTRIBS-- FLD START END LENGTH
|
|RECORD_COPYBOOK_1 1 30 30
| 5 GROUP_1 6 1 30 30
| 6 FIELD_1 2 1 10 10
| 6 FILLER 3 11 15 5
| 6 GROUP_2 6 16 30 15
| 10 NESTED_FIELD_1 5 16 25 10
| 10 FILLER 6 26 30 5"""
| 1 RECORD_COPYBOOK_1 7 1 30 30
| 5 GROUP_1 7 1 30 30
| 6 FIELD_1 3 1 10 10
| 6 FILLER 4 11 15 5
| 6 GROUP_2 7 16 30 15
| 10 NESTED_FIELD_1 6 16 25 10
| 10 FILLER 7 26 30 5"""
.stripMargin.replace("\r\n", "\n"))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ class ParseCommentsSpec extends FunSuite {
private val expectedLayout =
"""-------- FIELD LEVEL/NAME --------- --ATTRIBS-- FLD START END LENGTH
|
|GRP_01 1 11 11
| 3 FIELD1 1 1 1 1
| 3 FIELD2 2 2 11 10"""
| 1 GRP_01 3 1 11 11
| 3 FIELD1 2 1 1 1
| 3 FIELD2 3 2 11 10"""
.stripMargin.replace("\r\n", "\n")

test("Test copybook parser handles comment lines") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,21 @@ class ParseDebugRedefinedSpec extends FunSuite {
val expectedLayout =
"""-------- FIELD LEVEL/NAME --------- --ATTRIBS-- FLD START END LENGTH
|
|TRANSDATA 1 7 7
| 10 CURRENCY r 1 1 3 3
| 10 CURRENCY_debug R 2 1 3 3
| 10 FIELD1 r 3 4 7 4
| 10 FIELD1_debug rR 4 4 7 4
| 10 FIELD2 rR 5 4 7 4
| 10 FIELD2_debug rR 6 4 7 4
| 10 FIELD3 rR 7 4 7 4
| 10 FIELD3_debug rR 8 4 7 4
| 10 FIELD4 rR 9 4 7 4
| 10 FIELD4_debug R 10 4 7 4"""
| 1 TRANSDATA 11 1 7 7
| 10 CURRENCY r 2 1 3 3
| 10 CURRENCY_debug R 3 1 3 3
| 10 FIELD1 r 4 4 7 4
| 10 FIELD1_debug rR 5 4 7 4
| 10 FIELD2 rR 6 4 7 4
| 10 FIELD2_debug rR 7 4 7 4
| 10 FIELD3 rR 8 4 7 4
| 10 FIELD3_debug rR 9 4 7 4
| 10 FIELD4 rR 10 4 7 4
| 10 FIELD4_debug R 11 4 7 4"""
.stripMargin.replace("\r\n", "\n")

val copybook = CopybookParser.parseTree(copybookWithRedefined, debugFieldsPolicy = HexValue)


val actualLayout = copybook.generateRecordLayoutPositions()

assert(actualLayout == expectedLayout)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,18 @@ class ParseFieldNamesSpec extends FunSuite {
val expectedLayout =
"""-------- FIELD LEVEL/NAME --------- --ATTRIBS-- FLD START END LENGTH
|
|GRP_01_02 1 11 11
| 3 FIELD_1 1 1 1 1
| 3 FIELD_2 2 2 11 10
|GRP_01_02 12 22 11
| 3 FIELD_1 3 12 12 1
| 3 FIELD_2 4 13 22 10
|GRP0102 23 33 11
| 3 FIELD1 5 23 23 1
| 3 FIELD2 6 24 33 10
|SOMETHING_SOMETHING_DATE_NUM 34 44 11
| 3 FIELD1 7 34 34 1
| 3 FIELD2 8 35 44 10"""
| 1 GRP_01_02 3 1 11 11
| 3 FIELD_1 2 1 1 1
| 3 FIELD_2 3 2 11 10
| 1 GRP_01_02 6 12 22 11
| 3 FIELD_1 5 12 12 1
| 3 FIELD_2 6 13 22 10
| 1 GRP0102 9 23 33 11
| 3 FIELD1 8 23 23 1
| 3 FIELD2 9 24 33 10
| 1 SOMETHING_SOMETHING_DATE_NUM 12 34 44 11
| 3 FIELD1 11 34 34 1
| 3 FIELD2 12 35 44 10"""
.stripMargin.replace("\r\n", "\n")

val copybook = CopybookParser.parseTree(copybookWithCommentLines)
Expand Down
Loading

0 comments on commit 9fef3c5

Please sign in to comment.