/tst/org/diffkit/common/kvc/tst/TestKeyValueCoder.groovy

http://diffkit.googlecode.com/ · Groovy · 155 lines · 120 code · 28 blank · 7 comment · 40 complexity · 1a3972c2e229c8325597e050b0696d30 MD5 · raw file

  1. /**
  2. * Copyright Š 2010, Joseph Panico
  3. * All rights reserved.
  4. */
  5. package org.diffkit.common.kvc.tst
  6. import org.diffkit.common.kvc.DKKeyValueCoder;
  7. import groovy.util.GroovyTestCase;
  8. /**
  9. * @author jpanico
  10. */
  11. public class TestKeyValueCoder extends GroovyTestCase {
  12. public void testSetValueAtPath(){
  13. Person grandma = new Person('grandma', false, null, null, null)
  14. Person mom = ['mom', false, null, null, null]
  15. Person dad = ['dad', false, null, null, null]
  16. Person me = ['me', false, null, null, null]
  17. DKKeyValueCoder.instance.setValueAtPath("mother", mom, me)
  18. DKKeyValueCoder.instance.setValueAtPath("father", dad, me)
  19. DKKeyValueCoder.instance.setValueAtPath("mother.mother", grandma, me)
  20. DKKeyValueCoder.instance.setValueAtPath("mother.mother.isDead", true, me)
  21. DKKeyValueCoder.instance.setValueAtPath("mother.mother.state", 'FL', me)
  22. assert me.father == dad
  23. assert me.mother == mom
  24. assert me.mother.mother == grandma
  25. assert me.mother.mother.isDead
  26. assert me.mother.mother.state == 'FL'
  27. }
  28. public void testGetValueAtPath(){
  29. Person grandma = new Person('grandma', true, (Person)null, (Person)null, (Set)null)
  30. grandma.state = 'FL'
  31. Person mom = ['mom', false, grandma, null, null]
  32. mom.state = 'CA'
  33. Person dad = ['dad', true, null, null, null]
  34. dad.state = 'NJ'
  35. Person me = ['me', false, mom, dad, null]
  36. me.state = 'MA'
  37. assert DKKeyValueCoder.instance.getValueAtPath("name", me) == 'me'
  38. assert ! DKKeyValueCoder.instance.getValueAtPath("isDead", me)
  39. assert ! DKKeyValueCoder.instance.getValueAtPath("children", me)
  40. assert DKKeyValueCoder.instance.getValueAtPath("mother.name", me) == 'mom'
  41. assert ! DKKeyValueCoder.instance.getValueAtPath("mother.children", me)
  42. assert DKKeyValueCoder.instance.getValueAtPath("mother.mother.name", me) == 'grandma'
  43. assert DKKeyValueCoder.instance.getValueAtPath("mother.mother.isDead", me)
  44. }
  45. public void testSetValue(){
  46. SubTarget target = ['superA', 'superB', 'superC', 'subA', 'subB', 'subC']
  47. println "target->$target"
  48. DKKeyValueCoder.instance.setValue(null,null,target)
  49. DKKeyValueCoder.instance.setValue('junk',null,null)
  50. DKKeyValueCoder.instance.setValue('junk','junk',target)
  51. DKKeyValueCoder.instance.setValue('superVarB','newSuperB',target)
  52. assert target._superVarB == 'newSuperB'
  53. DKKeyValueCoder.instance.setValue('pointerToSuperVarC','newSuperC',target)
  54. assert target.pointerToSuperVarC == 'newSuperC'
  55. DKKeyValueCoder.instance.setValue('superVarA','newSuperA',target)
  56. assert target.superVarA == 'newSuperA'
  57. }
  58. public void testGetValue(){
  59. SubTarget target = ['superA', 'superB', 'superC', 'subA', 'subB', 'subC']
  60. println "target->$target"
  61. assert ! DKKeyValueCoder.instance.getValue(null,target)
  62. assert ! DKKeyValueCoder.instance.getValue('junk',null)
  63. assert ! DKKeyValueCoder.instance.getValue('junk',target)
  64. assert ! DKKeyValueCoder.instance.getValue('superVarA',null)
  65. assert DKKeyValueCoder.instance.getValue('superVarA',target) == 'superA'
  66. assert DKKeyValueCoder.instance.getValue('superVarB',target) == 'superB'
  67. assert DKKeyValueCoder.instance.getValue('pointerToSuperVarC',target) == 'superC'
  68. assert DKKeyValueCoder.instance.getValue('subVarA',target) == 'subA'
  69. assert DKKeyValueCoder.instance.getValue('subVarB',target) == 'subB'
  70. assert DKKeyValueCoder.instance.getValue('subVarC',target) == 'subC'
  71. assert DKKeyValueCoder.instance.getValue('pointerToSubVarC',target) == 'subC'
  72. }
  73. public void testFirstElement(){
  74. assert ! DKKeyValueCoder.firstKeyPathElement(null)
  75. assert DKKeyValueCoder.firstKeyPathElement("in this age") == 'in this age'
  76. assert DKKeyValueCoder.firstKeyPathElement("time.out") == 'time'
  77. assert ! DKKeyValueCoder.firstKeyPathElement(".")
  78. assert ! DKKeyValueCoder.firstKeyPathElement("..")
  79. assert DKKeyValueCoder.firstKeyPathElement(".hello.") == 'hello'
  80. assert DKKeyValueCoder.firstKeyPathElement("a.regular.normal.path.hello.") == 'a'
  81. }
  82. public void testLastElement(){
  83. assert ! DKKeyValueCoder.lastKeyPathElement(null)
  84. assert DKKeyValueCoder.lastKeyPathElement("in this age") == 'in this age'
  85. assert DKKeyValueCoder.lastKeyPathElement("time.out") == 'out'
  86. assert ! DKKeyValueCoder.lastKeyPathElement(".")
  87. assert ! DKKeyValueCoder.lastKeyPathElement("..")
  88. assert DKKeyValueCoder.lastKeyPathElement(".hello.") == 'hello'
  89. assert DKKeyValueCoder.lastKeyPathElement("a.regular.normal.path.hello.") == 'hello'
  90. }
  91. public void testPathByRemovingFirst(){
  92. assert ! DKKeyValueCoder.pathByRemovingFirstKeyPathElement(null)
  93. assert ! DKKeyValueCoder.pathByRemovingFirstKeyPathElement("in this age")
  94. assert DKKeyValueCoder.pathByRemovingFirstKeyPathElement("time.out") == 'out'
  95. assert ! DKKeyValueCoder.pathByRemovingFirstKeyPathElement(".")
  96. assert ! DKKeyValueCoder.pathByRemovingFirstKeyPathElement("..")
  97. assert ! DKKeyValueCoder.pathByRemovingFirstKeyPathElement(".hello.")
  98. assert DKKeyValueCoder.pathByRemovingFirstKeyPathElement("a.regular.normal.path.hello.") == 'regular.normal.path.hello'
  99. }
  100. public void testPathByRemovingLast(){
  101. assert ! DKKeyValueCoder.pathByRemovingLastKeyPathElement(null)
  102. assert !DKKeyValueCoder.pathByRemovingLastKeyPathElement("in this age")
  103. assert DKKeyValueCoder.pathByRemovingLastKeyPathElement("time.out") == 'time'
  104. assert !DKKeyValueCoder.pathByRemovingLastKeyPathElement(".")
  105. assert ! DKKeyValueCoder.pathByRemovingLastKeyPathElement("..")
  106. assert ! DKKeyValueCoder.pathByRemovingLastKeyPathElement(".hello.")
  107. assert DKKeyValueCoder.pathByRemovingLastKeyPathElement("a.regular.normal.path.hello.") == 'a.regular.normal.path'
  108. }
  109. public void testKeyPathElements(){
  110. assert ! DKKeyValueCoder.keyPathElements(null)
  111. assert DKKeyValueCoder.keyPathElements("in this age") == ((String[])['in this age'])
  112. assert DKKeyValueCoder.keyPathElements("time.out") == ((String[])['time','out'])
  113. assert ! DKKeyValueCoder.keyPathElements(".")
  114. assert ! DKKeyValueCoder.keyPathElements("..")
  115. assert DKKeyValueCoder.keyPathElements(".hello.") == ((String[])['hello'])
  116. assert DKKeyValueCoder.keyPathElements("a.regular.normal.path.hello.") == ((String[])['a','regular','normal','path','hello' ])
  117. }
  118. public void testKeyPathElementCount(){
  119. assert DKKeyValueCoder.keyPathElementCount(null) == 0
  120. assert DKKeyValueCoder.keyPathElementCount("in this age") == 1
  121. assert DKKeyValueCoder.keyPathElementCount("time.out") == 2
  122. assert DKKeyValueCoder.keyPathElementCount(".") == 0
  123. assert DKKeyValueCoder.keyPathElementCount("..") == 0
  124. assert DKKeyValueCoder.keyPathElementCount(".hello.") == 1
  125. assert DKKeyValueCoder.keyPathElementCount("a.regular.normal.path.hello.") == 5
  126. }
  127. }