Exemple #1
0
func Test_open(t *testing.T) {
	mgc, _ := open()
	defer func() {
		value := reflect.ValueOf(mgc).Elem().FieldByName("magic")
		value.Interface().(*magic).close()
	}()

	func(v interface{}) {
		if _, ok := v.(*Magic); !ok {
			t.Fatalf("not a Magic type: %s", reflect.TypeOf(v).String())
		}
	}(mgc)

	magic := reflect.ValueOf(mgc).Elem().FieldByName("magic").Elem()
	path := magic.FieldByName("path")
	cookie := magic.FieldByName("cookie").Elem().UnsafeAddr()

	if path.Kind() != reflect.Slice || path.Len() > 0 {
		t.Errorf("value given {%v ?}, want {%v %d}",
			path.Kind(), reflect.Slice, 0)
	}

	if reflect.ValueOf(cookie).Kind() != reflect.Uintptr || cookie <= 0 {
		t.Errorf("value given {%v 0x%x}, want {%v > %d}",
			reflect.ValueOf(cookie).Kind(),
			cookie, reflect.Uintptr, 0)
	}
}
Exemple #2
0
func TestMagic_String(t *testing.T) {
	mgc, _ := New()
	defer mgc.Close()

	magic := reflect.ValueOf(mgc).Elem().FieldByName("magic").Elem()
	path := magic.FieldByName("path")
	cookie := magic.FieldByName("cookie").Elem().UnsafeAddr()

	// Get whatever the underlying default path is ...
	paths := make([]string, path.Len())
	for i := 0; i < path.Len(); i++ {
		paths[i] = path.Index(i).String()
	}

	v := fmt.Sprintf("Magic{flags:%d path:%s cookie:0x%x}", 0, paths, cookie)
	if ok := CompareStrings(mgc.String(), v); !ok {
		t.Errorf("value given \"%s\", want \"%s\"", mgc.String(), v)
	}
}
Exemple #3
0
func Test_destroy(t *testing.T) {
	mgc, _ := open()
	mgc.destroy()

	magic := reflect.ValueOf(mgc).Elem().FieldByName("magic").Elem()
	path := magic.FieldByName("path")
	cookie := magic.FieldByName("cookie").Elem()

	if path.Kind() != reflect.Slice || path.Len() > 0 {
		t.Errorf("value given {%v ?}, want {%v %d}",
			path.Kind(), reflect.Slice, 0)
	}

	// Should be NULL (at C level) as magic_close() will free underlying Magic database.
	if ok := cookie.IsValid(); ok {
		t.Errorf("value given %v, want %v", ok, false)
	}
}