Class: Vigilant

Inherits:
Object
  • Object
show all
Defined in:
lib/vigilant.rb,
lib/vigilant/cli.rb,
lib/vigilant/test.rb

Defined Under Namespace

Classes: CLI, Test

Constant Summary collapse

VERSION =
'0.1.2'
PROLOGUE =
<<-PROLOGUE
// DO NOT MODIFY
// This file was auto-generated by Vigilant.

#include <stdio.h>
#include <stdlib.h>
#include <setjmp.h>

static jmpbuf __vigilant_jb;
static char __vigilant_msg_buffer[4096];

#define ___vigilant_stringfy( x ) #x
#define __vigilant_stringfy( x ) ___vigilant_stringfy(x)

#undef assert
#define assert( condition ) \
  do { if (!(_condition)) { sprintf(&__vigilant_msg_buffer[0], "'" #_condition "' failed on " __vigilant_stringfy(__LINE__) " in '" __vigilant_stringfy(__FILE__) "'"); longjmp(__vigilant_jb, 0); }} while (0, 0)

#define assert_equal( a, b ) \
  assert((a) == (b))

#define assert_not_equal( a, b ) \
  assert((a) != (b))


PROLOGUE

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(test_dir) ⇒ Vigilant

Returns a new instance of Vigilant.



9
10
11
12
# File 'lib/vigilant.rb', line 9

def initialize( test_dir )
  raise "You need to specify a tests directory" unless test_dir
  raise "You need to specify a valid tests directory" unless valid_test_dir(test_dir)
end

Instance Attribute Details

#num_testsObject (readonly)

Returns the value of attribute num_tests.



7
8
9
# File 'lib/vigilant.rb', line 7

def num_tests
  @num_tests
end

Instance Method Details

#generate_testsObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/vigilant.rb', line 14

def generate_tests()
  raise "You need to specify an output path" unless @options['output']
  includes = [] unless @options['includes']

  puts "Generating tests..."
  @num_tests = 0

  Dir.chdir(@project_dir) do
    File.open(@options['output'].dup.untaint, "w") do |out|
      out.write(PROLOGUE)
      includes.each { |include| out.write("#include <#{include}>\n") }

      tests = []
      Dir.glob("**/*.test") do |test|
        next if !File.file?(test)
        tests << test
      end

      @num_tests = tests.length

      run_tests = ""
      tests.each do |file|
        puts "#{file}"

        test = Vigilant::Test.load(file)
        raise "[#{file}] isn't a Vigilant::Test." unless test

        suite = File.dirname(file).split('/').join('_')
        name = File.basename(file, ".test")
        prefix = ""

        if suite.empty?
          prefix = "test_#{name}"
        else
          prefix = "test_#{suite}_#{name}"
        end

        prior_to = "static void " << suite << "_" << name << "__prior_to( void ) {\n"
        prior_to += test.prior_to.untaint
        prior_to += "}\n\n"
        out.write(prior_to)

        run = "static void " << suite << "_" << name << "__run( void ) {\n"
        run += test.run.untaint
        run += "}\n\n"
        out.write(run)

        after = "static void " << suite << "_" << name << "__after( void ) {\n"
        after += test.after.untaint
        after += "}\n\n"
        out.write(after)

        run_tests = run_tests << "
fprintf(stdout, \"Running #{prefix}\\n\");
#{prefix}__prior_to();
if( !setjmp(__vigilant_jb) ) {
#{prefix}__run();
++passed;
} else {
ret = EXIT_FAILURE;
fprintf(stdout, \" Failed: %s\\n\", &__test_msg[0]);
}
#{prefix}__after();\n"
      end

      out.write("
int main( int argc, char** argv ) {
int ret = EXIT_SUCCESS;
unsigned passed = 0;
fprintf(stdout, \"Running test suite...\\n\");

#{run_tests}

fprintf(stdout, \"\\n%u/%u passed (%%%u).\\n\", passed, (#{@num_tests}), (passed * 100) / (#{@num_tests}));
return ret;
}\n")
    end
  end

  puts "Generated #{num_tests} tests."
end

#valid_test_dir(dir) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/vigilant.rb', line 96

def valid_test_dir( dir )
  return false if !File.directory?(dir)
  path = File.expand_path(dir)
  @project_dir = File.dirname(path)
  @options = JSON.parse(File.read(File.join(dir, "vigilant.options")))
  true
end