summaryrefslogtreecommitdiff
path: root/spec/mspec/lib/mspec/runner/tag.rb
blob: e2275ad3a6275ad36fd8816e30a78e50adc88608 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class SpecTag
  attr_accessor :tag, :comment, :description

  def initialize(string=nil)
    parse(string) if string
  end

  def parse(string)
    m = /^([^()#:]+)(\(([^)]+)?\))?:(.*)$/.match string
    @tag, @comment, description = m.values_at(1, 3, 4) if m
    @description = unescape description
  end

  def unescape(str)
    return unless str
    if str[0] == ?" and str[-1] == ?"
      str[1..-2].gsub('\n', "\n")
    else
      str
    end
  end

  def escape(str)
    if str.include? "\n"
      %["#{str.gsub("\n", '\n')}"]
    else
      str
    end
  end

  def to_s
    "#{@tag}#{ "(#{@comment})" if @comment }:#{escape @description}"
  end

  def ==(o)
    @tag == o.tag and @comment == o.comment and @description == o.description
  end
end