PageRenderTime 124ms CodeModel.GetById 40ms app.highlight 41ms RepoModel.GetById 40ms app.codeStats 0ms

/waf_tools/test_scwaf.py

https://code.google.com/p/quirkysoft/
Python | 192 lines | 185 code | 7 blank | 0 comment | 0 complexity | 2eee9b0208592e4ddfed2d6548373404 MD5 | raw file
  1import unittest
  2import waftester
  3import scwaf
  4import Options
  5from Build import BuildContext
  6import Build
  7
  8class SCwafTest(unittest.TestCase):
  9    def testOptDecorator(self):
 10        @scwaf.option
 11        def foobar(opt):
 12            return 1
 13        self.assertTrue(Options.Handler.foobar)
 14        optionctx = Options.Handler()
 15        self.assertTrue(optionctx.foobar())
 16
 17    def testBuildDecorator(self):
 18        @scwaf.build
 19        def foobar(bld):
 20            return 1
 21        self.assertTrue(BuildContext.foobar)
 22        bldctx = BuildContext()
 23        self.assertTrue(bldctx.foobar())
 24
 25    def testCCStaticLib(self):
 26        features = scwaf.get_features('somefile.c other.c')
 27        self.assertEqual('cc', features)
 28        features = scwaf.get_features('somefile.cpp other.c')
 29        self.assertEqual('cxx', features)
 30
 31def CompilerOptions(opt):
 32    opt.tool_options('compiler_cxx')
 33
 34def CompilerConfigure(conf):
 35    conf.check_tool('compiler_cxx')
 36
 37def StaticLibBuild(bld):
 38    obj = bld.static_library('test', 'source')
 39    obj.include_directories('include')
 40
 41foo_h_data = """
 42int in_foo_h();
 43"""
 44foo_c_data = """
 45#include "foo.h"
 46int foo()
 47{
 48    in_foo_h();
 49    return 1;
 50}
 51"""
 52
 53main_cpp_data = """
 54int main()
 55{
 56    return 0;
 57}
 58"""
 59
 60def StaticLibBuild2(bld):
 61    obj = bld.static_library('test', 'source')
 62    obj.include_directories('include', export=False)
 63
 64bar_c_data = """
 65int bar()
 66{
 67    return 1;
 68}
 69"""
 70
 71baz_c_data = """
 72int baz()
 73{
 74    return 1;
 75}
 76"""
 77def StaticLibBuild3(bld):
 78    obj = bld.static_library('test', 'source')
 79    obj.source_directories('source2')
 80
 81def BuildWithArgs(bld):
 82    obj = bld.static_library('test', 'source',
 83            FOOFLAGS = 'foobar',
 84            install_path = '/opt/test')
 85
 86def BuildProgWithArgs(bld):
 87    bld.program('test', 'source')
 88
 89class SCwafTestFiles(waftester.WafTestCase):
 90
 91    def testStaticLib(self):
 92        self.suite.write_wscript(options=CompilerOptions, configure=CompilerConfigure, build=StaticLibBuild)
 93        self.suite.subdir('source')
 94        self.suite.subdir('include')
 95        self.suite.write_data(foo_c_data, 'source/foo.cpp')
 96        self.suite.write_data(foo_h_data, 'include/foo.h')
 97        self.suite.run_waf()
 98
 99        staticlib_PATTERN = Build.bld.env['staticlib_PATTERN']
