doctest-core 0.0.1

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 ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ODJmYWE0ZjE4Yjc4MTNkNDE4Y2UzOWI4YThhY2NlY2IwMTA3NDk1Yg==
5
+ data.tar.gz: !binary |-
6
+ NzUxYTc5YWQyNTZmOTAyZGE1MTk4MWJiZDVjNGRkNTAxZGExZTk0MQ==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ MTE5MTAyMWQxMGZmNTE4NDY1ZmQ4YzNhM2U1MThlMzBjNmRmNzNjYThiMGQz
10
+ MGExZGE3ODQzZWQxMzE5Y2FkNjdiNDYxMjY0ZWRlYzljNTlkMzRkZGVlMTgz
11
+ YmVhMGQ2N2MyZWM0MmU3MDU1ZmEyYzM0MDZiMWIwYTQ2ZTdjOGE=
12
+ data.tar.gz: !binary |-
13
+ NDE0NGYyODM1ZWJjYzQ3YjBiYTU5OWFiODU5ZmM0MTlhYjVmMWQ0ZThkMjI1
14
+ ZmQzN2Y3Y2M2M2FkM2ZhZDNiNWU2MzAyZWRiNzA3MTIxYjk4N2YzM2ViMjhi
15
+ Y2MwMzIwNWE4NmQ2OThlZTlkNjgxOGY2YmFjOGE5MmI1YmIwMTE=
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - jruby-19mode
5
+ - rbx-19mode
6
+ script: bundle exec rspec spec
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in doctest-core.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,9 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard :rspec do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+ end
9
+
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Heiko Zeus
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,69 @@
1
+ [![Code Climate](https://codeclimate.com/github/hzeus/doctest-core.png)](https://codeclimate.com/github/hzeus/doctest-core)
2
+ [![Build Status](https://travis-ci.org/hzeus/doctest-core.png)](https://travis-ci.org/hzeus/doctest-core)
3
+
4
+ # Doctest::Core
5
+
6
+ Extract doctests from your ruby files
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'doctest-core'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install doctest-core
21
+
22
+ ## Usage
23
+
24
+ Add doctest-comments to your ruby files:
25
+
26
+ ```ruby
27
+ class ClassWithDoctests
28
+
29
+ # Always returns 'a'
30
+ #
31
+ # >> ClassWithDoctests.a
32
+ # => 'a'
33
+ def self.a
34
+ 'a'
35
+ end
36
+
37
+ # Always returns 'b'
38
+ #
39
+ # >> ClassWithDoctests.b
40
+ # => 'b'
41
+ def self.b
42
+ 'b'
43
+ end
44
+
45
+ end
46
+ ```
47
+
48
+ You can then run
49
+ ```ruby
50
+ Doctest::Core.extract_from(ClassWithDoctests)
51
+ ```
52
+
53
+ to get a simple array of doctest occurrences. Those contain
54
+ * ```code_string```: The source code string (`'ClassWithDoctests.a'`)
55
+ * ```result_string```: The result code string (`'b'`)
56
+ * ```original_file```: The source location of the file that contains the doctest
57
+ * ```line```: The line number of the doctest occurrence
58
+
59
+ You can also access ```code_evaluation``` and ```result_evaluation``` for the results of the both code blocks.
60
+
61
+ Those results can be used to implement test framework-specific gems.
62
+
63
+ ## Contributing
64
+
65
+ 1. Fork it
66
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
67
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
68
+ 4. Push to the branch (`git push origin my-new-feature`)
69
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'doctest/core/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "doctest-core"
8
+ spec.version = Doctest::Core::VERSION
9
+ spec.authors = ["Heiko Zeus"]
10
+ spec.email = ["heiko.zeus@zweitag.de"]
11
+ spec.description = %q{Extract doctests from ruby files}
12
+ spec.summary = %q{This aims to provide the basis for elixir-like doctests. It should be used by test-framework-specific gems}
13
+ spec.homepage = "https://github.com/hzeus/doctest-core"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+
24
+ spec.add_development_dependency "rspec"
25
+ spec.add_development_dependency "guard-rspec"
26
+ spec.add_development_dependency "pry"
27
+ end
@@ -0,0 +1,75 @@
1
+ module Doctest
2
+ module Core
3
+ class Base
4
+
5
+ def self.analyze(str)
6
+ self.new(str).analyze
7
+ end
8
+
9
+ CODE_REGEXP = /^\s*#\s*>>\s*(?<code>.*)/
10
+ RESULT_REGEXP = /#\s*=>\s*(?<result>.*)$/
11
+
12
+ def initialize(source)
13
+ if File.exists?(source)
14
+ @filename = source
15
+ @str = File.read(source)
16
+ else
17
+ @filename = nil
18
+ @str = source
19
+ end
20
+ @state = :no_match
21
+ @current = nil
22
+ @doctests = []
23
+ end
24
+
25
+ def analyze
26
+ @str.lines.with_index do |line, index|
27
+ case line
28
+ when CODE_REGEXP
29
+ handle_code(Regexp.last_match['code'])
30
+ when RESULT_REGEXP
31
+ handle_result(Regexp.last_match['result'], index)
32
+ else
33
+ handle_no_doctest
34
+ end
35
+ end
36
+
37
+ @doctests
38
+ end
39
+
40
+ private
41
+
42
+ def add_doctest(code_lines, result, line_index)
43
+ @doctests << Doctest.new(code_lines.join(';'), result, @filename, line_index + 1)
44
+ end
45
+
46
+ def handle_code(code)
47
+ case @state
48
+ when :no_match
49
+ @current_code = [code]
50
+ @state = :match
51
+ when :match
52
+ @current << code
53
+ end
54
+ end
55
+
56
+ def handle_result(result, line_index)
57
+ case @state
58
+ when :match
59
+ add_doctest(@current_code, result, line_index)
60
+ reset
61
+ end
62
+ end
63
+
64
+ def handle_no_doctest
65
+ reset
66
+ end
67
+
68
+ def reset
69
+ @state = :no_match
70
+ @current
71
+ end
72
+
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,15 @@
1
+ module Doctest
2
+ module Core
3
+ class Doctest < Struct.new(:code_string, :result_string, :original_file, :line)
4
+
5
+ def result_evaluation
6
+ eval(result_string)
7
+ end
8
+
9
+ def code_evaluation
10
+ eval(code_string)
11
+ end
12
+
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,34 @@
1
+ module Doctest
2
+ module Core
3
+ module Source
4
+
5
+ def self.to_filenames(source)
6
+ case source
7
+ when Class
8
+ constant_filenames(source)
9
+ else
10
+ [ source ]
11
+ end
12
+ end
13
+
14
+ def self.constant_filenames(constant)
15
+ (filenames_of_instance_methods(constant) + filenames_of_methods(constant)).uniq
16
+ end
17
+
18
+ def self.filenames_of_instance_methods(constant)
19
+ constant.
20
+ instance_methods(false).
21
+ map{|m| constant.instance_method(m).source_location}.
22
+ map(&:first)
23
+ end
24
+
25
+ def self.filenames_of_methods(constant)
26
+ constant.
27
+ methods(false).
28
+ map{|m| constant.method(m).source_location}.
29
+ map(&:first)
30
+ end
31
+
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,5 @@
1
+ module Doctest
2
+ module Core
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,15 @@
1
+ require "doctest/core/version"
2
+ require "doctest/core/base"
3
+ require "doctest/core/doctest"
4
+ require "doctest/core/source"
5
+
6
+ module Doctest
7
+ module Core
8
+
9
+ def self.extract_from(source)
10
+ Source.to_filenames(source).
11
+ flat_map{|code_location| Base.analyze(code_location)}
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,19 @@
1
+ class ClassWithDoctests
2
+
3
+ # Always returns 'a'
4
+ #
5
+ # >> ClassWithDoctests.a
6
+ # => 'a'
7
+ def self.a
8
+ 'a'
9
+ end
10
+
11
+ # Always returns 'b'
12
+ #
13
+ # >> ClassWithDoctests.b
14
+ # => 'b'
15
+ def self.b
16
+ 'b'
17
+ end
18
+
19
+ end
@@ -0,0 +1,4 @@
1
+ class SimpleClass
2
+ def first_method; end
3
+ def second_method; end
4
+ end
@@ -0,0 +1,3 @@
1
+ class SimpleClass
2
+ def third_method; end
3
+ end
@@ -0,0 +1 @@
1
+ # This is just a simple file
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ describe Doctest::Core::Base do
4
+
5
+ it 'extracts simple code samples' do
6
+ str = <<-END.gsub(/^ {6}/, '')
7
+ # >> 3 + 2
8
+ # => 5
9
+ END
10
+ analyzer = Doctest::Core::Base.new(str)
11
+
12
+ doctests = analyzer.analyze
13
+ doctests.should have(1).entries
14
+
15
+ doctests[0].should == Doctest::Core::Doctest.new("3 + 2", "5", nil, 2)
16
+ end
17
+
18
+ it 'extracts multiple samples from a string' do
19
+ str = <<-END.gsub(/^ {6}/, '')
20
+ # >> 3 + 2
21
+ # => 5
22
+ def foo
23
+ end
24
+ # >> 4 + 2
25
+ # => 6
26
+ END
27
+ analyzer = Doctest::Core::Base.new(str)
28
+
29
+ doctests = analyzer.analyze
30
+ doctests.should have(2).entries
31
+
32
+ doctests[0].should == Doctest::Core::Doctest.new("3 + 2", "5", nil, 2)
33
+ doctests[1].should == Doctest::Core::Doctest.new("4 + 2", "6", nil, 6)
34
+ end
35
+
36
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ module Doctest::Core
4
+ describe Doctest do
5
+
6
+ def self.doctest(source)
7
+ context "doctests for #{source}" do
8
+ ::Doctest::Core.extract_from(source).each do |doctest|
9
+ it "at #{doctest.original_file}:#{doctest.line}" do
10
+ doctest.code_evaluation.should == doctest.result_evaluation
11
+ end
12
+ end
13
+ end
14
+ end
15
+
16
+ require fixture('class_with_doctests.rb')
17
+ doctest ClassWithDoctests
18
+
19
+ context 'evaluation' do
20
+
21
+ it 'evaluates the result string' do
22
+ Doctest.new(nil, '5', nil, nil).result_evaluation.should == 5
23
+ Doctest.new(nil, '3 + 2', nil, nil).result_evaluation.should == 5
24
+ end
25
+
26
+ it 'evaluates the code string' do
27
+ Doctest.new("a = 3\na + 2", nil, nil, nil).code_evaluation.should == 5
28
+ end
29
+
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+ require fixture('simple_class.rb')
3
+ require fixture('simple_class_reopened.rb')
4
+
5
+ module Doctest::Core
6
+ describe 'Source' do
7
+
8
+ context 'to_code_locations' do
9
+
10
+ it 'wraps a filename in an array' do
11
+ Source.to_filenames(fixture('simple_file.rb')).should == [ fixture('simple_file.rb') ]
12
+ end
13
+
14
+ it 'resolves the filenames of a ruby class' do
15
+ Source.to_filenames(SimpleClass).should == [
16
+ fixture('simple_class.rb'),
17
+ fixture('simple_class_reopened.rb')
18
+ ]
19
+ end
20
+
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+ require fixture('class_with_doctests.rb')
3
+
4
+ describe Doctest::Core do
5
+
6
+ it 'extracts doctests from ruby classes' do
7
+ orig_file = fixture('class_with_doctests.rb')
8
+ doctests = Doctest::Core.extract_from(ClassWithDoctests)
9
+ doctests.should have(2).entries
10
+
11
+ doctests[0].should == Doctest::Core::Doctest.new("ClassWithDoctests.a", "'a'", orig_file, 6)
12
+ doctests[1].should == Doctest::Core::Doctest.new("ClassWithDoctests.b", "'b'", orig_file, 14)
13
+ end
14
+
15
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ Bundler.require(:default)
5
+
6
+ require 'doctest/core'
7
+
8
+ def fixture(filename)
9
+ File.join(Gem::Specification.find_by_name('doctest-core').gem_dir, 'spec', 'fixtures', filename)
10
+ end
metadata ADDED
@@ -0,0 +1,147 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: doctest-core
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Heiko Zeus
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-10-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: guard-rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Extract doctests from ruby files
84
+ email:
85
+ - heiko.zeus@zweitag.de
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - .travis.yml
92
+ - Gemfile
93
+ - Guardfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - doctest-core.gemspec
98
+ - lib/doctest/core.rb
99
+ - lib/doctest/core/base.rb
100
+ - lib/doctest/core/doctest.rb
101
+ - lib/doctest/core/source.rb
102
+ - lib/doctest/core/version.rb
103
+ - spec/fixtures/class_with_doctests.rb
104
+ - spec/fixtures/simple_class.rb
105
+ - spec/fixtures/simple_class_reopened.rb
106
+ - spec/fixtures/simple_file.rb
107
+ - spec/lib/doctest/core/base_spec.rb
108
+ - spec/lib/doctest/core/doctest_spec.rb
109
+ - spec/lib/doctest/core/source_spec.rb
110
+ - spec/lib/doctest/core_spec.rb
111
+ - spec/spec_helper.rb
112
+ homepage: https://github.com/hzeus/doctest-core
113
+ licenses:
114
+ - MIT
115
+ metadata: {}
116
+ post_install_message:
117
+ rdoc_options: []
118
+ require_paths:
119
+ - lib
120
+ required_ruby_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ! '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ required_rubygems_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ! '>='
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ requirements: []
131
+ rubyforge_project:
132
+ rubygems_version: 2.1.5
133
+ signing_key:
134
+ specification_version: 4
135
+ summary: This aims to provide the basis for elixir-like doctests. It should be used
136
+ by test-framework-specific gems
137
+ test_files:
138
+ - spec/fixtures/class_with_doctests.rb
139
+ - spec/fixtures/simple_class.rb
140
+ - spec/fixtures/simple_class_reopened.rb
141
+ - spec/fixtures/simple_file.rb
142
+ - spec/lib/doctest/core/base_spec.rb
143
+ - spec/lib/doctest/core/doctest_spec.rb
144
+ - spec/lib/doctest/core/source_spec.rb
145
+ - spec/lib/doctest/core_spec.rb
146
+ - spec/spec_helper.rb
147
+ has_rdoc: