string_stubber 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.rspec +2 -0
- data/Gemfile +0 -3
- data/Gemfile.lock +14 -3
- data/README.md +2 -0
- data/lib/string_stubber/base.rb +15 -12
- data/lib/string_stubber/core_ext.rb +0 -4
- data/lib/string_stubber/version.rb +1 -1
- data/lib/string_stubber.rb +4 -0
- data/spec/quality_spec.rb +11 -0
- data/spec/spec_helper.rb +10 -0
- data/spec/string_stubber/base_spec.rb +88 -0
- data/spec/string_stubber/core_ext_spec.rb +2 -0
- data/spec/support/custom_matchers.rb +44 -0
- data/string_stubber.gemspec +4 -0
- metadata +44 -7
data/.rspec
ADDED
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,16 +1,27 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
string_stubber (0.0.
|
4
|
+
string_stubber (0.0.3)
|
5
|
+
yard (~> 0.6)
|
5
6
|
|
6
7
|
GEM
|
7
8
|
remote: http://rubygems.org/
|
8
9
|
specs:
|
9
|
-
|
10
|
+
diff-lcs (1.1.2)
|
11
|
+
rspec (2.4.0)
|
12
|
+
rspec-core (~> 2.4.0)
|
13
|
+
rspec-expectations (~> 2.4.0)
|
14
|
+
rspec-mocks (~> 2.4.0)
|
15
|
+
rspec-core (2.4.0)
|
16
|
+
rspec-expectations (2.4.0)
|
17
|
+
diff-lcs (~> 1.1.2)
|
18
|
+
rspec-mocks (2.4.0)
|
19
|
+
yard (0.6.4)
|
10
20
|
|
11
21
|
PLATFORMS
|
22
|
+
java
|
12
23
|
ruby
|
13
24
|
|
14
25
|
DEPENDENCIES
|
15
|
-
|
26
|
+
rspec (~> 2.4)
|
16
27
|
string_stubber!
|
data/README.md
CHANGED
data/lib/string_stubber/base.rb
CHANGED
@@ -1,41 +1,44 @@
|
|
1
1
|
module StringStubber
|
2
2
|
module Base
|
3
|
-
WORD =
|
4
|
-
SNIP = /\s
|
3
|
+
WORD = /[\w[:punct:]]+/
|
4
|
+
SNIP = /\s+$/
|
5
5
|
|
6
|
+
# Stubs a given text string, up to a given number of words
|
7
|
+
# @param [String] text Any piece of text
|
8
|
+
# @param [Fixnum] max_words The desired number of words
|
9
|
+
# @return [String] The text, stubbed at max_words number of words
|
6
10
|
def stub_words(text, max_words)
|
7
11
|
scanner = StringScanner.new(text.to_s)
|
8
12
|
|
9
13
|
return scan_words(scanner, max_words)
|
10
14
|
end
|
11
15
|
|
16
|
+
# Stubs the given text string a number of whole-words, not to go beyond the given text position
|
17
|
+
# @param [String] text Any piece of text
|
18
|
+
# @param [Fixnum] max_text Text position that delimits the desired number of whole words
|
19
|
+
# @return [String] The text, stubbed at the max_text position
|
12
20
|
def stub_text(text, max_text)
|
13
|
-
|
14
|
-
scanner = StringScanner.new(string)
|
21
|
+
scanner = StringScanner.new(text.to_s)
|
15
22
|
|
16
23
|
return scan_text(scanner, max_text)
|
17
24
|
end
|
18
25
|
|
19
26
|
def scan_word(scanner)
|
20
|
-
|
21
|
-
|
22
|
-
return str
|
27
|
+
scanner.scan_until(WORD)
|
23
28
|
end
|
24
29
|
|
25
30
|
def scan_words(scanner, max_words)
|
26
31
|
max_words.times.map {
|
27
32
|
scanner.scan_until(WORD)
|
28
|
-
}.
|
33
|
+
}.compact.join
|
29
34
|
end
|
30
35
|
|
31
36
|
def scan_text(scanner, max_text)
|
32
37
|
start = scanner.pos
|
33
38
|
|
34
|
-
until scanner.pos
|
35
|
-
|
36
|
-
(str = scanner.pre_match || scanner.string[start, max_text]).gsub!(SNIP, '')
|
39
|
+
until scanner.pos >= max_text || scanner.scan_until(WORD).nil?; end
|
37
40
|
|
38
|
-
|
41
|
+
(str = scanner.pre_match || scanner.string[start, max_text]).gsub!(SNIP, '')
|
39
42
|
end
|
40
43
|
end # module Base
|
41
44
|
end # module StringStubber
|
data/lib/string_stubber.rb
CHANGED
@@ -0,0 +1,11 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
IGNORE = /\.(gitmodules|png$|tar$|gz$|rbc$|gem$|pdf$)/
|
4
|
+
|
5
|
+
describe "The application itself" do
|
6
|
+
it "has no malformed whitespace" do
|
7
|
+
files = `git ls-files`.split("\n").select {|fn| fn !~ IGNORE}
|
8
|
+
|
9
|
+
files.should be_well_formed
|
10
|
+
end
|
11
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
require File.expand_path("../spec_helper", File.dirname(__FILE__))
|
2
|
+
|
3
|
+
describe StringStubber::Base do
|
4
|
+
describe 'The Base module should behave like a proper mix-in ;)' do
|
5
|
+
before :each do
|
6
|
+
@methods = [ :stub_words, :stub_text, :scan_word, :scan_words, :scan_text ]
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'should have class methods available' do
|
10
|
+
class TestMixin
|
11
|
+
extend StringStubber::Base
|
12
|
+
end
|
13
|
+
|
14
|
+
@methods.each {|method|
|
15
|
+
TestMixin.respond_to?(method).should be_true
|
16
|
+
}
|
17
|
+
end
|
18
|
+
end # Should be a Mix-In
|
19
|
+
|
20
|
+
describe 'The stubbing methods should behave as expected (Docs check)' do
|
21
|
+
before :each do
|
22
|
+
@text = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Vivamus vitae risus vitae lorem iaculis placerat. "
|
23
|
+
@text << "Aliquam sit amet felis. Etiam congue. Donec risus risus, pretium ac, tincidunt eu, tempor eu, quam. Morbi "
|
24
|
+
@text << "blandit mollis magna. Suspendisse eu tortor. Donec vitae felis nec ligula blandit rhoncus. Ut a pede ac neque "
|
25
|
+
@text << "mattis facilisis. Nulla nunc ipsum, sodales vitae, hendrerit non, imperdiet ac, ante. Morbi sit amet mi. Ut "
|
26
|
+
@text << "magna. Curabitur id est. Nulla velit. Sed consectetuer sodales justo. Aliquam dictum gravida libero. Sed eu "
|
27
|
+
@text << "turpis. Nunc id lorem. Aenean consequat tempor mi. Phasellus in neque. Nunc fermentum convallis ligula."
|
28
|
+
|
29
|
+
@count = @text.split(/\W+/).count
|
30
|
+
end
|
31
|
+
|
32
|
+
describe 'Method: stub_words' do
|
33
|
+
it 'should return the expected number of words' do
|
34
|
+
StringStubber.stub_words(@text, 10).split(/\W+/).count.should be(10)
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'should always return a string' do
|
38
|
+
stubs = [
|
39
|
+
StringStubber.stub_words(@text, -100),
|
40
|
+
StringStubber.stub_words(@text, -1),
|
41
|
+
StringStubber.stub_words(@text, 0),
|
42
|
+
StringStubber.stub_words(@text, 10),
|
43
|
+
StringStubber.stub_words(@text, 100)
|
44
|
+
]
|
45
|
+
|
46
|
+
stubs.each {|stub|
|
47
|
+
stub.is_a?(String).should be_true
|
48
|
+
}
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'should never have trailing spaces' do
|
52
|
+
@count.times {|x|
|
53
|
+
(StringStubber.stub_words(@text, x) !~ /\s+[\s\W]+$/).should be_true
|
54
|
+
}
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'Should only have trailing non-words if they immediately follow words' do
|
58
|
+
# regex = /[^\w\s]/
|
59
|
+
# scanner = StringScanner.new(@text)
|
60
|
+
# count = @text.split(/[^\w\s]/).count
|
61
|
+
# puncts = Hash[ count.times.map {
|
62
|
+
# scanner.scan_until(regex)
|
63
|
+
# [scanner.pre_match.split.count, scanner.pos - 1]
|
64
|
+
# }
|
65
|
+
# ]
|
66
|
+
#
|
67
|
+
# @count.times {|x|
|
68
|
+
# (StringStubber.stub_words(@text, x) !~ /\s+$/).should be_true
|
69
|
+
# }
|
70
|
+
end
|
71
|
+
|
72
|
+
#
|
73
|
+
end # describe 'Method: stub_words'
|
74
|
+
|
75
|
+
describe 'Method: stub_text' do
|
76
|
+
it 'should return less than the number of chars specified, if the offset is in the middle of a word' do
|
77
|
+
StringStubber.stub_text(@text, 33).size.should be(27)
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'should return less than the number of chars specified, if the offset is in the middle of a word\'s punctuation' do
|
81
|
+
StringStubber.stub_text(@text, 33).size.should be(27)
|
82
|
+
end
|
83
|
+
|
84
|
+
it 'should return exactly the number of chars specified, if the position lands on white-space' do
|
85
|
+
end
|
86
|
+
end # describe 'Method: stub_text'
|
87
|
+
end
|
88
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module CustomMatchers
|
2
|
+
class BeWellFormed
|
3
|
+
def matches?(files)
|
4
|
+
@errors = files.map {|filename|
|
5
|
+
[
|
6
|
+
check_for_tabs(filename),
|
7
|
+
excessive_spacing(filename),
|
8
|
+
newline_precedes_eof(filename)
|
9
|
+
]
|
10
|
+
}.flatten.compact
|
11
|
+
|
12
|
+
@errors.empty?
|
13
|
+
end
|
14
|
+
|
15
|
+
def failure_message_for_should
|
16
|
+
@errors.join("\n")
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
def check_for_tabs(filename)
|
21
|
+
bad_lines = File.readlines(filename).each_with_index.map do |line, line_no|
|
22
|
+
line_no + 1 if line["\t"] and line !~ /^\s+#.*\s+\n$/
|
23
|
+
end.flatten.compact
|
24
|
+
|
25
|
+
"#{filename} has tab characters on lines #{bad_lines.join(', ')}" if bad_lines.any?
|
26
|
+
end
|
27
|
+
|
28
|
+
def excessive_spacing(filename)
|
29
|
+
bad_lines = File.readlines(filename).each_with_index.map do |line, line_no|
|
30
|
+
line_no + 1 if line =~ /\s+\n$/ and line !~ /^\s+#.*\s+\n$/
|
31
|
+
end.flatten.compact
|
32
|
+
|
33
|
+
"#{filename} has spaces on the EOL on lines #{bad_lines.join(', ')}" if bad_lines.any?
|
34
|
+
end
|
35
|
+
|
36
|
+
def newline_precedes_eof(filename)
|
37
|
+
"#{filename} does not have a newline (\\n) before EOF" if File.read(filename) !~ /\n$/
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def be_well_formed
|
42
|
+
BeWellFormed.new
|
43
|
+
end
|
44
|
+
end
|
data/string_stubber.gemspec
CHANGED
@@ -14,6 +14,10 @@ Gem::Specification.new do |s|
|
|
14
14
|
|
15
15
|
s.rubyforge_project = "string_stubber"
|
16
16
|
|
17
|
+
s.add_dependency 'yard', '~>0.6'
|
18
|
+
|
19
|
+
s.add_development_dependency 'rspec', '~>2.4'
|
20
|
+
|
17
21
|
s.files = `git ls-files`.split("\n")
|
18
22
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
23
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 3
|
9
|
+
version: 0.0.3
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Scott Gonyea
|
@@ -14,10 +14,37 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-01-
|
17
|
+
date: 2011-01-20 00:00:00 -08:00
|
18
18
|
default_executable:
|
19
|
-
dependencies:
|
20
|
-
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: yard
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ~>
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 0
|
30
|
+
- 6
|
31
|
+
version: "0.6"
|
32
|
+
type: :runtime
|
33
|
+
version_requirements: *id001
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: rspec
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ~>
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
segments:
|
43
|
+
- 2
|
44
|
+
- 4
|
45
|
+
version: "2.4"
|
46
|
+
type: :development
|
47
|
+
version_requirements: *id002
|
21
48
|
description: StringStubber allows you to truncate Strings, while preserving whole-words.
|
22
49
|
email:
|
23
50
|
- me@sgonyea.com
|
@@ -29,6 +56,7 @@ extra_rdoc_files: []
|
|
29
56
|
|
30
57
|
files:
|
31
58
|
- .gitignore
|
59
|
+
- .rspec
|
32
60
|
- Gemfile
|
33
61
|
- Gemfile.lock
|
34
62
|
- README.md
|
@@ -37,6 +65,11 @@ files:
|
|
37
65
|
- lib/string_stubber/base.rb
|
38
66
|
- lib/string_stubber/core_ext.rb
|
39
67
|
- lib/string_stubber/version.rb
|
68
|
+
- spec/quality_spec.rb
|
69
|
+
- spec/spec_helper.rb
|
70
|
+
- spec/string_stubber/base_spec.rb
|
71
|
+
- spec/string_stubber/core_ext_spec.rb
|
72
|
+
- spec/support/custom_matchers.rb
|
40
73
|
- string_stubber.gemspec
|
41
74
|
has_rdoc: true
|
42
75
|
homepage: ""
|
@@ -70,5 +103,9 @@ rubygems_version: 1.3.7
|
|
70
103
|
signing_key:
|
71
104
|
specification_version: 3
|
72
105
|
summary: Allows you to truncate Strings, while preserving whole-words.
|
73
|
-
test_files:
|
74
|
-
|
106
|
+
test_files:
|
107
|
+
- spec/quality_spec.rb
|
108
|
+
- spec/spec_helper.rb
|
109
|
+
- spec/string_stubber/base_spec.rb
|
110
|
+
- spec/string_stubber/core_ext_spec.rb
|
111
|
+
- spec/support/custom_matchers.rb
|