100        self.assertTrue(self.suite.exists(staticlib_PATTERN % 'test'))
101        exports = Build.bld.all_task_gen[0].export_incdirs
102        self.assertEquals(sorted(['include', 'source']), sorted(exports))
103
104    def testExportInc(self):
105        self.suite.write_wscript(options=CompilerOptions, configure=CompilerConfigure, build=StaticLibBuild2)
106        self.suite.subdir('source')
107        self.suite.subdir('include')
108        self.suite.write_data(foo_c_data, 'source/foo.cpp')
109        self.suite.write_data(foo_h_data, 'include/foo.h')
110        self.suite.run_waf()
111        staticlib_PATTERN = Build.bld.env['staticlib_PATTERN']
112        self.assertTrue(self.suite.exists(staticlib_PATTERN % 'test'))
113        exports = Build.bld.all_task_gen[0].export_incdirs
114        self.assertEquals(['source'], exports)
115
116    def testAddSourceDir(self):
117        self.suite.write_wscript(options=CompilerOptions, configure=CompilerConfigure, build=StaticLibBuild3)
118        self.suite.subdir('source')
119        self.suite.subdir('source2')
120        self.suite.write_data(bar_c_data, 'source/bar.cpp')
121        self.suite.write_data(baz_c_data, 'source2/baz.cpp')
122        self.suite.run_waf()
123        staticlib_PATTERN = Build.bld.env['staticlib_PATTERN']
124        self.assertTrue(self.suite.exists(staticlib_PATTERN % 'test'))
125        exports = Build.bld.all_task_gen[0].export_incdirs
126        self.assertEquals(sorted(['source', 'source2']), sorted(exports))
127
128    def testStaticLibArgs(self):
129        self.suite.write_wscript(options=CompilerOptions, configure=CompilerConfigure, build=BuildWithArgs)
130        self.suite.subdir('source')
131        self.suite.write_data(bar_c_data, 'source/bar.cpp')
132        self.suite.run_waf()
133        taskgen = Build.bld.all_task_gen[0]
134        self.assertEqual('foobar', taskgen.env['FOOFLAGS'])
135        self.assertEqual('/opt/test', taskgen.install_path)
136
137    def testProgram(self):
138        self.suite.write_wscript(options=CompilerOptions, configure=CompilerConfigure, build=BuildProgWithArgs)
139        self.suite.subdir('source')
140        self.suite.write_data(main_cpp_data, 'source/main.cpp')
141        self.suite.run_waf()
142        program_PATTERN = Build.bld.env['program_PATTERN']
143        self.assertTrue(self.suite.exists(program_PATTERN % 'test'))
144
145class SCwafTestFunctions(unittest.TestCase):
146
147    def testParseTaskGenArgs(self):
148        args = {'foo':'foo_value',
149                'BAR12_V': 'bar_value'}
150        env, attr = scwaf.parse_task_gen_args(args)
151        self.assertEquals({'BAR12_V': 'bar_value'} , env)
152        self.assertEquals({'foo': 'foo_value'} , attr)
153
154    def testApplyEnv(self):
155        env = {'BAR_123': 'bar_value', 'FOO_456':'foo_value'}
156        class FakeTG:
157            env = {}
158        fake = FakeTG()
159        scwaf.apply_env_values(fake, env)
160        self.assertEquals('bar_value', fake.env['BAR_123'])
161        self.assertEquals('foo_value', fake.env['FOO_456'])
162
163def VariantConf(conf):
164    conf.addenv('debug')
165    CompilerConfigure(conf)
166    conf.setenv('debug')
167    CompilerConfigure(conf)
168
169    conf.addenv('another', is_copy=True)
170
171def DoNothing(bld):
172    for env in sorted(bld.all_envs):
173        bld.set_variant(env)
174        lib = bld.static_library('test', '.')
175
176class SCwafTestConfig(waftester.WafTestCase):
177
178    def testVariants(self):
179        self.suite.write_wscript(options=CompilerOptions, configure=VariantConf, build=DoNothing)
180        self.suite.write_data(bar_c_data, 'bar.cpp')
181        self.suite.run_waf()
182        self.assertTrue(self.suite.exists('bar_1.o', 'another'), "another bar.o")
183        self.assertTrue(self.suite.exists('bar_2.o', 'debug'), "debug bar.o")
184        self.assertTrue(self.suite.exists('bar_3.o'), "default bar.o")
185        tasks = Build.bld.all_task_gen
186        self.assertEqual('another', tasks[0].env.variant())
187        self.assertEqual('debug', tasks[1].env.variant())
188        self.assertEqual('default', tasks[2].env.variant())
189
190
191if __name__ == '__main__':
192    unittest.main()