Skip to content

Commit 2038539

Browse files
committed
added test and ready to release
1 parent 6f4b278 commit 2038539

File tree

10 files changed

+123
-41
lines changed

10 files changed

+123
-41
lines changed

MANIFEST.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include cppguts/tests/data/*.in

README.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# cppguts
2-
If your C/C++ project depends on external C/C++ projects and
2+
If your C/C++ project depends on some external C/C++ projects and
33
you want to make some changes in external functions/methods
44
and you would like to copy/paste these changes automatically
55
then this package may help you.
@@ -13,7 +13,7 @@ We will discuss `editcpp` as it is the objective tool.
1313
**`editcpp` doesn't work with templates.**
1414

1515
## The idea behind `editcpp` tool
16-
`editcpp` tool uses `libclang` to find function/method definition.
16+
`editcpp` uses `libclang` to find function/method definition start/end lines in text file (.c, .h, .hpp, .cpp or whatever extension you use for your C/C++ project).
1717
`libclang` parses each `dest.cpp` and `src.cpp` and everything that is
1818
included by `#include` preprocessor directives. Then `editcpp` tool
1919
selects all functions and methods defined in `dest.cpp` and `src.cpp`
@@ -127,10 +127,15 @@ namespace ns {
127127
}
128128
}
129129
```
130-
Run: `editcpp --source-file=src.h --dest-file=dest.h --oldfile-keep -std=c++17`
130+
Run:
131131

132-
The `-std=c++17` is simply for illustration but you can pass any clang flag
133-
for example `-I` to include directories that are required by the files.
132+
`editcpp --src-file=src.h --dest-file=dest.h --oldfile-keep -std=c++03`
133+
134+
Another option is to run test:
135+
136+
`python -m unittest cppguts.tests.test_cppguts`
137+
138+
The `-std=c++03` tells the clang to parse the files as C++. Also you may need to use any other clang flags like `-I` to include directories that are required by the files.
134139

135140
`--oldfile-keep` is used to keep the original file (it will be renamed
136141
by adding `_OLD_N` suffix). Otherwise use `--oldfile-delete` to delete the

cppguts/editcpp.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,13 +174,13 @@ def compare_method_argument_types(node_1: Cursor, node_2: Cursor) -> bool:
174174

175175
def main():
176176
parser = argparse.ArgumentParser(description=
177-
'Replace C++ function/method definitions in destination file '
177+
'Replace C++ function/method definition in destination file '
178178
'(but doesn`t work with templates). '
179-
'One source file may contain several function/method definitions. '
179+
'One source file may contain several functions/methods to replace. '
180180
'After passing `editcpp` flags you are allowed to pass clang '
181181
'commands like `-I` (to include dir), `-std=c++17` and other. '
182182
'Dont pass a file without flag to clang! Use `--dest-file=` instead.')
183-
parser.add_argument('--source-file', dest='srcfile', action='store',
183+
parser.add_argument('--src-file', dest='srcfile', action='store',
184184
type=type('string'), required=True, default=None,
185185
help='file with new functions definitions')
186186
parser.add_argument('--dest-file', dest='destfile', action='store',
@@ -218,12 +218,14 @@ def main():
218218
method_def_nodes_src = []
219219
find_method_def_nodes(tu_src.cursor, method_def_nodes_src, args.srcfile)
220220
if not method_def_nodes_src:
221-
parser.error("unable to find any method definition in source file:\t" + args.srcfile)
221+
parser.error("unable to find any method definition in source file:\n" +
222+
args.srcfile + "\nprobably you forgot to pass `-std=c++03` (or higher) flag?")
222223

223224
method_def_nodes_dest = []
224225
find_method_def_nodes(tu_dest.cursor, method_def_nodes_dest, args.destfile)
225226
if not method_def_nodes_dest:
226-
parser.error("unable to find any function/method definition in destination file:\t" + args.destfile)
227+
parser.error("unable to find any function/method definition in destination file:\n" +
228+
args.destfile + "\nprobably you forgot to pass `-std=c++11` (or higher) flag?")
227229

228230
# read source file
229231
with open(args.srcfile, mode='r') as file:

cppguts/tests/data/tmp/dest.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include <iostream>
2+
3+
// helper class that is used by the target class `Src`
4+
class SrcPrivate {
5+
public:
6+
SrcPrivate(){};
7+
8+
void add(int v){
9+
val += v;
10+
}
11+
12+
void substract(int v){
13+
val -= v;
14+
}
15+
16+
private:
17+
int val;
18+
}
19+
20+
// target class
21+
class Src {
22+
// method defined inside the class
23+
void add(SrcPrivate p, int v){
24+
25+
p.add(v) + 10;
26+
27+
}
28+
29+
// method defined outside the class
30+
void substract(SrcPrivate p, int v);
31+
32+
// we won't tuch this method
33+
void untouched_print(int v){
34+
std::cout << "The value is:\t" << v << std::endl;
35+
}
36+
}
37+
38+
void Src::substract(SrcPrivate p, int v){
39+
p.substract(v) - 10;
40+
}
41+
42+
// simple function
43+
void foo(int &v){
44+
v -= 10;
45+
}
46+
47+
namespace ns {
48+
// function in namespace
49+
void bar(int &v){
50+
v += 10;
51+
}
52+
}
File renamed without changes.

cppguts/tests/data/src.h renamed to cppguts/tests/data/tmp/src.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ void Src::substract(SrcPrivate p, int v){
1717
p.substract(v) - 10;
1818
}
1919

20-
// NEW simple function definition
20+
// NEW simple function definition
2121
void foo(int &v){
2222
v -= 10;
2323
}

cppguts/tests/test_cppguts.py

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,30 @@
1-
import unittest, pathlib
1+
from pathlib import Path
2+
import shutil
3+
import subprocess, os, filecmp
4+
import unittest
25

36

47
class test_basics(unittest.TestCase):
5-
def setUp(self):
6-
self.srcin = 'data/src.h.in'
7-
self.destin = 'data/dest.h.in'
8-
self.src = 'data/tmp/src.h'
9-
self.dest = 'data/tmp/dest.h'
10-
11-
def tearDown(self):
12-
h5File = self.seisContainer.getH5File()
13-
14-
seisContainer = None
15-
p = h5geo.SeisParam()
16-
FILE_NAME = None
17-
SEIS_NAME1 = None
18-
SEIS_NAME2 = None
8+
this_dir = os.path.dirname(__file__)
9+
data_dir = this_dir + '/data'
10+
tmp_dir = data_dir + '/tmp'
11+
src = tmp_dir + '/src.h'
12+
dest = tmp_dir + '/dest.h'
13+
srcin = data_dir + '/src.h.in'
14+
destin = data_dir + '/dest.h.in'
1915

20-
def test_createContainer(self):
21-
self.assertTrue(os.path.isfile(self.FILE_NAME))
22-
23-
def test_createSeisWithDifferentCreateFlags(self):
24-
seis = self.seisContainer.createSeis(self.SEIS_NAME1, self.p, h5geo.CreationType.OPEN_OR_CREATE)
25-
self.assertFalse(seis is None)
16+
def setUp(self):
17+
shutil.rmtree(self.tmp_dir, ignore_errors=True)
18+
Path(self.tmp_dir).mkdir(parents=True, exist_ok=True)
19+
shutil.copy(self.srcin, self.src)
20+
shutil.copy(self.destin, self.dest)
2621

27-
seis = self.seisContainer.createSeis(self.SEIS_NAME1, self.p, h5geo.CreationType.CREATE_OR_OVERWRITE)
28-
self.assertFalse(seis is None)
22+
# def tearDown(self):
23+
# shutil.rmtree(self.tmp_dir, ignore_errors=True)
2924

30-
seis = self.seisContainer.createSeis(self.SEIS_NAME1, self.p, h5geo.CreationType.CREATE_UNDER_NEW_NAME)
31-
self.assertFalse(seis is None)
25+
def test_basics(self):
26+
subprocess.run(['editcpp', '--src-file', self.src, '--dest-file', self.dest, '--oldfile-keep', '-std=c++03'])
3227

33-
seis = self.seisContainer.createSeis(self.SEIS_NAME1, self.p, h5geo.CreationType.OPEN_OR_CREATE)
34-
self.assertFalse(seis is None)
28+
with open(self.dest) as f:
29+
with open(self.destin) as fin:
30+
self.assertTrue(f != fin)

pyproject.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[build-system]
2+
requires = [
3+
"setuptools>=42",
4+
"wheel"
5+
]
6+
build-backend = "setuptools.build_meta"

setup.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[metadata]
2+
description-file = README.md

setup.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,37 @@
1-
from setuptools import setup
1+
import setuptools
22

3-
setup(
3+
setuptools.setup(
44
name='cppguts',
55
version='0.1.0',
6-
packages=['cppguts'],
6+
packages=setuptools.find_packages(),
77
url='https://github.com/tierra-colada/cppguts',
88
license='MIT',
99
author='kerim khemrev',
1010
author_email='tierracolada@gmail.com',
1111
description='python package aimed at C++ source code correction',
12+
download_url='https://github.com/tierra-colada/cppguts/archive/refs/tags/v0.1.0.tar.gz',
13+
classifiers=[
14+
'Development Status :: 5 - Production/Stable',
15+
'Environment :: Console',
16+
'Intended Audience :: Developers',
17+
'Topic :: Software Development :: Build Tools',
18+
'Programming Language :: Python :: 3.6',
19+
'Topic :: Software Development :: Code Generators',
20+
'License :: OSI Approved :: MIT License',
21+
'Programming Language :: Python :: 3',
22+
'Programming Language :: Python :: 3.6',
23+
'Programming Language :: Python :: 3.7',
24+
'Programming Language :: Python :: 3.8',
25+
'Programming Language :: Python :: 3.9',
26+
],
27+
keywords='c cpp c-parser cpp-parser c-editor cpp-editor c-generator cpp-generator',
1228
entry_points={
1329
'console_scripts': ['editcpp=cppguts.editcpp:main',
1430
'dumpcpp=cppguts.dumpcpp:main']
1531
},
32+
python_requires='>=3',
1633
install_requires=[
1734
'libclang',
1835
],
36+
include_package_data=True # important to copy MANIFEST.in files
1937
)

0 commit comments

Comments
 (0)