独望枫

人在尘世间,有缘自相见,变化千千万,未开窍,已迷恋
posts - 45, comments - 0, trackbacks - 0, articles - 1
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

python在C++单元测试中的应用

Posted on 2023-01-22 01:15 小菜枫 阅读(223) 评论(0)  编辑 收藏 引用 所属分类: 器者,工具也

C/C++在进行单元测试的时候,总会遇到一些例如访问权限之类的问题,GCC下有-fno-access-control,window下的主流VS系列并没找到相似功能的编译参数可选,导致window下单元测试时如果遇到private的访问令人头痛。

普遍的做法是通过一个宏定义进行转换[private --> public],单元测试工程共定义有该宏,则编译运行时,实际都是public的,源码工程中则没有定义该宏,private的控制起效。但这样需要在源码工程的文件中进行上述转换的定义,限制性较强。

这里使用python进行遍历处理,通过IDEbuildevent进行触发:

编译前进行遍历,将private注释

编译后进行反注释,还原源码

由于工程原因,部分源码文件需要指定的编码格式保存,这里需要使用到一个依赖库chardet,默认安装完的python并无该库,可以通过下列指令进行安装:

pip install chardet

上述提到的两个脚本源码[基于python3]如下:

编译前进行遍历,将private注释

  1 import os
  2 import io
  3 import sys
  4 import chardet
  5 import codecs
  6 
  7 def get_platform():
  8     return sys.platform
  9 
 10 def is_linux():
 11     if 'linux' in get_platform():
 12         return True
 13     return False
 14 
 15 def is_windows():
 16     if 'win' in get_platform():
 17         return True
 18     return False
 19 
 20 def find_pro():
 21     filelists = os.listdir('./')
 22     # print(filelists)
 23     pro = ''
 24     for filename in filelists:
 25         if '.vcxproj' == os.path.splitext(filename)[-1].lower()\
 26             and filename.lower().startswith('ut'):
 27             pro = filename
 28     return pro
 29 
 30 
 31 def read_file_content_lines(pro_name):
 32     fp = open(pro_name, 'r', encoding='utf-8')
 33     filecontent = fp.readlines()
 34     fp.close()
 35     return filecontent
 36 
 37 def get_modulename(contentlines):
 38     modulename=''
 39     for line in contentlines:
 40         if "TARGET" == line.upper().replace(' ','').strip().split('=')[0]:
 41             modulename = line.replace(' ','').strip().split('=')[1][4:]
 42             break
 43     return modulename
 44 
 45 def get_dependent_heads(contentlines, modulename):
 46     dependents = []
 47     isdepend = False
 48     _start = ' start'
 49     _end = ' end'
 50     _head_dependent = ' head dependent'
 51     for line in contentlines:
 52         if (modulename + _head_dependent + _start) in line:
 53             isdepend = True
 54             continue
 55         if (modulename + _head_dependent + _end) in line:
 56             isdepend = False
 57             break
 58         if isdepend:
 59             if modulename.lower() in line.lower():
 60                 dependents.append(line)
 61     return dependents
 62 
 63 def get_dependent_heads_vs(contentlines):
 64     dependents = []
 65     _head_flag = '<ClInclude Include='
 66     for line in contentlines:
 67         if line.strip().startswith(_head_flag):
 68             headsplit = line.split("\"")[1]
 69             dependents.append(headsplit)
 70     return dependents
 71 
 72 def get_file_encoding(file):
 73     fenc = open(file, 'rb')
 74     data = fenc.read()
 75     fenc.close()
 76     return chardet.detect(data)['encoding']
 77 
 78 def read_content_lines(filename, enc):
 79     with codecs.open(filename, 'r', encoding=enc) as fr:
 80         filecontent = fr.readlines()
 81         return filecontent
 82 
 83 def write_content(filename, enc, content):
 84     flagline = 0
 85     bef = b'\xef'
 86     bbb = b'\xbb'
 87     bbf = b'\xbf'
 88     with codecs.open(filename, 'w', encoding=enc) as fw:
 89         for l in content :
 90             if u"private:\r\n" == l:
 91                 commentprivate = u"//" + l
 92                 fw.write(commentprivate)
 93             else:
 94                 fw.write(l)
 95     
 96 def change_private_to_public(dependents):
 97     for dirname in dependents:
 98         filename = os.path.abspath(dirname)
 99         encod = get_file_encoding(filename)
100         filecontent = read_content_lines(filename, encod)
101         write_content(filename, encod, filecontent)
102 
103 
104 if __name__ == '__main__':
105     testpro = find_pro()
106     if '' != testpro:
107         filecontent = read_file_content_lines(testpro)
108         dependent_heads = get_dependent_heads_vs(filecontent)
109         change_private_to_public(dependent_heads)
110     else:
111         print('not found *.pro')
112 

编译完成后进行反注释,还原代码

 1 import os
 2 import codecs
 3 from pre_unittest import find_pro
 4 from pre_unittest import read_file_content_lines
 5 from pre_unittest import get_modulename
 6 from pre_unittest import get_dependent_heads_vs
 7 from pre_unittest import get_file_encoding
 8 
 9 def read_content_lines(filename, enc):
10     with codecs.open(filename, 'r', encoding=enc) as fr:
11         filecontent = fr.readlines()
12         return filecontent
13 
14 def write_content(filename, enc, content):
15     linezero = 0
16     ff = bytes()
17     with codecs.open(filename, 'w', encoding=enc) as fw:
18         for l in content :
19             if u"//private:\r\n" == l:
20                 fw.write("private:\r\n")
21             else:
22                 fw.write(l)
23 
24 def revert_public_to_private(dependents):
25     for dirname in dependents:
26         filename = os.path.abspath(dirname)
27         encod = get_file_encoding(filename)
28         filecontent = read_content_lines(filename, encod)
29         write_content(filename, encod, filecontent)
30 
31 if __name__ == '__main__':
32     testpro = find_pro()
33     if '' != testpro:
34         filecontent = read_file_content_lines(testpro)
35         dependent_heads = get_dependent_heads_vs(filecontent)
36         revert_public_to_private(dependent_heads)
37     else:
38         print('not found *.pro')
39 

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理