comment_tester 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README +42 -0
- data/bin/ruby_comment_tester +145 -0
- metadata +48 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 434c90a0584ec6c3ee606397de58c52db244dbcd
|
4
|
+
data.tar.gz: 4ee6fbc6a304e54052f6b6c2bb4ee1c352c04b6d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d569191b50f972b1b6d10e36ffc4361d0542cc116ee0509aaf60993c135055eedce02d155fae27fe167aa39e3cf32d75342f4b7575fded706ad5f15b845e52e8
|
7
|
+
data.tar.gz: e0781e070b027bd05ad850aac33a3ab14c4ed2782cc8aa3189c2c1573a08a9c974064905f6d91ff8b2f91df69984ed8feda5f998fc093aa7e6e09cf95df39b70
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
Copyright (c) 2013, Michael H. Buselli
|
2
|
+
All rights reserved.
|
3
|
+
|
4
|
+
Redistribution and use in source and binary forms, with or without
|
5
|
+
modification, are permitted provided that the following conditions are met:
|
6
|
+
* Redistributions of source code must retain the above copyright
|
7
|
+
notice, this list of conditions and the following disclaimer.
|
8
|
+
* Redistributions in binary form must reproduce the above copyright
|
9
|
+
notice, this list of conditions and the following disclaimer in the
|
10
|
+
documentation and/or other materials provided with the distribution.
|
11
|
+
|
12
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ''AS IS'' AND ANY
|
13
|
+
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
14
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
15
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
|
16
|
+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
17
|
+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
18
|
+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
19
|
+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
20
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
21
|
+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/README
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
# Copyright (c) 2013, Michael H. Buselli
|
2
|
+
# See LICENSE for details. All other rights reserved.
|
3
|
+
#
|
4
|
+
# ruby_comment_tester works by auto-generating Ruby test-unit files from
|
5
|
+
# the comments of a Ruby program. The idea is that you mimic what you
|
6
|
+
# would type into irb that could provide a test. In doing so, you write
|
7
|
+
# both a test and document the behavior of your program or method in the
|
8
|
+
# same place. Some example test comments are as follows:
|
9
|
+
|
10
|
+
# Code and assertions in the same comment block are grouped into
|
11
|
+
# a single test.
|
12
|
+
#
|
13
|
+
# irb> a = "123"
|
14
|
+
# irb> b = a.split(/2/)
|
15
|
+
# => ["1", "3"]
|
16
|
+
#
|
17
|
+
# irb> b.map(&:to_i).inject(0) { |s, x| s + x } + 38
|
18
|
+
# => 42
|
19
|
+
|
20
|
+
# Variables are not sticky between tests, and you can also test for
|
21
|
+
# exceptions or thrown objects with raises=> and throws=>.
|
22
|
+
#
|
23
|
+
# irb> a
|
24
|
+
# raises=> NameError
|
25
|
+
# irb> throw :catch_me
|
26
|
+
# throws=> :catch_me
|
27
|
+
|
28
|
+
# Sometimes you have to initialize something for your code or tests to
|
29
|
+
# work. Use irb-setup> for that.
|
30
|
+
#
|
31
|
+
# irb-setup> puts "This runs outside the tests before code or tests are loaded."
|
32
|
+
# irb> [:hello, :world].map { |s| s.to_s.capitalize }.join(" ")
|
33
|
+
# => "Hello World"
|
34
|
+
|
35
|
+
# You can comment out tests with double hashes of any sort.
|
36
|
+
#
|
37
|
+
# # irb> this won't run
|
38
|
+
# # => foo
|
39
|
+
## irb> neither does this
|
40
|
+
## => bar
|
41
|
+
|
42
|
+
# You can run the above tests by running this program on itself.
|
@@ -0,0 +1,145 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# Copyright (c) 2013, Michael H. Buselli
|
4
|
+
# See LICENSE for details. All other rights reserved.
|
5
|
+
#
|
6
|
+
# ruby_comment_tester works by auto-generating Ruby test-unit files from
|
7
|
+
# the comments of a Ruby program. The idea is that you mimic what you
|
8
|
+
# would type into irb that could provide a test. In doing so, you write
|
9
|
+
# both a test and document the behavior of your program or method in the
|
10
|
+
# same place. Some example test comments are as follows:
|
11
|
+
|
12
|
+
# Code and assertions in the same comment block are grouped into
|
13
|
+
# a single test.
|
14
|
+
#
|
15
|
+
# irb> a = "123"
|
16
|
+
# irb> b = a.split(/2/)
|
17
|
+
# => ["1", "3"]
|
18
|
+
#
|
19
|
+
# irb> b.map(&:to_i).inject(0) { |s, x| s + x } + 38
|
20
|
+
# => 42
|
21
|
+
|
22
|
+
# Variables are not sticky between tests, and you can also test for
|
23
|
+
# exceptions or thrown objects with raises=> and throws=>.
|
24
|
+
#
|
25
|
+
# irb> a
|
26
|
+
# raises=> NameError
|
27
|
+
# irb> throw :catch_me
|
28
|
+
# throws=> :catch_me
|
29
|
+
|
30
|
+
# Sometimes you have to initialize something for your code or tests to
|
31
|
+
# work. Use irb-setup> for that.
|
32
|
+
#
|
33
|
+
# irb-setup> puts "This runs outside the tests before code or tests are loaded."
|
34
|
+
# irb> [:hello, :world].map { |s| s.to_s.capitalize }.join(" ")
|
35
|
+
# => "Hello World"
|
36
|
+
|
37
|
+
# You can comment out tests with double hashes of any sort.
|
38
|
+
#
|
39
|
+
# # irb> this won't run
|
40
|
+
# # => foo
|
41
|
+
## irb> neither does this
|
42
|
+
## => bar
|
43
|
+
|
44
|
+
# You can run the above tests by running this program on itself.
|
45
|
+
|
46
|
+
require "pp" if ENV["DEBUG"]
|
47
|
+
|
48
|
+
class CommentTester
|
49
|
+
class << self
|
50
|
+
def run (argv)
|
51
|
+
argv.each { |filename| run_one(filename) }
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def run_one (filename)
|
57
|
+
eval(make_test_file(filename))
|
58
|
+
load(filename) unless filename == $0
|
59
|
+
end
|
60
|
+
|
61
|
+
def make_test_file (filename)
|
62
|
+
comment_groups = comment_extraction(filename)
|
63
|
+
test_file = %'require "test/unit"\nclass CommentTesterTestClass < Test::Unit::TestCase\n'
|
64
|
+
|
65
|
+
test_file << comment_groups.map do |cg|
|
66
|
+
comment_group_to_test_case(cg)
|
67
|
+
end.join
|
68
|
+
|
69
|
+
(test_file << "end\n").tap { |x| puts x if ENV["DEBUG"] }
|
70
|
+
end
|
71
|
+
|
72
|
+
def comment_extraction (filename)
|
73
|
+
comment_groups = []
|
74
|
+
last_line = :code
|
75
|
+
# or :comment, :test_setup, :test_code, :assertion, :assert_raise, :assert_throw
|
76
|
+
|
77
|
+
# Read the Ruby code file.
|
78
|
+
File.open(filename, "r") do |fh|
|
79
|
+
fh.each_line do |line|
|
80
|
+
if line =~ /\A\s*#/
|
81
|
+
if last_line == :code
|
82
|
+
comment_groups << []
|
83
|
+
end
|
84
|
+
|
85
|
+
if line =~ /\A\s*#\s*irb-setup>\s*(.*)\Z/
|
86
|
+
comment_groups[-1] << {test_setup: $1.chomp}
|
87
|
+
last_line = :test_setup
|
88
|
+
elsif line =~ /\A\s*#\s*irb>\s*(.*)\Z/
|
89
|
+
comment_groups[-1] << {test_code: $1.chomp}
|
90
|
+
last_line = :test_code
|
91
|
+
elsif line =~ /\A\s*#\s*=>\s*(.*)\Z/
|
92
|
+
raise "you can only use => after irb>" if last_line != :test_code
|
93
|
+
comment_groups[-1] << {assertion: $1.chomp}
|
94
|
+
last_line = :assertion
|
95
|
+
elsif line =~ /\A\s*#\s*raises=>\s*(.*)\Z/
|
96
|
+
raise "you can only use raises=> after irb>" if last_line != :test_code
|
97
|
+
comment_groups[-1][-1][:assert_raise] = $1.chomp
|
98
|
+
last_line = :assert_raise
|
99
|
+
elsif line =~ /\A\s*#\s*throws=>\s*(.*)\Z/
|
100
|
+
raise "you can only use throws=> after irb>" if last_line != :test_code
|
101
|
+
comment_groups[-1][-1][:assert_throw] = $1.chomp
|
102
|
+
last_line = :assert_throw
|
103
|
+
else
|
104
|
+
last_line = :comment
|
105
|
+
end
|
106
|
+
else
|
107
|
+
last_line = :code
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
comment_groups.reject { |a| a.size == 0 }.tap { |x| pp x if ENV["DEBUG"] }
|
113
|
+
end
|
114
|
+
|
115
|
+
def comment_group_to_test_case (comment_group)
|
116
|
+
@test_cases_made ||= 0
|
117
|
+
@test_cases_made += 1
|
118
|
+
|
119
|
+
test_body = comment_group.map do |comment_line_data|
|
120
|
+
raise_code = comment_line_data.delete(:assert_raise)
|
121
|
+
throw_code = comment_line_data.delete(:assert_throw)
|
122
|
+
line_type = comment_line_data.keys[0]
|
123
|
+
code = comment_line_data.values[0]
|
124
|
+
|
125
|
+
if line_type == :test_setup
|
126
|
+
Object.class_eval(code)
|
127
|
+
elsif line_type == :test_code
|
128
|
+
if raise_code
|
129
|
+
"assert_raise(#{raise_code}) { #{code} }"
|
130
|
+
elsif throw_code
|
131
|
+
"assert_throws(#{throw_code}) { #{code} }"
|
132
|
+
else
|
133
|
+
"@result_for_assertion = #{code}"
|
134
|
+
end
|
135
|
+
else
|
136
|
+
"assert_equal(#{code}, @result_for_assertion)"
|
137
|
+
end
|
138
|
+
end.join("\n ")
|
139
|
+
|
140
|
+
" def test_#{@test_cases_made}\n #{test_body}\n end\n"
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
CommentTester.run(ARGV)
|
metadata
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: comment_tester
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Michael H. Buselli
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-09-10 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: |
|
14
|
+
Write and run tests from your code comments
|
15
|
+
email: cosine@cosine.org
|
16
|
+
executables:
|
17
|
+
- ruby_comment_tester
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- README
|
22
|
+
- LICENSE
|
23
|
+
- bin/ruby_comment_tester
|
24
|
+
homepage: http://github.com/cosine/ruby_comment_tester
|
25
|
+
licenses:
|
26
|
+
- BSD
|
27
|
+
metadata: {}
|
28
|
+
post_install_message:
|
29
|
+
rdoc_options: []
|
30
|
+
require_paths:
|
31
|
+
- lib
|
32
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
33
|
+
requirements:
|
34
|
+
- - '>='
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '0'
|
37
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
requirements: []
|
43
|
+
rubyforge_project: comment_tester
|
44
|
+
rubygems_version: 2.0.3
|
45
|
+
signing_key:
|
46
|
+
specification_version: 4
|
47
|
+
summary: Write and run tests from your code comments
|
48
|
+
test_files: []
|