orb 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -7,9 +7,6 @@ When the runner gets to that line, it will open a REPL where you can
7
7
  interact with the environment and build up a test to be written back to
8
8
  the file.
9
9
 
10
- Orb is still a work in progress. It does not currently actually write
11
- back to the test file, but it will format a test to be pasted in.
12
-
13
10
  ## Usage
14
11
 
15
12
  The first step, after adding `gem "orb"` to your Gemfile, is to
@@ -28,8 +25,8 @@ commands, in addition to the functionality of a typical IRB
28
25
 
29
26
  `,n`: Give a name to the current test. Called like `,n music is loud`.
30
27
 
31
- `,w`: Print a version of the buffer suitable for pasting back into the
32
- test file. Eventually this will modify the test file with the new test.
28
+ `,w`: Write the buffer back to the test file in place of the call to
29
+ `orb!`.
33
30
 
34
31
  Within the context of the REPL, assertions are evaluated immediately,
35
32
  rather than being deferred until later in the run. This lets you run
@@ -2,11 +2,12 @@ require 'test/unit/testcase'
2
2
 
3
3
  class Test::Unit::TestCase
4
4
  def self.orb!
5
- define_method("test_orb_#{rand 100000}") { orb!(self) }
5
+ source_location = caller[0]
6
+ define_method("test_orb_#{rand 100000}") { orb!(self, source_location) }
6
7
  end
7
8
 
8
- def orb!(ctx)
9
- with_immediate_asserts(ctx) { Orb::Runner.new(ctx).run }
9
+ def orb!(ctx, source_location = caller[0])
10
+ with_immediate_asserts(ctx) { Orb::Runner.new(ctx, source_location).run }
10
11
  end
11
12
 
12
13
  def with_immediate_asserts(ctx)
@@ -40,6 +40,8 @@ module Orb
40
40
 
41
41
  command ",w", "Write the buffer to file" do
42
42
  orb_test_buffer.write
43
+ puts "Test written!"
44
+ throw(:breakout)
43
45
  end
44
46
  end
45
47
  end
data/lib/orb/runner.rb CHANGED
@@ -2,18 +2,24 @@ require 'pry'
2
2
 
3
3
  module Orb
4
4
  class Runner
5
- attr_reader :test_buffer
5
+ attr_reader :test_buffer, :source_file, :source_line
6
+
6
7
  class << self
7
8
  attr_accessor :current
8
9
  end
9
10
 
10
- def initialize(context)
11
+ def initialize(context, source_location)
11
12
  @context = context
12
- @test_buffer = TestBuffer.new
13
+ @test_buffer = TestBuffer.new(*parse_source_location(source_location))
13
14
  setup_pry
14
15
  Orb::Runner.current = self
15
16
  end
16
17
 
18
+ def parse_source_location(source_location)
19
+ source_location.scan(/(.*):(\d)+:.*/)
20
+ [$1, $2.to_i]
21
+ end
22
+
17
23
  def setup_pry
18
24
  Pry.instance_eval do
19
25
  if initial_session?
@@ -3,6 +3,11 @@ require 'tempfile'
3
3
  class TestBuffer < Array
4
4
  attr_accessor :name
5
5
 
6
+ def initialize(source_file, source_line)
7
+ @source_file = source_file
8
+ @source_line = source_line
9
+ end
10
+
6
11
  def edit_contents!
7
12
  path = "#{Dir::tmpdir}/orb-#{Kernel.rand 1000000}.rb"
8
13
  begin
@@ -24,15 +29,30 @@ class TestBuffer < Array
24
29
  end
25
30
 
26
31
  def write
27
- str = "".tap do |test|
28
- test << "test \"#{name}\" do\n"
32
+ splice_into_file(generate_test(indentation_level))
33
+ end
34
+
35
+ def generate_test(indentation_level)
36
+ indent = " " * indentation_level
37
+ "".tap do |test|
38
+ test << indent << "test \"#{name}\" do\n"
29
39
  each do |line|
30
- test << " " << line << "\n"
40
+ test << indent << " " << line << "\n"
31
41
  end
32
- test << "end\n"
42
+ test << indent << "end\n"
33
43
  end
44
+ end
45
+
46
+ def indentation_level
47
+ orb_line = File.readlines(@source_file)[@source_line - 1]
48
+ orb_line.scan(/(\s*).*/).flatten.first.size / 2
49
+ end
34
50
 
35
- puts str
51
+ def splice_into_file(content)
52
+ lines = File.readlines(@source_file)
53
+ index = @source_line - 1
54
+ lines[index] = content
55
+ File.open(@source_file, "w") { |f| f.write lines.join("") }
36
56
  end
37
57
 
38
58
  end
data/lib/orb/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Orb
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: orb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-02-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: pry
16
- requirement: &70161996083800 !ruby/object:Gem::Requirement
16
+ requirement: &70232544483980 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - =
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 0.9.8.2
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70161996083800
24
+ version_requirements: *70232544483980
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: test-unit
27
- requirement: &70161995983400 !ruby/object:Gem::Requirement
27
+ requirement: &70232544483480 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70161995983400
35
+ version_requirements: *70232544483480
36
36
  description: REPL-driven testing for ruby -- integrates with pry and lets you build
37
37
  tests incrementally in the REPL
38
38
  email: