gamifying_formatter 0.0.2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- NjE1NTRlMDU2YjdiOTdjNjcyOGRlZmMzYTY2ZTg1MDkyMzgzMGM1ZA==
5
- data.tar.gz: !binary |-
6
- Njc0N2ZlZWY1OGNiMmVhYjYyMWQyOWU0OTMwZjVmOTY0NTU2ZjQ1MA==
7
- !binary "U0hBNTEy":
8
- metadata.gz: !binary |-
9
- YWRmZDY2N2NiZTY4OGRhNzAwMmZhZmRhMWUzNWJlZjc4ZTAwMDA2Y2Q4Mzli
10
- ZGVkMzI1YzA5ZWM3ZTMyNzc4YzZjNTU1NDVhY2FlNjkzYzkyMDkyOTNlNDRi
11
- MDM4ZTZhYjA0YzE2MzAyYTg2NThkNTBiOGM4NDhlYmYzNzNiOTU=
12
- data.tar.gz: !binary |-
13
- ZmJhZTZiYmY5YTVjZjJjMTBkNTlmZDI3MjVlZGY0NzYyY2NkODJhZTQ1MTJk
14
- MmI2NzE2MmQ5YzNhNmJlOTIxZTk3ZTQyY2EyYTc2OTc2MThlYzE1YzZiODNk
15
- MmI3MTdjNTc0YWFmODY3NmQwZjY1OWVkNzkxNzg0MzkyYmZhNTY=
2
+ SHA1:
3
+ metadata.gz: 308f6e820cd5c35d1f0e1cb880c9d9c9947d9a34
4
+ data.tar.gz: 9149b823cba62dea23420ab53a8b10a8114f9de9
5
+ SHA512:
6
+ metadata.gz: 8d73b00ff3ed98a0fe1ed512be8f5ea794ed19035157e0964de5ae89b6b85e2b6db074546adf09fa384062003ee0779b78a07e7e0acead924cdc579864188570
7
+ data.tar.gz: eda62592b01de6398116e154f84f8ba14b526c20025a20187cd8f36fccf6b31d116ac972af4a397f98e476f33905a4a55debc431064687e9e3bde25e2913468f
data/README.md CHANGED
@@ -10,6 +10,10 @@ Adding tests
10
10
 
11
11
  You receive the trophies based on the amount of points you get which start out small and get bigger.
12
12
 
