Skip to content

Commit 59f078e

Browse files
committed
compiler options using '--'
1 parent 320ef25 commit 59f078e

File tree

5 files changed

+56
-6
lines changed

5 files changed

+56
-6
lines changed

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,16 @@ will create mocks files in current directory for all interfaces
3232
will create directory 'test/mocks' and mocks files within this directory for all interfaces (contains at least one pure virtual function)
3333
which will be within 'namespace::class' declaration
3434

35+
```sh
36+
./gmock.py -d "test/mocks" file1.hpp file2.hpp -- -D LINUX -Iproject/externals
37+
```
38+
'--' separates arguments between gmock.py and compiler options
39+
3540
### Integration with the build system
3641
```sh
3742
find project -iname "*.h" -or -iname "*.hpp" | xargs "project/externals/gmock.py" \
3843
-c "project/conf/gmock.conf" \
39-
-d "project/generated/mocks" \
44+
-d "project/test/mocks" \
4045
-l "Project"
4146
```
4247

@@ -68,8 +73,8 @@ find project -iname "*.h" -or -iname "*.hpp" | xargs "project/externals/gmock.py
6873
# interface: interface class
6974
# mock_methods: generated gmock methods
7075
# generated_dir: generated directory
71-
# mock_file_hpp: mock header file
72-
# mock_file_cpp: mock source file
76+
# mock_file_hpp: mock header file
77+
# mock_file_cpp: mock source file
7378

7479
mock_file_hpp = "%(interface)sMock.hpp"
7580

gmock.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ def generate(self):
227227
return 0
228228

229229
def main(args):
230-
def parse(files):
230+
def parse(files, args):
231231
def generate_includes(includes):
232232
result = []
233233
for include in includes:
@@ -236,12 +236,20 @@ def generate_includes(includes):
236236

237237
return Index.create(excludeDecls = True).parse(
238238
path = "~.hpp"
239+
, args = args
239240
, unsaved_files = [("~.hpp", generate_includes(files))]
240241
, options = TranslationUnit.PARSE_SKIP_FUNCTION_BODIES | TranslationUnit.PARSE_INCOMPLETE
241242
)
242243

244+
clang_args = None
245+
args_split = [i for i, arg in enumerate(args) if arg == '--']
246+
if args_split:
247+
args, clang_args = args[:args_split[0]], args[args_split[0] + 1:]
248+
249+
default_config = os.path.dirname(args[0]) + "/gmock.conf"
250+
243251
parser = OptionParser(usage="usage: %prog [options] files...")
244-
parser.add_option("-c", "--config", dest="config", default=os.path.dirname(args[0]) + "/gmock.conf", help="config FILE (default='gmock.conf')", metavar="FILE")
252+
parser.add_option("-c", "--config", dest="config", default=default_config, help="config FILE (default='gmock.conf')", metavar="FILE")
245253
parser.add_option("-d", "--dir", dest="path", default=".", help="dir for generated mocks (default='.')", metavar="DIR")
246254
parser.add_option("-l", "--limit", dest="decl", default="", help="limit to interfaces within declaration (default='')", metavar="LIMIT")
247255
(options, args) = parser.parse_args(args)
@@ -252,7 +260,7 @@ def generate_includes(includes):
252260
config = {}
253261
execfile(options.config, config)
254262
return mock_generator(
255-
cursor = parse(files = args[1:]).cursor,
263+
cursor = parse(files = args[1:], args = clang_args).cursor,
256264
decl = options.decl,
257265
path = options.path,
258266
mock_file_hpp = config['mock_file_hpp'],

test/given/I3I4.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ class I4
3131
virtual Enum f2()const = 0; // comment
3232
};
3333

34+
#ifdef CLASS_I5
35+
class I5
36+
{
37+
public:
38+
virtual void f0(void) = 0;
39+
};
40+
#endif
41+
3442
} // namespace n1
3543

3644
#endif

test/gmock_test.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ def test_gmock_many_files(self):
5454
self.assertTrue('I4Mock.hpp' in os.listdir(self.generated_dir))
5555
self.assertMocks();
5656

57+
def test_gmock_clang_args(self):
58+
self.assertEqual(0, gmock.main(['../gmock.py', '-d', self.generated_dir, '-l', 'n1', 'given/I3I4.hpp', '--', '-D CLASS_I5']))
59+
self.assertEqual(3, len([name for name in os.listdir(self.generated_dir)]))
60+
self.assertTrue('I3Mock.hpp' in os.listdir(self.generated_dir))
61+
self.assertTrue('I4Mock.hpp' in os.listdir(self.generated_dir))
62+
self.assertTrue('I5Mock.hpp' in os.listdir(self.generated_dir))
63+
self.assertMocks();
64+
5765
def test_gmock_custom_conf(self):
5866
self.assertEqual(0, gmock.main(['../gmock.py', '-c', 'test.conf', '-d', self.generated_dir, '-l', 'n1', 'given/I2.hpp']))
5967
self.assertEqual(2, len([name for name in os.listdir(self.generated_dir)]))

test/then/I5Mock.hpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* file generated by gmock: I5Mock.hpp
3+
*/
4+
#ifndef I5MOCK_HPP
5+
#define I5MOCK_HPP
6+
7+
#include <gmock/gmock.h>
8+
#include "./given/I3I4.hpp"
9+
10+
namespace n1 {
11+
12+
class I5Mock : public I5
13+
{
14+
public:
15+
MOCK_METHOD0(f0, void());
16+
};
17+
18+
} // namespace n1
19+
20+
#endif // I5MOCK_HPP
21+

0 commit comments

Comments
 (0)