Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # auto generating spec files from doc.
- =begin spec: just sample
- @ary = [1, 2, 3]
- @ary ##=> [ 1, 2, 3]
- @ary ##== [ 1, 2, 3]
- @ary ## not be_empty
- # it's just comment
- @ary.size ## != 0
- =end
- class DocSpec
- SPEC_STATRT_REGEX = /^=begin\sspec:\s(.+)$/
- SPEC_END_REGEX = /^=end/
- EXAMPLE_REGEX = /^(.+)\s+##\s*?(\S+)\s*?(.+)\s*?$/
- def initialize file
- @file = file
- File.open(@file) do |f|
- @lines = f.readlines
- end
- end
- def parse
- result = ''
- is_started = false
- is_begin = false
- @lines.each do |line|
- if is_started
- if match = EXAMPLE_REGEX.match(line)
- actual, op, expected = match[1..match.size]
- unless is_begin
- is_begin = true
- puts @buffer
- @buffer = []
- puts " end"
- end
- puts gen_spec(op, expected, actual)
- elsif match = SPEC_END_REGEX.match(line)
- is_started = false
- is_begin = false
- @buffer = []
- puts "end"
- else
- @buffer ||= []
- @buffer.push(line)
- end
- else
- if match = SPEC_STATRT_REGEX.match(line)
- is_started = true
- @buffer ||= []
- @buffer.push(start_spec(match[1]))
- end
- end
- end
- end
- def start_spec spec
- str = <<-EOF
- describe #{spec.inspect} do
- before do
- EOF
- end
- def gen_spec op, expected, actual
- is_negative = false
- case op
- when "=>"
- op = "=="
- when '!='
- op = "=="
- is_negative = true
- when "not"
- op = ''
- is_negative = true
- end
- str = <<-EOF
- it do
- #{actual}.#{is_negative ? "should_not" : "should" } #{op} #{expected}
- end
- EOF
- end
- end
- if $0 == __FILE__
- DocSpec.new(__FILE__).parse
- end
Add Comment
Please, Sign In to add comment