PageRenderTime 44ms CodeModel.GetById 12ms app.highlight 27ms RepoModel.GetById 1ms app.codeStats 1ms

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