inline_testing 0.1.0 → 0.1.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.
data/Gemfile CHANGED
@@ -3,6 +3,10 @@ source "http://rubygems.org"
3
3
  # Example:
4
4
  # gem "activesupport", ">= 2.3.5"
5
5
 
6
+ gem "ruby_parser"
7
+ gem "file-tail"
8
+ gem "sourcify"
9
+
6
10
  # Add dependencies to develop your gem here.
7
11
  # Include everything needed to run rake, tests, features, etc.
8
12
  group :development do
@@ -1,22 +1,38 @@
1
- GEM
2
- remote: http://rubygems.org/
3
- specs:
4
- git (1.2.5)
5
- jeweler (1.5.2)
6
- bundler (~> 1.0.0)
7
- git (>= 1.2.5)
8
- rake
9
- rake (0.8.7)
10
- rcov (0.9.9)
11
- riot (0.12.3)
12
- rr
13
- rr (1.0.2)
14
-
15
- PLATFORMS
16
- x86-mingw32
17
-
18
- DEPENDENCIES
19
- bundler (~> 1.0.0)
20
- jeweler (~> 1.5.2)
21
- rcov
22
- riot
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ file-tail (1.0.5)
5
+ spruz (>= 0.1.0)
6
+ git (1.2.5)
7
+ jeweler (1.5.2)
8
+ bundler (~> 1.0.0)
9
+ git (>= 1.2.5)
10
+ rake
11
+ rake (0.8.7)
12
+ rcov (0.9.9)
13
+ riot (0.12.3)
14
+ rr
15
+ rr (1.0.2)
16
+ ruby2ruby (1.2.5)
17
+ ruby_parser (~> 2.0)
18
+ sexp_processor (~> 3.0)
19
+ ruby_parser (2.0.6)
20
+ sexp_processor (~> 3.0)
21
+ sexp_processor (3.0.5)
22
+ sourcify (0.4.2)
23
+ file-tail (>= 1.0.5)
24
+ ruby2ruby (>= 1.2.5)
25
+ sexp_processor (>= 3.0.5)
26
+ spruz (0.2.5)
27
+
28
+ PLATFORMS
29
+ x86-mingw32
30
+
31
+ DEPENDENCIES
32
+ bundler (~> 1.0.0)
33
+ file-tail
34
+ jeweler (~> 1.5.2)
35
+ rcov
36
+ riot
37
+ ruby_parser
38
+ sourcify
data/README.md CHANGED
@@ -4,6 +4,10 @@ The name says it all.
4
4
 
5
5
  Check out `examples/simple.rb`.
6
6
 
7
+ ## Install
8
+
9
+ `gem install inline_testing`
10
+
7
11
  ## Contributing to inline_testing
8
12
 
9
13
  * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
data/Rakefile CHANGED
@@ -1,4 +1,10 @@
1
1
  require 'psych'
2
+
3
+ # Gay as hell jeweler workaround
4
+
5
+ require 'psych'
6
+ YAML::ENGINE.yamler = 'psych'
7
+
2
8
  require 'rubygems'
3
9
  require 'bundler'
4
10
  begin
@@ -34,21 +40,4 @@ Rake::TestTask.new(:test) do |test|
34
40
  test.verbose = true
35
41
  end
36
42
 
37
- require 'rcov/rcovtask'
38
- Rcov::RcovTask.new do |test|
39
- test.libs << 'test'
40
- test.pattern = 'test/**/*_test.rb'
41
- test.verbose = true
42
- end
43
-
44
- task :default => :test
45
-
46
- require 'rake/rdoctask'
47
- Rake::RDocTask.new do |rdoc|
48
- version = File.exist?('VERSION') ? File.read('VERSION') : ""
49
-
50
- rdoc.rdoc_dir = 'rdoc'
51
- rdoc.title = "inline_testing #{version}"
52
- rdoc.rdoc_files.include('README*')
53
- rdoc.rdoc_files.include('lib/**/*.rb')
54
- end
43
+ task :default => :test
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
@@ -1,36 +1,51 @@
1
1
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
2
2
  require 'inline_testing'
3
- InlineTesting.init(__FILE__)
4
3
 
