alias 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +5 -0
- data/README.rdoc +5 -1
- data/Rakefile +24 -39
- data/gemspec +20 -0
- data/lib/alias.rb +1 -2
- data/lib/alias/version.rb +3 -0
- data/test/alias_test.rb +52 -54
- data/test/any_to_instance_method_creator_test.rb +28 -30
- data/test/class_method_creator_test.rb +31 -34
- data/test/class_to_instance_method_creator_test.rb +42 -44
- data/test/console_test.rb +29 -26
- data/test/constant_creator_test.rb +27 -29
- data/test/creator_test.rb +19 -23
- data/test/instance_method_creator_test.rb +31 -33
- data/test/manager_test.rb +102 -101
- data/test/test_helper.rb +7 -7
- data/test/util_test.rb +36 -38
- data/test/validator_test.rb +54 -56
- metadata +70 -31
- data/VERSION.yml +0 -4
data/test/console_test.rb
CHANGED
@@ -1,60 +1,63 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), 'test_helper.rb')
|
2
2
|
|
3
|
-
|
4
|
-
before
|
3
|
+
describe "Console" do
|
4
|
+
before { @console = nil }
|
5
|
+
def console
|
6
|
+
console ||= Object.new.extend(Console)
|
7
|
+
end
|
5
8
|
|
6
|
-
|
7
|
-
capture_stderr {
|
9
|
+
it "create_aliases doesn't save failed alias creation" do
|
10
|
+
capture_stderr { console.create_aliases :blah, {} }
|
8
11
|
Alias.manager.instance_eval("@created_aliases").should == {}
|
9
12
|
end
|
10
13
|
|
11
|
-
|
14
|
+
it "create_aliases takes partial creator names" do
|
12
15
|
Alias.manager.expects(:create_aliases).with(:instance_method, {}, anything)
|
13
|
-
|
16
|
+
console.create_aliases(:in, {})
|
14
17
|
end
|
15
18
|
|
16
|
-
|
19
|
+
it "search_aliases lists aliases if given nothing" do
|
17
20
|
Alias.manager.expects(:all_aliases)
|
18
|
-
|
21
|
+
console.search_aliases
|
19
22
|
end
|
20
23
|
|
21
|
-
|
22
|
-
|
23
|
-
before
|
24
|
+
describe "save_aliases" do
|
25
|
+
before_all { eval "module ::Bluh; def blah; end; end" }
|
26
|
+
before { Alias.manager.instance_eval("@created_aliases = nil") }
|
24
27
|
|
25
|
-
|
28
|
+
it "saves created aliases" do
|
26
29
|
hash = {"Bluh"=>{"blah"=>"bl"}}
|
27
|
-
|
30
|
+
console.create_aliases :instance_method, hash
|
28
31
|
File.expects(:exists?).returns(false)
|
29
32
|
Alias.expects(:save_to_file).with("#{ENV['HOME']}/.alias.yml", {:aliases=>{:instance_method=>hash}}.to_yaml)
|
30
|
-
capture_stdout {
|
33
|
+
capture_stdout { console.save_aliases }.should =~ /Save/
|
31
34
|
end
|
32
35
|
|
33
|
-
|
36
|
+
it "saves to given file" do
|
34
37
|
hash = {"Bluh"=>{"blah"=>"b"}}
|
35
|
-
|
38
|
+
console.create_aliases :instance_method, hash
|
36
39
|
Alias.expects(:save_to_file).with("explicit", {:aliases=>{:instance_method=>hash}}.to_yaml)
|
37
|
-
capture_stdout {
|
40
|
+
capture_stdout { console.save_aliases('explicit') }.should =~ /explicit/
|
38
41
|
end
|
39
42
|
|
40
|
-
|
41
|
-
capture_stdout {
|
43
|
+
it "prints message if nothing to save" do
|
44
|
+
capture_stdout { console.save_aliases }.should =~ /Didn't save/
|
42
45
|
end
|
43
46
|
|
44
|
-
|
47
|
+
it "saves aliases to config/alias.yml if config/ exists" do
|
45
48
|
File.expects(:directory?).returns(true)
|
46
|
-
|
49
|
+
console.create_aliases :instance_method, {"Bluh"=>{"blah"=>"b2"}}
|
47
50
|
Alias.expects(:save_to_file).with("config/alias.yml", anything)
|
48
|
-
capture_stdout {
|
51
|
+
capture_stdout { console.save_aliases }
|
49
52
|
end
|
50
53
|
|
51
|
-
|
54
|
+
it "merges existing aliases with created aliases" do
|
52
55
|
hash = {"Bluh"=>{"blah"=>"b3"}}
|
53
|
-
|
56
|
+
console.create_aliases :instance_method, hash
|
54
57
|
Alias.expects(:read_config_file).returns({:aliases=>{:instance_method=>{"Bluh"=>{"bling"=>"bl"}}}})
|
55
58
|
Alias.expects(:save_to_file).with("#{ENV['HOME']}/.alias.yml",
|
56
59
|
{:aliases=>{:instance_method=>{"Bluh"=>{'bling'=>'bl', 'blah'=>'b3'}}}}.to_yaml)
|
57
|
-
capture_stdout {
|
60
|
+
capture_stdout { console.save_aliases }
|
58
61
|
end
|
59
62
|
end
|
60
|
-
end
|
63
|
+
end
|
@@ -1,38 +1,36 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), 'test_helper.rb')
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
before(:each) { @manager = Alias::Manager.new }
|
3
|
+
describe "AliasConstantCreator" do
|
4
|
+
before { @manager = Manager.new }
|
6
5
|
|
7
|
-
|
8
|
-
|
9
|
-
|
6
|
+
def create_aliases(hash, options={})
|
7
|
+
@manager.create_aliases(:constant, hash, options)
|
8
|
+
end
|
10
9
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
10
|
+
def expect_aliases(hash)
|
11
|
+
arr = Creators::ConstantCreator.maps_config(hash)
|
12
|
+
Creators::ConstantCreator.expects(:generates_aliases).with(arr).returns('')
|
13
|
+
end
|
14
|
+
|
15
|
+
it "deletes existing aliases" do
|
16
|
+
expect_aliases "Array"=>'Ar'
|
17
|
+
create_aliases "Alias::Creators::ConstantCreator"=>"Alias::Creator", "Array"=>"Ar"
|
18
|
+
end
|
20
19
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
20
|
+
it "deletes existing aliases unless force option" do
|
21
|
+
h1 = {"Alias::Creators::ConstantCreator"=>"Alias::Creator", "Array"=>"Ar"}
|
22
|
+
expect_aliases h1
|
23
|
+
create_aliases h1, :force=>true
|
24
|
+
end
|
26
25
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
26
|
+
it "deletes invalid classes" do
|
27
|
+
eval "module ::Bling; end"
|
28
|
+
expect_aliases 'Array'=>'Ar'
|
29
|
+
create_aliases "Blah"=>"Bling", "Array"=>"Ar"
|
30
|
+
end
|
32
31
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
end
|
32
|
+
it "creates aliases" do
|
33
|
+
create_aliases 'Time'=>'T'
|
34
|
+
::Time.should == ::T
|
37
35
|
end
|
38
36
|
end
|
data/test/creator_test.rb
CHANGED
@@ -1,29 +1,25 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), 'test_helper.rb')
|
2
2
|
|
3
|
-
|
4
|
-
class
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
}
|
11
|
-
end
|
3
|
+
describe "invalid creator" do
|
4
|
+
before_all { eval "class Alias::TestCreator < Alias::Creator; end"}
|
5
|
+
it "raises AbstractMethodError if map not defined" do
|
6
|
+
should.raise(Creator::AbstractMethodError) {
|
7
|
+
TestCreator.maps_config({})
|
8
|
+
}
|
9
|
+
end
|
12
10
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
11
|
+
it "raises AbstractMethodError if generate not defined" do
|
12
|
+
should.raise(Creator::AbstractMethodError) {
|
13
|
+
TestCreator.generates_aliases([])
|
14
|
+
}
|
15
|
+
end
|
18
16
|
|
19
|
-
|
20
|
-
|
21
|
-
|
17
|
+
it "raises ArgumentError when no validator is given" do
|
18
|
+
should.raise(ArgumentError) { TestCreator.valid :name }
|
19
|
+
end
|
22
20
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
end
|
27
|
-
end
|
21
|
+
it "prints error and deletes validator when invalid one is given" do
|
22
|
+
capture_stderr { TestCreator.valid(:name, :if=>:blah) }.should =~ /not set/
|
23
|
+
TestCreator.validators[:name].should == nil
|
28
24
|
end
|
29
|
-
end
|
25
|
+
end
|
@@ -1,41 +1,39 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), 'test_helper.rb')
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
end
|
3
|
+
describe "InstanceMethodCreator" do
|
4
|
+
before { @manager = Manager.new }
|
5
|
+
def expect_aliases(hash)
|
6
|
+
arr = Creators::InstanceMethodCreator.maps_config(hash)
|
7
|
+
Creators::InstanceMethodCreator.expects(:generates_aliases).with(arr).returns('')
|
8
|
+
end
|
10
9
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
10
|
+
def create_aliases(hash)
|
11
|
+
@manager.create_aliases(:instance_method, hash)
|
12
|
+
end
|
13
|
+
|
14
|
+
it "deletes existing instance method aliases" do
|
15
|
+
expect_aliases "String"=>{"strip"=>"st"}
|
16
|
+
create_aliases 'String'=>{'strip'=>'st', 'chomp'=>'chop'}
|
17
|
+
end
|
19
18
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
19
|
+
it "deletes invalid classes" do
|
20
|
+
expect_aliases "String"=>{'strip'=>'st'}
|
21
|
+
create_aliases "String"=>{'strip'=>'st'}, 'Blah'=>{'map'=>'m'}
|
22
|
+
end
|
24
23
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
24
|
+
it "deletes invalid instance method keys" do
|
25
|
+
expect_aliases "Array"=>{}, "String"=>{"strip"=>"st"}
|
26
|
+
create_aliases 'String'=>{'strip'=>'st'},'Array'=>{'blah'=>'bl'}
|
27
|
+
end
|
29
28
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
end
|
29
|
+
it "creates aliases" do
|
30
|
+
Kernel.eval %[
|
31
|
+
class ::SampleClass
|
32
|
+
def whoop; 'WHOOP'; end
|
33
|
+
end
|
34
|
+
]
|
35
|
+
obj = SampleClass.new
|
36
|
+
create_aliases 'SampleClass'=>{:whoop=>:can_of_wass, :blah=>:bl}
|
37
|
+
SampleClass.new.whoop.should == SampleClass.new.can_of_wass
|
40
38
|
end
|
41
39
|
end
|
data/test/manager_test.rb
CHANGED
@@ -1,105 +1,106 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), 'test_helper.rb')
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
test "creates aliases" do
|
13
|
-
Kernel.expects(:eval).with(' ')
|
14
|
-
create_aliases
|
15
|
-
end
|
16
|
-
|
17
|
-
test "doesn't create aliases with pretend option" do
|
18
|
-
Kernel.expects(:eval).never
|
19
|
-
capture_stdout { create_aliases :pretend=>true }.should == "\n \n"
|
20
|
-
end
|
21
|
-
|
22
|
-
test "with manager's verbose sets creator's verbose" do
|
23
|
-
@manager.verbose = true
|
24
|
-
create_aliases
|
25
|
-
@manager.creators[:valid_test].verbose.should == true
|
26
|
-
end
|
27
|
-
|
28
|
-
test "with manager's verbose array sets creator's verbose" do
|
29
|
-
@manager.verbose = [:valid_test]
|
30
|
-
create_aliases
|
31
|
-
@manager.creators[:valid_test].verbose.should == true
|
32
|
-
end
|
33
|
-
|
34
|
-
test "with manager's verbose array doesn't set creator's verbose" do
|
35
|
-
@manager.verbose = [:another]
|
36
|
-
create_aliases
|
37
|
-
@manager.creators[:valid_test].verbose.should == false
|
38
|
-
end
|
39
|
-
|
40
|
-
test "with manager's force sets creator's force" do
|
41
|
-
@manager.force = true
|
42
|
-
create_aliases
|
43
|
-
@manager.creators[:valid_test].force.should == true
|
44
|
-
end
|
45
|
-
|
46
|
-
test "with manager's force array sets creator's force" do
|
47
|
-
@manager.force = [:valid_test]
|
48
|
-
create_aliases
|
49
|
-
@manager.creators[:valid_test].force.should == true
|
50
|
-
end
|
51
|
-
|
52
|
-
test "with manager's force array doesn't set creators force" do
|
53
|
-
@manager.force = [:another]
|
54
|
-
create_aliases
|
55
|
-
@manager.creators[:valid_test].force.should == false
|
56
|
-
end
|
57
|
-
|
58
|
-
test "force option sets force in creator object" do
|
59
|
-
create_aliases :force=>true
|
60
|
-
@manager.creators[:valid_test].force.should == true
|
61
|
-
end
|
62
|
-
|
63
|
-
test "verbose option sets verbose in creator object" do
|
64
|
-
create_aliases :verbose=>true
|
65
|
-
@manager.creators[:valid_test].verbose.should == true
|
66
|
-
end
|
67
|
-
|
68
|
-
test "prints error if nonexistent creator given" do
|
69
|
-
capture_stderr {@manager.create_aliases :blah, {} }.should =~ /not found/
|
70
|
-
end
|
71
|
-
|
72
|
-
test "prints error if necessary creator methods not defined" do
|
73
|
-
eval "class Alias::Creators::BlingCreator < Alias::Creator; end"
|
74
|
-
capture_stderr { @manager.create_aliases :bling, {} }.should =~ /BlingCreator/
|
75
|
-
end
|
76
|
-
|
77
|
-
test "prints error if aliases fail to create" do
|
78
|
-
eval "class Alias::Creators::Bling2Creator < Alias::Creator; map {[]}; generate { 'blah' }; end"
|
79
|
-
capture_stderr { @manager.create_aliases :bling2, {} }.should =~ /failed to create aliases/
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
context "search" do
|
84
|
-
before(:each) {
|
85
|
-
@manager.instance_variable_set "@creators", {:constant=>Alias::Creators::ConstantCreator.new}
|
86
|
-
@manager.stubs(:all_aliases).returns([{:name=>'Array', :alias=>'A'}, {:name=>'Abbrev', :alias=>'B'}])
|
87
|
-
}
|
88
|
-
|
89
|
-
test "key and symbol value" do
|
90
|
-
@manager.search(:name=>:Array).should == [{:name=>'Array', :alias=>'A'}]
|
91
|
-
end
|
92
|
-
|
93
|
-
test "with key and string value" do
|
94
|
-
@manager.search(:name=>'A').should == [{:name=>'Array', :alias=>'A'}, {:name=>'Abbrev', :alias=>'B'}]
|
95
|
-
end
|
96
|
-
|
97
|
-
test "with a string" do
|
98
|
-
@manager.search('Array').should == [{:name=>'Array', :alias=>'A'}]
|
99
|
-
end
|
100
|
-
|
101
|
-
test "with multiple search terms" do
|
102
|
-
@manager.search(:name=>'A', :alias=>'A').should == [{:name=>'Array', :alias=>'A'}]
|
103
|
-
end
|
3
|
+
describe "Manager" do
|
4
|
+
before { @manager = nil }
|
5
|
+
def manager; @manager ||= Manager.new; end
|
6
|
+
|
7
|
+
describe "create_aliases" do
|
8
|
+
before_all { eval %[class Alias::Creators::ValidTestCreator < Alias::Creator; map { [] }; generate { ' '}; end]}
|
9
|
+
def create_aliases(options={})
|
10
|
+
manager.create_aliases :valid_test, {}, options
|
104
11
|
end
|
12
|
+
|
13
|
+
it "creates aliases" do
|
14
|
+
Kernel.expects(:eval).with(' ')
|
15
|
+
create_aliases
|
16
|
+
end
|
17
|
+
|
18
|
+
it "doesn't create aliases with pretend option" do
|
19
|
+
Kernel.expects(:eval).never
|
20
|
+
capture_stdout { create_aliases :pretend=>true }.should == "\n \n"
|
21
|
+
end
|
22
|
+
|
23
|
+
it "with manager's verbose sets creator's verbose" do
|
24
|
+
manager.verbose = true
|
25
|
+
create_aliases
|
26
|
+
manager.creators[:valid_test].verbose.should == true
|
27
|
+
end
|
28
|
+
|
29
|
+
it "with manager's verbose array sets creator's verbose" do
|
30
|
+
manager.verbose = [:valid_test]
|
31
|
+
create_aliases
|
32
|
+
manager.creators[:valid_test].verbose.should == true
|
33
|
+
end
|
34
|
+
|
35
|
+
it "with manager's verbose array doesn't set creator's verbose" do
|
36
|
+
manager.verbose = [:another]
|
37
|
+
create_aliases
|
38
|
+
manager.creators[:valid_test].verbose.should == false
|
39
|
+
end
|
40
|
+
|
41
|
+
it "with manager's force sets creator's force" do
|
42
|
+
manager.force = true
|
43
|
+
create_aliases
|
44
|
+
manager.creators[:valid_test].force.should == true
|
45
|
+
end
|
46
|
+
|
47
|
+
it "with manager's force array sets creator's force" do
|
48
|
+
manager.force = [:valid_test]
|
49
|
+
create_aliases
|
50
|
+
manager.creators[:valid_test].force.should == true
|
51
|
+
end
|
52
|
+
|
53
|
+
it "with manager's force array doesn't set creators force" do
|
54
|
+
manager.force = [:another]
|
55
|
+
create_aliases
|
56
|
+
manager.creators[:valid_test].force.should == false
|
57
|
+
end
|
58
|
+
|
59
|
+
it "force option sets force in creator object" do
|
60
|
+
create_aliases :force=>true
|
61
|
+
manager.creators[:valid_test].force.should == true
|
62
|
+
end
|
63
|
+
|
64
|
+
it "verbose option sets verbose in creator object" do
|
65
|
+
create_aliases :verbose=>true
|
66
|
+
manager.creators[:valid_test].verbose.should == true
|
67
|
+
end
|
68
|
+
|
69
|
+
it "prints error if nonexistent creator given" do
|
70
|
+
capture_stderr {manager.create_aliases :blah, {} }.should =~ /not found/
|
71
|
+
end
|
72
|
+
|
73
|
+
it "prints error if necessary creator methods not defined" do
|
74
|
+
eval "class Alias::Creators::BlingCreator < Alias::Creator; end"
|
75
|
+
capture_stderr { manager.create_aliases :bling, {} }.should =~ /BlingCreator/
|
76
|
+
end
|
77
|
+
|
78
|
+
it "prints error if aliases fail to create" do
|
79
|
+
eval "class Alias::Creators::Bling2Creator < Alias::Creator; map {[]}; generate { 'blah' }; end"
|
80
|
+
capture_stderr { manager.create_aliases :bling2, {} }.should =~ /failed to create aliases/
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
describe "search" do
|
85
|
+
before {
|
86
|
+
manager.instance_variable_set "@creators", {:constant=>Creators::ConstantCreator.new}
|
87
|
+
manager.stubs(:all_aliases).returns([{:name=>'Array', :alias=>'A'}, {:name=>'Abbrev', :alias=>'B'}])
|
88
|
+
}
|
89
|
+
|
90
|
+
it "key and symbol value" do
|
91
|
+
manager.search(:name=>:Array).should == [{:name=>'Array', :alias=>'A'}]
|
92
|
+
end
|
93
|
+
|
94
|
+
it "with key and string value" do
|
95
|
+
manager.search(:name=>'A').should == [{:name=>'Array', :alias=>'A'}, {:name=>'Abbrev', :alias=>'B'}]
|
96
|
+
end
|
97
|
+
|
98
|
+
it "with a string" do
|
99
|
+
manager.search('Array').should == [{:name=>'Array', :alias=>'A'}]
|
100
|
+
end
|
101
|
+
|
102
|
+
it "with multiple search terms" do
|
103
|
+
manager.search(:name=>'A', :alias=>'A').should == [{:name=>'Array', :alias=>'A'}]
|
104
|
+
end
|
105
|
+
end
|
105
106
|
end
|