Class: Cellophane::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/cellophane/parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Parser

Returns a new instance of Parser.



3
4
5
# File 'lib/cellophane/parser.rb', line 3

def initialize(options)
	@options = options
end

Instance Method Details

#featuresObject



7
8
9
10
11
12
# File 'lib/cellophane/parser.rb', line 7

def features
	# if no pattern is specified, let cucumber run 'em all
	return [] if @options[:pattern].nil?
	collected_features = @options[:regexp] ? collect_features_by_regexp : collect_features_by_glob
	return collected_features.any? ? collected_features : nil
end

#tagsObject

features



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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/cellophane/parser.rb', line 14

def tags
	tags = {
		:or => [],
		:and => [],
		:not => []
	}

	return '' if @options[:tags].nil?

	@options[:tags].split(',').each do |t|
		# if tags are numeric, let's support ranges !!!
		if t =~ /^(~)?([0-9]+)-([0-9]+)$/
			x = $2.to_i
			y = $3.to_i
			exclude = $1
		
			# in case the user put them in the wrong order ... doh!
			if x > y
				z = x.dup
				x = y.dup
				y = z.dup
			end
		
			(x..y).each do |i|
				if exclude
					tags[:not] << "#{i}"
				else
					tags[:or] << "#{i}"
				end
			end
		else
			if t =~ /^~(.+)/
				tags[:not] << $1
			elsif t =~ /^\+(.+)/
				tags[:and] << $1
			else
				tags[:or] << t
			end
		end
	end # each

	[:and, :or, :not].each { |type| tags[type].uniq! }
	
	# if there are AND/OR tags, remove any NOT tags so we avoid
	# duplicating the tag when passing to cucumber...so instead of
	# cucumber -t @1,@2,@3 -t ~@2
	# we'd like to see
	# cucumber -t @1,@3

	intersection = tags[:or] & tags[:not]
	tags[:or] -= intersection
	tags[:not] -= intersection

	intersection = tags[:and] & tags[:not]
	tags[:and] -= intersection
	tags[:not] -= intersection

	# now add @ and ~ as appropriate
	tags[:or].each_with_index { |tag, i| tags[:or][i] = "@#{tag}" }
	tags[:and].each_with_index { |tag, i| tags[:and][i] = "@#{tag}" }
	tags[:not].each_with_index { |tag, i| tags[:not][i] = "~@#{tag}" }

	tags_fragment = ''
	tags_fragment += "-t #{tags[:or].join(',')} " if tags[:or].any?
	tags_fragment += "-t #{tags[:and].join(' -t ')} " if tags[:and].any?
	tags_fragment += "-t #{tags[:not].join(' -t ')}" if tags[:not].any?
	
	# if the user passes in tags with @ already in it
	tags_fragment.gsub('@@', '@')
end