13
+ Now works on Rspec 1/2/3.
14
+
15
+ Thanks goes to the good folks at [nyan-cat-formatter](https://github.com/mattsears/nyan-cat-formatter) for the inspiration to do this and blazing for the trail for creating formatters.
16
+
13
17
  ## Installation
14
18
 
15
19
  Add this line to your application's Gemfile:
@@ -34,6 +38,12 @@ To use the GamifyingFormatter all you need to do is add this option when you are
34
38
 
35
39
  --format GamifyingFormatter
36
40
 
41
+ ## Example
42
+
43
+ Run the example using:
44
+
45
+ rspec example/example.rb --require gamifying_formatter.rb --format GamifyingFormatter
46
+
37
47
  ## Contributing
38
48
 
39
49
  1. Fork it
data/achievement.png CHANGED
Binary file
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |gem|
6
6
  gem.name = "gamifying_formatter"
7
- gem.version = '0.0.2'
7
+ gem.version = '1.0.0'
8
8
  gem.authors = ["Chris Belsole"]
9
9
  gem.email = ["cbelsole@gmail.com"]
10
10
  gem.description = %q{The Gamifying Formatter}
@@ -15,5 +15,5 @@ Gem::Specification.new do |gem|
15
15
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
16
16
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
17
17
  gem.require_paths = ["lib"]
18
- gem.add_dependency 'rspec'
18
+ gem.add_dependency 'rspec', '= 3.0.0'
19
19
  end
@@ -0,0 +1,139 @@
1
+ require 'test_info'
2
+ require 'yaml'
3
+
4
+ module GamifyingFormatters
5
+ module Common
6
+
7
+ def load_test_info
8
+ if File.exists?('.past_results.yml')
9
+ @test_info = YAML::load(File.new('.past_results.yml', 'r'))
10
+ else
11
+ @test_info = TestInfo.new
12
+ end
13
+ end
14
+
15
+ def wrap_up(num_of_examples, num_of_failed_examples, duration)
16
+ @test_info.number_of_tests = num_of_examples
17
+ @test_info.number_of_failed_tests = num_of_failed_examples
18
+ @test_info.total_time = duration
19
+ File.open('.past_results.yml', 'w') { |file| file.puts @test_info.to_yaml }
20
+ end
21
+
22
+ def calculate_achevements(example_count, failure_count, duration)
23
+ achievements = []
24
+ achievements << number_of_tests_achievement(@test_info.number_of_tests, example_count)
25
+ achievements << number_of_fixed_tests_achievement(@test_info.number_of_failed_tests, failure_count)
26
+ achievements << decreased_test_time_achievement(@test_info.total_time, duration)
27
+
28
+ achievements.delete_if(&:nil?).each { |achievement| @test_info.addAchievement(achievement) }
29
+ end
30
+
31
+ def show_achievements
32
+ output.puts "\n!!!!!!!!Achievements!!!!!!!!"
33
+ output.puts get_trophie(@test_info.level)
34
+ output.puts '-' * (@test_info.achievements.uniq.join(" | ").size + 4)
35
+ output.puts "| #{@test_info.achievements.uniq.join(" | ")} |"
36
+ output.puts '-' * (@test_info.achievements.uniq.join(" | ").size + 4)
37
+ end
38
+
39
+ def show_xp_bar
40
+ case @test_info.level
41
+ when 1
42
+ goal_xp = 10
43
+ when 2
44
+ goal_xp = 25
45
+ when 3
46
+ goal_xp = 50
47
+ when 4
48
+ goal_xp = 100
49
+ else
50
+ goal_xp = 1000
51
+ end
52
+
53
+ least_common_multiple = goal_xp.lcm(25)
54
+ numerator = least_common_multiple / goal_xp
55
+ denominator = least_common_multiple / 25
56
+
57
+ xp_chars = '=' * ((@test_info.xp * numerator) / denominator).ceil
58
+ xp_filler = ' ' * (25 - xp_chars.size)
59
+ output.puts "Level #{@test_info.level}: #{@test_info.xp}/#{goal_xp} [#{xp_chars}#{xp_filler}]"
60
+ end
61
+
62
+ def number_of_tests_achievement(old_total_tests, new_total_tests)
63
+ goal = [100, 50, 25, 10, 5, 1].select { |target|
64
+ new_total_tests - old_total_tests >= target
65
+ }.max
66
+
67
+ goal ? "Added #{goal} test(s)!" : nil
68
+ end
69
+
70
+ def decreased_test_time_achievement(old_total_time, new_total_time)
71
+ goal = [100, 50, 25, 10, 5, 1, 0.5, 0.2].select { |target|
72
+ (old_total_time * 100 - new_total_time * 100).round / 100.0 >= target
73
+ }.max
74
+
75
+ goal ? "Reduced testing time by #{goal} second(s)!" : nil
76
+ end
77
+
78
+ def number_of_fixed_tests_achievement(old_failed_tests, new_failed_tests)
79
+ goal = [100, 50, 25, 10, 5, 1].select { |target|
80
+ old_failed_tests - new_failed_tests >= target
81
+ }.max
82
+
83
+ goal ? "Fixed #{goal} test(s)!" : nil
84
+ end
85
+
86
+ def get_trophie(level)
87
+ case level
88
+ when 1
89
+ <<-EOS
90
+ .__.
91
+ (| |)
92
+ ( )
93
+ _)(_
94
+ EOS
95
+ when 2
96
+ <<-EOS
97
+ {}
98
+ /__\\
99
+ /| |\\
100
+ (_| |_)
101
+ \\ /
102
+ )(
103
+ _|__|_
104
+ _|______|_
105
+ |__________|
106
+ EOS
107
+ when 3
108
+ <<-EOS
109
+ ___________
110
+ '._==_==_=_.'
111
+ .-\\: /-.
112
+ | (|:. |) |
113
+ '-|:. |-'
114
+ \\::. /
115
+ '::. .'
116
+ ) (
117
+ _.' '._
118
+ `"""""""`"
119
+ EOS
120
+ when 4
121
+ <<-EOS
122
+ .-=========-.
123
+ \\'-=======-'/
124
+ _| .=. |_
125
+ ((| {{1}} |))
126
+ \\| /|\\ |/
127
+ \\__ '`' __/
128
+ _`) (`_
129
+ _/_______\\_
130
+ /___________\\
131
+ EOS
132
+ else
133
+ ''
134
+ end
135
+ end
136
+
137
+ end
138
+ end
139
+
@@ -0,0 +1,24 @@
1
+ include GamifyingFormatters::Common
2
+
3
+ class RSpec1 < Spec::Runner::Formatter::BaseTextFormatter
4
+ def start(example_count)
5
+ super(example_count)
6
+ @test_info = load_test_info
7
+ end
8
+
9
+ def close
10
+ super
11
+ wrap_up(example_count, failure_count, duration)
12
+ end
13
+
14
+ def dump_summary(duration, example_count, failure_count, pending_count)
15
+ super(duration, example_count, failure_count, pending_count)
16
+
17
+ achievements = calculate_achevements(example_count, failure_count, duration)
18
+
19
+ show_achievements unless achievements.empty?
20
+
21
+ show_xp_bar
22
+ end
23
+ end
24
+
@@ -0,0 +1,26 @@
1
+ require 'rspec/core/formatters/base_text_formatter'
2
+
3
+ include GamifyingFormatters::Common
4
+
5
+ class RSpec2 < RSpec::Core::Formatters::BaseTextFormatter
6
+ def initialize(output)
7
+ super(output)
8
+ @test_info = load_test_info
9
+ end
10
+
11
+ def close
12
+ super
13
+ wrap_up(example_count, failure_count, duration)
14
+ end
15
+
16
+ def dump_summary(duration, example_count, failure_count, pending_count)
17
+ super(duration, example_count, failure_count, pending_count)
18
+
19
+ achievements = calculate_achevements(example_count, failure_count, duration)
20
+
21
+ show_achievements unless achievements.empty?
22
+
23
+ show_xp_bar
24
+ end
25
+ end
26
+
@@ -0,0 +1,41 @@
1
+ require 'rspec/core/formatters/base_text_formatter'
2
+
3
+ include GamifyingFormatters::Common
4
+
5
+ class RSpec3 < RSpec::Core::Formatters::BaseTextFormatter
6
+
7
+ RSpec::Core::Formatters.register self, :dump_summary
8
+
9
+ def initialize(output)
10
+ super(output)
11
+ @test_info = load_test_info
12
+ end
13
+
14
+ def wrap_up(num_of_examples, num_of_failed_examples, duration)
15
+ @test_info.number_of_tests = num_of_examples
16
+ @test_info.number_of_failed_tests = num_of_failed_examples
17
+ @test_info.total_time = duration
18
+ File.open('.past_results.yml', 'w') { |file| file.puts @test_info.to_yaml }
19
+ end
20
+
21
+ def dump_summary(summary_notification)
22
+ super(summary_notification)
23
+
24
+ achievements = calculate_achevements(
25
+ summary_notification.examples.size,
26
+ summary_notification.failed_examples.size,
27
+ summary_notification.duration
28
+ )
29
+
30
+ show_achievements unless achievements.empty?
31
+
32
+ show_xp_bar
33
+
34
+ wrap_up(
35
+ summary_notification.examples.size,
36
+ summary_notification.failed_examples.size,
37
+ summary_notification.duration
38
+ )
39
+ end
40
+ end
41
+
@@ -1,179 +1,24 @@
1
- require "rspec/core/formatters/base_text_formatter"
2
- require 'test_info'
3
- require 'yaml'
4
-
5
- class GamifyingFormatter < RSpec::Core::Formatters::BaseTextFormatter
6
- def initialize(output)
7
- super(output)
8
-
9
- if File.exists?('.past_results.yml')
10
- @test_info = YAML::load(File.new('.past_results.yml', 'r'))
11
- else
12
- @test_info = TestInfo.new
13
- @test_info.level = 1
14
- @test_info.xp = 0
15
- @test_info.number_of_tests = 0
16
- @test_info.number_of_failed_tests = 0
17
- @test_info.total_time = 0.0
18
- end
19
-
20
- @got_achievement = false
21
- end
22
-
23
- def close
24
- super
25
-
26
- @test_info.number_of_tests = example_count
27
- @test_info.total_time = duration
28
- @test_info.number_of_failed_tests = failure_count
29
-
30
- File.open('.past_results.yml', 'w') { |file| file.puts @test_info.to_yaml }
31
- end
32
-
33
- def dump_summary(duration, example_count, failure_count, pending_count)
34
-
35
- super(duration, example_count, failure_count, pending_count)
36
-
37
- calculate_achevements(example_count, failure_count, duration)
38
-
39
- output_achievements
40
-
41
- output_xp_bar
42
- end
43
-
44
- def calculate_achevements(example_count, failure_count, duration)
45
- number_of_tests_achievement(@test_info.number_of_tests, example_count)
46
-
47
- number_of_fixed_tests_achievement(@test_info.number_of_failed_tests, failure_count)
48
-
49
- decreased_test_time_achievement(@test_info.total_time, duration)
50
- end
51
-
52
- def output_achievements
53
- return unless @got_achievement
54
- output.puts "\n!!!!!!!!Achievements!!!!!!!!"
55
- output.puts get_trophie(@test_info.level)
56
- output.puts '-' * (@test_info.achievements.uniq.join(" | ").size + 4)
57
- output.puts "| #{@test_info.achievements.uniq.join(" | ")} |"
58
- output.puts '-' * (@test_info.achievements.uniq.join(" | ").size + 4)
59
- end
60
-
61
- def output_xp_bar
62
- case @test_info.level
63
- when 1
64
- goal_xp = 10
65
- when 2
66
- goal_xp = 25
67
- when 3
68
- goal_xp = 50
69
- when 4
70
- goal_xp = 100
71
- else
72
- goal_xp = 1000
73
- end
74
-
75
- least_common_multiple = goal_xp.lcm(25)
76
- numerator = least_common_multiple / goal_xp
77
- denominator = least_common_multiple / 25
78
-
79
- xp_chars = '=' * ((@test_info.xp * numerator) / denominator).ceil
80
- xp_filler = ' ' * (25 - xp_chars.size)
81
-
82
- output.puts "Level #{@test_info.level}: #{@test_info.xp}/#{goal_xp} [#{xp_chars}#{xp_filler}]"
83
- end
84
-
85
- def number_of_tests_achievement(old_total_tests, new_total_tests)
86
- [100, 50, 25, 10, 5, 1].each do |number|
87
- if new_total_tests - old_total_tests >= number
88
- @test_info.achievements.push("Added #{number} test(s)!")
89
- @test_info.xp += 1
90
- @got_achievement = true
91
- check_level_up(@test_info.xp)
92
- break
93
- end
94
- end
95
- end
96
-
97
- def decreased_test_time_achievement(old_total_time, new_total_time)
98
- [100, 50, 25, 10, 5, 1, 0.5, 0.2].each do |number|
99
- if (old_total_time * 100 - new_total_time * 100).round / 100.0 >= number
100
- @test_info.achievements.push("Reduced testing time by #{number} second(s)!")
101
- @test_info.xp += 1
102
- @got_achievement = true
103
- check_level_up(@test_info.xp)
104
- break
105
- end
106
- end
107
- end
108
-
109
- def number_of_fixed_tests_achievement(old_failed_tests, new_failed_tests)
110
- [100, 50, 25, 10, 5, 1].each do |number|
111
- if old_failed_tests - new_failed_tests >= number
112
- @test_info.achievements.push("Fixed #{number} test(s)!")
113
- @test_info.xp += 1
114
- @got_achievement = true
115
- check_level_up(@test_info.xp)
116
- break
117
- end
118
- end
119
- end
120
-
121
- def check_level_up(xp)
122
- if xp == 10 || xp == 25 || xp == 50 || xp == 100
123
- @test_info.level += 1
124
- @test_info.xp = 0
125
- end
126
- end
127
-
128
- def get_trophie(level)
129
- case level
130
- when 3
131
- <<-EOS
132
- ___________
133
- '._==_==_=_.'
134
- .-\\: /-.
135
- | (|:. |) |
136
- '-|:. |-'
137
- \\::. /
138
- '::. .'
139
- ) (
140
- _.' '._
141
- `"""""""`"
142
- EOS
143
- when 2
144
- <<-EOS
145
- {}
146
- /__\\
147
- /| |\\
148
- (_| |_)
149
- \\ /
150
- )(
151
- _|__|_
152
- _|______|_
153
- |__________|
154
- EOS
155
- when 4
156
- <<-EOS
157
- .-=========-.
158
- \\'-=======-'/
159
- _| .=. |_
160
- ((| {{1}} |))
161
- \\| /|\\ |/
162
- \\__ '`' __/
163
- _`) (`_
164
- _/_______\\_
165
- /___________\\
166
- EOS
167
- when 1
168
- <<-EOS
169
- .__.
170
- (| |)
171
- ( )
172
- _)(_
173
- EOS
174
- else
175
- ''
176
- end
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'gamifying_formatter/common'
4
+
5
+ rspec_bin = $0.split('/').last
6
+ if rspec_bin == 'spec'
7
+ [
8
+ 'spec',
9
+ 'spec/runner/formatter/base_text_formatter',
10
+ 'gamifying_formatter/rspec1'
11
+ ].each { |f| require f }
12
+ formatter = RSpec1
13
+ else
14
+ require 'rspec/core/formatters/base_text_formatter'
15
+ if Gem::Version.new(RSpec::Core::Version::STRING).release >= Gem::Version.new('3.0.0')
16
+ require 'gamifying_formatter/rspec3'
17
+ formatter = RSpec3
18
+ else
19
+ require 'gamifying_formatter/rspec2'
20
+ formatter = RSpec2
177
21
  end
178
22
  end
179
23
 
24
+ GamifyingFormatter = formatter
data/lib/test_info.rb CHANGED
@@ -1,15 +1,45 @@
1
1
  class TestInfo
2
- attr_accessor :number_of_tests, :total_time, :number_of_failed_tests, :xp, :level
2
+ attr_accessor :number_of_tests, :total_time, :number_of_failed_tests
3
+ attr_reader :xp, :level, :achievements
3
4
 
4
5
  def initialize
5
6
  @achievements = []
7
+ @level = 1
8
+ @xp = 0
9
+ @number_of_tests = 0
10
+ @number_of_failed_tests = 0
11
+ @total_time = 0.0
6
12
  end
7
13
 
8
- def achievements=(value)
9
- @achievements = value
14
+ def addAchievement(value)
15
+ unless value && value.empty?
16
+ incrimentXpAndLevel
17
+ @achievements << value
18
+ end
19
+
20
+ nil
21
+ end
22
+
23
+ def ==(o)
24
+ o.class == self.class &&
25
+ o.number_of_tests == number_of_tests &&
26
+ o.total_time == total_time &&
27
+ o.number_of_failed_tests == number_of_failed_tests &&
28
+ o.xp == xp &&
29
+ o.level == level &&
30
+ o.achievements == achievements
10
31
  end
11
32
 
12
- def achievements
13
- @achievements
33
+ private
34
+
35
+ def incrimentXpAndLevel
36
+ @xp += 1
37
+
38
+ if @xp == 10 || @xp == 25 || @xp == 50 || @xp == 100
39
+ @level += 1
40
+ @xp = 0
41
+ end
42
+
43
+ nil
14
44
  end
15
45
  end
@@ -0,0 +1,412 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'GamifyingFormatters::Common' do
4
+
5
+ let(:output) { StringIO.new }
6
+ let(:formatter) { TestFormatter.new(output) }
7
+
8
+ describe '#load_test_info' do
9
+ it 'loads yml file when present' do
10
+ allow(File).to receive(:exists?).and_return(true)
11
+ expect(File).to receive(:new).with('.past_results.yml', 'r').and_return('--- foo')
12
+ formatter.load_test_info
13
+ expect(formatter.instance_variable_get(:@test_info)).to eq('foo')
14
+ end
15
+
16
+ it 'creates new TestInfo when yml file is not present' do
17
+ allow(File).to receive(:exists?).and_return(false)
18
+ formatter.load_test_info
19
+ expect(formatter.instance_variable_get(:@test_info)).to eq(TestInfo.new)
20
+ end
21
+ end
22
+
23
+ describe '#wrap_up' do
24
+ let(:test_info) { formatter.instance_variable_get(:@test_info) }
25
+
26
+ it 'saves info to .past_results.yml' do
27
+ test_info.instance_variable_set(:@xp, 34)
28
+ test_info.instance_variable_set(:@level, 5)
29
+ test_info.instance_variable_set(:@achievements, ['something'])
30
+
31
+ allow(File).to receive(:open)
32
+
33
+ formatter.wrap_up(100, 2, 99)
34
+ end
35
+ end
36
+
37
+ describe '#show_achievements' do
38
+ let(:test_info) { formatter.instance_variable_get(:@test_info) }
39
+
40
+ it 'shows all achievements' do
41
+ test_info.instance_variable_set(:@achievements, ['one', 'two', 'three'])
42
+
43
+ expect(output).to receive(:puts)
44
+ .with("\n!!!!!!!!Achievements!!!!!!!!")
45
+ expect(output).to receive(:puts)
46
+ .with(" .__.\n (| |)\n ( )\n _)(_\n")
47
+ expect(output).to receive(:puts)
48
+ .with('---------------------')
49
+ expect(output).to receive(:puts)
50
+ .with('| one | two | three |')
51
+ expect(output).to receive(:puts)
52
+ .with('---------------------')
53
+ formatter.show_achievements
54
+ end
55
+ end
56
+
57
+ describe '#show_xp_bar' do
58
+ let(:test_info) { formatter.instance_variable_get(:@test_info) }
59
+
60
+ context 'when level is 1' do
61
+ it 'shows xp bar for empty xp' do
62
+ expect(output).to receive(:puts)
63
+ .with('Level 1: 0/10 [ ]')
64
+
65
+ formatter.show_xp_bar
66
+ end
67
+
68
+ it 'shows xp bar for almost full' do
69
+ expect(output).to receive(:puts)
70
+ .with('Level 1: 9/10 [====================== ]')
71
+ test_info.instance_variable_set(:@xp, 9)
72
+ formatter.show_xp_bar
73
+ end
74
+
75
+ it 'shows xp bar half full' do
76
+ expect(output).to receive(:puts)
77
+ .with('Level 1: 5/10 [============ ]')
78
+ test_info.instance_variable_set(:@xp, 5)
79
+ formatter.show_xp_bar
80
+ end
81
+ end
82
+
83
+ context 'when level is 2' do
84
+ before do
85
+ test_info.instance_variable_set(:@level, 2)
86
+ end
87
+
88
+ it 'shows xp bar for empty xp' do
89
+ expect(output).to receive(:puts)
90
+ .with('Level 2: 0/25 [ ]')
91
+
92
+ formatter.show_xp_bar
93
+ end
94
+
95
+ it 'shows xp bar for almost full' do
96
+ expect(output).to receive(:puts)
97
+ .with('Level 2: 24/25 [======================== ]')
98
+ test_info.instance_variable_set(:@xp, 24)
99
+ formatter.show_xp_bar
100
+ end
101
+
102
+ it 'shows xp bar half full' do
103
+ expect(output).to receive(:puts)
104
+ .with('Level 2: 13/25 [============= ]')
105
+ test_info.instance_variable_set(:@xp, 13)
106
+ formatter.show_xp_bar
107
+ end
108
+ end
109
+
110
+ context 'when level is 3' do
111
+ before do
112
+ test_info.instance_variable_set(:@level, 3)
113
+ end
114
+
115
+ it 'shows xp bar for empty xp' do
116
+ expect(output).to receive(:puts)
117
+ .with('Level 3: 0/50 [ ]')
118
+
119
+ formatter.show_xp_bar
120
+ end
121
+
122
+ it 'shows xp bar for almost full' do
123
+ expect(output).to receive(:puts)
124
+ .with('Level 3: 49/50 [======================== ]')
125
+ test_info.instance_variable_set(:@xp, 49)
126
+ formatter.show_xp_bar
127
+ end
128
+
129
+ it 'shows xp bar half full' do
130
+ expect(output).to receive(:puts)
131
+ .with('Level 3: 25/50 [============ ]')
132
+ test_info.instance_variable_set(:@xp, 25)
133
+ formatter.show_xp_bar
134
+ end
135
+ end
136
+
137
+ context 'when level is 4' do
138
+ before do
139
+ test_info.instance_variable_set(:@level, 4)
140
+ end
141
+
142
+ it 'shows xp bar for empty xp' do
143
+ expect(output).to receive(:puts)
144
+ .with('Level 4: 0/100 [ ]')
145
+
146
+ formatter.show_xp_bar
147
+ end
148
+
149
+ it 'shows xp bar for almost full' do
150
+ expect(output).to receive(:puts)
151
+ .with('Level 4: 99/100 [======================== ]')
152
+ test_info.instance_variable_set(:@xp, 99)
153
+ formatter.show_xp_bar
154
+ end
155
+
156
+ it 'shows xp bar half full' do
157
+ expect(output).to receive(:puts)
158
+ .with('Level 4: 50/100 [============ ]')
159
+ test_info.instance_variable_set(:@xp, 50)
160
+ formatter.show_xp_bar
161
+ end
162
+ end
163
+
164
+ context 'when level is above 4' do
165
+ before do
166
+ test_info.instance_variable_set(:@level, 5)
167
+ end
168
+
169
+ it 'shows xp bar for empty xp' do
170
+ expect(output).to receive(:puts)
171
+ .with('Level 5: 0/1000 [ ]')
172
+
173
+ formatter.show_xp_bar
174
+ end
175
+
176
+ it 'shows xp bar for almost full' do
177
+ expect(output).to receive(:puts)
178
+ .with('Level 5: 999/1000 [======================== ]')
179
+ test_info.instance_variable_set(:@xp, 999)
180
+ formatter.show_xp_bar
181
+ end
182
+
183
+ it 'shows xp bar half full' do
184
+ expect(output).to receive(:puts)
185
+ .with('Level 5: 500/1000 [============ ]')
186
+ test_info.instance_variable_set(:@xp, 500)
187
+ formatter.show_xp_bar
188
+ end
189
+ end
190
+ end
191
+
192
+ describe '#number_of_tests_achievement' do
193
+ it 'returns achievement for adding 1 test' do
194
+ expect(
195
+ formatter.number_of_tests_achievement(1, 2)
196
+ ).to eq("Added 1 test(s)!")
197
+ end
198
+
199
+ it 'returns achievement for adding 5 tests' do
200
+ expect(
201
+ formatter.number_of_tests_achievement(1, 6)
202
+ ).to eq("Added 5 test(s)!")
203
+ end
204
+
205
+ it 'returns achievement for adding 10 tests' do
206
+ expect(
207
+ formatter.number_of_tests_achievement(1, 11)
208
+ ).to eq("Added 10 test(s)!")
209
+ end
210
+
211
+ it 'returns achievement for adding 25 tests' do
212
+ expect(
213
+ formatter.number_of_tests_achievement(1, 26)
214
+ ).to eq("Added 25 test(s)!")
215
+ end
216
+
217
+ it 'returns achievement for adding 50 tests' do
218
+ expect(
219
+ formatter.number_of_tests_achievement(1, 51)
220
+ ).to eq("Added 50 test(s)!")
221
+ end
222
+
223
+ it 'returns achievement for adding 100 tests' do
224
+ expect(
225
+ formatter.number_of_tests_achievement(1, 101)
226
+ ).to eq("Added 100 test(s)!")
227
+ end
228
+
229
+ it 'returns nil if added tests are less than 1' do
230
+ expect(
231
+ formatter.number_of_tests_achievement(1, 0)
232
+ ).to eq(nil)
233
+ end
234
+ end
235
+
236
+ describe '#decreased_test_time_achievement' do
237
+ it 'returns achievement for .2 saved seconds' do
238
+ expect(
239
+ formatter.decreased_test_time_achievement(0.3, 0.1)
240
+ ).to eq("Reduced testing time by 0.2 second(s)!")
241
+ end
242
+
243
+ it 'returns achievement for .5 saved seconds' do
244
+ expect(
245
+ formatter.decreased_test_time_achievement(0.6, 0.1)
246
+ ).to eq("Reduced testing time by 0.5 second(s)!")
247
+ end
248
+
249
+ it 'returns achievement for 1 saved seconds' do
250
+ expect(
251
+ formatter.decreased_test_time_achievement(2, 1)
252
+ ).to eq("Reduced testing time by 1 second(s)!")
253
+ end
254
+
255
+ it 'returns achievement for 5 saved seconds' do
256
+ expect(
257
+ formatter.decreased_test_time_achievement(6, 1)
258
+ ).to eq("Reduced testing time by 5 second(s)!")
259
+ end
260
+
261
+ it 'returns achievement for 10 saved seconds' do
262
+ expect(
263
+ formatter.decreased_test_time_achievement(11, 1)
264
+ ).to eq("Reduced testing time by 10 second(s)!")
265
+ end
266
+
267
+ it 'returns achievement for 25 saved seconds' do
268
+ expect(
269
+ formatter.decreased_test_time_achievement(26, 1)
270
+ ).to eq("Reduced testing time by 25 second(s)!")
271
+ end
272
+
273
+ it 'returns achievement for 50 saved seconds' do
274
+ expect(
275
+ formatter.decreased_test_time_achievement(51, 1)
276
+ ).to eq("Reduced testing time by 50 second(s)!")
277
+ end
278
+
279
+ it 'returns achievement for 100 saved seconds' do
280
+ expect(
281
+ formatter.decreased_test_time_achievement(101, 1)
282
+ ).to eq("Reduced testing time by 100 second(s)!")
283
+ end
284
+
285
+ it 'returns nil if seconds saved are less than .2' do
286
+ expect(
287
+ formatter.decreased_test_time_achievement(0, 1)
288
+ ).to eq(nil)
289
+ end
290
+ end
291
+
292
+ describe '#number_of_fixed_tests_achievement' do
293
+ it 'returns achievement for 1 fixed test' do
294
+ expect(
295
+ formatter.number_of_fixed_tests_achievement(2, 1)
296
+ ).to eq("Fixed 1 test(s)!")
297
+ end
298
+
299
+ it 'returns achievement for 5 fixed test' do
300
+ expect(
301
+ formatter.number_of_fixed_tests_achievement(6, 1)
302
+ ).to eq("Fixed 5 test(s)!")
303
+ end
304
+
305
+ it 'returns achievement for 10 fixed test' do
306
+ expect(
307
+ formatter.number_of_fixed_tests_achievement(11, 1)
308
+ ).to eq("Fixed 10 test(s)!")
309
+ end
310
+
311
+ it 'returns achievement for 25 fixed test' do
312
+ expect(
313
+ formatter.number_of_fixed_tests_achievement(26, 1)
314
+ ).to eq("Fixed 25 test(s)!")
315
+ end
316
+
317
+ it 'returns achievement for 50 fixed test' do
318
+ expect(
319
+ formatter.number_of_fixed_tests_achievement(51, 1)
320
+ ).to eq("Fixed 50 test(s)!")
321
+ end
322
+
323
+ it 'returns achievement for 100 fixed test' do
324
+ expect(
325
+ formatter.number_of_fixed_tests_achievement(101, 1)
326
+ ).to eq("Fixed 100 test(s)!")
327
+ end
328
+
329
+ it 'returns nil if fixed tests are less than 1' do
330
+ expect(
331
+ formatter.number_of_fixed_tests_achievement(0, 1)
332
+ ).to eq(nil)
333
+ end
334
+ end
335
+
336
+ describe '#get_trophie' do
337
+ it 'returns level 1 trophy when level equals 1' do
338
+ expect(formatter.get_trophie(1)).to eq(
339
+ <<-EOS
340
+ .__.
341
+ (| |)
342
+ ( )
343
+ _)(_
344
+ EOS
345
+ )
346
+ end
347
+
348
+ it 'returns level 2 trophy when level equals 2' do
349
+ expect(formatter.get_trophie(2)).to eq(
350
+ <<-EOS
351
+ {}
352
+ /__\\
353
+ /| |\\
354
+ (_| |_)
355
+ \\ /
356
+ )(
357
+ _|__|_
358
+ _|______|_
359
+ |__________|
360
+ EOS
361
+ )
362
+ end
363
+
364
+ it 'returns level 3 trophy when level equals 3' do
365
+ expect(formatter.get_trophie(3)).to eq(
366
+ <<-EOS
367
+ ___________
368
+ '._==_==_=_.'
369
+ .-\\: /-.
370
+ | (|:. |) |
371
+ '-|:. |-'
372
+ \\::. /
373
+ '::. .'
374
+ ) (
375
+ _.' '._
376
+ `"""""""`"
377
+ EOS
378
+ )
379
+ end
380
+
381
+ it 'returns level 4 trophy when level equals 4' do
382
+ expect(formatter.get_trophie(4)).to eq(
383
+ <<-EOS
384
+ .-=========-.
385
+ \\'-=======-'/
386
+ _| .=. |_
387
+ ((| {{1}} |))
388
+ \\| /|\\ |/
389
+ \\__ '`' __/
390
+ _`) (`_
391
+ _/_______\\_
392
+ /___________\\
393
+ EOS
394
+ )
395
+ end
396
+
397
+ it 'returns empty string when level is anything else' do
398
+ expect(formatter.get_trophie(10)).to eq('')
399
+ end
400
+ end
401
+ end
402
+
403
+ class TestFormatter
404
+ include GamifyingFormatters::Common
405
+
406
+ attr_reader :output
407
+
408
+ def initialize(output)
409
+ @test_info = TestInfo.new
410
+ @output = output
411
+ end
412
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,4 @@
1
1
  $LOAD_PATH.unshift(File.dirname(__FILE__))
2
2
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
3
  require 'gamifying_formatter'
4
+ require 'stringio'
@@ -0,0 +1,66 @@
1
+ require 'spec_helper'
2
+
3
+ describe TestInfo do
4
+ let(:test_info) { TestInfo.new }
5
+
6
+ describe '#addAchievement' do
7
+ context 'for a valid achievement' do
8
+ it 'adds an achievement to the list when the value is not blank' do
9
+ test_info.addAchievement('some achievement')
10
+
11
+ expect(test_info.achievements.size).to eq(1)
12
+ expect(test_info.achievements.at(0)).to eq('some achievement')
13
+ end
14
+
15
+ it 'increments the level and resets the xp at 10' do
16
+ test_info.instance_variable_set('@xp', 9)
17
+ test_info.addAchievement('some achievement')
18
+
19
+ expect(test_info.level).to eq(2)
20
+ expect(test_info.xp).to eq(0)
21
+ end
22
+
23
+ it 'increments the level and resets the xp at 25' do
24
+ test_info.instance_variable_set('@xp', 24)
25
+ test_info.addAchievement('some achievement')
26
+
27
+ expect(test_info.level).to eq(2)
28
+ expect(test_info.xp).to eq(0)
29
+ end
30
+
31
+ it 'increments the level and resets the xp at 50' do
32
+ test_info.instance_variable_set('@xp', 49)
33
+ test_info.addAchievement('some achievement')
34
+
35
+ expect(test_info.level).to eq(2)
36
+ expect(test_info.xp).to eq(0)
37
+ end
38
+
39
+ it 'increments the level and resets the xp at 100' do
40
+ test_info.instance_variable_set('@xp', 99)
41
+ test_info.addAchievement('some achievement')
42
+
43
+ expect(test_info.level).to eq(2)
44
+ expect(test_info.xp).to eq(0)
45
+ end
46
+
47
+ it 'increments the xp and not the level at any other number' do
48
+ test_info.addAchievement('some achievement')
49
+
50
+ expect(test_info.level).to eq(1)
51
+ expect(test_info.xp).to eq(1)
52
+ end
53
+ end
54
+
55
+ it 'does not add an achievement to the list when it is blank' do
56
+ test_info.addAchievement('')
57
+
58
+ expect(test_info.achievements.size).to eq(0)
59
+ end
60
+
61
+ it 'returns nil' do
62
+ expect(test_info.addAchievement('something')).to be_nil
63
+ end
64
+ end
65
+
66
+ end
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gamifying_formatter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Belsole
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-03 00:00:00.000000000 Z
11
+ date: 2014-08-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ! '>='
17
+ - - '='
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: 3.0.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ! '>='
24
+ - - '='
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
26
+ version: 3.0.0
27
27
  description: The Gamifying Formatter
28
28
  email:
29
29
  - cbelsole@gmail.com
@@ -31,7 +31,7 @@ executables: []
31
31
  extensions: []
32
32
  extra_rdoc_files: []
33
33
  files:
34
- - .gitignore
34
+ - ".gitignore"
35
35
  - Gemfile
36
36
  - LICENSE.txt
37
37
  - README.md
@@ -40,9 +40,14 @@ files:
40
40
  - example/example.rb
41
41
  - gamifying_formatter.gemspec
42
42
  - lib/gamifying_formatter.rb
43
+ - lib/gamifying_formatter/common.rb
44
+ - lib/gamifying_formatter/rspec1.rb
45
+ - lib/gamifying_formatter/rspec2.rb
46
+ - lib/gamifying_formatter/rspec3.rb
43
47
  - lib/test_info.rb
44
- - spec/gamifying_formatter_spec.rb
48
+ - spec/gamifying_formatter/gamifying_formatter_spec.rb
45
49
  - spec/spec_helper.rb
50
+ - spec/test_info_spec.rb
46
51
  homepage: ''
47
52
  licenses: []
48
53
  metadata: {}
@@ -52,21 +57,22 @@ require_paths:
52
57
  - lib
53
58
  required_ruby_version: !ruby/object:Gem::Requirement
54
59
  requirements:
55
- - - ! '>='
60
+ - - ">="
56
61
  - !ruby/object:Gem::Version
57
62
  version: '0'
58
63
  required_rubygems_version: !ruby/object:Gem::Requirement
59
64
  requirements:
60
- - - ! '>='
65
+ - - ">="
61
66
  - !ruby/object:Gem::Version
62
67
  version: '0'
63
68
  requirements: []
64
69
  rubyforge_project:
65
- rubygems_version: 2.0.3
70
+ rubygems_version: 2.4.1
66
71
  signing_key:
67
72
  specification_version: 4
68
73
  summary: An rspec formatter for making testing fun.
69
74
  test_files:
70
- - spec/gamifying_formatter_spec.rb
75
+ - spec/gamifying_formatter/gamifying_formatter_spec.rb
71
76
  - spec/spec_helper.rb
77
+ - spec/test_info_spec.rb
72
78
  has_rdoc:
@@ -1,93 +0,0 @@
1
- require 'spec_helper'
2
- require 'stringio'
3
-
4
- describe GamifyingFormatter do
5
- before do
6
- @output = StringIO.new
7
- @formatter = GamifyingFormatter.new(@output)
8
- @formatter.start(2)
9
- end
10
-
11
- context 'achievements' do
12
- describe '.number_of_tests_achievement' do
13
- it 'should set an achievment for adding 1 test' do
14
- @formatter.number_of_tests_achievement(3, 4)
15
- expect(@formatter.instance_variable_get(:@test_info).achievements.size).to eq(1)
16
- expect(@formatter.instance_variable_get(:@test_info).achievements[0]).to eq('Added 1 test(s)!')
17
- end
18
-
19
- it 'should set and achievement for adding 100 tests' do
20
- @formatter.number_of_tests_achievement(1, 1000)
21
- expect(@formatter.instance_variable_get(:@test_info).achievements.size).to eq(1)
22
- expect(@formatter.instance_variable_get(:@test_info).achievements[0]).to eq('Added 100 test(s)!')
23
- end
24
-
25
- it 'should not set an achievement for adding tests' do
26
- @formatter.number_of_tests_achievement(4, 3)
27
- expect(@formatter.instance_variable_get(:@test_info).achievements.size).to eq(0)
28
- end
29
- end
30
-
31
- describe '.decreased_test_time_achievement' do
32
- it 'should set an achievment for improving tests time by .2 seconds' do
33
- @formatter.decreased_test_time_achievement(0.3, 0.1)
34
- expect(@formatter.instance_variable_get(:@test_info).achievements.size).to eq(1)
35
- expect(@formatter.instance_variable_get(:@test_info).achievements[0]).to eq('Reduced testing time by 0.2 second(s)!')
36
- end
37
-
38
- it 'should set and achievement for improving tests time by 100 seconds' do
39
- @formatter.decreased_test_time_achievement(1000, 1)
40
- expect(@formatter.instance_variable_get(:@test_info).achievements.size).to eq(1)
41
- expect(@formatter.instance_variable_get(:@test_info).achievements[0]).to eq('Reduced testing time by 100 second(s)!')
42
- end
43
-
44
- it 'should not set an achievement for improving test time' do
45
- @formatter.decreased_test_time_achievement(3, 4)
46
- expect(@formatter.instance_variable_get(:@test_info).achievements.size).to eq(0)
47
- end
48
- end
49
-
50
- describe '.number_of_fixed_tests_achievement' do
51
- it 'should set an achievment for fixing 1 test' do
52
- @formatter.number_of_fixed_tests_achievement(4, 3)
53
- expect(@formatter.instance_variable_get(:@test_info).achievements.size).to eq(1)
54
- expect(@formatter.instance_variable_get(:@test_info).achievements[0]).to eq('Fixed 1 test(s)!')
55
- end
56
-
57
- it 'should set and achievement for fixing 100 tests' do
58
- @formatter.number_of_fixed_tests_achievement(10000, 1)
59
- expect(@formatter.instance_variable_get(:@test_info).achievements.size).to eq(1)
60
- expect(@formatter.instance_variable_get(:@test_info).achievements[0]).to eq('Fixed 100 test(s)!')
61
- end
62
-
63
- it 'should not set an achievement for fixing tests' do
64
- @formatter.number_of_fixed_tests_achievement(3, 4)
65
- expect(@formatter.instance_variable_get(:@test_info).achievements.size).to eq(0)
66
- end
67
- end
68
- end
69
-
70
- context 'output' do
71
- describe '.output_achievements' do
72
- it 'outputs all achievemnts in the list' do
73
- achievement_array = Array.new(5) { |i| "Achievement #{i}"}
74
- @formatter.instance_variable_get(:@test_info).achievements = achievement_array
75
-
76
- @output.should_receive(:puts).exactly(5).times
77
-
78
- @formatter.output_achievements
79
- end
80
- end
81
-
82
- describe '.get_trophies' do
83
- it 'returns the correct trophie based on the number' do
84
- expect(@formatter.get_trophie(1)).to eq([
85
- " .__.",
86
- " (| |)",
87
- " ( )",
88
- " _)(_"
89
- ].join("\n"))
90
- end
91
- end
92
- end
93
- end