fixnames 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,10 @@
1
+ class Fixnames
2
+ module Version
3
+ MAJOR = 0
4
+ MINOR = 3
5
+ PATCH = 0
6
+ BUILD = nil
7
+
8
+ STRING = [MAJOR, MINOR, PATCH, BUILD].compact.join('.')
9
+ end
10
+ end
@@ -0,0 +1,36 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe Fixnames::Filters do
4
+ describe "#banners" do
5
+ context "when TRUE" do
6
+ before do
7
+ @testopt.banners = true
8
+ end
9
+
10
+ describe "should remove banner blocks" do
11
+ subject { 'foobar' }
12
+ it_should_fix 'foo[xvid]bar', 'foo[divx]bar'
13
+ it_should_fix 'foo[ xvid ]bar', 'foo[_divx_]bar'
14
+ end
15
+
16
+ describe "should recognize advers with various bracket styles" do
17
+ subject { 'foobar' }
18
+ it_should_fix 'foo[xvid]bar', 'foo(xvid)bar'
19
+ it_should_fix 'foo{xvid}bar', 'foo<xvid>bar'
20
+ end
21
+ end
22
+
23
+ context "when FALSE" do
24
+ before do
25
+ @testopt.banners = false
26
+ @testopt.charstrip_allow_brackets = true
27
+ end
28
+
29
+ describe "should ALLOW banner blocks" do
30
+ it_should_not_change 'abc[XVID]xyz', 'foo[divx]bar'
31
+ it_should_not_change 'foo[xvid]bar', 'foo(xvid)bar'
32
+ it_should_not_change 'foo{xvid}bar', 'foo<xvid>bar'
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,26 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe Fixnames do
4
+ context "when option[:brackets] is TRUE" do
5
+ before do
6
+ @testopt.brackets = true
7
+ end
8
+
9
+ describe "should remove bracket expressions" do
10
+ subject { 'foobar' }
11
+ it_should_fix 'foo[x]bar', 'fo(foo)ob<bar>ar'
12
+ it_should_fix 'foo[ x ]bar', 'fo(-foo-)ob<_bar_>ar'
13
+ end
14
+ end
15
+
16
+ context "when option[:brackets] is FALSE" do
17
+ before do
18
+ @testopt.brackets = false
19
+ @testopt.charstrip_allow_brackets = true
20
+ end
21
+
22
+ describe "should replace ALLOW bracket expressions" do
23
+ it_should_not_change 'foo[x]bar', 'fo(foo)ob<bar>ar'
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,24 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe Fixnames do
4
+ context "when option[:camelcase] is TRUE" do
5
+ before do
6
+ @testopt.camelcase = true
7
+ end
8
+
9
+ describe "should replace camelCase with underscore_case" do
10
+ subject { 'foo_bar' }
11
+ it_should_fix 'fooBar'
12
+ end
13
+ end
14
+
15
+ context "when option[:camelcase] is FALSE" do
16
+ before do
17
+ @testopt.camelcase = false
18
+ end
19
+
20
+ describe "should allow aA style case patterns" do
21
+ it_should_not_change 'xYzZy', 'fooBarBaz'
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,20 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe Fixnames do
4
+ context "when option[:charstrip] is non-nil" do
5
+ describe "should allow common filename characters" do
6
+ it_should_not_change 'abc', 'a_b', 'a-b', 'a.b'
7
+ end
8
+
9
+ describe "should strip most punctuation" do
10
+ subject { 'ab' }
11
+ it_should_fix 'a[b', 'a]b'
12
+ it_should_fix 'a{b', 'a}b'
13
+ it_should_fix 'a(b', 'a)b'
14
+ it_should_fix 'a\'b', 'a"b'
15
+ it_should_fix 'a,b', 'a+b'
16
+ it_should_fix 'a!b', 'a~b'
17
+ it_should_fix 'a#b', 'a@b'
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,29 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe Fixnames do
4
+ context "when option[:checksums] is TRUE" do
5
+ before do
6
+ @testopt.checksums = true
7
+ end
8
+
9
+ describe "should remove [1A2B3C4D] style checksum markers" do
10
+ subject { 'foobar' }
11
+ it_should_fix 'foo[1A2B3C4D]bar', 'foo[1a2b3c4d]bar'
12
+ it_should_fix 'foo(1A2B3C4D)bar', 'foo(1a2b3c4d)bar'
13
+ it_should_fix 'foo{1A2B3C4D}bar', 'foo{1a2b3c4d}bar'
14
+ end
15
+ end
16
+
17
+ context "when option[:checksums] is FALSE" do
18
+ before do
19
+ @testopt.checksums = false
20
+ @testopt.charstrip_allow_brackets = true
21
+ end
22
+
23
+ describe "should allow checksum markers" do
24
+ it_should_not_change 'foo[1A2B3C4D]bar', 'foo[1a2b3c4d]bar'
25
+ it_should_not_change 'foo(1A2B3C4D)bar', 'foo(1a2b3c4d)bar'
26
+ it_should_not_change 'foo{1A2B3C4D}bar', 'foo{1a2b3c4d}bar'
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,35 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe Fixnames do
4
+ context "when option[:fix_dots] is TRUE" do
5
+ before do
6
+ @testopt.fix_dots = true
7
+ end
8
+
9
+ describe "should replace early dots with underbars" do
10
+ subject { 'file_name.txt' }
11
+ it_should_fix 'file.name.txt'
12
+ end
13
+
14
+ describe "should replace ALL early dots, recursively" do
15
+ subject { 'it_should_replace_it_even_with_all_these.dots' }
16
+ it_should_fix 'it.should.replace.it.even.with.all.these.dots'
17
+ end
18
+
19
+ describe "should compress neighboring dots to a single character" do
20
+ subject { 'file.txt' }
21
+ it_should_fix 'file..txt', 'file...txt', 'file....txt'
22
+ end
23
+ end
24
+
25
+ context "when option[:fix_dots] is FALSE" do
26
+ before do
27
+ @testopt.fix_dots = false
28
+ end
29
+
30
+ describe "should allow multiple dots" do
31
+ it_should_not_change 'file.name.txt'
32
+ it_should_not_change 'file..txt', 'file...txt', 'file....txt'
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,24 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe Fixnames do
4
+ context "when option[:hack_and] is TRUE" do
5
+ before do
6
+ @testopt.hack_and = true
7
+ end
8
+
9
+ describe "should replace \"&\" with \"_and_\"" do
10
+ subject { 'a_and_b' }
11
+ it_should_fix 'a&b', 'a & b', 'a_&_b', 'a &_b', 'a_& b'
12
+ end
13
+ end
14
+
15
+ context "when option[:hack_and] is FALSE" do
16
+ before do
17
+ @testopt.hack_and = false
18
+ end
19
+
20
+ describe "should replace ALLOW \"&\"" do
21
+ it_should_not_change 'a&b'
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,28 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe Fixnames do
4
+ context "when option[:lowercase] is TRUE" do
5
+ before do
6
+ @testopt.lowercase = true
7
+ end
8
+
9
+ describe "should allow lowercase" do
10
+ it_should_not_change 'ab_c', 'ab-c', 'ab.c'
11
+ end
12
+
13
+ describe "should fix uppercase" do
14
+ subject { 'abc' }
15
+ it_should_fix 'ABC', 'Abc', 'aBc', 'abC'
16
+ end
17
+ end
18
+
19
+ context "when option[:lowercase] is FALSE" do
20
+ before do
21
+ @testopt.lowercase = false
22
+ end
23
+
24
+ describe "should ALLOW uppercase" do
25
+ it_should_not_change 'ABC', 'Abc', 'aBc', 'abC'
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,24 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe Fixnames do
4
+ context "when option[:semicolon] is TRUE" do
5
+ before do
6
+ @testopt.semicolon = true
7
+ end
8
+
9
+ describe "should replace semicolons and surrounding whitespace with a single dash" do
10
+ subject { 'a-b' }
11
+ it_should_fix 'a;b', 'a; b', 'a ;b', 'a ; b', 'a-; b', 'a ;-b'
12
+ end
13
+ end
14
+
15
+ context "when option[:semicolon] is FALSE" do
16
+ before do
17
+ @testopt.semicolon = false
18
+ end
19
+
20
+ describe "should ALLOW semicolons" do
21
+ it_should_not_change 'a;b'
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,65 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe Fixnames do
4
+ context "when option[:whitespace] is non-nil" do
5
+ describe "should preserve simple names" do
6
+ it_should_not_change 'abc', 'a-b', 'a.b'
7
+ end
8
+
9
+ describe "should convert all spaces to underscores" do
10
+ subject { 'ab_c' }
11
+ it_should_fix 'ab c'
12
+ end
13
+
14
+ describe "should convert all tabs to underscores" do
15
+ subject { 'ab_c' }
16
+ it_should_fix "ab\tc"
17
+ end
18
+
19
+ describe "should pass single underscores unchanged" do
20
+ it_should_not_change 'a_b'
21
+ end
22
+
23
+ describe "should collapse multiple spaces" do
24
+ subject { 'a_b' }
25
+ it_should_fix 'a b', 'a b', 'a b'
26
+ end
27
+
28
+ describe "should collapse multiple underscores to a single character" do
29
+ subject { 'a_b' }
30
+ it_should_fix 'a__b', 'a___b', 'a____b'
31
+ end
32
+
33
+ describe "should collapse multiple mixed whitespaces characters to a single underscore" do
34
+ subject { 'a_b' }
35
+ it_should_fix 'a_ b', 'a _b', 'a _ b', 'a_ _b'
36
+ end
37
+
38
+ describe "should erase trailing whitespace" do
39
+ subject { 'ab' }
40
+ it_should_fix 'ab ', 'ab_', 'ab_ ', 'ab _', 'ab__', 'ab '
41
+ end
42
+
43
+ describe "should erase leading whitespace" do
44
+ subject { 'ab' }
45
+ it_should_fix ' ab', '_ab', '_ ab', ' _ab', '__ab', ' ab'
46
+ end
47
+
48
+ describe "should pass a single dot unchanged" do
49
+ it_should_not_change 'abc.d', 'ab_c.d', 'a-bc.d'
50
+ end
51
+
52
+ describe "should remove whitespace preceding a dot" do
53
+ subject { 'a.b' }
54
+ it_should_fix 'a .b', 'a_.b', 'a-.b'
55
+ it_should_fix 'a .b', 'a _.b', 'a_ .b'
56
+ it_should_fix 'a__.b', 'a _ .b', 'a- _.b'
57
+ end
58
+
59
+ describe "should remove whitespace around a single dash" do
60
+ subject { 'a-b' }
61
+ it_should_fix 'a- b', 'a-_b', 'a -b', 'a_-b'
62
+ it_should_fix 'a_-_b', 'a - b', 'a_- b', 'a -_b'
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,18 @@
1
+ require 'simplecov'
2
+ SimpleCov.start
3
+
4
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
5
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
6
+ require 'rspec'
7
+ require 'fixnames'
8
+
9
+ # Requires supporting files with custom matchers and macros, etc,
10
+ # in ./support/ and its subdirectories.
11
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
12
+
13
+ RSpec.configure do |config|
14
+ config.before do
15
+ @testopt = Fixnames::Option.new
16
+ end
17
+ end
18
+
@@ -0,0 +1,19 @@
1
+ def fixed_for(x)
2
+ Fixnames::FixFile.fix_name x, @testopt
3
+ end
4
+
5
+ def it_should_not_change(*args)
6
+ args.each do |x|
7
+ specify "unchanged: #{x.inspect}" do
8
+ fixed_for(x).should eq(x)
9
+ end
10
+ end
11
+ end
12
+
13
+ def it_should_fix(*args)
14
+ args.each do |x|
15
+ specify "produces #{subject.call.inspect} when fixing: #{x.inspect}" do
16
+ fixed_for(x).should eq(subject)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,34 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+
4
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
5
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
6
+ require 'fixnames'
7
+
8
+ class Test::Unit::TestCase
9
+ def fixopt_global
10
+ { }
11
+ # { :verbose => 3 }
12
+ end
13
+
14
+ def obj(str)
15
+ FixFileNames.new str, FixFileNames::DEFAULT_OPTIONS.merge(fixopt_global).merge(testopt)
16
+ end
17
+
18
+ def fix(str)
19
+ obj(str).fixed
20
+ end
21
+
22
+ def assert_nofix(str)
23
+ assert_equal str, fix(str), "Expected NO FIXES to affect the filename."
24
+ end
25
+
26
+ def fixed_is(val=nil)
27
+ @fixed_is ||= val if val
28
+ @fixed_is
29
+ end
30
+
31
+ def assert_fix(orig, fixed=fixed_is)
32
+ assert_equal fixed, fix(orig)
33
+ end
34
+ end
@@ -0,0 +1,35 @@
1
+ require 'helper'
2
+
3
+ class TestCharstrip < Test::Unit::TestCase
4
+ def testopt
5
+ { }
6
+ end
7
+
8
+ def test_nospace
9
+ assert_nofix 'abc'
10
+ assert_nofix 'a_b'
11
+ assert_nofix 'a-b'
12
+ assert_nofix 'a.b'
13
+ end
14
+
15
+ def test_charstrip
16
+ fixed_is 'ab'
17
+
18
+ assert_fix 'a[b'
19
+ assert_fix 'a]b'
20
+ assert_fix 'a{b'
21
+ assert_fix 'a}b'
22
+ assert_fix 'a(b'
23
+ assert_fix 'a)b'
24
+ assert_fix 'a\'b'
25
+ assert_fix 'a"b'
26
+ assert_fix 'a,b'
27
+ assert_fix 'a+b'
28
+ assert_fix 'a!b'
29
+ assert_fix 'a~b'
30
+ assert_fix 'a/b'
31
+ assert_fix "a\\b"
32
+ assert_fix 'a#b'
33
+ assert_fix 'a@b'
34
+ end
35
+ end