loading
Generated 2020-03-13T07:18:12+00:00

All Files ( 100.0% covered at 8.26 hits/line )

6 files in total.
116 relevant lines, 116 lines covered and 0 lines missed. ( 100.0% )
File % covered Lines Relevant Lines Lines covered Lines missed Avg. Hits / Line
lib/rubycritic_small_badge.rb 100.00 % 19 10 10 0 1.80
lib/rubycritic_small_badge/configuration.rb 100.00 % 54 14 14 0 16.93
lib/rubycritic_small_badge/report.rb 100.00 % 66 37 37 0 12.05
spec/rubycritic_small_badge_report_spec.rb 100.00 % 37 21 21 0 1.14
spec/rubycritic_small_badge_spec.rb 100.00 % 26 13 13 0 1.00
spec/support/mocks.rb 100.00 % 46 21 21 0 10.48

lib/rubycritic_small_badge.rb

100.0% lines covered

10 relevant lines. 10 lines covered and 0 lines missed.
    
  1. # frozen_string_literal: true
  2. 1 require 'rubycritic_small_badge/configuration'
  3. # :nodoc:
  4. 1 module RubyCriticSmallBadge
  5. 1 @configuration = Configuration.new
  6. 1 def self.configure
  7. 2 yield config
  8. end
  9. 1 def self.config
  10. 8 @configuration
  11. end
  12. end
  13. 1 $LOAD_PATH.unshift(File.join(File.dirname(__FILE__)))
  14. 1 require 'rubycritic_small_badge/version'
  15. 1 require 'rubycritic_small_badge/report'

lib/rubycritic_small_badge/configuration.rb

100.0% lines covered

14 relevant lines. 14 lines covered and 0 lines missed.
    
  1. # frozen_string_literal: true
  2. 1 module RubyCriticSmallBadge
  3. # Class to keep all the valid documentations that are required to build the
  4. # badge
  5. 1 class Configuration
  6. # Set up config variables.
  7. # rubocop:disable Metrics/MethodLength
  8. 1 def self.options
  9. 5 {
  10. with_analysers: false,
  11. background: '#fff',
  12. title_prefix: 'rubycritic',
  13. title_background: '#555',
  14. title_font: 'Verdana,sans-serif',
  15. title_font_color: '#fff',
  16. score_background_bad: '#ff0000',
  17. score_background_unknown: '#cccc00',
  18. score_background_good: '#4dc71f',
  19. score_font: 'Verdana,sans-serif',
  20. score_font_color: '#fff',
  21. font: 'Verdana,sans-serif',
  22. font_size: 11,
  23. badge_height: 20,
  24. badge_width: 200,
  25. filename_prefix: 'rubycritic_badge',
  26. output_path: 'badges',
  27. rounded_border: true,
  28. rounded_edge_radius: 3,
  29. minimum_score: nil
  30. }
  31. end
  32. # rubocop:enable Metrics/MethodLength
  33. # set up class variables and getters/setters
  34. 1 options.each_key do |opt|
  35. 26 define_method(opt) { instance_variable_get "@#{opt}" }
  36. 41 define_method("#{opt}=") { |val| instance_variable_set("@#{opt}", val) }
  37. end
  38. 1 def initialize(**opts)
  39. RubyCriticSmallBadge::Configuration
  40. 21 .options.merge(opts).each { |opt, v| send(:"#{opt}=", v) }
  41. end
  42. 1 def to_hash
  43. 6 hash = {}
  44. 6 instance_variables.each do |var|
  45. 120 hash[var.to_s.delete('@').to_sym] = instance_variable_get(var)
  46. end
  47. 6 hash
  48. end
  49. end
  50. end

lib/rubycritic_small_badge/report.rb

100.0% lines covered

37 relevant lines. 37 lines covered and 0 lines missed.
    
  1. # frozen_string_literal: true
  2. 1 require 'repo_small_badge/image'
  3. 1 require 'rubycritic_small_badge/configuration'
  4. 1 module RubyCriticSmallBadge
  5. # Basic Badge Formater Class that creates the badges.
  6. 1 class Report
  7. 1 def initialize(analysed_modules)
  8. 3 @config = RubyCriticSmallBadge.config
  9. 3 @analysed_modules = analysed_modules
  10. end
  11. 1 def generate_report
  12. 3 mk_output_dir!
  13. 3 score = @analysed_modules.score
  14. 3 @image = RepoSmallBadge::Image.new(map_image_config(state(score)))
  15. 3 badge('score', 'score', score)
  16. 3 true
  17. end
  18. 1 private
  19. 1 def max_score
  20. 3 100.0
  21. end
  22. 1 def mk_output_dir!
  23. 3 FileUtils.mkdir_p(@config.output_path)
  24. end
  25. 1 def badge(name, title, score)
  26. 3 value_txt = score_text(score)
  27. 3 @image.config_merge(map_image_config(state(score)))
  28. 3 @image.badge(name, title, value_txt)
  29. end
  30. 1 def score_text(score)
  31. 3 "#{score}/#{max_score}"
  32. end
  33. 1 def state(value)
  34. 6 if @config.minimum_score&.positive?
  35. 4 if value >= @config.minimum_score
  36. 2 'good'
  37. else
  38. 2 'bad'
  39. end
  40. else
  41. 2 'unknown'
  42. end
  43. end
  44. 1 def map_image_config(state)
  45. 6 hash = {}
  46. 6 @config.to_hash.map do |key, value|
  47. 120 key = key
  48. .to_s.sub(/^score_background_#{state}/, 'value_background')
  49. .to_sym
  50. 120 key = key.to_s.sub(/^score_/, 'value_').to_sym
  51. 120 hash[key] = value
  52. end
  53. 6 hash
  54. end
  55. end
  56. end

spec/rubycritic_small_badge_report_spec.rb

100.0% lines covered

21 relevant lines. 21 lines covered and 0 lines missed.
    
  1. # frozen_string_literal: true
  2. 1 require 'spec_helper'
  3. 1 describe RubyCriticSmallBadge::Report do
  4. 1 include TestRubyCriticSmallBadge::Mocks
  5. 1 describe '#generate_report' do
  6. 1 context 'bad result' do
  7. 2 subject { described_class.new(mock_analysed_modules(50.0)) }
  8. 1 it do
  9. 1 allow(RubyCriticSmallBadge.config).to receive(:minimum_score)
  10. .and_return(90.0)
  11. 1 mock_repo_badge_image(score: '50.0/100.0', state: 'bad')
  12. 1 expect(subject.generate_report).to be_truthy
  13. end
  14. end
  15. 1 context 'good result' do
  16. 2 subject { described_class.new(mock_analysed_modules(100.0)) }
  17. 1 it do
  18. 1 allow(RubyCriticSmallBadge.config).to receive(:minimum_score)
  19. .and_return(90.0)
  20. 1 mock_repo_badge_image(score: '100.0/100.0')
  21. 1 expect(subject.generate_report).to be_truthy
  22. end
  23. end
  24. 1 context 'unknown result' do
  25. 2 subject { described_class.new(mock_analysed_modules(100.0)) }
  26. 1 it do
  27. 1 mock_repo_badge_image(score: '100.0/100.0', state: 'unknown')
  28. 1 expect(subject.generate_report).to be_truthy
  29. end
  30. end
  31. end
  32. end

spec/rubycritic_small_badge_spec.rb

100.0% lines covered

13 relevant lines. 13 lines covered and 0 lines missed.
    
  1. # frozen_string_literal: true
  2. 1 require 'spec_helper'
  3. 1 describe RubyCriticSmallBadge do
  4. 1 describe '#configure' do
  5. 1 context 'set config' do
  6. 1 it do
  7. 1 described_class.configure do |config|
  8. 1 config.score_font = 'TestFont'
  9. end
  10. 1 expect(described_class.config.score_font).to eq 'TestFont'
  11. end
  12. end
  13. 1 context 'wrong config' do
  14. 1 it do
  15. 1 expect do
  16. 1 described_class.configure do |config|
  17. 1 config.wrong_value = 'test12'
  18. end
  19. end.to raise_error(NoMethodError)
  20. end
  21. end
  22. end
  23. end

spec/support/mocks.rb

100.0% lines covered

21 relevant lines. 21 lines covered and 0 lines missed.
    
  1. # frozen_string_literal: true
  2. 1 module RubyCritic
  3. 1 class Rating
  4. end
  5. end
  6. 1 module TestRubyCriticSmallBadge
  7. 1 module Mocks
  8. # rubocop:disable Metrics/ParameterLists, Metrics/AbcSize
  9. 1 def mock_repo_badge_image(score: 100, name: 'score',
  10. title: 'score',
  11. state: 'good',
  12. config: {},
  13. mock: instance_double('Image'))
  14. 3 config = map_config_options(config, state)
  15. 3 allow(RepoSmallBadge::Image).to receive(:new)
  16. .with(config).and_return(mock)
  17. 3 allow(mock).to receive(:config_merge).with(config).and_return(config)
  18. 3 allow(mock).to receive(:badge).with(name, title, score)
  19. 3 mock
  20. end
  21. # rubocop:enable Metrics/ParameterLists, Metrics/AbcSize
  22. 1 def map_config_options(config_hash, state)
  23. 3 hash = {}
  24. RubyCriticSmallBadge::Configuration.options.merge(config_hash)
  25. 3 .map do |key, value|
  26. 60 key = key.to_s
  27. .sub(/^score_background_#{state}/, 'value_background')
  28. .to_sym
  29. 60 key = key.to_s.sub(/^score_/, 'value_').to_sym
  30. 60 hash[key] = value
  31. end
  32. 3 hash
  33. end
  34. 1 def mock_analysed_modules(score = 100.0)
  35. 3 analysed_mod_double = instance_double('RubyCritic::AnalysedModules')
  36. 3 allow(analysed_mod_double)
  37. .to receive('score')
  38. .and_return(score)
  39. 3 analysed_mod_double
  40. end
  41. end
  42. end