enumstats 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 1.9.3
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
- # Enumstats
1
+ # Enumerable Stats
2
2
 
3
- TODO: Write a gem description
3
+ Provides simple statistics for any Enumerable.
4
+
5
+ [![Build Status](https://secure.travis-ci.org/nerab/enumstats.png?branch=master)](http://travis-ci.org/nerab/enumstats)
4
6
 
5
7
  ## Installation
6
8
 
@@ -18,7 +20,7 @@ Or install it yourself as:
18
20
 
19
21
  ## Usage
20
22
 
21
- TODO: Write usage instructions here
23
+ See unit tests for usage.
22
24
 
23
25
  ## Contributing
24
26
 
data/Rakefile CHANGED
@@ -3,8 +3,8 @@ require "bundler/gem_tasks"
3
3
  require 'rake/testtask'
4
4
 
5
5
  Rake::TestTask.new do |test|
6
- test.libs << 'lib' << 'test' << 'test/unit'
7
- test.pattern = 'test/unit/test_*.rb'
6
+ test.libs << 'lib' << 'test' << 'test/unit' << 'test/acceptance'
7
+ test.pattern = 'test/*/test_*.rb'
8
8
  end
9
9
 
10
10
  task :default => :test
data/bin/stats ADDED
@@ -0,0 +1,76 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'enumstats'
5
+ require 'optparse'
6
+
7
+ output_available = %w[count mean variance standard_deviation sum min max]
8
+ output_selected = []
9
+
10
+ option_parser = OptionParser.new do |opts|
11
+ opts.banner = <<ABOUT
12
+ Calculates simple statistics like average, variance, and standard deviation.
13
+ Input is read from STDIN, separated by $IFS.
14
+
15
+ Author: Nicolas E. Rabenau <nerab@gmx.at>
16
+
17
+ Synopsis:
18
+
19
+ stats [options]
20
+
21
+ Example Usage:
22
+
23
+ # print all stats for the given array
24
+ echo '5.0 10.0 15.0' | stats
25
+
26
+ # print all stats except the mean
27
+ echo '5.0 10.0 15.0' | stats --no-mean
28
+
29
+ # print just the variance
30
+ echo '5.0 10.0 15.0' | stats --variance
31
+
32
+ # combine more than one stat
33
+ echo '5.0 10.0 15.0' | stats --min --variance
34
+
35
+ Options:
36
+ ABOUT
37
+
38
+ output_available.each do |stat|
39
+ opts.on("--[no-]#{stat}", "Do [only|not] print the #{stat}") do |selected|
40
+ if selected
41
+ output_selected << stat
42
+ else
43
+ output_available.delete(stat)
44
+ end
45
+ end
46
+ end
47
+ end
48
+
49
+ begin
50
+ option_parser.parse!
51
+ rescue
52
+ STDERR.puts "Error: #{$!.message}"
53
+ STDERR.puts
54
+ STDERR.puts option_parser.banner
55
+ exit 1
56
+ end
57
+
58
+ data = []
59
+
60
+ ARGF.read.split.each do |line|
61
+ data << line.to_f
62
+ end
63
+
64
+ case output_selected.size
65
+ when 0
66
+ output_available.each do |stat|
67
+ puts "#{stat}: #{data.send(stat)}"
68
+ end
69
+ when 1
70
+ puts data.send(output_selected.first)
71
+ else
72
+ output_selected.each do |stat|
73
+ puts "#{stat}: #{data.send(stat)}"
74
+ end
75
+ end
76
+
data/enumstats.gemspec CHANGED
@@ -6,7 +6,7 @@ Gem::Specification.new do |gem|
6
6
  gem.email = ["nerab@gmx.net"]
7
7
  gem.description = %q{Adds simple stats like average, variance and standard deviation to Enumerable}
8
8
  gem.summary = %q{Stats for Enumerables}
9
- gem.homepage = ""
9
+ gem.homepage = "https://github.com/nerab/enumstats"
10
10
 
11
11
  gem.files = `git ls-files`.split($\)
12
12
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
@@ -16,4 +16,6 @@ Gem::Specification.new do |gem|
16
16
  gem.version = Enumstats::VERSION
17
17
 
18
18
  gem.add_development_dependency 'rake'
19
+ gem.add_development_dependency 'open4'
20
+ gem.add_development_dependency 'pry'
19
21
  end
@@ -1,3 +1,3 @@
1
1
  module Enumstats
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/enumstats.rb CHANGED
@@ -1,2 +1,5 @@
1
1
  require "enumstats/version"
2
2
  require "enumstats/enumerable"
3
+
4
+ module Enumstats
5
+ end
@@ -0,0 +1,94 @@
1
+ require 'helper'
2
+ require 'open4'
3
+
4
+ #
5
+ # End-to-end application test
6
+ #
7
+ class TestEnumerableStatsApp < MiniTest::Unit::TestCase
8
+ include EnumStatsTest
9
+ APP_SCRIPT = 'ruby bin/stats'
10
+ ALL_STATS = %w[count mean variance standard_deviation sum min max]
11
+
12
+ def test_no_options
13
+ assert_app
14
+ end
15
+
16
+ def test_mean
17
+ assert_app_include('mean')
18
+ assert_app_exclude('mean')
19
+ end
20
+
21
+ def test_two
22
+ assert_app_include(['min', 'standard_deviation'])
23
+ assert_app_exclude(['max', 'variance', 'sum'])
24
+ end
25
+
26
+ private
27
+
28
+ def assert_app
29
+ status = Open4::popen4("cat #{fixture_name('example.data')} | #{APP_SCRIPT}") do |pid, stdin, stdout, stderr|
30
+ lines = stdout.read.lines
31
+ assert_equal(ALL_STATS.count, lines.count, "expect to see these stats printed: #{ALL_STATS}. Instead, got: #{lines.inspect}")
32
+
33
+ lines.each do |line|
34
+ assert_match(/^(.*): (.*)$/, line)
35
+ end
36
+
37
+ assert_empty(stderr.read)
38
+ end
39
+
40
+ assert_equal(0, status)
41
+ end
42
+
43
+ #
44
+ # Tests the app called to print nothing but those stats that are explicitely included
45
+ #
46
+ # e.g.
47
+ # # set --mean to only display mean
48
+ # assert_app_include('mean')
49
+ #
50
+ def assert_app_include(args)
51
+ args = Array(args)
52
+ argstr = args.collect{|a| "--#{a}"}.join(' ')
53
+ status = Open4::popen4("cat #{fixture_name('example.data')} | #{APP_SCRIPT} #{argstr}") do |pid, stdin, stdout, stderr|
54
+ lines = stdout.read.lines
55
+ assert_equal(args.count, lines.count, "expect to see only those printed: #{args}. Instead, got: #{lines.inspect}")
56
+
57
+ lines.each do |line|
58
+ if 1 == args.count
59
+ assert_match(/^\d*\.\d*$/, line)
60
+ else
61
+ assert_match(/^(.*): (.*)$/, line)
62
+ end
63
+ end
64
+
65
+ assert_empty(stderr.read)
66
+ end
67
+
68
+ assert_equal(0, status)
69
+ end
70
+
71
+ #
72
+ # Tests the app called to print all except those stats that are explicitely excluded
73
+ #
74
+ # e.g.
75
+ # # set --no-mean to display all except mean
76
+ # assert_app_exclude('mean')
77
+ #
78
+ def assert_app_exclude(args)
79
+ expected = ALL_STATS - Array(args)
80
+ argstr = Array(args).collect{|a| "--no-#{a}"}.join(' ')
81
+ status = Open4::popen4("cat #{fixture_name('example.data')} | #{APP_SCRIPT} #{argstr}") do |pid, stdin, stdout, stderr|
82
+ lines = stdout.read.lines
83
+ assert_equal(expected.count, lines.count, "expect to see only those printed: #{expected}. Instead, got: #{lines.inspect}")
84
+
85
+ lines.each do |line|
86
+ assert_match(/^(.*): (.*)$/, line)
87
+ end
88
+
89
+ assert_empty(stderr.read)
90
+ end
91
+
92
+ assert_equal(0, status)
93
+ end
94
+ end
@@ -0,0 +1,3 @@
1
+ value: 20 response: 10.2
2
+ value: 40 response: 19.3
3
+ value: 31 response: 15.4
@@ -0,0 +1,3 @@
1
+ 5
2
+ 10
3
+ 15
data/test/helper.rb CHANGED
@@ -1,2 +1,8 @@
1
1
  require 'minitest/autorun'
2
2
  require 'enumstats'
3
+
4
+ module EnumStatsTest
5
+ def fixture_name(name)
6
+ File.join(File.dirname(__FILE__), 'fixtures', name)
7
+ end
8
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: enumstats
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -27,26 +27,64 @@ dependencies:
27
27
  - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
29
  version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: open4
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: pry
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
30
62
  description: Adds simple stats like average, variance and standard deviation to Enumerable
31
63
  email:
32
64
  - nerab@gmx.net
33
- executables: []
65
+ executables:
66
+ - stats
34
67
  extensions: []
35
68
  extra_rdoc_files: []
36
69
  files:
37
70
  - .gitignore
71
+ - .travis.yml
38
72
  - Gemfile
39
73
  - LICENSE
40
74
  - README.md
41
75
  - Rakefile
76
+ - bin/stats
42
77
  - enumstats.gemspec
43
78
  - lib/enumstats.rb
44
79
  - lib/enumstats/enumerable.rb
45
80
  - lib/enumstats/version.rb
81
+ - test/acceptance/test_app.rb
82
+ - test/fixtures/example.2.data
83
+ - test/fixtures/example.data
46
84
  - test/helper.rb
47
85
  - test/unit/test_enumerable_stats.rb
48
86
  - test/unit/test_enumerable_stats_block.rb
49
- homepage: ''
87
+ homepage: https://github.com/nerab/enumstats
50
88
  licenses: []
51
89
  post_install_message:
52
90
  rdoc_options: []
@@ -71,6 +109,9 @@ signing_key:
71
109
  specification_version: 3
72
110
  summary: Stats for Enumerables
73
111
  test_files:
112
+ - test/acceptance/test_app.rb
113
+ - test/fixtures/example.2.data
114
+ - test/fixtures/example.data
74
115
  - test/helper.rb
75
116
  - test/unit/test_enumerable_stats.rb
76
117
  - test/unit/test_enumerable_stats_block.rb