nyan-cat-formatter 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format NyanCatFormatter
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm 1.9.3@nyan-cat-formatter@nyan-cat-formatter --create
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in nyan-cat-formatter.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,45 @@
1
+ Nyan Cat RSpec Formatter
2
+ ========
3
+
4
+ ```
5
+ -_-_-_-_-_-_-_,------,
6
+ _-_-_-_-_-_-_-| /\_/\
7
+ -_-_-_-_-_-_-~|__( ^ .^)
8
+ _-_-_-_-_-_-_-"" ""
9
+ ```
10
+
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
+
13
+ The rainbow changes colors as it runs. See it in action [here](http://vimeo.com).
14
+
15
+ ```
16
+ rspec --format NyanCatFormatter
17
+ ```
18
+
19
+ Installing Nyan Cat
20
+ ----------
21
+
22
+ ```
23
+ $ gem install nyan-cat-formatter
24
+ ```
25
+
26
+ If you want to use Nyan Cat as your default formatter, simply put the options in your .rspec file:
27
+
28
+ --format NyanCatFormatter
29
+ --color
30
+
31
+ Contributing
32
+ ----------
33
+
34
+ Once you've made your great commits:
35
+
36
+ 1. Fork Nyan Cat
37
+ 2. Create a topic branch - git checkout -b my_branch
38
+ 3. Push to your branch - git push origin my_branch
39
+ 4. Create a Pull Request from your branch
40
+ 5. That's it!
41
+
42
+ Author
43
+ ----------
44
+ [Matt Sears](https://wwww.mattsears.com) :: @mattsears
45
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ task :default => :spec
4
+
5
+ require 'rspec/core/rake_task'
6
+ RSpec::Core::RakeTask.new
@@ -0,0 +1,148 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'rspec/core/formatters/base_text_formatter'
3
+
4
+ class NyanCatFormatter < RSpec::Core::Formatters::BaseTextFormatter
5
+
6
+ ESC = "\e["
7
+ NND = "#{ESC}0m"
8
+ PASS = '='
9
+ FAIL = '*'
10
+ ERROR = '!'
11
+ PENDING = '·'
12
+
13
+ attr_reader :title, :current, :example_results, :color_index
14
+
15
+ def start(example_count)
16
+ super(example_count)
17
+ @current, @color_index = 0,0
18
+ @bar_length = 70
19
+ @example_results = []
20
+ end
21
+
22
+ def example_passed(example)
23
+ super(example)
24
+ tick PASS
25
+ end
26
+
27
+ def example_pending(example)
28
+ super(example)
29
+ @pending_count =+1
30
+ tick PENDING
31
+ end
32
+
33
+ def example_failed(example)
34
+ super(example)
35
+ @failure_count =+1
36
+ tick FAIL
37
+ end
38
+
39
+ def start_dump
40
+ @current = @example_count
41
+ end
42
+
43
+ def dump_summary(duration, example_count, failure_count, pending_count)
44
+ dump_profile if profile_examples? && failure_count == 0
45
+ summary = "\nNyan Cat flew #{format_seconds(duration)} seconds".split(//).map { |c| rainbowify(c) }
46
+ output.puts summary.join
47
+ output.puts colorise_summary(summary_line(example_count, failure_count, pending_count))
48
+ dump_commands_to_rerun_failed_examples
49
+ end
50
+
51
+ def dump_failures
52
+ # noop
53
+ end
54
+
55
+ # Increments the example count and displays the current progress
56
+ #
57
+ # Returns nothing
58
+ def tick(mark = PASS)
59
+ @example_results << mark
60
+ @current = (@current > @example_count) ? @example_count : @current + 1
61
+ @title = " #{current}/#{example_count}"
62
+ dump_progress
63
+ end
64
+
65
+ # Creates a rainbow trail
66
+ #
67
+ # Returns the sprintf format of the Nyan cat
68
+ 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) )
72
+ end
73
+
74
+ # Calculates the percentage completed any given point
75
+ #
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
85
+ def nyan_cat
86
+ if @failure_count > 0 || @pending_count > 0
87
+ '~|_(o.o)'
88
+ elsif (@current == @example_count)
89
+ '~|_(-.-)'
90
+ else
91
+ '~|_(^.^)'
92
+ end
93
+ end
94
+
95
+ # Displays the current progress in all Nyan Cat glory
96
+ #
97
+ 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
112
+ end
113
+
114
+ # Colorizes the string with raindow colors of the rainbow
115
+ #
116
+ def rainbowify(string)
117
+ c = colors[@color_index % colors.size]
118
+ @color_index += 1
119
+ "#{ESC}38;5;#{c}m#{string}#{NND}"
120
+ end
121
+
122
+ # Calculates the colors of the rainbow
123
+ #
124
+ def colors
125
+ @colors ||= (0...(6 * 7)).map do |n|
126
+ pi_3 = Math::PI / 3
127
+ n *= 1.0 / 6
128
+ r = (3 * Math.sin(n ) + 3).to_i
129
+ g = (3 * Math.sin(n + 2 * pi_3) + 3).to_i
130
+ b = (3 * Math.sin(n + 4 * pi_3) + 3).to_i
131
+ 36 * r + 6 * g + b + 16
132
+ end
133
+ end
134
+
135
+ # Determines how to color the example. If pass, it is rainbowified, otherwise
136
+ # we assign red if failed or yellow if an error occurred.
137
+ #
138
+ def highlight(mark = PASS)
139
+ case mark
140
+ when PASS; rainbowify mark
141
+ when FAIL; red mark
142
+ when ERROR; yellow mark
143
+ else mark
144
+ end
145
+ end
146
+
147
+ end
148
+
@@ -0,0 +1,21 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "nyan-cat-formatter"
5
+ s.version = "0.0.1"
6
+ s.authors = ["Matt Sears"]
7
+ s.email = ["matt@mattsears.com"]
8
+ s.homepage = ""
9
+ s.summary = %q{Nyan Cat inspired RSpec formatter! }
10
+ s.description = %q{Nyan Cat inspired RSpec formatter! }
11
+
12
+ s.rubyforge_project = "nyan-cat-formatter"
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
+ s.require_paths = ["lib"]
18
+
19
+ # specify any dependencies here; for example:
20
+ s.add_development_dependency "rspec"
21
+ end
@@ -0,0 +1,135 @@
1
+ require 'spec_helper'
2
+ require 'stringio'
3
+
4
+ describe NyanCatFormatter do
5
+
6
+ before do
7
+ @output = StringIO.new
8
+ @formatter = NyanCatFormatter.new(@output)
9
+ @formatter.start(2)
10
+ @example = RSpec::Core::ExampleGroup.describe.example
11
+ sleep(0.2) # Just to slow it down a little :-)
12
+ end
13
+
14
+ describe 'passed, pending and failed' do
15
+
16
+ before do
17
+ @formatter.stub!(:tick)
18
+ end
19
+
20
+ describe 'example_passed' do
21
+
22
+ it 'should call the increment method' do
23
+ @formatter.should_receive :tick
24
+ @formatter.example_passed(@example)
25
+ end
26
+
27
+ it 'should relax Nyan Cat' do
28
+ @formatter.example_passed(@example)
29
+ @formatter.nyan_cat.should == '~|_(^.^)'
30
+ end
31
+
32
+ end
33
+
34
+ describe 'example_pending' do
35
+
36
+ it 'should call the tick method' do
37
+ @formatter.should_receive :tick
38
+ @formatter.example_pending(@example)
39
+ end
40
+
41
+ it 'should increment the pending count' do
42
+ lambda { @formatter.example_pending(@example)}.
43
+ should change(@formatter, :pending_count).by(1)
44
+ end
45
+
46
+ it 'should alert Nyan Cat' do
47
+ @formatter.example_pending(@example)
48
+ @formatter.nyan_cat.should == '~|_(o.o)'
49
+ end
50
+
51
+ end
52
+
53
+ describe 'example_failed' do
54
+
55
+ it 'should call the increment method' do
56
+ @formatter.should_receive :tick
57
+ @formatter.example_failed(@example)
58
+ end
59
+
60
+ it 'should increment the failure count' do
61
+ lambda { @formatter.example_failed(@example)}.
62
+ should change(@formatter, :failure_count).by(1)
63
+ end
64
+
65
+ it 'should alert Nyan Cat' do
66
+ @formatter.example_failed(@example)
67
+ @formatter.nyan_cat.should == '~|_(o.o)'
68
+ end
69
+
70
+ end
71
+ end
72
+
73
+ describe 'tick' do
74
+
75
+ before do
76
+ @formatter.stub!(:current).and_return(1)
77
+ @formatter.stub!(:example_count).and_return(2)
78
+ @formatter.tick
79
+ end
80
+
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
+ it 'should increment the current' do
90
+ @formatter.current.should == 1
91
+ end
92
+
93
+ it 'should store the marks in an array' do
94
+ @formatter.example_results.should include('=')
95
+ end
96
+
97
+ end
98
+
99
+ describe 'rainbowify' do
100
+
101
+ it 'should increment the color index count' do
102
+ lambda { @formatter.rainbowify('=') }.should change(@formatter, :color_index).by(1)
103
+ end
104
+
105
+ end
106
+
107
+ describe 'highlight' do
108
+
109
+ it 'should rainbowify passing examples' do
110
+ @formatter.highlight('=').should == "\e[38;5;154m=\e[0m"
111
+ end
112
+
113
+ it 'should mark failing examples as red' do
114
+ @formatter.highlight('*').should == "\e[31m*\e[0m"
115
+ end
116
+
117
+ it 'should mark pending examples as yellow' do
118
+ @formatter.highlight('!').should == "\e[33m!\e[0m"
119
+ end
120
+
121
+ end
122
+
123
+ describe 'start' do
124
+
125
+ it 'should set the total amount of specs' do
126
+ @formatter.example_count.should == 2
127
+ end
128
+
129
+ it 'should set the current to 0' do
130
+ @formatter.current.should == 0
131
+ end
132
+
133
+ end
134
+
135
+ end
@@ -0,0 +1,3 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'nyan_cat_formatter'
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nyan-cat-formatter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Matt Sears
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-11-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &70257566354640 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70257566354640
25
+ description: ! 'Nyan Cat inspired RSpec formatter! '
26
+ email:
27
+ - matt@mattsears.com
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - .gitignore
33
+ - .rspec
34
+ - .rvmrc
35
+ - Gemfile
36
+ - README.md
37
+ - Rakefile
38
+ - lib/nyan_cat_formatter.rb
39
+ - nyan-cat-formatter.gemspec
40
+ - spec/nyan_cat_formatter_spec.rb
41
+ - spec/spec_helper.rb
42
+ homepage: ''
43
+ licenses: []
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ! '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubyforge_project: nyan-cat-formatter
62
+ rubygems_version: 1.8.10
63
+ signing_key:
64
+ specification_version: 3
65
+ summary: Nyan Cat inspired RSpec formatter!
66
+ test_files:
67
+ - spec/nyan_cat_formatter_spec.rb
68
+ - spec/spec_helper.rb