class SpecProducer::RspecBuilders::Base

Spec Producer Rspec Builders

Provides all the the methods required to produce an RSpec string in order to create the corresponding spec files. The base class includes some methods to create a string like add method.

An example on how to build a spec would be:

resource = SomeResource.new ...

def for_each_column
  resource.column_names.each { |c| yield(c) }
end
def for_each_attr
  resource.attributes.each { |a| ield(a) }
end

# Build the spec text
text = RspecBuilder::Base.build do |b|
  b.write('require \'spec_helper\'')
  b.spec(resource.class.name, 'models') do
    b.subject { build(#{resource.class.name.underscore}) }
    b.context '#respond_to?' do
      for_each_attr do |attr|
        b.responds_to(attr)
      end
    end
    b.context 'DB columns' do
      for_each_column { |c|
        b.responds_to(c)
      }
    end
    b.context 'some other context' do
      b.subject { 'foo' }
      b.it { expect('subject').to eq 'foo' }
      context 'a nested context' do
        b.subject { 'create(:user).profile' }
        b.it { should_be(:present) }
      end
    end
  end
end

# Write the generated text to spec_file
Utils::FileUtils.try_to_create_spec_file('models', resource.class.name.underscore, text)

puts text

> require 'spec_helper'

describe SomeResource, type: :model do
  subject { builder(:resource) }
  context '#responds_to?' do
    it { is_expected.to respond_to(:attr1) }
    it { is_expected.to respond_to(:attr2) }
  end

  context 'DB columns' do
    it { is_expected.to have_db_column(:email) }
    it { is_expected.to respond_to(:name) }
  end

  context 'some other context' do
    subject { 'foo' }
    it { expect(subject).to eq 'foo' }

    context 'a nested context' do
      subject { create(:user).profile }
      it { expect(subject).to be_present }
    end
  end
end

Note that intentation is automatially done based on the current block we are.

Attributes

_text[R]
intend[R]

Public Class Methods

new(t = "") { |self| ... } click to toggle source
# File lib/spec_producer/rspec_builders/base.rb, line 86
def initialize(t = "")
  @_text = t.to_s
  @intend = 0

  yield self if block_given?
end

Public Instance Methods

<<(text, add_intent = true)
Alias for: add
add(text, add_intent = true) click to toggle source

Adds a string to buffer. by default auto intent is handled here In order to bypas it we pas false as the current argument. For example when we add a new line we don't want to add a n times tabs

# File lib/spec_producer/rspec_builders/base.rb, line 97
def add(text, add_intent = true)
  add_tabs if add_intent == true

  _text << text
  self
end
Also aliased as: <<, append, write
add_tabs() click to toggle source

Adds @_intend tabs to the buffer.

# File lib/spec_producer/rspec_builders/base.rb, line 117
def add_tabs
  intend.times { _text << '  ' }
end
append(text, add_intent = true)
Alias for: add
flush!() click to toggle source

Flushes the current buffer to an empty String. Also reset intend count to zero.

# File lib/spec_producer/rspec_builders/base.rb, line 110
def flush!
  @_text = ""
  @intend = 0
end
inspect() click to toggle source
# File lib/spec_producer/rspec_builders/base.rb, line 131
def inspect
  to_s
end
new_line() click to toggle source

Adds a new line to the buffer

# File lib/spec_producer/rspec_builders/base.rb, line 123
def new_line
  add "\n", false
end
to_s() click to toggle source
# File lib/spec_producer/rspec_builders/base.rb, line 127
def to_s
  _text
end
write(text, add_intent = true)
Alias for: add

Private Instance Methods

decrease_intent() click to toggle source
# File lib/spec_producer/rspec_builders/base.rb, line 142
def decrease_intent
  @intend -= 1
end
increase_intent() click to toggle source

Increases and decreases the intentation counter. We increase the counter when we are on nested contexts like nested context block.

# File lib/spec_producer/rspec_builders/base.rb, line 138
def increase_intent
  @intend += 1
end