5
4
  class User
6
5
  attr_accessor :name, :email
7
6
  def initialize(name, email)
8
- @name, @email = name, email
7
+ @name, @email, @title = name, email, :user
9
8
  end
9
+ def set_as_admin; @title = :admin; end
10
+ def is_admin?; @title == :admin; end
10
11
  end
11
12
 
12
- InlineTesting.start(__LINE__)
13
-
14
- my_user = User.new("Ryan", "c00lryguy@gmail.com")
15
- # some ignored comment
16
- my_user.name # => "Ryan"
17
- my_user.email # => "c00lryguy@gmail.com"
18
-
19
- InlineTesting.stop(__LINE__)
20
- InlineTesting.test(binding)
21
-
22
- class UserList < Array
23
- def create_new(name, email)
24
- self << User.new(name, email)
13
+ # All DSLs MUST FOLLOW SPECS.
14
+ # Don't expect InlineTesting to be able to parse it if you're doing screwy stuff.
15
+ # If you think your "screwy stuff" actually should work,
16
+ # then feel free to open an issue at github.com/c00lryguy/inline_testing/issues
17
+ InlineTesting.test do
18
+ # This is where locals for test are defined.
19
+ # Names for messages can be defined either in a second argument or a block.
20
+ # You can redefine locals at any time if you want.. must have called it at
21
+ # least once before calling `test`.
22
+ locals do
23
+ user User.new("Ryan", "c00lryguy@gmail.com"), "A User (Ryan)" # Cool but lets set the message automatically...
24
+
25
+ # This overwrites the `user` local
26
+ user( User.new("Ryan", "c00lryguy@gmail.com") ) { |user| "A User (#{user.name})" }
25
27
  end
26
- end
27
-
28
- InlineTesting.start(__LINE__)
29
-
30
- users = UserList.new
31
- users.create_new("James", "jbones1234567@james.org")
32
- users.create_new("Joe", "joejoejoejoe@four_joes.info")
33
- users.sort.collect { |u| u.name } # => ["Joe", "James"]
34
-
35
- InlineTesting.stop(__LINE__)
36
- InlineTesting.test(binding)
28
+
29
+ # This is where assertions are made.
30
+ # We only assert methods that return `true` or `false`
31
+ # Assertion messages are defined with comments.
32
+ #
33
+ # NOTE: Must keep assertions to a single line, this works by parsing each
34
+ # line and running that line within the context of your locals.
35
+ test do
36
+ # some ignored comment
37
+ user.name == "Ryan" # Name is set correctly
38
+ user.email == "c00lryguy@gmail.com" # Email is set correctly
39
+ user.name == "Syd Barret" # Will fail =(
40
+ end
41
+
42
+ # context "" do
43
+ # setup { user.set_as_admin }
44
+ # # Can't use comments on one line blocks:
45
+ # # test { user.is_admin? # Test if admin }
46
+ # # So you have to do the following:
47
+ # test do
48
+ # user.is_admin? # Test if admin
49
+ # end
50
+ # end
51
+ end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{inline_testing}
8
- s.version = "0.1.0"
8
+ s.version = "0.1.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Ryan Lewis"]
12
- s.date = %q{2011-04-25}
12
+ s.date = %q{2011-05-06}
13
13
  s.description = %q{Test from within your code}
14
14
  s.email = %q{c00lryguy@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -25,9 +25,12 @@ Gem::Specification.new do |s|
25
25
  "Rakefile",
26
26
  "VERSION",
27
27
  "examples/simple.rb",
28
- "inline_testing-0.1.0.gem",
29
28
  "inline_testing.gemspec",
30
29
  "lib/inline_testing.rb",
30
+ "lib/inline_testing/comment_parser.rb",
31
+ "lib/inline_testing/test_environment.rb",
32
+ "lib/inline_testing/test_environment/context.rb",
33
+ "lib/inline_testing/test_environment/locals.rb",
31
34
  "test/inline_testing_test.rb",
32
35
  "test/teststrap.rb"
33
36
  ]
@@ -47,17 +50,26 @@ Gem::Specification.new do |s|
47
50
  s.specification_version = 3
48
51
 
49
52
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
53
+ s.add_runtime_dependency(%q<ruby_parser>, [">= 0"])
54
+ s.add_runtime_dependency(%q<file-tail>, [">= 0"])
55
+ s.add_runtime_dependency(%q<sourcify>, [">= 0"])
50
56
  s.add_development_dependency(%q<riot>, [">= 0"])
51
57
  s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
52
58
  s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
53
59
  s.add_development_dependency(%q<rcov>, [">= 0"])
54
60
  else
61
+ s.add_dependency(%q<ruby_parser>, [">= 0"])
62
+ s.add_dependency(%q<file-tail>, [">= 0"])
63
+ s.add_dependency(%q<sourcify>, [">= 0"])
55
64
  s.add_dependency(%q<riot>, [">= 0"])
56
65
  s.add_dependency(%q<bundler>, ["~> 1.0.0"])
57
66
  s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
58
67
  s.add_dependency(%q<rcov>, [">= 0"])
59
68
  end
60
69
  else
70
+ s.add_dependency(%q<ruby_parser>, [">= 0"])
71
+ s.add_dependency(%q<file-tail>, [">= 0"])
72
+ s.add_dependency(%q<sourcify>, [">= 0"])
61
73
  s.add_dependency(%q<riot>, [">= 0"])
62
74
  s.add_dependency(%q<bundler>, ["~> 1.0.0"])
63
75
  s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
@@ -1,33 +1,17 @@
1
1
 
2
+ require 'pp'
3
+ require 'ap'
4
+
5
+ require 'meta_tools'
6
+
7
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
8
+
9
+ module InlineTesting; end
10
+
11
+ require 'inline_testing/test_environment'
12
+
2
13
  module InlineTesting
3
- def self.init(filename)
4
- @filename, @init = filename, true
5
- end
6
- def self.start(line_number)
7
- @start_line_number = line_number
8
- end
9
- def self.stop(line_number)
10
- if @init
11
- @data = File.open(@filename).readlines[@start_line_number..line_number]
12
- @start_line_number = nil
13
- end
14
- end
15
- private
16
- Regex = /^\x20*(.+)\x20*#\x20*=>\x20*(.+)\x20*$/
17
- def self.test(binding)
18
- puts "+-" * 40
19
- if @init && @data
20
- @data.find_all { |line| line =~ Regex }.each_with_index do |line, i|
21
- code, result = line.match(Regex).captures
22
- string_output = {
23
- true => "Passed!",
24
- false => "Failed!"
25
- }
26
- # All we need is to be able to eval the `code` within the context of the source file
27
- assertion = eval(code, binding) == eval(result)
28
- puts "#{i}: " + string_output[assertion]
29
- end
30
- @data = nil
31
- end
14
+ def self.test(&blk)
15
+ test_env = TestEnvironment.new(&blk)
32
16
  end
33
17
  end
