当前位置: 首页 > 工具软件 > Testify > 使用案例 >

stretchr/testify-源码解读

尉迟卓
2023-12-01

src/github.com/stretchr/testify/require/requirements_test.go:48/requirements_test.go:48

```

func TestIsType(t *testing.T) {

   IsType(t, new(AssertionTesterConformingObject), new(AssertionTesterConformingObject))

   mockT := new(MockT)
   IsType(mockT, new(AssertionTesterConformingObject), new(AssertionTesterNonConformingObject))
   if !mockT.Failed {
      t.Error("Check should fail")
   }
}

 

require.IsType-->assert.IsType-->assert.ObjectsAreEqual(reflect.TypeOf(object), reflect.TypeOf(expectedType))

-->call the reflect.DeepEqual
exp, ok := expected.([]byte)
if !ok {
return reflect.DeepEqual(expected, actual)
}
//else return bytes.Equal(exp, act)

//detail of the reflect.DeepEqual
reflect.DeepEqual(expected, actual)==>
v1 := ValueOf(x)
v2 := ValueOf(y)
if v1.Type() != v2.Type() {
   return false
}
return deepValueEqual(v1, v2, make(map[visit]bool), 0)

deepValueEqual(v1, v2, make(map[visit]bool), 0)==>

hard := func(k Kind) bool {//define hard type
   switch k {
   case Map, Slice, Ptr, Interface:
      return true
   }
   return false
}

if v1.CanAddr() && v2.CanAddr() && hard(v1.Kind()) {
   addr1 := unsafe.Pointer(v1.UnsafeAddr())
   addr2 := unsafe.Pointer(v2.UnsafeAddr())
   if uintptr(addr1) > uintptr(addr2) {
      // Canonicalize order to reduce number of entries in visited.
      // Assumes non-moving garbage collector.
      addr1, addr2 = addr2, addr1
   }

   // Short circuit if references are already seen.
   typ := v1.Type()
   v := visit{addr1, addr2, typ}
   if visited[v] {
      return true
   }

   // Remember for later.
   visited[v] = true
}

 

switch v1.Kind() {
case Array:
   for i := 0; i < v1.Len(); i++ {
      if !deepValueEqual(v1.Index(i), v2.Index(i), visited, depth+1) {
         return false
      }
   }
   return true
case Slice:
   if v1.IsNil() != v2.IsNil() {
      return false
   }
   if v1.Len() != v2.Len() {
      return false
   }
   if v1.Pointer() == v2.Pointer() {
      return true
   }
   for i := 0; i < v1.Len(); i++ {
      if !deepValueEqual(v1.Index(i), v2.Index(i), visited, depth+1) {
         return false
      }
   }
   return true
case Interface:
   if v1.IsNil() || v2.IsNil() {
      return v1.IsNil() == v2.IsNil()
   }
   return deepValueEqual(v1.Elem(), v2.Elem(), visited, depth+1)
case Ptr:
   if v1.Pointer() == v2.Pointer() {
      return true
   }
   return deepValueEqual(v1.Elem(), v2.Elem(), visited, depth+1)
case Struct:
   for i, n := 0, v1.NumField(); i < n; i++ {
      if !deepValueEqual(v1.Field(i), v2.Field(i), visited, depth+1) {
         return false
      }
   }
   return true
case Map:
   if v1.IsNil() != v2.IsNil() {
      return false
   }
   if v1.Len() != v2.Len() {
      return false
   }
   if v1.Pointer() == v2.Pointer() {
      return true
   }
   for _, k := range v1.MapKeys() {
      val1 := v1.MapIndex(k)
      val2 := v2.MapIndex(k)
      if !val1.IsValid() || !val2.IsValid() || !deepValueEqual(val1, val2, visited, depth+1) {
         return false
      }
   }
   return true
case Func:
   if v1.IsNil() && v2.IsNil() {
      return true
   }
   // Can't do better than this:
   return false
default:
   // Normal equality suffices
   return valueInterface(v1, false) == valueInterface(v2, false)
}

 

 

 

```

 

reflect.DeepEaqual is a base function for us.

 

 

 

 类似资料: