66
77RSpec . describe "integration" do
88 module IntegrationHelper
9+ @compiled_parsers = { }
10+
11+ class << self
12+ attr_accessor :compiled_parsers
13+ end
14+
915 def exec_command ( command )
1016 `#{ command } `
1117 raise "#{ command } failed." unless $?. success?
@@ -35,9 +41,14 @@ def test_parser(parser_name, input, expected, expect_success: true, lrama_comman
3541 debug = true
3642 end
3743
38- Lrama ::Command . new ( %W[ -H#{ parser_h_path } -o#{ parser_c_path } ] + lrama_command_args + %W[ #{ grammar_file_path } ] ) . run
39- exec_command ( "flex --header-file=#{ lexer_h_path } -o #{ lexer_c_path } #{ lexer_file_path } " )
40- exec_command ( "#{ compiler } -Wall -ggdb3 -I#{ tmpdir } #{ parser_c_path } #{ lexer_c_path } -o #{ obj_path } " )
44+ cache_key = "#{ parser_name } _#{ lrama_command_args . join ( '_' ) } "
45+
46+ unless IntegrationHelper . compiled_parsers [ cache_key ] && File . exist? ( obj_path )
47+ Lrama ::Command . new ( %W[ -H#{ parser_h_path } -o#{ parser_c_path } ] + lrama_command_args + %W[ #{ grammar_file_path } ] ) . run
48+ exec_command ( "flex --header-file=#{ lexer_h_path } -o #{ lexer_c_path } #{ lexer_file_path } " )
49+ exec_command ( "#{ compiler } -Wall -O0 -g -I#{ tmpdir } #{ parser_c_path } #{ lexer_c_path } -o #{ obj_path } " )
50+ IntegrationHelper . compiled_parsers [ cache_key ] = true
51+ end
4152
4253 out = err = status = nil
4354
@@ -58,12 +69,17 @@ def test_parser(parser_name, input, expected, expect_success: true, lrama_comman
5869
5970 def generate_object ( grammar_file_path , c_path , obj_path , command_args : [ ] )
6071 Lrama ::Command . new ( %W[ -d -o #{ c_path } ] + command_args + %W[ #{ grammar_file_path } ] ) . run
61- exec_command ( "#{ compiler } -Wall #{ c_path } -o #{ obj_path } " )
72+ exec_command ( "#{ compiler } -Wall -O0 #{ c_path } -o #{ obj_path } " )
6273 end
6374 end
6475
6576 include IntegrationHelper
6677
78+ # Clear cache after all tests to save memory
79+ after ( :all ) do
80+ IntegrationHelper . compiled_parsers . clear
81+ end
82+
6783 describe "calculator" do
6884 it "returns 9 for '(1+2)*3'" do
6985 test_parser ( "calculator" , "( 1 + 2 ) * 3" , "=> 9" )
0 commit comments