Permalink
Browse files

Make output match that of holman/spark

  • Loading branch information...
1 parent 9da8aea commit 2589732333736124d170496541e248779fa69ab0 @rrrene committed Jan 21, 2014
Showing with 32 additions and 31 deletions.
  1. +1 −1 .ruby-gemset
  2. +2 −2 bin/sparkr
  3. +5 −10 lib/sparkr/sparkline.rb
  4. +0 −1 sparkr.gemspec
  5. +12 −0 test/cli_test.rb
  6. +8 −15 test/sparkr_test.rb
  7. +4 −2 test/test_helper.rb
View
@@ -1 +1 @@
-Sparkr
+sparkr
View
@@ -4,8 +4,8 @@ require 'bundler'
begin
Bundler.require
rescue Bundler::GemfileNotFound
- require 'Sparkr'
+ require 'sparkr'
end
-require 'Sparkr'
+require 'sparkr'
Sparkr::CLI.run(*ARGV)
View
@@ -1,20 +1,15 @@
-require 'pry'
-
module Sparkr
class Sparkline
TICKS = %w(▁ ▂ ▃ ▄ ▅ ▆ ▇ █)
- def initialize(numbers)
- @step_count = TICKS.size
- @step_height = (numbers.max - numbers.min) / @step_count + 1
+ def initialize(_numbers)
+ numbers = _numbers.map(&:to_i)
+ step_height = (numbers.max - numbers.min) / (TICKS.size - 1).to_f
- indexes = numbers.map do |n|
- ((n - numbers.min) / @step_height).to_i
- end
- @ticks = indexes.map do |index|
+ @ticks = numbers.map do |n|
+ index = ((n - numbers.min) / step_height).to_i
TICKS[index]
end
- binding.pry
end
def to_s
View
@@ -20,5 +20,4 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "bundler", "~> 1.5"
spec.add_development_dependency "rake"
- spec.add_development_dependency "pry"
end
View
@@ -0,0 +1,12 @@
+require File.expand_path(File.dirname(__FILE__) + '/test_helper')
+
+describe ::Sparkr::CLI do
+ it "should find work" do
+ out, err = capture_io do
+ ::Sparkr::CLI.run("1", "2", "3", "4", "100", "5", "10", "20", "50", "300")
+ end
+ refute out.empty?, "there should be some output"
+ assert err.empty?, "there should be no errors"
+ assert_equal "▁▁▁▁▃▁▁▁▂█\n", out
+ end
+end
View
@@ -2,20 +2,13 @@
describe ::Sparkr do
it "should find work" do
- puts Sparkr.sparkline([1,5,22,13,5])
- puts Sparkr.sparkline([0,30,55,80,33,150])
- puts Sparkr.sparkline([5.5,20])
- puts Sparkr.sparkline([1,2,3,4,100,5,10,20,50,300])
- puts Sparkr.sparkline([1,50,100])
- puts Sparkr.sparkline([2,4,8])
- puts Sparkr.sparkline([1,2,3,4,5])
-
- assert_equal '▁▂█▅▂', Sparkr.sparkline([1,5,22,13,5])
- assert_equal '▁▂▃▅▂▇', Sparkr.sparkline([0,30,55,80,33,150])
- assert_equal '▁█', Sparkr.sparkline([5.5,20])
- assert_equal '▁▁▁▁▃▁▁▁▂█', Sparkr.sparkline([1,2,3,4,100,5,10,20,50,300])
- assert_equal '▁▄█', Sparkr.sparkline([1,50,100])
- assert_equal '▁▃█', Sparkr.sparkline([2,4,8])
- assert_equal '▁▂▄▆█', Sparkr.sparkline([1,2,3,4,5])
+ assert_sparkline '▁▂▃▄▅▆▇█', Sparkr.sparkline([1,2,3,4,5,6,7,8])
+ assert_sparkline '▁▂█▅▂', Sparkr.sparkline([1,5,22,13,5])
+ assert_sparkline '▁█', Sparkr.sparkline([5.5,20])
+ assert_sparkline '▁▁▁▁▃▁▁▁▂█', Sparkr.sparkline([1,2,3,4,100,5,10,20,50,300])
+ assert_sparkline '▁▄█', Sparkr.sparkline([1,50,100])
+ assert_sparkline '▁▃█', Sparkr.sparkline([2,4,8])
+ assert_sparkline '▁▂▃▄▂█', Sparkr.sparkline([0,30,55,80,33,150])
+ assert_sparkline '▁▂▄▆█', Sparkr.sparkline([1,2,3,4,5])
end
end
View
@@ -8,6 +8,8 @@
require 'bundler'
Bundler.require
-def fixture_path(name)
- File.join(File.dirname(__FILE__), "fixtures", name.to_s)
+def assert_sparkline(expected, actual)
+ assert actual.index(''), "there must be a minimum"
+ assert actual.index(''), "there must be a maximum"
+ assert_equal expected, actual
end

0 comments on commit 2589732

Please sign in to comment.