Class: SpecMe

Inherits:
Object
  • Object
show all
Defined in:
lib/spec_me.rb,
lib/spec_me/config.rb,
lib/spec_me/version.rb,
lib/spec_me/renderer.rb

Defined Under Namespace

Classes: Renderer

Constant Summary collapse

Config =
OpenStruct.new \
spec_me_root:   File.join( 'spec', 'spec_me' ),
spec_me_helper: 'spec_helper',
spec_me_tag:    'spec_me',

start_code_block: %r{\A```ruby\z},
end_code_block:   %r{\A```\z},
code_comment:     %r{\A\s*#\s*(.*)\z},
equality_xp:      %r{\A\s*(.*)#-->(.*)\z}
VERSION =
"0.0.1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSpecMe

Returns a new instance of SpecMe.



15
16
17
# File 'lib/spec_me.rb', line 15

def initialize
  self.config = SpecMe::Config
end

Instance Attribute Details

#astObject

Returns the value of attribute ast.



5
6
7
# File 'lib/spec_me.rb', line 5

def ast
  @ast
end

#configObject

Returns the value of attribute config.



5
6
7
# File 'lib/spec_me.rb', line 5

def config
  @config
end

#fileObject

Returns the value of attribute file.



5
6
7
# File 'lib/spec_me.rb', line 5

def file
  @file
end

Instance Method Details

#ast_to_specObject



19
20
21
# File 'lib/spec_me.rb', line 19

def ast_to_spec
  Renderer.new( config ).render ast, count: 1, file_name: @base_file_name 
end

#generate_specObject



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/spec_me.rb', line 23

def generate_spec
  return if ast.empty?
  @base_file_name = File
    .basename( file )
    .sub( %r{\..*\z}, "" )
  @output_file = File.join config.spec_me_root, "#{@base_file_name}_spec.rb"

  File.open( @output_file, "w" ) do | f | 
    f.puts ast_to_spec
  end 
end

#parse(lines) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/spec_me.rb', line 35

def parse lines
  self.ast = []
  @state = :outside
  lines.each do | line |
    state_machine line.chomp
  end
  # post_parse
end

#parse_line(line) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/spec_me.rb', line 44

def parse_line line
  case line
  when config.code_comment
    [:comment, Regexp.last_match[1]]
  when config.equality_xp
    [:equality_xp] + Regexp.last_match[1..2].map(&:strip)
  else
    [:ruby, line.strip]
  end
end

#post_parseObject



55
56
57
# File 'lib/spec_me.rb', line 55

def post_parse
  # No need to create specs if there are no expectations
end

#run(args) ⇒ Object

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
# File 'lib/spec_me.rb', line 7

def run args
  file = args.shift or raise ArgumentError, "Missing file name"
  raise ArgumentError, "Input file `#{file}' missing or not readable" unless File.readable? file
  self.file = file
  parse File.readlines( file )
  generate_spec
end