spruz 0.2.12 → 0.2.13

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -14,6 +14,7 @@ GemHadar do
14
14
  test_dir 'tests'
15
15
  ignore '.*.sw[pon]', 'pkg', 'Gemfile.lock', '.rvmrc'
16
16
  readme 'README.rdoc'
17
+ development_dependency 'test-unit', '~>2.3'
17
18
 
18
19
  install_library do
19
20
  libdir = CONFIG["sitelibdir"]
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.12
1
+ 0.2.13
@@ -19,10 +19,11 @@ module Spruz
19
19
  require 'spruz/time_dummy'
20
20
  require 'spruz/to_proc'
21
21
  require 'spruz/uniq_by'
22
- require 'spruz/write'
23
22
  require 'spruz/secure_write'
23
+ require 'spruz/write'
24
24
  require 'spruz/deep_dup'
25
25
  require 'spruz/attempt'
26
26
  require 'spruz/hash_symbolize_keys_recursive'
27
27
  require 'spruz/range_plus'
28
+ require 'spruz/file_binary'
28
29
  end
@@ -0,0 +1,83 @@
1
+ require 'spruz/xt/hash_union'
2
+
3
+ module Spruz
4
+ module FileBinary
5
+ module Constants
6
+ SEEK_SET = ::File::SEEK_SET
7
+
8
+ ZERO = "\x00"
9
+ BINARY = "\x01-\x1f\x7f-\xff"
10
+
11
+ if defined?(::Encoding)
12
+ ZERO.force_encoding(Encoding::ASCII_8BIT)
13
+ BINARY.force_encoding(Encoding::ASCII_8BIT)
14
+ end
15
+ end
16
+
17
+ class << self
18
+ # Default options can be queried/set via this hash.
19
+ attr_accessor :default_options
20
+ end
21
+ self.default_options = {
22
+ :offset => 0,
23
+ :buffer_size => 2 ** 13,
24
+ :percentage_binary => 30.0,
25
+ :percentage_zeros => 0.0,
26
+ }
27
+
28
+ # Returns true if this file is considered to be binary, false if it is not
29
+ # considered to be binary, and nil if it was empty.
30
+ #
31
+ # A file is considered to be binary if the percentage of zeros exceeds
32
+ # <tt>options[:percentage_zeros]</tt> or the percentage of binary bytes
33
+ # (8-th bit is 1) exceeds <tt>options[:percentage_binary]</tt> in the
34
+ # buffer of size <tt>options[:buffer_size]</tt> that is checked (beginning
35
+ # from offset <tt>options[:offset]</tt>). If an option isn't given the one
36
+ # from FileBinary.default_options is used instead.
37
+ def binary?(options = {})
38
+ options |= FileBinary.default_options
39
+ old_pos = tell
40
+ seek options[:offset], Constants::SEEK_SET
41
+ data = read options[:buffer_size]
42
+ !data or data.empty? and return nil
43
+ data_size = data.size
44
+ data.count(Constants::ZERO).to_f / data_size >
45
+ options[:percentage_zeros] / 100.0 and return true
46
+ data.count(Constants::BINARY).to_f / data_size >
47
+ options[:percentage_binary] / 100.0
48
+ ensure
49
+ old_pos and seek old_pos, Constants::SEEK_SET
50
+ end
51
+
52
+ # Returns true if FileBinary#binary? returns false, false if
53
+ # FileBinary#binary? returns true, and nil otherwise. For an explanation of
54
+ # +options+, see FileBinary#binary?.
55
+ def ascii?(options = {})
56
+ case binary?(options)
57
+ when true then false
58
+ when false then true
59
+ end
60
+ end
61
+
62
+ def self.included(modul)
63
+ modul.instance_eval do
64
+ extend ClassMethods
65
+ end
66
+ super
67
+ end
68
+
69
+ module ClassMethods
70
+ # Returns true if the file with name +name+ is considered to be binary
71
+ # using the FileBinary#binary? method.
72
+ def binary?(name, options = {})
73
+ open(name, 'rb') { |f| f.binary?(options) }
74
+ end
75
+
76
+ # Returns true if the file with name +name+ is considered to be ascii
77
+ # using the FileBinary#ascii? method.
78
+ def ascii?(name, options = {})
79
+ open(name, 'rb') { |f| f.ascii?(options) }
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,121 @@
1
+ module Spruz
2
+ class LinesFile
3
+ module LineExtension
4
+ attr_reader :line_number
5
+
6
+ def filename
7
+ lines_file.filename.dup
8
+ end
9
+ end
10
+
11
+ def self.for_filename(filename, line_number = nil)
12
+ obj = new(File.readlines(filename), line_number)
13
+ obj.filename = filename
14
+ obj
15
+ end
16
+
17
+ def self.for_file(file, line_number = nil)
18
+ obj = new(file.readlines, line_number)
19
+ obj.filename = file.path
20
+ obj
21
+ end
22
+
23
+ def self.for_lines(lines, line_number = nil)
24
+ new(lines, line_number)
25
+ end
26
+
27
+ def initialize(lines, line_number = nil)
28
+ @lines = lines
29
+ @lines.each_with_index do |line, i|
30
+ line.extend LineExtension
31
+ line.instance_variable_set :@line_number, i + 1
32
+ line.instance_variable_set :@lines_file, self
33
+ end
34
+ instance_variable_set :@line_number, line_number || (@lines.empty? ? 0 : 1)
35
+ end
36
+
37
+ attr_accessor :filename
38
+
39
+ attr_reader :line_number
40
+
41
+ def rewind
42
+ self.line_number = 1
43
+ self
44
+ end
45
+
46
+ def next!
47
+ old = line_number
48
+ self.line_number += 1
49
+ line_number > old ? self : nil
50
+ end
51
+
52
+ def previous!
53
+ old = line_number
54
+ self.line_number -= 1
55
+ line_number < old ? self : nil
56
+ end
57
+
58
+ def line_number=(number)
59
+ number = number.to_i
60
+ if number > 0 && number <= last_line_number
61
+ @line_number = number
62
+ end
63
+ end
64
+
65
+ def last_line_number
66
+ @lines.size
67
+ end
68
+
69
+ def empty?
70
+ @lines.empty?
71
+ end
72
+
73
+ def each(&block)
74
+ empty? and return self
75
+ old_line_number = line_number
76
+ 1.upto(last_line_number) do |number|
77
+ self.line_number = number
78
+ block.call(line)
79
+ end
80
+ self
81
+ ensure
82
+ self.line_number = old_line_number
83
+ end
84
+ include Enumerable
85
+
86
+ def line
87
+ index = line_number - 1
88
+ @lines[index] if index >= 0
89
+ end
90
+
91
+ def file_linenumber
92
+ "#{filename}:#{line_number}"
93
+ end
94
+
95
+ def match_backward(regexp, previous_after_match = false)
96
+ begin
97
+ if line =~ regexp
98
+ previous_after_match and previous!
99
+ return $~.captures
100
+ end
101
+ end while previous!
102
+ end
103
+
104
+ def match_forward(regexp, next_after_match = false)
105
+ begin
106
+ if line =~ regexp
107
+ next_after_match and next!
108
+ return $~.captures
109
+ end
110
+ end while next!
111
+ end
112
+
113
+ def to_s
114
+ "#{line_number} #{line.chomp}"
115
+ end
116
+
117
+ def inspect
118
+ "#<#{self.class}: #{to_s.inspect}>"
119
+ end
120
+ end
121
+ end
@@ -1,6 +1,6 @@
1
1
  module Spruz
2
2
  # Spruz version
3
- VERSION = '0.2.12'
3
+ VERSION = '0.2.13'
4
4
  VERSION_ARRAY = VERSION.split(/\./).map { |x| x.to_i } # :nodoc:
5
5
  VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc:
6
6
  VERSION_MINOR = VERSION_ARRAY[1] # :nodoc:
@@ -21,4 +21,5 @@ module Spruz
21
21
  require 'spruz/xt/range_plus'
22
22
  require 'spruz/xt/named'
23
23
  require 'spruz/xt/string'
24
+ require 'spruz/xt/file_binary'
24
25
  end
@@ -0,0 +1,7 @@
1
+ require 'spruz/file_binary'
2
+
3
+ module Spruz
4
+ class ::File
5
+ include FileBinary
6
+ end
7
+ end
@@ -2,32 +2,35 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{spruz}
5
- s.version = "0.2.12"
5
+ s.version = "0.2.13"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = [%q{Florian Frank}]
9
- s.date = %q{2011-08-05}
9
+ s.date = %q{2011-08-17}
10
10
  s.description = %q{All the stuff that isn't good/big enough for a real library.}
11
11
  s.email = %q{flori@ping.de}
12
12
  s.executables = [%q{enum}]
13
- s.extra_rdoc_files = [%q{README.rdoc}, %q{lib/spruz.rb}, %q{lib/spruz/hash_symbolize_keys_recursive.rb}, %q{lib/spruz/string_underscore.rb}, %q{lib/spruz/hash_union.rb}, %q{lib/spruz/write.rb}, %q{lib/spruz/version.rb}, %q{lib/spruz/round.rb}, %q{lib/spruz/go.rb}, %q{lib/spruz/generator.rb}, %q{lib/spruz/string_camelize.rb}, %q{lib/spruz/shuffle.rb}, %q{lib/spruz/deep_dup.rb}, %q{lib/spruz/memoize.rb}, %q{lib/spruz/subhash.rb}, %q{lib/spruz/bijection.rb}, %q{lib/spruz/limited.rb}, %q{lib/spruz/attempt.rb}, %q{lib/spruz/to_proc.rb}, %q{lib/spruz/module_group.rb}, %q{lib/spruz/null.rb}, %q{lib/spruz/secure_write.rb}, %q{lib/spruz/xt.rb}, %q{lib/spruz/string_version.rb}, %q{lib/spruz/once.rb}, %q{lib/spruz/xt/named.rb}, %q{lib/spruz/xt/hash_symbolize_keys_recursive.rb}, %q{lib/spruz/xt/string_underscore.rb}, %q{lib/spruz/xt/hash_union.rb}, %q{lib/spruz/xt/write.rb}, %q{lib/spruz/xt/irb.rb}, %q{lib/spruz/xt/round.rb}, %q{lib/spruz/xt/string_camelize.rb}, %q{lib/spruz/xt/shuffle.rb}, %q{lib/spruz/xt/deep_dup.rb}, %q{lib/spruz/xt/symbol_to_proc.rb}, %q{lib/spruz/xt/subhash.rb}, %q{lib/spruz/xt/attempt.rb}, %q{lib/spruz/xt/null.rb}, %q{lib/spruz/xt/secure_write.rb}, %q{lib/spruz/xt/string_version.rb}, %q{lib/spruz/xt/p.rb}, %q{lib/spruz/xt/partial_application.rb}, %q{lib/spruz/xt/time_dummy.rb}, %q{lib/spruz/xt/string.rb}, %q{lib/spruz/xt/full.rb}, %q{lib/spruz/xt/range_plus.rb}, %q{lib/spruz/xt/count_by.rb}, %q{lib/spruz/xt/blank.rb}, %q{lib/spruz/xt/uniq_by.rb}, %q{lib/spruz/p.rb}, %q{lib/spruz/minimize.rb}, %q{lib/spruz/partial_application.rb}, %q{lib/spruz/time_dummy.rb}, %q{lib/spruz/range_plus.rb}, %q{lib/spruz/count_by.rb}, %q{lib/spruz/uniq_by.rb}]
14
- s.files = [%q{.gitignore}, %q{.travis.yml}, %q{Gemfile}, %q{LICENSE}, %q{README.rdoc}, %q{Rakefile}, %q{TODO}, %q{VERSION}, %q{bin/enum}, %q{lib/spruz.rb}, %q{lib/spruz/attempt.rb}, %q{lib/spruz/bijection.rb}, %q{lib/spruz/count_by.rb}, %q{lib/spruz/deep_dup.rb}, %q{lib/spruz/generator.rb}, %q{lib/spruz/go.rb}, %q{lib/spruz/hash_symbolize_keys_recursive.rb}, %q{lib/spruz/hash_union.rb}, %q{lib/spruz/limited.rb}, %q{lib/spruz/memoize.rb}, %q{lib/spruz/minimize.rb}, %q{lib/spruz/module_group.rb}, %q{lib/spruz/null.rb}, %q{lib/spruz/once.rb}, %q{lib/spruz/p.rb}, %q{lib/spruz/partial_application.rb}, %q{lib/spruz/range_plus.rb}, %q{lib/spruz/round.rb}, %q{lib/spruz/secure_write.rb}, %q{lib/spruz/shuffle.rb}, %q{lib/spruz/string_camelize.rb}, %q{lib/spruz/string_underscore.rb}, %q{lib/spruz/string_version.rb}, %q{lib/spruz/subhash.rb}, %q{lib/spruz/time_dummy.rb}, %q{lib/spruz/to_proc.rb}, %q{lib/spruz/uniq_by.rb}, %q{lib/spruz/version.rb}, %q{lib/spruz/write.rb}, %q{lib/spruz/xt.rb}, %q{lib/spruz/xt/attempt.rb}, %q{lib/spruz/xt/blank.rb}, %q{lib/spruz/xt/count_by.rb}, %q{lib/spruz/xt/deep_dup.rb}, %q{lib/spruz/xt/full.rb}, %q{lib/spruz/xt/hash_symbolize_keys_recursive.rb}, %q{lib/spruz/xt/hash_union.rb}, %q{lib/spruz/xt/irb.rb}, %q{lib/spruz/xt/named.rb}, %q{lib/spruz/xt/null.rb}, %q{lib/spruz/xt/p.rb}, %q{lib/spruz/xt/partial_application.rb}, %q{lib/spruz/xt/range_plus.rb}, %q{lib/spruz/xt/round.rb}, %q{lib/spruz/xt/secure_write.rb}, %q{lib/spruz/xt/shuffle.rb}, %q{lib/spruz/xt/string.rb}, %q{lib/spruz/xt/string_camelize.rb}, %q{lib/spruz/xt/string_underscore.rb}, %q{lib/spruz/xt/string_version.rb}, %q{lib/spruz/xt/subhash.rb}, %q{lib/spruz/xt/symbol_to_proc.rb}, %q{lib/spruz/xt/time_dummy.rb}, %q{lib/spruz/xt/uniq_by.rb}, %q{lib/spruz/xt/write.rb}, %q{spruz.gemspec}, %q{tests/spruz_memoize_test.rb}, %q{tests/spruz_secure_write_test.rb}, %q{tests/spruz_test.rb}]
13
+ s.extra_rdoc_files = [%q{README.rdoc}, %q{lib/spruz.rb}, %q{lib/spruz/hash_symbolize_keys_recursive.rb}, %q{lib/spruz/string_underscore.rb}, %q{lib/spruz/hash_union.rb}, %q{lib/spruz/write.rb}, %q{lib/spruz/version.rb}, %q{lib/spruz/round.rb}, %q{lib/spruz/go.rb}, %q{lib/spruz/generator.rb}, %q{lib/spruz/string_camelize.rb}, %q{lib/spruz/shuffle.rb}, %q{lib/spruz/deep_dup.rb}, %q{lib/spruz/memoize.rb}, %q{lib/spruz/lines_file.rb}, %q{lib/spruz/subhash.rb}, %q{lib/spruz/bijection.rb}, %q{lib/spruz/limited.rb}, %q{lib/spruz/attempt.rb}, %q{lib/spruz/to_proc.rb}, %q{lib/spruz/module_group.rb}, %q{lib/spruz/null.rb}, %q{lib/spruz/secure_write.rb}, %q{lib/spruz/xt.rb}, %q{lib/spruz/file_binary.rb}, %q{lib/spruz/string_version.rb}, %q{lib/spruz/once.rb}, %q{lib/spruz/xt/named.rb}, %q{lib/spruz/xt/hash_symbolize_keys_recursive.rb}, %q{lib/spruz/xt/string_underscore.rb}, %q{lib/spruz/xt/hash_union.rb}, %q{lib/spruz/xt/write.rb}, %q{lib/spruz/xt/irb.rb}, %q{lib/spruz/xt/round.rb}, %q{lib/spruz/xt/string_camelize.rb}, %q{lib/spruz/xt/shuffle.rb}, %q{lib/spruz/xt/deep_dup.rb}, %q{lib/spruz/xt/symbol_to_proc.rb}, %q{lib/spruz/xt/subhash.rb}, %q{lib/spruz/xt/attempt.rb}, %q{lib/spruz/xt/null.rb}, %q{lib/spruz/xt/secure_write.rb}, %q{lib/spruz/xt/file_binary.rb}, %q{lib/spruz/xt/string_version.rb}, %q{lib/spruz/xt/p.rb}, %q{lib/spruz/xt/partial_application.rb}, %q{lib/spruz/xt/time_dummy.rb}, %q{lib/spruz/xt/string.rb}, %q{lib/spruz/xt/full.rb}, %q{lib/spruz/xt/range_plus.rb}, %q{lib/spruz/xt/count_by.rb}, %q{lib/spruz/xt/blank.rb}, %q{lib/spruz/xt/uniq_by.rb}, %q{lib/spruz/p.rb}, %q{lib/spruz/minimize.rb}, %q{lib/spruz/partial_application.rb}, %q{lib/spruz/time_dummy.rb}, %q{lib/spruz/range_plus.rb}, %q{lib/spruz/count_by.rb}, %q{lib/spruz/uniq_by.rb}]
14
+ s.files = [%q{.gitignore}, %q{.travis.yml}, %q{Gemfile}, %q{LICENSE}, %q{README.rdoc}, %q{Rakefile}, %q{TODO}, %q{VERSION}, %q{bin/enum}, %q{lib/spruz.rb}, %q{lib/spruz/attempt.rb}, %q{lib/spruz/bijection.rb}, %q{lib/spruz/count_by.rb}, %q{lib/spruz/deep_dup.rb}, %q{lib/spruz/file_binary.rb}, %q{lib/spruz/generator.rb}, %q{lib/spruz/go.rb}, %q{lib/spruz/hash_symbolize_keys_recursive.rb}, %q{lib/spruz/hash_union.rb}, %q{lib/spruz/limited.rb}, %q{lib/spruz/lines_file.rb}, %q{lib/spruz/memoize.rb}, %q{lib/spruz/minimize.rb}, %q{lib/spruz/module_group.rb}, %q{lib/spruz/null.rb}, %q{lib/spruz/once.rb}, %q{lib/spruz/p.rb}, %q{lib/spruz/partial_application.rb}, %q{lib/spruz/range_plus.rb}, %q{lib/spruz/round.rb}, %q{lib/spruz/secure_write.rb}, %q{lib/spruz/shuffle.rb}, %q{lib/spruz/string_camelize.rb}, %q{lib/spruz/string_underscore.rb}, %q{lib/spruz/string_version.rb}, %q{lib/spruz/subhash.rb}, %q{lib/spruz/time_dummy.rb}, %q{lib/spruz/to_proc.rb}, %q{lib/spruz/uniq_by.rb}, %q{lib/spruz/version.rb}, %q{lib/spruz/write.rb}, %q{lib/spruz/xt.rb}, %q{lib/spruz/xt/attempt.rb}, %q{lib/spruz/xt/blank.rb}, %q{lib/spruz/xt/count_by.rb}, %q{lib/spruz/xt/deep_dup.rb}, %q{lib/spruz/xt/file_binary.rb}, %q{lib/spruz/xt/full.rb}, %q{lib/spruz/xt/hash_symbolize_keys_recursive.rb}, %q{lib/spruz/xt/hash_union.rb}, %q{lib/spruz/xt/irb.rb}, %q{lib/spruz/xt/named.rb}, %q{lib/spruz/xt/null.rb}, %q{lib/spruz/xt/p.rb}, %q{lib/spruz/xt/partial_application.rb}, %q{lib/spruz/xt/range_plus.rb}, %q{lib/spruz/xt/round.rb}, %q{lib/spruz/xt/secure_write.rb}, %q{lib/spruz/xt/shuffle.rb}, %q{lib/spruz/xt/string.rb}, %q{lib/spruz/xt/string_camelize.rb}, %q{lib/spruz/xt/string_underscore.rb}, %q{lib/spruz/xt/string_version.rb}, %q{lib/spruz/xt/subhash.rb}, %q{lib/spruz/xt/symbol_to_proc.rb}, %q{lib/spruz/xt/time_dummy.rb}, %q{lib/spruz/xt/uniq_by.rb}, %q{lib/spruz/xt/write.rb}, %q{spruz.gemspec}, %q{tests/spruz_file_binary_test.rb}, %q{tests/spruz_lines_file_test.rb}, %q{tests/spruz_memoize_test.rb}, %q{tests/spruz_secure_write_test.rb}, %q{tests/spruz_test.rb}]
15
15
  s.homepage = %q{http://flori.github.com/spruz}
16
16
  s.rdoc_options = [%q{--title}, %q{Spruz - Useful stuff.}, %q{--main}, %q{README.rdoc}]
17
17
  s.require_paths = [%q{lib}]
18
- s.rubygems_version = %q{1.8.6}
18
+ s.rubygems_version = %q{1.8.7}
19
19
  s.summary = %q{Useful stuff.}
20
- s.test_files = [%q{tests/spruz_secure_write_test.rb}, %q{tests/spruz_test.rb}, %q{tests/spruz_memoize_test.rb}]
20
+ s.test_files = [%q{tests/spruz_secure_write_test.rb}, %q{tests/spruz_test.rb}, %q{tests/spruz_file_binary_test.rb}, %q{tests/spruz_lines_file_test.rb}, %q{tests/spruz_memoize_test.rb}]
21
21
 
22
22
  if s.respond_to? :specification_version then
23
23
  s.specification_version = 3
24
24
 
25
25
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
26
- s.add_development_dependency(%q<gem_hadar>, ["~> 0.0.9"])
26
+ s.add_development_dependency(%q<gem_hadar>, ["~> 0.0.11"])
27
+ s.add_development_dependency(%q<test-unit>, ["~> 2.3"])
27
28
  else
28
- s.add_dependency(%q<gem_hadar>, ["~> 0.0.9"])
29
+ s.add_dependency(%q<gem_hadar>, ["~> 0.0.11"])
30
+ s.add_dependency(%q<test-unit>, ["~> 2.3"])
29
31
  end
30
32
  else
31
- s.add_dependency(%q<gem_hadar>, ["~> 0.0.9"])
33
+ s.add_dependency(%q<gem_hadar>, ["~> 0.0.11"])
34
+ s.add_dependency(%q<test-unit>, ["~> 2.3"])
32
35
  end
33
36
  end
@@ -0,0 +1,67 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'test/unit'
4
+ require 'tempfile'
5
+ require 'spruz/xt/file_binary'
6
+
7
+ module Spruz
8
+ class SpruzFileBinaryTest < Test::Unit::TestCase
9
+ def test_ascii_buffer_size
10
+ write_file do |file|
11
+ file.write "A" * 10 + "\x00"
12
+ assert_equal true, file.ascii?(:buffer_size => 10)
13
+ assert_equal true, File.ascii?(file.path, :buffer_size => 10)
14
+ assert_equal false, file.binary?(:buffer_size => 10)
15
+ assert_equal false, File.binary?(file.path, :buffer_size => 10)
16
+ end
17
+ end
18
+
19
+ def test_binary
20
+ write_file do |file|
21
+ file.write "A" * 69 + "\x01" * 31
22
+ assert_equal true, file.binary?
23
+ assert_equal true, File.binary?(file.path)
24
+ assert_equal false, file.ascii?
25
+ assert_equal false, File.ascii?(file.path)
26
+ end
27
+ end
28
+
29
+ def test_ascii_offset
30
+ write_file do |file|
31
+ file.write "\x01" * 31 + "A" * 70
32
+ assert_equal false, file.binary?(:offset => 1)
33
+ assert_equal false, File.binary?(file.path, :offset => 1)
34
+ assert_equal true, file.ascii?(:offset => 1)
35
+ assert_equal true, File.ascii?(file.path, :offset => 1)
36
+ end
37
+ end
38
+
39
+ def test_binary_zero
40
+ write_file do |file|
41
+ file.write "A" * 50 + "\0" + "A" * 49
42
+ assert_equal true, file.binary?
43
+ assert_equal true, File.binary?(file.path)
44
+ assert_equal false, file.ascii?
45
+ assert_equal false, File.ascii?(file.path)
46
+ end
47
+ end
48
+
49
+ def test_ascii
50
+ write_file do |file|
51
+ file.write "A" * 100
52
+ assert_equal false, file.binary?
53
+ assert_equal false, File.binary?(file.path)
54
+ assert_equal true, file.ascii?
55
+ assert_equal true, File.ascii?(file.path)
56
+ end
57
+ end
58
+
59
+ private
60
+
61
+ def write_file
62
+ File.open(File.join(Dir.tmpdir, "temp.#$$"), 'wb+') do |file|
63
+ yield file
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,84 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'test/unit'
4
+ require 'tempfile'
5
+ require 'spruz/lines_file'
6
+
7
+ module Spruz
8
+ class SpruzLinesFileTest < Test::Unit::TestCase
9
+ FILE = <<EOT
10
+ def foo
11
+ end
12
+
13
+ def bar
14
+ end
15
+
16
+ def baz
17
+ end
18
+ EOT
19
+
20
+ def test_instantiation
21
+ write_file do |file|
22
+ file.write FILE
23
+ file.rewind
24
+ assert_kind_of Spruz::LinesFile, lf = Spruz::LinesFile.for_file(file)
25
+ assert_equal [ "def foo\n", 1 ], [ lf.line, lf.line.line_number ]
26
+ assert_kind_of Spruz::LinesFile, lf = Spruz::LinesFile.for_filename(file.path)
27
+ assert_equal [ "def foo\n", 1 ], [ lf.line, lf.line.line_number ]
28
+ file.rewind
29
+ assert_kind_of Spruz::LinesFile, lf = Spruz::LinesFile.for_lines(file.readlines)
30
+ assert_equal [ "def foo\n", 1 ], [ lf.line, lf.line.line_number ]
31
+ end
32
+ end
33
+
34
+ def test_match
35
+ write_file do |file|
36
+ file.write FILE
37
+ file.rewind
38
+ lf = Spruz::LinesFile.for_file(file)
39
+ assert_equal "def foo\n", lf.line
40
+ assert_equal 1, lf.line.line_number
41
+ assert_equal %w[foo], lf.match_forward(/def (\S+)/)
42
+ assert_equal "def foo\n", lf.line
43
+ assert_equal %w[foo], lf.match_forward(/def (\S+)/, true)
44
+ assert_equal 2, lf.line.line_number
45
+ assert_equal "end\n", lf.line
46
+ assert_equal %w[bar], lf.match_forward(/def (\S+)/, true)
47
+ assert_equal 5, lf.line.line_number
48
+ assert_equal %w[baz], lf.match_forward(/def (\S+)/, true)
49
+ assert_nil lf.match_forward(/def (\S+)/, true)
50
+ assert_equal "end\n", lf.line
51
+ assert_equal 8, lf.line.line_number
52
+ assert_equal %w[baz], lf.match_backward(/def (\S+)/)
53
+ assert_equal "def baz\n", lf.line
54
+ assert_equal 7, lf.line.line_number
55
+ assert_equal %w[baz], lf.match_backward(/def (\S+)/, true)
56
+ assert_equal "\n", lf.line
57
+ assert_equal 6, lf.line.line_number
58
+ assert_equal %w[bar], lf.match_backward(/def (\S+)/, true)
59
+ assert_equal %w[foo], lf.match_backward(/def (\S+)/, true)
60
+ assert_equal nil, lf.match_backward(/nada/, true)
61
+ end
62
+ end
63
+
64
+ def test_empty_and_not
65
+ lf = Spruz::LinesFile.for_lines []
66
+ assert_equal true, lf.empty?
67
+ assert_equal 0, lf.last_line_number
68
+ assert_equal [], lf.to_a
69
+ lf = Spruz::LinesFile.for_lines [ "foo\n" ]
70
+ assert_equal false, lf.empty?
71
+ assert_equal 1, lf.last_line_number
72
+ assert_equal [ "foo\n" ], lf.to_a
73
+ assert_equal [ "foo\n", 1 ], [ lf.line, lf.line.line_number ]
74
+ end
75
+
76
+ private
77
+
78
+ def write_file
79
+ File.open(File.join(Dir.tmpdir, "temp.#$$"), 'wb+') do |file|
80
+ yield file
81
+ end
82
+ end
83
+ end
84
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spruz
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.12
4
+ version: 0.2.13
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,19 +9,30 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-08-05 00:00:00.000000000Z
12
+ date: 2011-08-17 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: gem_hadar
16
- requirement: &2157109200 !ruby/object:Gem::Requirement
16
+ requirement: &2152539880 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
20
20
  - !ruby/object:Gem::Version
21
- version: 0.0.9
21
+ version: 0.0.11
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *2157109200
24
+ version_requirements: *2152539880
25
+ - !ruby/object:Gem::Dependency
26
+ name: test-unit
27
+ requirement: &2152539400 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: '2.3'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *2152539400
25
36
  description: All the stuff that isn't good/big enough for a real library.
26
37
  email: flori@ping.de
27
38
  executables:
@@ -42,6 +53,7 @@ extra_rdoc_files:
42
53
  - lib/spruz/shuffle.rb
43
54
  - lib/spruz/deep_dup.rb
44
55
  - lib/spruz/memoize.rb
56
+ - lib/spruz/lines_file.rb
45
57
  - lib/spruz/subhash.rb
46
58
  - lib/spruz/bijection.rb
47
59
  - lib/spruz/limited.rb
@@ -51,6 +63,7 @@ extra_rdoc_files:
51
63
  - lib/spruz/null.rb
52
64
  - lib/spruz/secure_write.rb
53
65
  - lib/spruz/xt.rb
66
+ - lib/spruz/file_binary.rb
54
67
  - lib/spruz/string_version.rb
55
68
  - lib/spruz/once.rb
56
69
  - lib/spruz/xt/named.rb
@@ -68,6 +81,7 @@ extra_rdoc_files:
68
81
  - lib/spruz/xt/attempt.rb
69
82
  - lib/spruz/xt/null.rb
70
83
  - lib/spruz/xt/secure_write.rb
84
+ - lib/spruz/xt/file_binary.rb
71
85
  - lib/spruz/xt/string_version.rb
72
86
  - lib/spruz/xt/p.rb
73
87
  - lib/spruz/xt/partial_application.rb
@@ -100,11 +114,13 @@ files:
100
114
  - lib/spruz/bijection.rb
101
115
  - lib/spruz/count_by.rb
102
116
  - lib/spruz/deep_dup.rb
117
+ - lib/spruz/file_binary.rb
103
118
  - lib/spruz/generator.rb
104
119
  - lib/spruz/go.rb
105
120
  - lib/spruz/hash_symbolize_keys_recursive.rb
106
121
  - lib/spruz/hash_union.rb
107
122
  - lib/spruz/limited.rb
123
+ - lib/spruz/lines_file.rb
108
124
  - lib/spruz/memoize.rb
109
125
  - lib/spruz/minimize.rb
110
126
  - lib/spruz/module_group.rb
@@ -130,6 +146,7 @@ files:
130
146
  - lib/spruz/xt/blank.rb
131
147
  - lib/spruz/xt/count_by.rb
132
148
  - lib/spruz/xt/deep_dup.rb
149
+ - lib/spruz/xt/file_binary.rb
133
150
  - lib/spruz/xt/full.rb
134
151
  - lib/spruz/xt/hash_symbolize_keys_recursive.rb
135
152
  - lib/spruz/xt/hash_union.rb
@@ -152,6 +169,8 @@ files:
152
169
  - lib/spruz/xt/uniq_by.rb
153
170
  - lib/spruz/xt/write.rb
154
171
  - spruz.gemspec
172
+ - tests/spruz_file_binary_test.rb
173
+ - tests/spruz_lines_file_test.rb
155
174
  - tests/spruz_memoize_test.rb
156
175
  - tests/spruz_secure_write_test.rb
157
176
  - tests/spruz_test.rb
@@ -179,11 +198,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
179
198
  version: '0'
180
199
  requirements: []
181
200
  rubyforge_project:
182
- rubygems_version: 1.8.6
201
+ rubygems_version: 1.8.7
183
202
  signing_key:
184
203
  specification_version: 3
185
204
  summary: Useful stuff.
186
205
  test_files:
187
206
  - tests/spruz_secure_write_test.rb
188
207
  - tests/spruz_test.rb
208
+ - tests/spruz_file_binary_test.rb
209
+ - tests/spruz_lines_file_test.rb
189
210
  - tests/spruz_memoize_test.rb