vigilant 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ # Ignore generated .gems.
2
+ *.gem
data/bin/vigilant ADDED
@@ -0,0 +1,28 @@
1
+ #!/usr/bin/env ruby
2
+ $LOAD_PATH.unshift File.dirname(__FILE__) + "/../lib"
3
+
4
+ require "rubygems"
5
+ require "gli"
6
+ require "vigilant"
7
+ require "vigilant/cli"
8
+
9
+ version Vigilant::version
10
+
11
+ desc "Generate unit test file"
12
+ long_desc "Generate C/C++ unit testing file from a Vigilant config file"
13
+ command :gen do |c|
14
+ c.action do |global_options, options, args|
15
+ file = args.first
16
+ Vigilant::CLI.gen(file, options)
17
+ end
18
+ end
19
+
20
+ pre { |global, command, options, args| true }
21
+ post {|global, command, options, args| true }
22
+
23
+ on_error do |exception|
24
+ puts exception
25
+ puts exception.backtrace
26
+ end
27
+
28
+ exit GLI.run(ARGV)
data/lib/vigilant.rb ADDED
@@ -0,0 +1,130 @@
1
+ require 'json'
2
+ require 'vigilant/test'
3
+
4
+ class Vigilant
5
+ VERSION = '0.1.0'
6
+
7
+ attr_reader :num_tests
8
+
9
+ def initialize( test_dir )
10
+ raise "You need to specify a tests directory" unless test_dir
11
+ raise "You need to specify a valid tests directory" unless valid_test_dir(test_dir)
12
+ end
13
+
14
+ def generate_tests()
15
+ raise "You need to specify an output path" unless @options['output']
16
+ includes = [] unless @options['includes']
17
+
18
+ puts "Generating tests..."
19
+ @num_tests = 0
20
+
21
+ Dir.chdir(@project_dir) do
22
+ File.open(@options['output'].dup.untaint, "w") do |out|
23
+ out.write(PROLOGUE)
24
+ includes.each { |include| out.write("#include <#{include}>\n") }
25
+
26
+ tests = []
27
+ Dir.glob("**/*.test") do |test|
28
+ next if !File.file?(test)
29
+ tests << test
30
+ end
31
+
32
+ @num_tests = tests.length
33
+
34
+ run_tests = ""
35
+ tests.each do |file|
36
+ puts "#{file}"
37
+
38
+ test = Vigilant::Test.load(file)
39
+ raise "[#{file}] isn't a Vigilant::Test." unless test
40
+
41
+ suite = File.dirname(file).split('/').join('_')
42
+ name = File.basename(file, ".test")
43
+ prefix = ""
44
+
45
+ if suite.empty?
46
+ prefix = "test_#{name}"
47
+ else
48
+ prefix = "test_#{suite}_#{name}"
49
+ end
50
+
51
+ prior_to = "static void " << suite << "_" << name << "__prior_to( void ) {\n"
52
+ prior_to += test.prior_to.untaint
53
+ prior_to += "}\n\n"
54
+ out.write(prior_to)
55
+
56
+ run = "static void " << suite << "_" << name << "__run( void ) {\n"
57
+ run += test.run.untaint
58
+ run += "}\n\n"
59
+ out.write(run)
60
+
61
+ after = "static void " << suite << "_" << name << "__after( void ) {\n"
62
+ after += test.after.untaint
63
+ after += "}\n\n"
64
+ out.write(after)
65
+
66
+ run_tests = run_tests << "
67
+ fprintf(stdout, \"Running #{prefix}\\n\");
68
+ #{prefix}__prior_to();
69
+ if( !setjmp(__vigilant_jb) ) {
70
+ #{prefix}__run();
71
+ ++passed;
72
+ } else {
73
+ ret = EXIT_FAILURE;
74
+ fprintf(stdout, \" Failed: %s\\n\", &__test_msg[0]);
75
+ }
76
+ #{prefix}__after();\n"
77
+ end
78
+
79
+ out.write("
80
+ int main( int argc, char** argv ) {
81
+ int ret = EXIT_SUCCESS;
82
+ unsigned passed = 0;
83
+ fprintf(stdout, \"Running test suite...\\n\");
84
+
85
+ #{run_tests}
86
+
87
+ fprintf(stdout, \"\\n%u/%u passed (%%%u).\\n\", passed, (#{@num_tests}), (passed * 100) / (#{@num_tests}));
88
+ return ret;
89
+ }\n")
90
+ end
91
+ end
92
+
93
+ puts "Generated #{num_tests} tests."
94
+ end
95
+
96
+ def valid_test_dir( dir )
97
+ return false if !File.directory?(dir)
98
+ path = File.expand_path(dir)
99
+ @project_dir = File.dirname(path)
100
+ @options = JSON.parse(File.read(File.join(dir, "vigilant.options")))
101
+ true
102
+ end
103
+
104
+ PROLOGUE = <<-PROLOGUE
105
+ // DO NOT MODIFY
106
+ // This file was auto-generated by Vigilant.
107
+
108
+ #include <stdio.h>
109
+ #include <stdlib.h>
110
+ #include <setjmp.h>
111
+
112
+ static jmpbuf __vigilant_jb;
113
+ static char __vigilant_msg_buffer[4096];
114
+
115
+ #define ___vigilant_stringfy( x ) #x
116
+ #define __vigilant_stringfy( x ) ___vigilant_stringfy(x)
117
+
118
+ #undef assert
119
+ #define assert( condition ) \
120
+ 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)
121
+
122
+ #define assert_equal( a, b ) \
123
+ assert((a) == (b))
124
+
125
+ #define assert_not_equal( a, b ) \
126
+ assert((a) != (b))
127
+
128
+
129
+ PROLOGUE
130
+ end
@@ -0,0 +1,19 @@
1
+ class Vigilant
2
+ class CLI
3
+ def self.doc( file, options )
4
+ Vigilant.new(file).generate_tests
5
+ end
6
+
7
+ def self.gen( file )
8
+ temp = <<-TEMPLATE
9
+ {
10
+ "output": "run_tests.c"
11
+ }
12
+ TEMPLATE
13
+ puts "Writing to #{file}"
14
+ File.open(file, 'w+') do |f|
15
+ f.write(temp)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,37 @@
1
+ class Vigilant
2
+ class Test
3
+ attr_accessor :prior_to
4
+ attr_accessor :run
5
+ attr_accessor :after
6
+
7
+ def initialize()
8
+ @prior_to = ""
9
+ @run = ""
10
+ @after = ""
11
+ end
12
+
13
+ def self.load file
14
+ return unless file
15
+ file = file.dup.untaint
16
+ return unless File.file?(file)
17
+
18
+ code = File.read(file).untaint
19
+
20
+ begin
21
+ test = eval(code, binding, file)
22
+
23
+ if Vigilant::Test === test
24
+ return test
25
+ end
26
+
27
+ warn "[#{file}] isn't a Vigilant::Test (#{test.class} instead)."
28
+ rescue SignalException, SystemExit
29
+ raise
30
+ rescue SyntaxError, Exception => e
31
+ warn "Invalid test in [#{file}]: #{e}"
32
+ end
33
+
34
+ nil
35
+ end
36
+ end
37
+ end
data/test/.gitignore ADDED
File without changes
@@ -0,0 +1,13 @@
1
+ Vigilant::Test.new do |test|
2
+ test.prior_to <<-PRIOR_TO
3
+ PRIOR_TO
4
+
5
+ test.run <<-RUN
6
+ assert("Hello, World!");
7
+ assert_equal(2 + 2, 4);
8
+ assert_not_equal(42, 42);
9
+ RUN
10
+
11
+ test.after <<-AFTER
12
+ AFTER
13
+ end
@@ -0,0 +1,3 @@
1
+ {
2
+ "output": "run_tests.c"
3
+ }
@@ -0,0 +1,13 @@
1
+ require File.expand_path "../helpers", __FILE__
2
+
3
+ context "Vigilant Test Generation" do
4
+ setup do
5
+ @path = File.join(File.dirname(__FILE__), "fixtures")
6
+ @generator = Vigilant.new(@path)
7
+ @generator.generate_tests
8
+ end
9
+
10
+ test "can parse tests" do
11
+ assert_equal 1, @generator.num_tests
12
+ end
13
+ end
data/test/helpers.rb ADDED
@@ -0,0 +1,28 @@
1
+ $LOAD_PATH.unshift File.dirname(File.expand_path(__FILE__)) + "/../lib"
2
+ $TESTING = true
3
+
4
+ require 'test/unit'
5
+ require 'rubygems'
6
+ require 'vigilant'
7
+
8
+ ##
9
+ # test/spec/mini 3
10
+ # http://gist.github.com/25455
11
+ # chris@ozmm.org
12
+ #
13
+ def context(*args, &block)
14
+ return super unless (name = args.first) && block
15
+ require 'test/unit'
16
+ klass = Class.new(defined?(ActiveSupport::TestCase) ? ActiveSupport::TestCase : Test::Unit::TestCase) do
17
+ def self.test(name, &block)
18
+ define_method("test_#{name.gsub(/\W/,'_')}", &block) if block
19
+ end
20
+ def self.xtest(*args) end
21
+ def self.setup(&block) define_method(:setup, &block) end
22
+ def self.teardown(&block) define_method(:teardown, &block) end
23
+ end
24
+ (class << klass; self end).send(:define_method, :name) { name.gsub(/\W/,'_') }
25
+ klass.class_eval &block
26
+ ($contexts ||= []) << klass # make sure klass doesn't get GC'd
27
+ klass
28
+ end
data/vigilant.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+ require 'vigilant'
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = "vigilant"
6
+ spec.version = Vigilant::VERSION
7
+ spec.platform = Gem::Platform::RUBY
8
+ spec.authors = ["Michael Williams"]
9
+ spec.email = ["devbug@bitbyte.ca"]
10
+ spec.homepage = "http://github.com/mtwilliams/vigilant"
11
+ spec.summary = "A simple C/C++ unit test generator."
12
+ spec.description = spec.summary
13
+
14
+ spec.rubyforge_project = "vigilant"
15
+
16
+ spec.add_development_dependency "bundler", "~>1.0"
17
+
18
+ spec.files = `git ls-files`.split("\n")
19
+ spec.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vigilant
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Michael Williams
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-19 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: &16148640 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *16148640
25
+ description: A simple C/C++ unit test generator.
26
+ email:
27
+ - devbug@bitbyte.ca
28
+ executables:
29
+ - vigilant
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - .gitignore
34
+ - bin/vigilant
35
+ - lib/vigilant.rb
36
+ - lib/vigilant/cli.rb
37
+ - lib/vigilant/test.rb
38
+ - test/.gitignore
39
+ - test/fixtures/foo.test
40
+ - test/fixtures/vigilant.options
41
+ - test/generate_test.rb
42
+ - test/helpers.rb
43
+ - vigilant.gemspec
44
+ homepage: http://github.com/mtwilliams/vigilant
45
+ licenses: []
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubyforge_project: vigilant
64
+ rubygems_version: 1.8.16
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: A simple C/C++ unit test generator.
68
+ test_files: []