Updates to PR 4374 from mwichmann to correct config file hash changes
[scons.git] / test / YACC / live.py
blob9214369561e0b13f55fffc0f140ea9833bb76d4b
1 #!/usr/bin/env python
3 # MIT License
5 # Copyright The SCons Foundation
7 # Permission is hereby granted, free of charge, to any person obtaining
8 # a copy of this software and associated documentation files (the
9 # "Software"), to deal in the Software without restriction, including
10 # without limitation the rights to use, copy, modify, merge, publish,
11 # distribute, sublicense, and/or sell copies of the Software, and to
12 # permit persons to whom the Software is furnished to do so, subject to
13 # the following conditions:
15 # The above copyright notice and this permission notice shall be included
16 # in all copies or substantial portions of the Software.
18 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
19 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
20 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 """
27 Test YACC and YACCFLAGS with a live yacc compiler.
28 """
30 import TestSCons
32 _exe = TestSCons._exe
33 _python_ = TestSCons._python_
35 test = TestSCons.TestSCons()
37 yacc = test.where_is('yacc') or test.where_is('bison') or test.where_is('win_bison')
39 if not yacc:
40 test.skip_test('No yacc or bison found; skipping test.\n')
42 test.file_fixture('wrapper.py')
44 test.write('SConstruct', """
45 DefaultEnvironment(tools=[])
46 foo = Environment(YACCFLAGS='-d', tools=['default', 'yacc'])
47 yacc = foo.Dictionary('YACC')
48 bar = Environment(YACC=r'%(_python_)s wrapper.py ' + yacc, tools=['default', 'yacc'])
49 foo.Program(target='foo', source='foo.y')
50 bar.Program(target='bar', source='bar.y')
51 foo.Program(target='hello', source=['hello.cpp'])
52 foo.CXXFile(target='file.cpp', source=['file.yy'], YACCFLAGS='-d')
53 foo.CFile(target='not_foo', source='foo.y')
54 """ % locals())
56 yacc = r"""
57 %%{
58 #include <stdio.h>
59 extern int yyparse();
60 int yyerror(char *s);
61 int yylex();
63 int main(void)
65 return yyparse();
68 int yyerror(s)
69 char *s;
71 fprintf(stderr, "%%s\n", s);
72 return 0;
75 int yylex()
77 int c;
79 c = fgetc(stdin);
80 return (c == EOF) ? 0 : c;
82 %%}
83 %%%%
84 input: letter newline { printf("%s\n"); };
85 letter: 'a' | 'b';
86 newline: '\n';
87 """
89 test.write("file.yy", """\
90 %token GRAPH_T NODE_T EDGE_T DIGRAPH_T EDGEOP_T SUBGRAPH_T
93 graph: GRAPH_T
97 """)
99 # Apparently, OS X now creates file.hpp like everybody else
100 # I have no idea when it changed; it was fixed in 10.4
101 #import sys
102 #if sys.platform[:6] == 'darwin':
103 # file_hpp = 'file.cpp.h'
104 #else:
105 # file_hpp = 'file.hpp'
106 file_hpp = 'file.hpp'
108 test.write("hello.cpp", """\
109 #include "%(file_hpp)s"
111 int main(void)
114 """ % locals())
116 test.write('foo.y', yacc % 'foo.y')
118 test.write('bar.y', yacc % 'bar.y')
122 test.run(arguments = 'foo' + _exe, stderr = None)
124 test.up_to_date(arguments = 'foo' + _exe)
126 test.must_not_exist(test.workpath('wrapper.out'))
128 test.run(program = test.workpath('foo'), stdin = "a\n", stdout = "foo.y\n")
130 test.must_exist(test.workpath('foo.h'))
132 test.run(arguments = '-c .')
134 test.must_not_exist(test.workpath('foo.h'))
138 test.run(arguments = 'not_foo.c')
140 test.up_to_date(arguments = 'not_foo.c')
142 test.must_not_exist(test.workpath('foo.h'))
143 test.must_exist(test.workpath('not_foo.h'))
145 test.run(arguments = '-c .')
147 test.must_not_exist(test.workpath('not_foo.h'))
151 test.run(arguments = 'bar' + _exe)
153 test.up_to_date(arguments = 'bar' + _exe)
155 test.must_match(test.workpath('wrapper.out'), "wrapper.py\n")
157 test.run(program = test.workpath('bar'), stdin = "b\n", stdout = "bar.y\n")
161 test.run(arguments = '.')
163 test.up_to_date(arguments = '.')
167 test.pass_test()
169 # Local Variables:
170 # tab-width:4
171 # indent-tabs-mode:nil
172 # End:
173 # vim: set expandtab tabstop=4 shiftwidth=4: