exemplor 2010.2.0 → 3000.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. data/README.md +221 -0
  2. data/Rakefile +8 -4
  3. data/TODO +13 -6
  4. data/VERSION +1 -1
  5. data/examples.rb +62 -51
  6. data/examples/assertion_failure.rb +3 -5
  7. data/examples/assertion_success.rb +2 -3
  8. data/examples/assertion_success_and_failure.rb +8 -12
  9. data/examples/assertion_success_and_info.rb +8 -11
  10. data/examples/check_parsing.rb +8 -17
  11. data/examples/checking_nil.rb +6 -5
  12. data/examples/failure_halts_execution.rb +3 -5
  13. data/examples/foobar.rb +9 -0
  14. data/examples/helpers.rb +1 -1
  15. data/examples/{with_checks.rb → multi_show.rb} +3 -3
  16. data/examples/no_checks_non_string.rb +1 -1
  17. data/examples/{check_with_disambiguation.rb → show_with_disambiguation.rb} +2 -2
  18. data/examples/{dumping_classes.rb → showing_classes.rb} +1 -1
  19. data/examples/simple_show.rb +15 -0
  20. data/examples/ten_percent_failures.rb +10 -10
  21. data/examples/with_setup.rb +5 -7
  22. data/lib/checker.rb +30 -10
  23. data/lib/environment.rb +32 -23
  24. data/lib/examples.rb +2 -1
  25. data/lib/exemplor.rb +2 -1
  26. data/lib/result_printer.rb +40 -21
  27. data/vendor/orderedhash-0.0.6/gemspec.rb +37 -0
  28. data/vendor/orderedhash-0.0.6/install.rb +213 -0
  29. data/vendor/orderedhash-0.0.6/lib/orderedautohash.rb +25 -0
  30. data/vendor/orderedhash-0.0.6/lib/orderedhash.rb +200 -0
  31. data/vendor/term-ansicolor-1.0.5/CHANGES +28 -0
  32. data/vendor/term-ansicolor-1.0.5/COPYING +340 -0
  33. data/vendor/term-ansicolor-1.0.5/README +137 -0
  34. data/vendor/term-ansicolor-1.0.5/Rakefile +88 -0
  35. data/vendor/term-ansicolor-1.0.5/VERSION +1 -0
  36. data/vendor/term-ansicolor-1.0.5/bin/cdiff +19 -0
  37. data/vendor/term-ansicolor-1.0.5/bin/decolor +12 -0
  38. data/vendor/term-ansicolor-1.0.5/examples/example.rb +90 -0
  39. data/vendor/term-ansicolor-1.0.5/install.rb +15 -0
  40. data/vendor/term-ansicolor-1.0.5/lib/term/ansicolor.rb +102 -0
  41. data/vendor/term-ansicolor-1.0.5/lib/term/ansicolor/version.rb +10 -0
  42. data/vendor/term-ansicolor-1.0.5/tests/ansicolor_test.rb +66 -0
  43. metadata +34 -41
  44. data/README +0 -152
@@ -1,4 +1,13 @@
1
1
  module Exemplor
2
+
3
+ # todo: remove this dependency at some point
4
+ def self.load_ansicolor
5
+ @aniscolor_loaded ||= begin
6
+ $:.unshift Exemplor.path('/../vendor/term-ansicolor-1.0.5/lib')
7
+ require 'term/ansicolor'
8
+ end
9
+ end
10
+
2
11
  class ResultPrinter
3
12
 
4
13
  attr_reader :name,:status,:result,:stderr
@@ -25,34 +34,44 @@ module Exemplor
25
34
  end
26
35
 
27
36
  def fancy
28
- # •∙ are inverted in my terminal font (Incosolata) so I'm swapping them
29
- require 'term/ansicolor'
37
+ Exemplor.load_ansicolor
30
38
  case status
31
- when :info : blue format_info("• #{name}", result)
32
- when :infos
33
- formatted_result = result.map do |r|
34
- # TODO: successful ones should be green
35
- format_info("#{{'success' => '✓', 'info' => '•' }[r['status']]} #{r['name']}", r['result']).rstrip
36
- end.join("\n")
37
- blue("∙ #{name}\n#{formatted_result.indent}")
38
- when :success
39
- green("✓ #{name}")
40
- when :failure
41
- # sooo hacky
42
- failure = result.find { |r| r['status'] == 'failure' }
43
- out = failure.dup
44
- out.delete('status')
45
- out.delete('name')
46
- color(:red, "✗ #{name} - #{failure['name']}\n#{YAML.without_header(out).indent}")
39
+ when :info : blue format_info(name, result)
40
+ when :success : green icon(status) + ' ' + name
41
+ when :infos : blue icon(status) + ' ' + name + "\n" + fancy_result(result).indent
42
+ when :failure : red icon(status) + ' ' + name + "\n" + fancy_result(result).indent
47
43
  when :error
48
44
  class_and_message = "#{result['class']} - #{result['message']}"
49
45
  backtrace = result['backtrace'].join("\n")
50
- color(:red, "☠ #{name}\n#{class_and_message.indent}\n#{backtrace.indent}")
46
+ red icon(status) + ' ' + name + "\n" + class_and_message.indent + "\n" + backtrace.indent
47
+ end
48
+ end
49
+
50
+ def fancy_result(checks)
51
+ result.map do |r|
52
+ status, name, result = r['status'].to_sym, r['name'], r['result']
53
+ case status
54
+ when :success : green icon(status) + ' ' + name
55
+ when :failure : red icon(status) + ' ' + name
56
+ when :info : blue format_info(name, result)
57
+ end
58
+ end.join("\n")
59
+ end
60
+
61
+ def icon(status)
62
+ case status.to_sym
63
+ # in some font faces, the big dot is little and the little dot is big. sadness
64
+ when :info : '•'
65
+ when :infos : '∙'
66
+ when :failure : '✗'
67
+ when :success : '✓'
68
+ when :error : '☠' # skull and crossbone, aww yeah
51
69
  end
52
70
  end
53
71
 
54
- def blue(str) color(:blue,str) end
72
+ def blue(str) color(:blue,str) end
55
73
  def green(str) color(:green,str) end
74
+ def red(str) color(:red,str) end
56
75
 
57
76
  def color(color, str)
58
77
  [Term::ANSIColor.send(color), str, Term::ANSIColor.reset].join
@@ -60,7 +79,7 @@ module Exemplor
60
79
 
61
80
  # whatahack
62
81
  def format_info(str, result)
63
- YAML.without_header({'FANCY' => result}).sub('FANCY', str)
82
+ YAML.without_header({'FANCY' => result}).sub('FANCY', icon(:info) + ' ' + str).rstrip
64
83
  end
65
84
 
66
85
  end
@@ -0,0 +1,37 @@
1
+ lib, version = File::basename(File::dirname(File::expand_path(__FILE__))).split %r/-/, 2
2
+
3
+ require 'rubygems'
4
+
5
+ Gem::Specification::new do |spec|
6
+ $VERBOSE = nil
7
+
8
+ shiteless = lambda do |list|
9
+ list.delete_if do |file|
10
+ file =~ %r/\.svn/ or
11
+ file =~ %r/\.tmp/
12
+ end
13
+ end
14
+
15
+ spec.name = lib
16
+ spec.version = version
17
+ spec.platform = Gem::Platform::RUBY
18
+ spec.summary = lib
19
+
20
+ spec.files = shiteless[Dir::glob("**/**")]
21
+ spec.executables = shiteless[Dir::glob("bin/*")].map{|exe| File::basename exe}
22
+
23
+ spec.require_path = "lib"
24
+ #spec.autorequire = lib
25
+
26
+ spec.has_rdoc = File::exist? "doc"
27
+ spec.test_suite_file = "test/#{ lib }.rb" if File::directory? "test"
28
+ #spec.add_dependency 'lib', '>= version'
29
+
30
+ spec.extensions << "extconf.rb" if File::exists? "extconf.rb"
31
+
32
+ spec.author = "Ara T. Howard"
33
+ spec.email = "ara.t.howard@gmail.com"
34
+ spec.homepage = "http://codeforpeople.com/lib/ruby/#{ lib }/"
35
+
36
+ spec.rubyforge_project = 'codeforpeople'
37
+ end
@@ -0,0 +1,213 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rbconfig'
3
+ require 'find'
4
+ require 'ftools'
5
+ require 'tempfile'
6
+ include Config
7
+
8
+ LIBDIR = "lib"
9
+ LIBDIR_MODE = 0644
10
+
11
+ BINDIR = "bin"
12
+ BINDIR_MODE = 0755
13
+
14
+
15
+ $srcdir = CONFIG["srcdir"]
16
+ $version = CONFIG["MAJOR"]+"."+CONFIG["MINOR"]
17
+ $libdir = File.join(CONFIG["libdir"], "ruby", $version)
18
+ $archdir = File.join($libdir, CONFIG["arch"])
19
+ $site_libdir = $:.find {|x| x =~ /site_ruby$/}
20
+ $bindir = CONFIG["bindir"] || CONFIG['BINDIR']
21
+ $ruby_install_name = CONFIG['ruby_install_name'] || CONFIG['RUBY_INSTALL_NAME'] || 'ruby'
22
+ $ruby_ext = CONFIG['EXEEXT'] || ''
23
+ $ruby = File.join($bindir, ($ruby_install_name + $ruby_ext))
24
+
25
+ if !$site_libdir
26
+ $site_libdir = File.join($libdir, "site_ruby")
27
+ elsif $site_libdir !~ %r/#{Regexp.quote($version)}/
28
+ $site_libdir = File.join($site_libdir, $version)
29
+ end
30
+
31
+ def install_rb(srcdir=nil, destdir=nil, mode=nil, bin=nil)
32
+ #{{{
33
+ path = []
34
+ dir = []
35
+ Find.find(srcdir) do |f|
36
+ next unless FileTest.file?(f)
37
+ next if (f = f[srcdir.length+1..-1]) == nil
38
+ next if (/CVS$/ =~ File.dirname(f))
39
+ next if f =~ %r/\.lnk/
40
+ next if f =~ %r/\.svn/
41
+ next if f =~ %r/\.swp/
42
+ next if f =~ %r/\.svn/
43
+ path.push f
44
+ dir |= [File.dirname(f)]
45
+ end
46
+ for f in dir
47
+ next if f == "."
48
+ next if f == "CVS"
49
+ File::makedirs(File.join(destdir, f))
50
+ end
51
+ for f in path
52
+ next if (/\~$/ =~ f)
53
+ next if (/^\./ =~ File.basename(f))
54
+ unless bin
55
+ File::install(File.join(srcdir, f), File.join(destdir, f), mode, true)
56
+ else
57
+ from = File.join(srcdir, f)
58
+ to = File.join(destdir, f)
59
+ shebangify(from) do |sf|
60
+ $deferr.print from, " -> ", File::catname(from, to), "\n"
61
+ $deferr.printf "chmod %04o %s\n", mode, to
62
+ File::install(sf, to, mode, false)
63
+ end
64
+ end
65
+ end
66
+ #}}}
67
+ end
68
+ def shebangify f
69
+ #{{{
70
+ open(f) do |fd|
71
+ buf = fd.read 42
72
+ if buf =~ %r/^\s*#\s*!.*ruby/o
73
+ ftmp = Tempfile::new("#{ $$ }_#{ File::basename(f) }")
74
+ begin
75
+ fd.rewind
76
+ ftmp.puts "#!#{ $ruby }"
77
+ while((buf = fd.read(8192)))
78
+ ftmp.write buf
79
+ end
80
+ ftmp.close
81
+ yield ftmp.path
82
+ ensure
83
+ ftmp.close!
84
+ end
85
+ else
86
+ yield f
87
+ end
88
+ end
89
+ #}}}
90
+ end
91
+ def ARGV.switch
92
+ #{{{
93
+ return nil if self.empty?
94
+ arg = self.shift
95
+ return nil if arg == '--'
96
+ if arg =~ /^-(.)(.*)/
97
+ return arg if $1 == '-'
98
+ raise 'unknown switch "-"' if $2.index('-')
99
+ self.unshift "-#{$2}" if $2.size > 0
100
+ "-#{$1}"
101
+ else
102
+ self.unshift arg
103
+ nil
104
+ end
105
+ #}}}
106
+ end
107
+ def ARGV.req_arg
108
+ #{{{
109
+ self.shift || raise('missing argument')
110
+ #}}}
111
+ end
112
+ def linkify d, linked = []
113
+ #--{{{
114
+ if test ?d, d
115
+ versioned = Dir[ File::join(d, "*-[0-9].[0-9].[0-9].rb") ]
116
+ versioned.each do |v|
117
+ src, dst = v, v.gsub(%r/\-[\d\.]+\.rb$/, '.rb')
118
+ lnk = nil
119
+ begin
120
+ if test ?l, dst
121
+ lnk = "#{ dst }.lnk"
122
+ puts "#{ dst } -> #{ lnk }"
123
+ File::rename dst, lnk
124
+ end
125
+ unless test ?e, dst
126
+ puts "#{ src } -> #{ dst }"
127
+ File::copy src, dst
128
+ linked << dst
129
+ end
130
+ ensure
131
+ if lnk
132
+ at_exit do
133
+ puts "#{ lnk } -> #{ dst }"
134
+ File::rename lnk, dst
135
+ end
136
+ end
137
+ end
138
+ end
139
+ end
140
+ linked
141
+ #--}}}
142
+ end
143
+
144
+
145
+ #
146
+ # main program
147
+ #
148
+
149
+ libdir = $site_libdir
150
+ bindir = $bindir
151
+ no_linkify = false
152
+ linked = nil
153
+ help = false
154
+
155
+ usage = <<-usage
156
+ #{ File::basename $0 }
157
+ -d, --destdir <destdir>
158
+ -l, --libdir <libdir>
159
+ -b, --bindir <bindir>
160
+ -r, --ruby <ruby>
161
+ -n, --no_linkify
162
+ -s, --sudo
163
+ -h, --help
164
+ usage
165
+
166
+ begin
167
+ while switch = ARGV.switch
168
+ case switch
169
+ when '-d', '--destdir'
170
+ libdir = ARGV.req_arg
171
+ when '-l', '--libdir'
172
+ libdir = ARGV.req_arg
173
+ when '-b', '--bindir'
174
+ bindir = ARGV.req_arg
175
+ when '-r', '--ruby'
176
+ $ruby = ARGV.req_arg
177
+ when '-n', '--no_linkify'
178
+ no_linkify = true
179
+ when '-s', '--sudo'
180
+ sudo = 'sudo'
181
+ when '-h', '--help'
182
+ help = true
183
+ else
184
+ raise "unknown switch #{switch.dump}"
185
+ end
186
+ end
187
+ rescue
188
+ STDERR.puts $!.to_s
189
+ STDERR.puts usage
190
+ exit 1
191
+ end
192
+
193
+ if help
194
+ STDOUT.puts usage
195
+ exit
196
+ end
197
+
198
+ system "#{ sudo } #{ $ruby } pre-install.rb" if test(?s, 'pre-install.rb')
199
+
200
+ unless no_linkify
201
+ linked = linkify('lib') + linkify('bin')
202
+ end
203
+
204
+ system "#{ $ruby } extconf.rb && make && #{ sudo } make install" if test(?s, 'extconf.rb')
205
+
206
+ install_rb(LIBDIR, libdir, LIBDIR_MODE)
207
+ install_rb(BINDIR, bindir, BINDIR_MODE, bin=true)
208
+
209
+ if linked
210
+ linked.each{|path| File::rm_f path}
211
+ end
212
+
213
+ system "#{ sudo } #{ $ruby } post-install.rb" if test(?s, 'post-install.rb')
@@ -0,0 +1,25 @@
1
+ #
2
+ # auto vivifying ordered hash that dumps as yaml nicely
3
+ #
4
+ require 'orderedhash' unless defined? OrderedHash
5
+
6
+ class AutoOrderedHash < OrderedHash
7
+ def initialize(*args)
8
+ super(*args){|a,k| a[k] = __class__.new(*args)}
9
+ end
10
+ def class # for nice yaml
11
+ Hash
12
+ end
13
+ def __class__
14
+ AutoOrderedHash
15
+ end
16
+ end # class AutoOrderedHash
17
+
18
+ OrderedAutoHash = AutoOrderedHash
19
+
20
+ def OrderedAutoHash(*a, &b)
21
+ OrderedAutoHash.new(*a, &b)
22
+ end
23
+ def AutoOrderedHash(*a, &b)
24
+ AutoOrderedHash.new(*a, &b)
25
+ end
@@ -0,0 +1,200 @@
1
+
2
+ # AUTHOR
3
+ # jan molic /mig/at/1984/dot/cz/
4
+ #
5
+ # DESCRIPTION
6
+ # Hash with preserved order and some array-like extensions
7
+ # Public domain.
8
+ #
9
+ # THANKS
10
+ # Andrew Johnson for his suggestions and fixes of Hash[],
11
+ # merge, to_a, inspect and shift
12
+ class OrderedHash < ::Hash
13
+ attr_accessor :order
14
+
15
+ class << self
16
+ def [] *args
17
+ hsh = OrderedHash.new
18
+ if Hash === args[0]
19
+ hsh.replace args[0]
20
+ elsif (args.size % 2) != 0
21
+ raise ArgumentError, "odd number of elements for Hash"
22
+ else
23
+ 0.step(args.size - 1, 2) do |a|
24
+ b = a + 1
25
+ hsh[args[a]] = args[b]
26
+ end
27
+ end
28
+ hsh
29
+ end
30
+ end
31
+ def initialize(*a, &b)
32
+ super
33
+ @order = []
34
+ end
35
+ def store_only a,b
36
+ store a,b
37
+ end
38
+ alias orig_store store
39
+ def store a,b
40
+ @order.push a unless has_key? a
41
+ super a,b
42
+ end
43
+ alias []= store
44
+ def == hsh2
45
+ return false if @order != hsh2.order
46
+ super hsh2
47
+ end
48
+ def clear
49
+ @order = []
50
+ super
51
+ end
52
+ def delete key
53
+ @order.delete key
54
+ super
55
+ end
56
+ def each_key
57
+ @order.each { |k| yield k }
58
+ self
59
+ end
60
+ def each_value
61
+ @order.each { |k| yield self[k] }
62
+ self
63
+ end
64
+ def each
65
+ @order.each { |k| yield k,self[k] }
66
+ self
67
+ end
68
+ alias each_pair each
69
+ def delete_if
70
+ @order.clone.each { |k|
71
+ delete k if yield(k)
72
+ }
73
+ self
74
+ end
75
+ def values
76
+ ary = []
77
+ @order.each { |k| ary.push self[k] }
78
+ ary
79
+ end
80
+ def keys
81
+ @order
82
+ end
83
+ def first
84
+ {@order.first => self[@order.first]}
85
+ end
86
+ def last
87
+ {@order.last => self[@order.last]}
88
+ end
89
+ def invert
90
+ hsh2 = Hash.new
91
+ @order.each { |k| hsh2[self[k]] = k }
92
+ hsh2
93
+ end
94
+ def reject &block
95
+ self.dup.delete_if &block
96
+ end
97
+ def reject! &block
98
+ hsh2 = reject &block
99
+ self == hsh2 ? nil : hsh2
100
+ end
101
+ def replace hsh2
102
+ @order = hsh2.keys
103
+ super hsh2
104
+ end
105
+ def shift
106
+ key = @order.first
107
+ key ? [key,delete(key)] : super
108
+ end
109
+ def unshift k,v
110
+ unless self.include? k
111
+ @order.unshift k
112
+ orig_store(k,v)
113
+ true
114
+ else
115
+ false
116
+ end
117
+ end
118
+ def push k,v
119
+ unless self.include? k
120
+ @order.push k
121
+ orig_store(k,v)
122
+ true
123
+ else
124
+ false
125
+ end
126
+ end
127
+ def pop
128
+ key = @order.last
129
+ key ? [key,delete(key)] : nil
130
+ end
131
+ def to_a
132
+ ary = []
133
+ each { |k,v| ary << [k,v] }
134
+ ary
135
+ end
136
+ def to_s
137
+ self.to_a.to_s
138
+ end
139
+ def inspect
140
+ ary = []
141
+ each {|k,v| ary << k.inspect + "=>" + v.inspect}
142
+ '{' + ary.join(", ") + '}'
143
+ end
144
+ def update hsh2
145
+ hsh2.each { |k,v| self[k] = v }
146
+ self
147
+ end
148
+ alias :merge! update
149
+ def merge hsh2
150
+ self.dup update(hsh2)
151
+ end
152
+ def select
153
+ ary = []
154
+ each { |k,v| ary << [k,v] if yield k,v }
155
+ ary
156
+ end
157
+ def class
158
+ Hash
159
+ end
160
+ def __class__
161
+ OrderedHash
162
+ end
163
+
164
+ attr_accessor "to_yaml_style"
165
+ def yaml_inline= bool
166
+ if respond_to?("to_yaml_style")
167
+ self.to_yaml_style = :inline
168
+ else
169
+ unless defined? @__yaml_inline_meth
170
+ @__yaml_inline_meth =
171
+ lambda {|opts|
172
+ YAML::quick_emit(object_id, opts) {|emitter|
173
+ emitter << '{ ' << map{|kv| kv.join ': '}.join(', ') << ' }'
174
+ }
175
+ }
176
+ class << self
177
+ def to_yaml opts = {}
178
+ begin
179
+ @__yaml_inline ? @__yaml_inline_meth[ opts ] : super
180
+ rescue
181
+ @to_yaml_style = :inline
182
+ super
183
+ end
184
+ end
185
+ end
186
+ end
187
+ end
188
+ @__yaml_inline = bool
189
+ end
190
+ def yaml_inline!() self.yaml_inline = true end
191
+
192
+ def each_with_index
193
+ @order.each_with_index { |k, index| yield k, self[k], index }
194
+ self
195
+ end
196
+ end # class OrderedHash
197
+
198
+ def OrderedHash(*a, &b)
199
+ OrderedHash.new(*a, &b)
200
+ end