Commit 503c44ee authored by Grzegorz Bizon's avatar Grzegorz Bizon

Add badge template class to use with SVG ERB template

parent 0c4fa861
module Gitlab
module Badge
class Build
##
# Abstract class for build badge template.
#
# Template object will be passed to badge.svg.erb template.
#
class Template
STATUS_COLOR = {
success: '#4c1',
failed: '#e05d44',
running: '#dfb317',
pending: '#dfb317',
canceled: '#9f9f9f',
skipped: '#9f9f9f',
unknown: '#9f9f9f'
}
def initialize(status)
@status = status
end
def key_text
'build'
end
def value_text
@status
end
def key_width
38
end
def value_width
54
end
def key_color
'#555'
end
def value_color
STATUS_COLOR[@status.to_sym] ||
STATUS_COLOR[:unknown]
end
def key_text_anchor
key_width / 2
end
def value_text_anchor
key_width + (value_width / 2)
end
def width
key_width + value_width
end
end
end
end
end
require 'spec_helper'
describe Gitlab::Badge::Build::Template do
let(:status) { 'success' }
let(:template) { described_class.new(status) }
describe '#key_text' do
it 'is always says build' do
expect(template.key_text).to eq 'build'
end
end
describe '#value_text' do
it 'is status value' do
expect(template.value_text).to eq 'success'
end
end
describe 'widths and text anchors' do
it 'has fixed width and text anchors' do
expect(template.width).to eq 92
expect(template.key_width).to eq 38
expect(template.value_width).to eq 54
expect(template.key_text_anchor).to eq 19
expect(template.value_text_anchor).to eq 65
end
end
describe '#key_color' do
it 'is always the same' do
expect(template.key_color).to eq '#555'
end
end
describe '#value_color' do
context 'when status is success' do
let(:status) { 'success' }
it 'has expected color' do
expect(template.value_color).to eq '#4c1'
end
end
context 'when status is failed' do
let(:status) { 'failed' }
it 'has expected color' do
expect(template.value_color).to eq '#e05d44'
end
end
context 'when status is running' do
let(:status) { 'running' }
it 'has expected color' do
expect(template.value_color).to eq '#dfb317'
end
end
context 'when status is unknown' do
let(:status) { 'unknown' }
it 'has expected color' do
expect(template.value_color).to eq '#9f9f9f'
end
end
context 'when status does not match any known statuses' do
let(:status) { 'invalid status' }
it 'has expected color' do
expect(template.value_color).to eq '#9f9f9f'
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment