Guest User

Untitled

a guest
Feb 19th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. # auto generating spec files from doc.
  2.  
  3. =begin spec: just sample
  4.  
  5. @ary = [1, 2, 3]
  6.  
  7. @ary ##=> [ 1, 2, 3]
  8. @ary ##== [ 1, 2, 3]
  9.  
  10. @ary ## not be_empty
  11.  
  12. # it's just comment
  13.  
  14. @ary.size ## != 0
  15.  
  16.  
  17. =end
  18. class DocSpec
  19. SPEC_STATRT_REGEX = /^=begin\sspec:\s(.+)$/
  20. SPEC_END_REGEX = /^=end/
  21.  
  22. EXAMPLE_REGEX = /^(.+)\s+##\s*?(\S+)\s*?(.+)\s*?$/
  23.  
  24. def initialize file
  25. @file = file
  26. File.open(@file) do |f|
  27. @lines = f.readlines
  28. end
  29. end
  30.  
  31. def parse
  32. result = ''
  33.  
  34. is_started = false
  35. is_begin = false
  36. @lines.each do |line|
  37. if is_started
  38. if match = EXAMPLE_REGEX.match(line)
  39. actual, op, expected = match[1..match.size]
  40. unless is_begin
  41. is_begin = true
  42. puts @buffer
  43. @buffer = []
  44. puts " end"
  45. end
  46. puts gen_spec(op, expected, actual)
  47. elsif match = SPEC_END_REGEX.match(line)
  48. is_started = false
  49. is_begin = false
  50. @buffer = []
  51. puts "end"
  52. else
  53. @buffer ||= []
  54. @buffer.push(line)
  55. end
  56. else
  57. if match = SPEC_STATRT_REGEX.match(line)
  58. is_started = true
  59. @buffer ||= []
  60. @buffer.push(start_spec(match[1]))
  61. end
  62. end
  63. end
  64. end
  65.  
  66.  
  67. def start_spec spec
  68. str = <<-EOF
  69. describe #{spec.inspect} do
  70. before do
  71. EOF
  72. end
  73.  
  74. def gen_spec op, expected, actual
  75. is_negative = false
  76. case op
  77. when "=>"
  78. op = "=="
  79. when '!='
  80. op = "=="
  81. is_negative = true
  82. when "not"
  83. op = ''
  84. is_negative = true
  85. end
  86. str = <<-EOF
  87. it do
  88. #{actual}.#{is_negative ? "should_not" : "should" } #{op} #{expected}
  89. end
  90. EOF
  91. end
  92. end
  93.  
  94. if $0 == __FILE__
  95. DocSpec.new(__FILE__).parse
  96. end
Add Comment
Please, Sign In to add comment