Skip to content

Commit

Permalink
Add some initial specs (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
jfairbank committed Jan 13, 2015
1 parent b038749 commit 6e9b746
Show file tree
Hide file tree
Showing 7 changed files with 380 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--color
--require spec_helper
33 changes: 33 additions & 0 deletions spec/chroma/define_palette_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
describe Chroma, '.define_palette' do
def add_palette
Chroma.define_palette :foo do
spin 60
spin 180
spin(60).brighten(20)
greyscale
end
end

def remove_palette
if Chroma::Harmonies.method_defined? :foo
Chroma::Harmonies.send(:remove_method, :foo)
end
end

after(:example) { remove_palette }

let(:red) { '#ff0000'.paint }

it 'adds the new palette method' do
expect(red.palette).to_not respond_to(:foo)
add_palette
expect(red.palette).to respond_to(:foo)
end

it 'generates the correct colors' do
add_palette

expect(red.palette.foo).
to generate_palette %w(#ff0000 #ffff00 #00ffff #ffff33 #808080)
end
end
55 changes: 55 additions & 0 deletions spec/chroma/paint_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
require 'spec_helper'

describe Chroma do
describe '.paint' do
context 'with named color' do
it 'creates a color' do
expect(Chroma.paint('red')).to be_a(Chroma::Color)
end
end

context 'with 6 character hexadecimal' do
it 'creates a color' do
expect(Chroma.paint('#ff0000')).to be_a(Chroma::Color)
expect(Chroma.paint('ff0000')).to be_a(Chroma::Color)
end
end

context 'with 3 character hexadecimal' do
it 'creates a color' do
expect(Chroma.paint('#f00')).to be_a(Chroma::Color)
expect(Chroma.paint('f00')).to be_a(Chroma::Color)
end
end

context 'with 8 character hexadecimal' do
let(:hex) { '#80ff0000' }

it 'creates a color' do
expect(Chroma.paint(hex)).to be_a(Chroma::Color)
expect(Chroma.paint(hex[1..-1])).to be_a(Chroma::Color)
end

it 'sets alpha' do
expect(Chroma.paint(hex).alpha).to be_within(0.1).of(0.5)
end
end

context 'with rgb' do
it 'creates a color' do
expect(Chroma.paint('rgb(255, 0, 0)')).to be_a(Chroma::Color)
expect(Chroma.paint('rgba(255, 0, 0, 0.5)')).to be_a(Chroma::Color)
end

it 'sets alpha' do
expect(Chroma.paint('rgba(255, 0, 0, 0.5)').alpha).to eq(0.5)
end
end

context 'with hsl' do
it 'creates a color' do
expect(Chroma.paint('hsl(120, 100%, 50%)')).to be_a(Chroma::Color)
end
end
end
end
9 changes: 9 additions & 0 deletions spec/color/modifiers_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'spec_helper'

describe Chroma::Color do
describe '#spin' do
it 'generates the correct color' do
expect('red'.paint.spin(60)).to eq('yellow'.paint)
end
end
end
152 changes: 152 additions & 0 deletions spec/color/serializers_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
describe Chroma::Color do
context 'with serializers' do
let(:green) { 'green'.paint }
let(:blue) { 'rgba(0, 0, 255, 0.5)'.paint }
let(:transparent) { 'rgba(0, 0, 0, 0)'.paint }

describe '#to_hsv' do
it 'returns the hsv string' do
expect(green.to_hsv).to eq('hsv(120, 100%, 50%)')
expect(blue.to_hsv).to eq('hsva(240, 100%, 100%, 0.5)')
end
end

describe '#to_hsl' do
it 'returns the hsl string' do
expect(green.to_hsl).to eq('hsl(120, 100%, 25%)')
expect(blue.to_hsl).to eq('hsla(240, 100%, 50%, 0.5)')
end
end

describe '#to_hex' do
context 'with allow_3 set to false' do
it 'returns the hex string' do
expect(green.to_hex).to eq('#008000')
expect(blue.to_hex).to eq('#0000ff')
end
end

context 'with allow_3 set to true' do
it 'returns the hex string' do
expect(green.to_hex(true)).to eq('#008000')
expect(blue.to_hex(true)).to eq('#00f')
end
end
end

describe '#to_hex8' do
it 'returns the hex8 string' do
expect(green.to_hex8).to eq('#ff008000')
expect(blue.to_hex8).to eq('#800000ff')
end
end

describe '#to_rgb' do
it 'returns the rgb string' do
expect(green.to_rgb).to eq('rgb(0, 128, 0)')
expect(blue.to_rgb).to eq('rgba(0, 0, 255, 0.5)')
end
end

describe '#to_name' do
context 'with hex_for_unknown set to false' do
context 'with known named color' do
context 'when alpha = 1' do
it 'returns the named color' do
expect(green.to_name).to eq('green')
end
end

context 'when alpha < 1' do
it 'returns "<unknown>"' do
expect(blue.to_name).to eq('<unknown>')
end
end
end

context 'when alpha = 0' do
it 'returns "transparent"' do
expect(transparent.to_name).to eq('transparent')
end
end

context 'with unknown named color' do
it 'returns "<unknown>"' do
expect('#123'.paint.to_name).to eq('<unknown>')
end
end
end

context 'with hex_for_unknown set to true' do
context 'with known named color' do
context 'when alpha = 1' do
it 'returns the named color' do
expect(green.to_name(true)).to eq('green')
end
end

context 'when alpha < 1' do
it 'returns the hex string' do
expect(blue.to_name(true)).to eq('#0000ff')
end
end
end

context 'when alpha = 0' do
it 'returns "transparent"' do
expect(transparent.to_name(true)).to eq('transparent')
end
end

context 'with unknown named color' do
it 'returns returns the hex string' do
expect('#123'.paint.to_name(true)).to eq('#112233')
end
end
end
end

describe '#to_s' do
it 'returns the appropriate string according to format' do
expect('#ff0000'.paint.to_s).to eq('#ff0000')
expect('#f00'.paint.to_s).to eq('#f00')
expect('#80ff0000'.paint.to_s).to eq('#80ff0000')
expect('hsl(120, 100%, 50%)'.paint.to_s).to eq('hsl(120, 100%, 50%)')
expect('hsla(120, 100%, 50%, 0.5)'.paint.to_s).to eq('hsla(120, 100%, 50%, 0.5)')
expect('hsv(120, 100%, 50%)'.paint.to_s).to eq('hsv(120, 100%, 50%)')
expect('hsva(120, 100%, 50%, 0.5)'.paint.to_s).to eq('hsva(120, 100%, 50%, 0.5)')
expect('red'.paint.to_s).to eq('red')
end
end

describe '#hsv' do
it 'returns an hsv instance' do
hsv = green.hsv

expect(hsv).to be_a(Chroma::ColorModes::Hsv)
expect(hsv.h).to be_within(0.01).of(120)
expect(hsv.s).to be_within(0.01).of(1)
expect(hsv.v).to be_within(0.01).of(0.5)
expect(hsv.a).to eq(1)
end
end

describe '#hsl' do
it 'returns an hsl instance' do
hsl = green.hsl

expect(hsl).to be_a(Chroma::ColorModes::Hsl)
expect(hsl.h).to be_within(0.01).of(120)
expect(hsl.s).to be_within(0.01).of(1)
expect(hsl.l).to be_within(0.01).of(0.25)
expect(hsl.a).to eq(1)
end
end

describe '#rgb' do
it 'returns the underlying @rgb iv' do
expect(green.rgb).to equal(green.instance_variable_get(:@rgb))
end
end
end
end
30 changes: 30 additions & 0 deletions spec/color_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require 'spec_helper'

describe Chroma::Color do
let(:red) { 'red'.paint }
let(:other_red) { '#f00'.paint }
let(:blue) { 'blue'.paint }

context 'with equality' do
it 'equals itself' do
expect(red).to eql(red)
expect(red).to eq(red)
end

it 'equals another instance of the same color' do
expect(red).to eql(other_red)
expect(red).to eq(other_red)
end

it 'does not equal another instance of a different color' do
expect(red).to_not eql(blue)
expect(red).to_not eq(blue)
end
end

describe '#paint' do
it 'returns itself' do
expect(red.paint).to equal(red)
end
end
end
99 changes: 99 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
require 'chroma'

# This file was generated by the `rspec --init` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
# The generated `.rspec` file contains `--require spec_helper` which will cause this
# file to always be loaded, without a need to explicitly require it in any files.
#
# Given that it is always loaded, you are encouraged to keep this file as
# light-weight as possible. Requiring heavyweight dependencies from this file
# will add to the boot time of your test suite on EVERY test run, even for an
# individual file that may not need all of that loaded. Instead, consider making
# a separate helper file that requires the additional dependencies and performs
# the additional setup, and require it from the spec files that actually need it.
#
# The `.rspec` file also contains a few flags that are not defaults but that
# users commonly want.
#
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
RSpec.configure do |config|
# rspec-expectations config goes here. You can use an alternate
# assertion/expectation library such as wrong or the stdlib/minitest
# assertions if you prefer.
config.expect_with :rspec do |expectations|
# This option will default to `true` in RSpec 4. It makes the `description`
# and `failure_message` of custom matchers include text for helper methods
# defined using `chain`, e.g.:
# be_bigger_than(2).and_smaller_than(4).description
# # => "be bigger than 2 and smaller than 4"
# ...rather than:
# # => "be bigger than 2"
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end

# rspec-mocks config goes here. You can use an alternate test double
# library (such as bogus or mocha) by changing the `mock_with` option here.
config.mock_with :rspec do |mocks|
# Prevents you from mocking or stubbing a method that does not exist on
# a real object. This is generally recommended, and will default to
# `true` in RSpec 4.
mocks.verify_partial_doubles = true
end

# The settings below are suggested to provide a good initial experience
# with RSpec, but feel free to customize to your heart's content.
=begin
# These two settings work together to allow you to limit a spec run
# to individual examples or groups you care about by tagging them with
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
# get run.
config.filter_run :focus
config.run_all_when_everything_filtered = true
# Limits the available syntax to the non-monkey patched syntax that is recommended.
# For more details, see:
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
# - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
config.disable_monkey_patching!
# This setting enables warnings. It's recommended, but in some cases may
# be too noisy due to issues in dependencies.
config.warnings = true
# Many RSpec users commonly either run the entire suite or an individual
# file, and it's useful to allow more verbose output when running an
# individual spec file.
if config.files_to_run.one?
# Use the documentation formatter for detailed output,
# unless a formatter has already been configured
# (e.g. via a command-line flag).
config.default_formatter = 'doc'
end
# Print the 10 slowest examples and example groups at the
# end of the spec run, to help surface which specs are running
# particularly slow.
config.profile_examples = 10
# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = :random
# Seed global randomization in this process using the `--seed` CLI option.
# Setting this allows you to use `--seed` to deterministically reproduce
# test failures related to randomization by passing the same `--seed` value
# as the one that triggered the failure.
Kernel.srand config.seed
=end
end

RSpec::Matchers.define :generate_palette do |expected|
expected.map!(&:paint)

match do |actual|
actual == expected
end
end

0 comments on commit 6e9b746

Please sign in to comment.