@@ -0,0 +1,38 @@
1
+ module InlineTesting
2
+
3
+ module CommentParser
4
+ class << self
5
+ # @return [Array] an Array of Hash objects.
6
+ # @example Parse some comments
7
+ # cp = CommentParser.parse do
8
+ # # ignored
9
+ # 1 != 2 # One obviously isn't two. # ignored
10
+ # end
11
+ #
12
+ # p cp.first
13
+ #
14
+ # outputs:
15
+ #
16
+ # [
17
+ # {
18
+ # :code => "1 != 2",
19
+ # :comment => "One obviously isn't two."
20
+ # }
21
+ # ]
22
+ def parse(&blk)
23
+ blk.to_raw_source(:strip_enclosure => true).lines.collect { |str|
24
+ # Delete lines that only consist of comments.... like this one! Irony.
25
+ str.strip.gsub(/^#.*$/, "")
26
+ }.delete_if { |str| str.empty? }.collect { |str|
27
+ str.strip.match(/^\s*([^\s#][^#\n\r]*)(?:#([^#\n\r]*))?/).captures.collect { |str|
28
+ str.strip if str.respond_to?(:strip) # Try getting it drunk first
29
+ }
30
+ }.inject([]) { |result, ary|
31
+ result << { :code => ary.first, :comment => ary.last }
32
+ result # Needed? comment this line..
33
+ }
34
+ end
35
+ end
36
+ end
37
+
38
+ end
@@ -0,0 +1,35 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ require 'test_environment/locals'
3
+ require 'test_environment/context'
4
+ require 'comment_parser'
5
+
6
+ module InlineTesting
7
+
8
+ class TestEnvironment
9
+ def initialize(&blk)
10
+ instance_eval(&blk) if block_given?
11
+ end
12
+
13
+ def locals(&blk)
14
+ @locals ||= Locals.new
15
+ @locals.edit(&blk) if block_given?
16
+ @locals
17
+ end
18
+
19
+ def test(&blk)
20
+ @context ||= Context.new(@locals)
21
+ @context.update_methods
22
+ code_comments = CommentParser.parse(&blk)
23
+
24
+ code_comments.each do |code_comment|
25
+ @context.instance_exec(code_comment[:code]) do |code|
26
+ p eval(code)
27
+ end
28
+ end
29
+ end
30
+
31
+ def context(&blk); end
32
+ def call(&blk); end
33
+ end
34
+
35
+ end
@@ -0,0 +1,30 @@
1
+ module InlineTesting
2
+ class TestEnvironment
3
+
4
+ class Context
5
+ include MetaTools
6
+
7
+ def initialize(locals_obj)
8
+ @locals_obj, @delegated_methods = locals_obj, []
9
+ end
10
+
11
+ def update_methods
12
+
13
+ # TODO..
14
+ # Remove unused delegated methods
15
+ # (@locals_obj.locals.keys - @delegated_methods).each do |local|
16
+ # @delegated_methods.delete(local)
17
+ # metaclass.remove_method(local)
18
+ # end
19
+
20
+ # Delegate locals methods
21
+ @locals_obj.locals.keys.each do |local|
22
+ meta_def(local) { @locals_obj.send(local) } unless metaclass.method_defined?(local)
23
+ @delegated_methods << local
24
+ end
25
+ end
26
+
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,53 @@
1
+ require 'sourcify'
2
+
3
+ module InlineTesting
4
+ class TestEnvironment
5
+
6
+ class Locals
7
+ class DSL
8
+ def self.call(&blk)
9
+ instance = self.new
10
+ instance.instance_variable_set(:@result, {})
11
+ instance.instance_eval(&blk)
12
+ instance.instance_variable_get(:@result) || {}
13
+ end
14
+
15
+ # @return [Hash] `local` is the key and it holds another Hash with :obj and :msg.
16
+ def method_missing(local, *args, &blk)
17
+ if args.empty?
18
+ raise(ArgumentError, "No argument given.")
19
+ elsif args.length == 2 && !blk.nil?
20
+ raise(ArgumentError, "You can either give one arg and a block or two args, not both.")
21
+ end
22
+ @result[local] = { :obj => args.first }
23
+ @result[local][:msg] = if args.length == 1
24
+ blk.call(@result[local][:obj]) if block_given?
25
+ elsif args.length == 2
26
+ args.last
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ class Locals
33
+ include MetaTools
34
+ attr_reader :locals
35
+
36
+ def initialize; @locals = {}; end
37
+
38
+ def edit(&blk)
39
+ new_locals = DSL.call(&blk)
40
+ # Check for removed locals.. remove associated methods.. is this ever needed? hmm..
41
+ (@locals.keys - new_locals.keys).each { |local| metaclass.remove_method(local) }
42
+ @locals = new_locals
43
+ # Add/update methods
44
+ @locals.each do |local, values|
45
+ meta_def(local) { @locals[local][:obj] } unless metaclass.method_defined?(local)
46
+ end
47
+
48
+ self
49
+ end
50
+ end
51
+
52
+ end
53
+ end
metadata CHANGED
@@ -1,11 +1,11 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: inline_testing
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 0
8
+ - 1
9
9
  prerelease: false
10
10
  platform: ruby
11
11
  authors:
@@ -13,12 +13,51 @@ authors:
13
13
  autorequire: !!null
14
14
  bindir: bin
15
15
  cert_chain: []
16
- date: 2011-04-25 00:00:00.000000000 -04:00
16
+ date: 2011-05-06 00:00:00.000000000 -04:00
17
17
  default_executable: !!null
18
18
  dependencies:
19
+ - !ruby/object:Gem::Dependency
20
+ name: ruby_parser
21
+ requirement: &30716784 !ruby/object:Gem::Requirement
22
+ none: false
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ segments:
28
+ - 0
29
+ type: :runtime
30
+ prerelease: false
31
+ version_requirements: *30716784
32
+ - !ruby/object:Gem::Dependency
33
+ name: file-tail
34
+ requirement: &30716364 !ruby/object:Gem::Requirement
35
+ none: false
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ segments:
41
+ - 0
42
+ type: :runtime
43
+ prerelease: false
44
+ version_requirements: *30716364
45
+ - !ruby/object:Gem::Dependency
46
+ name: sourcify
47
+ requirement: &30715932 !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ segments:
54
+ - 0
55
+ type: :runtime
56
+ prerelease: false
57
+ version_requirements: *30715932
19
58
  - !ruby/object:Gem::Dependency
20
59
  name: riot
21
- requirement: &21995640 !ruby/object:Gem::Requirement
60
+ requirement: &30715524 !ruby/object:Gem::Requirement
22
61
  none: false
23
62
  requirements:
24
63
  - - ! '>='
@@ -28,10 +67,10 @@ dependencies:
28
67
  - 0
29
68
  type: :development
30
69
  prerelease: false
31
- version_requirements: *21995640
70
+ version_requirements: *30715524
32
71
  - !ruby/object:Gem::Dependency
33
72
  name: bundler
34
- requirement: &21994992 !ruby/object:Gem::Requirement
73
+ requirement: &30715128 !ruby/object:Gem::Requirement
35
74
  none: false
36
75
  requirements:
37
76
  - - ~>
@@ -43,10 +82,10 @@ dependencies:
43
82
  - 0
44
83
  type: :development
45
84
  prerelease: false
46
- version_requirements: *21994992
85
+ version_requirements: *30715128
47
86
  - !ruby/object:Gem::Dependency
48
87
  name: jeweler
49
- requirement: &21994356 !ruby/object:Gem::Requirement
88
+ requirement: &30714660 !ruby/object:Gem::Requirement
50
89
  none: false
51
90
  requirements:
52
91
  - - ~>
@@ -58,10 +97,10 @@ dependencies:
58
97
  - 2
59
98
  type: :development
60
99
  prerelease: false
61
- version_requirements: *21994356
100
+ version_requirements: *30714660
62
101
  - !ruby/object:Gem::Dependency
63
102
  name: rcov
64
- requirement: &21993744 !ruby/object:Gem::Requirement
103
+ requirement: &30714144 !ruby/object:Gem::Requirement
65
104
  none: false
66
105
  requirements:
67
106
  - - ! '>='
@@ -71,7 +110,7 @@ dependencies:
71
110
  - 0
72
111
  type: :development
73
112
  prerelease: false
74
- version_requirements: *21993744
113
+ version_requirements: *30714144
75
114
  description: Test from within your code
76
115
  email: c00lryguy@gmail.com
77
116
  executables: []
@@ -88,9 +127,12 @@ files:
88
127
  - Rakefile
89
128
  - VERSION
90
129
  - examples/simple.rb
91
- - inline_testing-0.1.0.gem
92
130
  - inline_testing.gemspec
93
131
  - lib/inline_testing.rb
132
+ - lib/inline_testing/comment_parser.rb
133
+ - lib/inline_testing/test_environment.rb
134
+ - lib/inline_testing/test_environment/context.rb
135
+ - lib/inline_testing/test_environment/locals.rb
94
136
  - test/inline_testing_test.rb
95
137
  - test/teststrap.rb
96
138
  has_rdoc: true
@@ -109,7 +151,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
109
151
  version: '0'
110
152
  segments:
111
153
  - 0
112
- hash: 702226109
154
+ hash: -705559789
113
155
  required_rubygems_version: !ruby/object:Gem::Requirement
114
156
  none: false
115
157
  requirements: