Exemplo n.º 1
0
/*
	Should generate a map with all exported items
*/
func TestAll(t *testing.T) {
	fs := mk_struct1()
	bcount := 0
	fcount := 0
	zcount := 0
	athingp := 0
	athing := 0

	fmt.Fprintf(os.Stderr, "test: capture all fields in map\n")
	m := transform.Struct_to_map(fs, "_") // should capture all fields
	for k, _ := range m {
		//fmt.Fprintf( os.Stderr, "k=(%s) v=(%s)\n", k, v )
		if strings.Index(k, "Foo") == 0 {
			fcount++
		} else {
			if strings.Index(k, "Bar") == 0 {
				bcount++
			} else {
				if strings.Index(k, "Baz") == 0 {
					zcount++
				} else {
					if strings.Index(k, "Athingp") == 0 {
						athingp++
					} else {
						if strings.Index(k, "Athing") == 0 {
							athing++
						}
					}
				}
			}
		}
	}

	expect_fcount := 12 + 30 // two additional fields captured in foo space when doing all
	expect_bcount := 10
	expect_zcount := 2
	expect_acount := 2
	expect_apcount := 2
	if fcount != expect_fcount || bcount != expect_bcount || zcount != expect_zcount || athing != expect_acount || athingp != expect_apcount {
		fmt.Fprintf(os.Stderr, "all test: unexpected count, expected %d/%d/%d/%d/%d got %d/%d/%d/%d/%d\n",
			expect_fcount, expect_bcount, expect_zcount, expect_acount, expect_apcount,
			fcount, bcount, zcount, athingp, athing)
		for k, v := range m {
			fmt.Fprintf(os.Stderr, "m[%s] = (%s)\n", k, v)
		}
		t.Fail()
	}
}
Exemplo n.º 2
0
/*
	Should generate a map with only bar tagged items
*/
func TestBarOnly(t *testing.T) {
	fmt.Fprintf(os.Stderr, "test: capture only bar fields\n")
	count := 0
	acount := 0
	apcount := 0

	fs := mk_struct1()
	m := transform.Struct_to_map(fs, "Bar") // should only generate bar elements into map

	for k, v := range m {
		//fmt.Fprintf( os.Stderr, "checking bar: %s = (%s)\n", k, v )			//uncomment to dump as we check things
		if strings.Index(k, "Bar") != 0 {
			if strings.Index(k, "Athingp/Bar_") != 0 {
				if strings.Index(k, "Athing/Bar_") != 0 {
					fmt.Fprintf(os.Stderr, "BAD: unexpected key found in 'bar' map: %s (%v)\n", k, v)
					t.Fail()
				} else {
					acount++
				}
			} else {
				apcount++
			}
		} else {
			//fmt.Printf( "bar: %s = %s\n", k, v )
			count++
		}
	}

	expect_count := 10 + 0                                                             // top level + no array things for bar
	expect_apcount := 0                                                                // pointer to thing struct elements (athing and athingp)
	expect_acount := 1                                                                 // direct struct thing elements
	if count != expect_count || apcount != expect_apcount || acount != expect_acount { // should only find the athing pointer referenced thing
		fmt.Fprintf(os.Stderr, "didn't find right number of elements in bar map, expected %d Foo_, %d apointer things, and %d athings; found bar=%d apthing=%d athing=%d\n",
			expect_count, expect_apcount, expect_acount, count, apcount, acount)
		for k, v := range m {
			fmt.Fprintf(os.Stderr, "m[%s] = (%s)\n", k, v)
		}
	}
}
Exemplo n.º 3
0
/*
	Should generate a map with only foo tagged items
*/
func TestFooOnly(t *testing.T) {
	fs := mk_struct1()
	count := 0
	acount := 0
	apcount := 0

	fmt.Fprintf(os.Stderr, "test: capture only foo fields\n")
	m := transform.Struct_to_map(fs, "Foo") // should only generate foo elements into map
	for k, v := range m {
		fmt.Fprintf(os.Stderr, "checking foo: %s (%v)\n", k, v) // uncomment to dump the map

		if strings.Index(k, "Foo") != 0 { // count things; see struct and build function for expected counts
			if strings.Index(k, "Athingp/Foo_") != 0 {
				if strings.Index(k, "Athing/Foo_") != 0 {
					fmt.Fprintf(os.Stderr, "BAD: unexpected key found in 'foo' map: %s (%v)\n", k, v) // all fields should start with Foo_ or Athingp/Foo_
					t.Fail()
				} else {
					acount++
				}
			} else {
				apcount++
			}
		} else {
			count++
		}
	}

	expect_count := 12 + 28                                                            // top level + array and map elements (don't forget cap and len elements for each array)
	expect_apcount := 1                                                                // pointer to thing struct elements (athing and athingp)
	expect_acount := 0                                                                 // direct struct thing elements
	if count != expect_count || apcount != expect_apcount || acount != expect_acount { // should only find the athing pointer referenced thing
		fmt.Fprintf(os.Stderr, "didn't find right number of elements in foo map, expected %d Foo_, %d apointer things, and %d athings; found foo=%d apthing=%d athing=%d\n",
			expect_count, expect_apcount, expect_acount, count, apcount, acount)
		for k, v := range m {
			fmt.Fprintf(os.Stderr, "m[%s] = (%s)\n", k, v)
		}
	}
}
Exemplo n.º 4
0
/*
	Generate a map, and then use it to populate an empty struct.
*/
func TestPopulate(t *testing.T) {
	fmt.Fprintf(os.Stderr, "test: populate struct from map\n")
	ofs := mk_struct1()                       // make original struct
	fm := transform.Struct_to_map(ofs, "Foo") // generate foo map
	nfs := &Foo_bar{}                         // empty strut to populate

	if ofs.Foo_AJ1 == nfs.Foo_AJ1 { // spot check to ensure that the struct to fill is really 'empty'
		fmt.Fprintf(os.Stderr, "FAIL: old foo_aj1 matches new before transform: (%d)  != (%d)\n", ofs.Foo_AJ1, nfs.Foo_AJ1)
		t.Fail()
	}

	if nfs.Athingp != nil {
		fmt.Fprintf(os.Stderr, "FAIL: athing in new object is not nil before transform\n")
		t.Fail()
	}

	transform.Map_to_struct(fm, nfs, "Foo") // fill it in just basedon foo tags

	if nfs.Athingp == nil {
		fmt.Fprintf(os.Stderr, "FAIL: athing in new object is sitll nil after transform\n")
		t.Fail()
	}

	if ofs.Athingp.Foo_thing_s != nfs.Athingp.Foo_thing_s {
		fmt.Fprintf(os.Stderr, "FAIL: old athingp.foo_thing_s did not match new: (%s) != (%s)\n", ofs.Athingp.Foo_thing_s, nfs.Athingp.Foo_thing_s)
		t.Fail()
	}

	if ofs.Athing.Foo_thing_s == nfs.Athing.Foo_thing_s { // athing isn't foo tagged, so these should NOT match
		fmt.Fprintf(os.Stderr, "FAIL: old athing.foo_thing_s  DID match new and shouldn't: (%s) != (%s)\n", ofs.Athing.Foo_thing_s, nfs.Athing.Foo_thing_s)
		t.Fail()
	}

	if ofs.Foo_AJ1 != nfs.Foo_AJ1 {
		fmt.Fprintf(os.Stderr, "FAIL: old foo_aj1 did not match new: (%d) != (%d)\n", ofs.Foo_AJ1, nfs.Foo_AJ1)
		t.Fail()
	}

	if ofs.Foo_AJ2 != nfs.Foo_AJ2 {
		fmt.Fprintf(os.Stderr, "FAIL: old foo_aj1 did not match new: (%d) != (%d)\n", ofs.Foo_AJ2, nfs.Foo_AJ2)
		t.Fail()
	}

	if ofs.Foo_AJ3 != nfs.Foo_AJ3 {
		fmt.Fprintf(os.Stderr, "FAIL: old foo_aj1 did not match new: (%d) != (%d)\n", ofs.Foo_AJ3, nfs.Foo_AJ3)
		t.Fail()
	}

	if ofs.Foo_str != nfs.Foo_str {
		fmt.Fprintf(os.Stderr, "FAIL: old foo_str did not match new: (%s) != (%s)\n", ofs.Foo_str, nfs.Foo_str)
		t.Fail()
	}
	if ofs.Foo_int != nfs.Foo_int {
		fmt.Fprintf(os.Stderr, "FAIL: old foo_str did not match new: (%d) != (%d)\n", ofs.Foo_int, nfs.Foo_int)
		t.Fail()
	}

	if *ofs.Foo_intp1 != *nfs.Foo_intp1 {
		fmt.Fprintf(os.Stderr, "FAIL: old foo_str did not match new: (%d) != (%d)\n", *ofs.Foo_intp1, *nfs.Foo_intp1)
		t.Fail()
	}

	if ofs.Foo_intp1 == nfs.Foo_intp1 { // pointers should NOT be the same
		fmt.Fprintf(os.Stderr, "FAIL: old and new intp1 pointers are the same and shouldn't be!\n")
		t.Fail()
	}

	if *ofs.Foo_intp2 != *nfs.Foo_intp2 {
		fmt.Fprintf(os.Stderr, "FAIL: old foo_str did not match new: (%d) != (%d)\n", *ofs.Foo_intp2, *nfs.Foo_intp2)
		t.Fail()
	}

	if ofs.Foo_uint != nfs.Foo_uint {
		fmt.Fprintf(os.Stderr, "FAIL: old foo_str did not match new: (%d) != (%d)\n", ofs.Foo_uint, nfs.Foo_uint)
		t.Fail()
	}

	if *ofs.Foo_uintp1 != *nfs.Foo_uintp1 {
		fmt.Fprintf(os.Stderr, "FAIL: old foo_str did not match new: (%d) != (%d)\n", *ofs.Foo_uintp1, *nfs.Foo_uintp1)
		t.Fail()
	}

	if *ofs.Foo_uintp2 != *nfs.Foo_uintp2 {
		fmt.Fprintf(os.Stderr, "FAIL: old foo_str did not match new: (%d) != (%d)\n", *ofs.Foo_uintp2, *nfs.Foo_uintp2)
		t.Fail()
	}

	if ofs.Foo_bool != nfs.Foo_bool {
		fmt.Fprintf(os.Stderr, "FAIL: old foo_str did not match new: (%v) != (%v)\n", ofs.Foo_bool, nfs.Foo_bool)
		t.Fail()
	}

	if *ofs.Foo_boolp != *nfs.Foo_boolp {
		fmt.Fprintf(os.Stderr, "FAIL: old foo_str did not match new: (%v) != (%v)\n", *ofs.Foo_boolp, *nfs.Foo_boolp)
		t.Fail()
	}

	// ---------- validate contents of arrays -----------------------------
	if !array_ck(ofs.Foo_array, nfs.Foo_array, "foo_array") {
		t.Fail()
	}

	if !array_ck(ofs.Foo_arrayp, nfs.Foo_arrayp, "foo_array") {
		t.Fail()
	}

	// ---------- validate contents of maps --------------------------------
	if !imap_ck(ofs.Foo_mapi, nfs.Foo_mapi) {
		fmt.Fprintf(os.Stderr, "map foo_mapi doesn't validate; see above messages\n")
		t.Fail()
	}

	if !tmap_ck(ofs.Foo_mapp, nfs.Foo_mapp) {
		fmt.Fprintf(os.Stderr, "map foo_mapp doesn't validate; see above messages\n")
		t.Fail()
	}
}