evil-ruby 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (10) hide show
  1. data/COPYING +56 -0
  2. data/NEWS +7 -0
  3. data/README +15 -0
  4. data/Rakefile +109 -0
  5. data/lib/evil.rb +691 -0
  6. data/setup.rb +1360 -0
  7. data/test/tc_all.rb +8 -0
  8. data/test/tc_inline.rb +11 -0
  9. data/test/test-extract.rb +112 -0
  10. metadata +59 -0
@@ -0,0 +1,8 @@
1
+ # Runs all tests.
2
+
3
+ $LOAD_PATH.unshift "lib"
4
+
5
+ test_dir = File.split(Dir.pwd).last == "test" ? "." : "test"
6
+
7
+ tests = Dir["#{test_dir}/**/*.rb"].reject { |file| file == "tc_all.rb" }
8
+ tests.each { |test| require test }
@@ -0,0 +1,11 @@
1
+ $LOAD_PATH.unshift File.join(File.dirname(__FILE__), "../lib")
2
+ $LOAD_PATH.unshift File.dirname(__FILE__)
3
+
4
+ require "test-extract"
5
+ require "evil"
6
+
7
+ glob = File.join(File.dirname(__FILE__), "../lib/*")
8
+ Dir[glob].each do |file|
9
+ next unless File.file?(file)
10
+ Extracter.process(file)
11
+ end
@@ -0,0 +1,112 @@
1
+ require 'test/unit/testcase'
2
+ require 'test/unit/ui/console/testrunner'
3
+
4
+ class Extracter
5
+ def self.process(fn)
6
+ new(File.read(fn))
7
+ end
8
+
9
+ def initialize(content)
10
+ comment_block_re = /((?:^\s*?(?:#.*?)?\n)+)/m
11
+ component_re = /\s*(?:class|def|module|alias)\s+:?([^\s()]+)?/
12
+ blocks = content.scan(/#{comment_block_re}#{component_re}/)
13
+
14
+ test_suite = Class.new(Test::Unit::TestCase)
15
+
16
+ has_test = false
17
+ blocks.each do |(comment, component)|
18
+ code_in_doc_re = /^(\s*# +(?:.*?)$)/
19
+ tests = comment.scan(code_in_doc_re)
20
+ body = tests.map do |test|
21
+ test.map do |raw_line|
22
+ line = raw_line.sub(/^\s*#\s{0,3}/, "")
23
+ if md = /(.*?)#\s*(=>|~>|raises?)\s*(.*?)$/.match(line)
24
+ new_line, type, result = *md.captures
25
+ new_line.strip!
26
+ case type
27
+ when "=>"
28
+ ["begin",
29
+ " assert_equal(#{result}, #{new_line})",
30
+ "rescue => err",
31
+ " assert_equal(#{result.inspect}, (#{new_line}).inspect)",
32
+ "end"].join("\n")
33
+ when "~>", "raise", "raises"
34
+ "assert_raises(Object.const_get(#{result.inspect})) { #{new_line} }"
35
+ end
36
+ else
37
+ line
38
+ end
39
+ end.join("\n")
40
+ end.join("\n")
41
+
42
+ unless component
43
+ if $DEBUG
44
+ STDERR.puts "Can't get name for this code:",
45
+ body.gsub(/(?:\r?\n){2}/, "\n")
46
+ end
47
+ component = test.hash.abs
48
+ end
49
+
50
+ if body and not body.empty?
51
+ has_test = true
52
+ begin
53
+ test_suite.class_eval %{
54
+ def #{test_method_name(component)}
55
+ #{body}
56
+ end
57
+ }
58
+ rescue Object => err
59
+ STDERR.puts "Error in #{test_method_name(component)}: ",
60
+ err, "", "Code is: ", body, "" if $DEBUG
61
+ end
62
+ end
63
+ end
64
+
65
+ if not has_test
66
+ test_suite.class_eval do
67
+ def test_main; end
68
+ end
69
+ end
70
+
71
+ Test::Unit::UI::Console::TestRunner.new(test_suite).start
72
+ end
73
+
74
+ def test_method_name(component)
75
+ result = "test_#{component}"
76
+ {
77
+ "+" => "op_plus",
78
+ "-" => "op_minus",
79
+ "+@" => "op_plus_self",
80
+ "-@" => "op_minus_self",
81
+ "*" => "op_mul",
82
+ "**" => "op_pow",
83
+ "/" => "op_div",
84
+ "%" => "op_mod",
85
+ "<<" => "op_lshift",
86
+ ">>" => "op_rshift",
87
+ "~" => "op_tilde",
88
+ "<=>" => "op_cmp",
89
+ "<" => "op_lt",
90
+ ">" => "op_gt",
91
+ "==" => "op_equal",
92
+ "<=" => "op_lt_eq",
93
+ ">=" => "op_gt_eq",
94
+ "===" => "op_case_eq",
95
+ "=~" => "op_apply",
96
+ "|" => "op_or",
97
+ "&" => "op_and",
98
+ "^" => "op_xor",
99
+ "[]" => "op_fetch",
100
+ "[]=" => "op_store"
101
+ }.each do |(what, by)|
102
+ result.gsub!(what, by)
103
+ end
104
+ return result
105
+ end
106
+ end
107
+
108
+ if __FILE__ == $0
109
+ file = ARGV.shift
110
+ load(file)
111
+ Extracter.process(file)
112
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.0
3
+ specification_version: 1
4
+ name: evil-ruby
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.1.0
7
+ date: 2007-04-03 00:00:00 +02:00
8
+ summary: Extends Ruby's semantics by accessing its internals from pure Ruby code.
9
+ require_paths:
10
+ - lib
11
+ email:
12
+ homepage:
13
+ rubyforge_project:
14
+ description: Extends Ruby's semantics by accessing its internals from pure Ruby code.
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.8.4
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ -
31
+ files:
32
+ - README
33
+ - NEWS
34
+ - COPYING
35
+ - setup.rb
36
+ - Rakefile
37
+ - lib/evil.rb
38
+ - test/test-extract.rb
39
+ - test/tc_inline.rb
40
+ - test/tc_all.rb
41
+ test_files:
42
+ - test/tc_all.rb
43
+ rdoc_options:
44
+ - --all
45
+ - --main
46
+ - README
47
+ - --title
48
+ - "#{PKG_NAME}"
49
+ extra_rdoc_files:
50
+ - README
51
+ - NEWS
52
+ executables: []
53
+
54
+ extensions: []
55
+
56
+ requirements: []
57
+
58
+ dependencies: []
59
+