From 1ad5615abaa9464bd701d105616e0ac00dadc91c Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Wed, 8 Oct 2014 14:16:48 -0400 Subject: [PATCH] Added test framework for sclpl compiler --- Gemfile | 1 + Gemfile.lock | 14 ++++++++++++++ Rakefile | 26 ++++++++++++++++++++++++-- source/sclpl/main.c | 18 +++++++++--------- source/sclpl/parser.c | 1 + spec/lexer_spec.rb | 15 +++++++++++++++ spec/parser_spec.rb | 1 + spec/spec_helper.rb | 7 +++++++ 8 files changed, 72 insertions(+), 11 deletions(-) create mode 100644 spec/lexer_spec.rb create mode 100644 spec/parser_spec.rb create mode 100644 spec/spec_helper.rb diff --git a/Gemfile b/Gemfile index c8d6a78..2787500 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,4 @@ source 'http://rubygems.org' gem 'rscons' gem 'rake' +gem 'rspec' diff --git a/Gemfile.lock b/Gemfile.lock index 7b3953a..203d395 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,10 +1,23 @@ GEM remote: http://rubygems.org/ specs: + diff-lcs (1.2.5) json (1.8.1) rake (10.3.2) rscons (1.6.0) json (~> 1.0) + rspec (3.0.0) + rspec-core (~> 3.0.0) + rspec-expectations (~> 3.0.0) + rspec-mocks (~> 3.0.0) + rspec-core (3.0.2) + rspec-support (~> 3.0.0) + rspec-expectations (3.0.2) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.0.0) + rspec-mocks (3.0.2) + rspec-support (~> 3.0.0) + rspec-support (3.0.2) PLATFORMS ruby @@ -13,3 +26,4 @@ PLATFORMS DEPENDENCIES rake rscons + rspec diff --git a/Rakefile b/Rakefile index 8562ab3..565caa5 100644 --- a/Rakefile +++ b/Rakefile @@ -1,3 +1,5 @@ +require "rspec" +require "rspec/core/rake_task" require './build-system/setup' def windows? @@ -11,10 +13,16 @@ end base_env = BuildEnv.new(echo: :command) do |env| env.build_dir('source','build/obj/source') env.build_dir('modules','build/obj/modules') - env.set_toolset(:clang) + env.set_toolset(:gcc) env["CFLAGS"] += ['-DLEAK_DETECT_LEVEL=1', '--std=c99', '-Wall', '-Wextra'] #, '-Werror'] env["CPPPATH"] += ['modules/libopts/source'] + Dir['modules/libcds/source/**/'] - env["AR"] = 'ar' +end + +test_env = base_env.clone do |env| + env.build_dir('source','build/obj/test/source') + env.build_dir('modules','build/obj/test/modules') + env['CFLAGS'] += ['--coverage'] + env['LDFLAGS'] += ['--coverage'] end #------------------------------------------------------------------------------ @@ -84,3 +92,17 @@ end desc "Clobber the build directory and all it's contents" task(:clobber) { FileUtils.rm_rf('build/') } +#------------------------------------------------------------------------------ +# RSpec Tests for the Compiler +#------------------------------------------------------------------------------ +# Make the specs depend on the libs +task :spec => [:libcds, :libopts] + +# Build and test the compiler with coverage +RSpec::Core::RakeTask.new(:spec) do |t,args| + t.rspec_opts = ['--format', 'documentation'] + test_env.Program('build/bin/sclpl-test', + FileList['source/sclpl/*.c', 'build/lib/libopts.a', 'build/lib/libcds.a']) + base_env.process + test_env.process +end diff --git a/source/sclpl/main.c b/source/sclpl/main.c index 10a77ff..5f0a839 100644 --- a/source/sclpl/main.c +++ b/source/sclpl/main.c @@ -8,15 +8,15 @@ /* Command Line Options *****************************************************************************/ -OptionConfig_T Options_Config[] = { - { "tokens", false, "mode", "Emit the token output of lexical analysis for the given file"}, - { "ast", false, "mode", "Emit the abstract syntax tree for the given file"}, - { "repl", false, "mode", "Execute the application in a REPL"}, - { "staticlib", false, "mode", "Compile the application as a static library"}, - { "sharedlib", false, "mode", "Compile the application as a shared library"}, - { "program", false, "mode", "Compile the application as an executable"}, - { "R", true, "include", "Add a path to the list of require paths"}, - { NULL, false, NULL, NULL } +opts_cfg_t Options_Config[] = { + {"tokens", false, "mode", "Emit the token output of lexical analysis for the given file"}, + {"ast", false, "mode", "Emit the abstract syntax tree for the given file"}, + {"repl", false, "mode", "Execute the application in a REPL"}, + {"staticlib", false, "mode", "Compile the application as a static library"}, + {"sharedlib", false, "mode", "Compile the application as a shared library"}, + {"program", false, "mode", "Compile the application as an executable"}, + {"R", true, "include", "Add a path to the list of require paths"}, + {NULL, false, NULL, NULL } }; /* Tree Printing diff --git a/source/sclpl/parser.c b/source/sclpl/parser.c index 87dac05..804416f 100644 --- a/source/sclpl/parser.c +++ b/source/sclpl/parser.c @@ -72,6 +72,7 @@ void parser_resume(parser_t* p_parser) { void parser_error(parser_t* p_parser, const char* p_text) { + (void)p_parser; throw_msg(ParseException, p_text); } diff --git a/spec/lexer_spec.rb b/spec/lexer_spec.rb new file mode 100644 index 0000000..cd7b4fb --- /dev/null +++ b/spec/lexer_spec.rb @@ -0,0 +1,15 @@ +require 'open3' + +def tokens(input) + out, err, status = Open3.capture3('./build/bin/sclpl-test', '--tokens', :stdin_data => input) + raise "Lexer command returned non-zero status" unless status.success? + raise "Lexer produced error messages" unless err == "" + out.gsub(//,'\1').split +end + +describe "lexer" do + it "should recognize punctuation" do + expect(tokens('[](){};\'",')).to eq( + ["T_LBRACK", "T_RBRACK", "T_LPAR", "T_RPAR", "T_VAR", "T_VAR", "T_END"]) + end +end diff --git a/spec/parser_spec.rb b/spec/parser_spec.rb new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/spec/parser_spec.rb @@ -0,0 +1 @@ + diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..ce150c6 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,7 @@ + +module TestUtils +def self.exec_cmd(cmd, input) + out, err, status = Open3.capture3(*cmd, :stdin_data => input) +end +end + -- 2.52.0