nyan-cat-formatter 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -10,7 +10,7 @@ _-_-_-_-_-_-_-"" ""
10
10
 
11
11
  This is my take on the Nyan Cat RSpec Formatter. It simply creates a rainbow trail of test results. It also counts the number of examples as they execute and highlights failed and pending specs.
12
12
 
13
- The rainbow changes colors as it runs. See it in action [here](http://vimeo.com).
13
+ The rainbow changes colors as it runs. See it in action [here](http://vimeo.com/32241727).
14
14
 
15
15
  ```
16
16
  rspec --format NyanCatFormatter
@@ -25,8 +25,25 @@ $ gem install nyan-cat-formatter
25
25
 
26
26
  If you want to use Nyan Cat as your default formatter, simply put the options in your .rspec file:
27
27
 
28
+ ```
28
29
  --format NyanCatFormatter
29
- --color
30
+ ```
31
+
32
+ Using with Rails rake spec
33
+ ----------
34
+
35
+ To use Nyan Cat with Rails "rake spec" you need to add Nyan Cat dependecy in your Gemfile.
36
+
37
+ ```
38
+ group :test do
39
+ gem "nyan-cat-formatter"
40
+ end
41
+ ```
42
+ And then
43
+
44
+ ```
45
+ bundle install
46
+ ```
30
47
 
31
48
  Contributing
32
49
  ----------
@@ -3,19 +3,19 @@ require 'rspec/core/formatters/base_text_formatter'
3
3
 
4
4
  class NyanCatFormatter < RSpec::Core::Formatters::BaseTextFormatter
5
5
 
6
- ESC = "\e["
7
- NND = "#{ESC}0m"
8
- PASS = '='
9
- FAIL = '*'
10
- ERROR = '!'
11
- PENDING = '·'
6
+ ESC = "\e["
7
+ NND = "#{ESC}0m"
8
+ PASS = '='
9
+ PASS_ARY = ['-', '_']
10
+ FAIL = '*'
11
+ ERROR = '!'
12
+ PENDING = '+'
12
13
 
13
- attr_reader :title, :current, :example_results, :color_index
14
+ attr_reader :current, :example_results, :color_index
14
15
 
15
16
  def start(example_count)
16
17
  super(example_count)
17
- @current, @color_index = 0,0
18
- @bar_length = 70
18
+ @current, @color_index, @passing_count = 0,0,0
19
19
  @example_results = []
20
20
  end
21
21
 
@@ -49,70 +49,115 @@ class NyanCatFormatter < RSpec::Core::Formatters::BaseTextFormatter
49
49
  end
50
50
 
51
51
  def dump_failures
52
- # noop
52
+ #no op
53
53
  end
54
54
 
55
55
  # Increments the example count and displays the current progress
56
56
  #
57
- # Returns nothing
57
+ # @returns nothing
58
58
  def tick(mark = PASS)
59
59
  @example_results << mark
60
60
  @current = (@current > @example_count) ? @example_count : @current + 1
61
- @title = " #{current}/#{example_count}"
62
61
  dump_progress
63
62
  end
64
63
 
65
64
  # Creates a rainbow trail
66
65
  #
67
- # Returns the sprintf format of the Nyan cat
66
+ # @return [String] the sprintf format of the Nyan cat
68
67
  def nyan_trail
69
- width = percentage * @bar_length / 100
70
- marker = @example_results.map{ |mark| highlight(mark) }.join
71
- sprintf("%s#{nyan_cat}%s", marker, " " * (@bar_length - width) )
68
+ marks = @example_results.map{ |mark| highlight(mark) }
69
+ marks.shift(current_width - terminal_width) if current_width > terminal_width
70
+ nyan_cat_lines = nyan_cat.split("\n").each_with_index.map do |line, index|
71
+ format("%s#{line}", marks.join)
72
+ end.join("\n")
72
73
  end
73
74
 
74
- # Calculates the percentage completed any given point
75
+ # Determine which Ascii Nyan Cat to display. If tests are complete,
76
+ # Nyan Cat goes to sleep. If there are failing or pending examples,
77
+ # Nyan Cat is concerned.
75
78
  #
76
- # Returns Fixnum of the percentage
77
- def percentage
78
- @example_count.zero? ? 100 : @current * 100 / @example_count
79
- end
80
-
81
- # Ascii Nyan Cat. If tests are complete, Nyan Cat goes to sleep. If
82
- # there are failing or pending examples, Nyan Cat is concerned.
83
- #
84
- # Returns String Nyan Cat
79
+ # @return [String] Nyan Cat
85
80
  def nyan_cat
86
81
  if @failure_count > 0 || @pending_count > 0
87
- '~|_(o.o)'
82
+ ascii_cat('o')[@color_index%2].join("\n") #'~|_(o.o)'
88
83
  elsif (@current == @example_count)
89
- '~|_(-.-)'
84
+ ascii_cat('-')[@color_index%2].join("\n") # '~|_(-.-)'
90
85
  else
91
- '~|_(^.^)'
86
+ ascii_cat('^')[@color_index%2].join("\n") # '~|_(^.^)'
92
87
  end
93
88
  end
94
89
 
95
90
  # Displays the current progress in all Nyan Cat glory
96
91
  #
92
+ # @return nothing
97
93
  def dump_progress
98
- max_width = 80
99
- line = sprintf("%-8s %s", @title[0,(7)] + ":", nyan_trail)
100
- tail = (@current == @example_count) ? "\n" : "\r"
101
-
102
- if line.length == max_width - 1
103
- output.print line + tail
104
- output.flush
105
- elsif line.length >= max_width
106
- @bar_length = [@bar_length - (line.length - max_width + 1), 0].max
107
- @bar_length == 0 ? output.print( rainbowify(line + tail) ) : dump_progress
108
- else
109
- @bar_length += max_width - line.length + 1
110
- dump_progress
111
- end
94
+ padding = @example_count.to_s.length * 2 + 2
95
+ line = nyan_trail.split("\n").each_with_index.inject([]) do |result, (trail, index)|
96
+ value = "#{scoreboard[index]}/#{@example_count}:"
97
+ result << format("%s %s", value, trail)
98
+ end.join("\n")
99
+ output.print line + eol
100
+ end
101
+
102
+ # Determines how we end the trail line. If complete, return a newline etc.
103
+ #
104
+ # @return [String]
105
+ def eol
106
+ return "\n" if @current == @example_count
107
+ length = (nyan_cat.split("\n").length - 1)
108
+ length > 0 ? format("\e[1A" * length + "\r") : "\r"
109
+ end
110
+
111
+ # Calculates the current flight length
112
+ #
113
+ # @return [Fixnum]
114
+ def current_width
115
+ padding = @example_count.to_s.length * 2 + 6
116
+ cat_length = nyan_cat.split("\n").group_by(&:size).max.first
117
+ padding + @current + cat_length
118
+ end
119
+
120
+ # A Unix trick using stty to get the console columns
121
+ # does not work in JRuby :-(
122
+ #
123
+ # @return [Fixnum]
124
+ def terminal_width
125
+ @terminal_width ||= `stty size`.split.map { |x| x.to_i }.reverse.first - 1
126
+ end
127
+
128
+ # Creates a data store of pass, failed, and pending example results
129
+ # We have to pad the results here because sprintf can't properly pad color
130
+ #
131
+ # @return [Array]
132
+ def scoreboard
133
+ padding = @example_count.to_s.length + 9
134
+ [ @current.to_s.rjust( @example_count.to_s.length),
135
+ green(@current - @pending_examples.size - @failed_examples.size).rjust(padding),
136
+ yellow(@pending_examples.size).rjust(padding),
137
+ red(@failed_examples.size).rjust(padding) ]
138
+ end
139
+
140
+ # Ascii version of Nyan cat. Two cats in the array allow Nyan to animate running.
141
+ #
142
+ # @param o [String] Nyan's eye
143
+ # @return [Array] Nyan cats
144
+ def ascii_cat(o = '^')
145
+ [[ "_,------, ",
146
+ "_| /\\_/\\ ",
147
+ "~|_( #{o} .#{o}) ",
148
+ " \"\" \"\" "
149
+ ],
150
+ [ "_,------, ",
151
+ "_| /\\_/\\",
152
+ "^|__( #{o} .#{o}) ",
153
+ " \"\" \"\" "
154
+ ]]
112
155
  end
113
156
 
114
157
  # Colorizes the string with raindow colors of the rainbow
115
158
  #
159
+ # @params string [String]
160
+ # @return [String]
116
161
  def rainbowify(string)
117
162
  c = colors[@color_index % colors.size]
118
163
  @color_index += 1
@@ -121,6 +166,7 @@ class NyanCatFormatter < RSpec::Core::Formatters::BaseTextFormatter
121
166
 
122
167
  # Calculates the colors of the rainbow
123
168
  #
169
+ # @return [Array]
124
170
  def colors
125
171
  @colors ||= (0...(6 * 7)).map do |n|
126
172
  pi_3 = Math::PI / 3
@@ -135,12 +181,13 @@ class NyanCatFormatter < RSpec::Core::Formatters::BaseTextFormatter
135
181
  # Determines how to color the example. If pass, it is rainbowified, otherwise
136
182
  # we assign red if failed or yellow if an error occurred.
137
183
  #
184
+ # @return [String]
138
185
  def highlight(mark = PASS)
139
186
  case mark
140
- when PASS; rainbowify mark
141
- when FAIL; red mark
142
- when ERROR; yellow mark
143
- else mark
187
+ when PASS; rainbowify PASS_ARY[@color_index%2]
188
+ when FAIL; red mark
189
+ when ERROR; yellow mark
190
+ else mark
144
191
  end
145
192
  end
146
193
 
@@ -2,7 +2,7 @@ $:.push File.expand_path("../lib", __FILE__)
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "nyan-cat-formatter"
5
- s.version = "0.0.1"
5
+ s.version = "0.0.2"
6
6
  s.authors = ["Matt Sears"]
7
7
  s.email = ["matt@mattsears.com"]
8
8
  s.homepage = ""
@@ -8,7 +8,7 @@ describe NyanCatFormatter do
8
8
  @formatter = NyanCatFormatter.new(@output)
9
9
  @formatter.start(2)
10
10
  @example = RSpec::Core::ExampleGroup.describe.example
11
- sleep(0.2) # Just to slow it down a little :-)
11
+ sleep(0.1) # Just to slow it down a little :-)
12
12
  end
13
13
 
14
14
  describe 'passed, pending and failed' do
@@ -26,7 +26,15 @@ describe NyanCatFormatter do
26
26
 
27
27
  it 'should relax Nyan Cat' do
28
28
  @formatter.example_passed(@example)
29
- @formatter.nyan_cat.should == '~|_(^.^)'
29
+ @formatter.nyan_cat.should == [ "_,------, ",
30
+ "_| /\\_/\\ ",
31
+ "~|_( ^ .^) ",
32
+ " \"\" \"\" "
33
+ ].join("\n")
34
+ end
35
+
36
+ it 'should update the scoreboard' do
37
+ @formatter.scoreboard.size.should == 4
30
38
  end
31
39
 
32
40
  end
@@ -45,7 +53,11 @@ describe NyanCatFormatter do
45
53
 
46
54
  it 'should alert Nyan Cat' do
47
55
  @formatter.example_pending(@example)
48
- @formatter.nyan_cat.should == '~|_(o.o)'
56
+ @formatter.nyan_cat.should == [ "_,------, ",
57
+ "_| /\\_/\\ ",
58
+ "~|_( o .o) ",
59
+ " \"\" \"\" "
60
+ ].join("\n")
49
61
  end
50
62
 
51
63
  end
@@ -64,7 +76,11 @@ describe NyanCatFormatter do
64
76
 
65
77
  it 'should alert Nyan Cat' do
66
78
  @formatter.example_failed(@example)
67
- @formatter.nyan_cat.should == '~|_(o.o)'
79
+ @formatter.nyan_cat.should == [ "_,------, ",
80
+ "_| /\\_/\\ ",
81
+ "~|_( o .o) ",
82
+ " \"\" \"\" "
83
+ ].join("\n")
68
84
  end
69
85
 
70
86
  end
@@ -78,14 +94,6 @@ describe NyanCatFormatter do
78
94
  @formatter.tick
79
95
  end
80
96
 
81
- it 'should change title' do
82
- @formatter.title.should == ' 1/2'
83
- end
84
-
85
- it 'should calculate the percentage done' do
86
- @formatter.percentage.should == 50
87
- end
88
-
89
97
  it 'should increment the current' do
90
98
  @formatter.current.should == 1
91
99
  end
@@ -107,7 +115,7 @@ describe NyanCatFormatter do
107
115
  describe 'highlight' do
108
116
 
109
117
  it 'should rainbowify passing examples' do
110
- @formatter.highlight('=').should == "\e[38;5;154m=\e[0m"
118
+ @formatter.highlight('=').should == "\e[38;5;154m-\e[0m"
111
119
  end
112
120
 
113
121
  it 'should mark failing examples as red' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nyan-cat-formatter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-11-15 00:00:00.000000000 Z
12
+ date: 2011-11-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &70257566354640 !ruby/object:Gem::Requirement
16
+ requirement: &70218576415240 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70257566354640
24
+ version_requirements: *70218576415240
25
25
  description: ! 'Nyan Cat inspired RSpec formatter! '
26
26
  email:
27
27
  - matt@mattsears.com