github.com/tencent/goom@v1.0.1/cache.go (about)

     1  // Package mocker 定义了 mock 的外层用户使用 API 定义,
     2  // 包括函数、方法、接口、未导出函数(或方法的)的 Mocker 的实现。
     3  // 当前文件实现了当对同一方法或函数名进行重复构造时可以沿用缓存中已建好的 Mocker,
     4  // 以防止在一个单测内重复构造 Mocker 时, 对上一个相同函数或方法的 Mocker 的内容规则造成覆盖。
     5  package mocker
     6  
     7  import (
     8  	"strings"
     9  
    10  	"github.com/tencent/goom/internal/iface"
    11  )
    12  
    13  // CachedMethodMocker 带缓存的方法 Mocker,将同一个函数或方法的 Mocker 进行 cache
    14  type CachedMethodMocker struct {
    15  	*MethodMocker
    16  	mCache  map[string]*MethodMocker
    17  	umCache map[string]UnExportedMocker
    18  }
    19  
    20  // NewCachedMethodMocker 创建新的带缓存的方法 Mocker
    21  func NewCachedMethodMocker(m *MethodMocker) *CachedMethodMocker {
    22  	return &CachedMethodMocker{
    23  		MethodMocker: m,
    24  		mCache:       make(map[string]*MethodMocker, 16),
    25  		umCache:      make(map[string]UnExportedMocker, 16),
    26  	}
    27  }
    28  
    29  // String mock 的名称
    30  func (m *CachedMethodMocker) String() string {
    31  	s := make([]string, 0, len(m.mCache)+len(m.umCache))
    32  	for _, v := range m.mCache {
    33  		s = append(s, v.String())
    34  	}
    35  	for _, v := range m.umCache {
    36  		s = append(s, v.String())
    37  	}
    38  	return strings.Join(s, ", ")
    39  }
    40  
    41  // Method 设置结构体的方法名
    42  func (m *CachedMethodMocker) Method(name string) ExportedMocker {
    43  	if mocker, ok := m.mCache[name]; ok && !mocker.Canceled() {
    44  		return mocker
    45  	}
    46  	mocker := NewMethodMocker(m.pkgName, m.MethodMocker.structDef)
    47  	mocker.Method(name)
    48  	m.mCache[name] = mocker
    49  	return mocker
    50  }
    51  
    52  // ExportMethod 导出私有方法
    53  func (m *CachedMethodMocker) ExportMethod(name string) UnExportedMocker {
    54  	if mocker, ok := m.umCache[name]; ok && !mocker.Canceled() {
    55  		return mocker
    56  	}
    57  	mocker := NewMethodMocker(m.pkgName, m.MethodMocker.structDef)
    58  	exportedMocker := mocker.ExportMethod(name)
    59  	m.umCache[name] = exportedMocker
    60  	return exportedMocker
    61  }
    62  
    63  // Cancel 取消 mock
    64  func (m *CachedMethodMocker) Cancel() {
    65  	for _, v := range m.mCache {
    66  		v.Cancel()
    67  	}
    68  	for _, v := range m.umCache {
    69  		v.Cancel()
    70  	}
    71  }
    72  
    73  // CachedUnexportedMethodMocker 带缓存的未导出方法 Mocker
    74  type CachedUnexportedMethodMocker struct {
    75  	*UnexportedMethodMocker
    76  	mockers map[string]*UnexportedMethodMocker
    77  }
    78  
    79  // NewCachedUnexportedMethodMocker 创建新的带缓存的未导出方法 Mocker
    80  func NewCachedUnexportedMethodMocker(m *UnexportedMethodMocker) *CachedUnexportedMethodMocker {
    81  	return &CachedUnexportedMethodMocker{
    82  		UnexportedMethodMocker: m,
    83  		mockers:                make(map[string]*UnexportedMethodMocker, 16),
    84  	}
    85  }
    86  
    87  // String mock 的名称或描述
    88  func (m *CachedUnexportedMethodMocker) String() string {
    89  	s := make([]string, 0, len(m.mockers))
    90  	for _, v := range m.mockers {
    91  		s = append(s, v.String())
    92  	}
    93  	return strings.Join(s, ", ")
    94  }
    95  
    96  // Method 设置结构体的方法名
    97  func (m *CachedUnexportedMethodMocker) Method(name string) UnExportedMocker {
    98  	if mocker, ok := m.mockers[name]; ok && !mocker.Canceled() {
    99  		return mocker
   100  	}
   101  	mocker := NewUnexportedMethodMocker(m.pkgName, m.UnexportedMethodMocker.structName)
   102  	mocker.Method(name)
   103  	m.mockers[name] = mocker
   104  	return mocker
   105  }
   106  
   107  // Cancel 清除 mock
   108  func (m *CachedUnexportedMethodMocker) Cancel() {
   109  	for _, v := range m.mockers {
   110  		v.Cancel()
   111  	}
   112  }
   113  
   114  // CachedInterfaceMocker 带缓存的 Interface Mocker
   115  type CachedInterfaceMocker struct {
   116  	*DefaultInterfaceMocker
   117  	mockers map[string]InterfaceMocker
   118  	ctx     *iface.IContext
   119  }
   120  
   121  // NewCachedInterfaceMocker 创建新的带缓存的 Interface Mocker
   122  func NewCachedInterfaceMocker(interfaceMocker *DefaultInterfaceMocker) *CachedInterfaceMocker {
   123  	return &CachedInterfaceMocker{
   124  		DefaultInterfaceMocker: interfaceMocker,
   125  		mockers:                make(map[string]InterfaceMocker, 16),
   126  		ctx:                    interfaceMocker.ctx,
   127  	}
   128  }
   129  
   130  // String mock 的名称或描述
   131  func (m *CachedInterfaceMocker) String() string {
   132  	s := make([]string, 0, len(m.mockers))
   133  	for _, v := range m.mockers {
   134  		s = append(s, v.String())
   135  	}
   136  	return strings.Join(s, ", ")
   137  }
   138  
   139  // Method 指定方法名
   140  func (m *CachedInterfaceMocker) Method(name string) InterfaceMocker {
   141  	if mocker, ok := m.mockers[name]; ok && !mocker.Canceled() {
   142  		return mocker
   143  	}
   144  	mocker := NewDefaultInterfaceMocker(m.pkgName, m.iFace, m.ctx)
   145  	mocker.Method(name)
   146  	m.mockers[name] = mocker
   147  	return mocker
   148  }
   149  
   150  // Cancel 取消 mock
   151  func (m *CachedInterfaceMocker) Cancel() {
   152  	for _, v := range m.mockers {
   153  		v.Cancel()
   154  	}
   155  }
   156  
   157  // Canceled 是否取消了 mock
   158  func (m *CachedInterfaceMocker) Canceled() bool {
   159  	return m.ctx.Canceled()
   160  }