alias 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,39 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper.rb')
2
+
3
+ class Alias::Creators::AnyToInstanceMethodCreatorTest < Test::Unit::TestCase
4
+ context "AnyToInstanceMethodCreator" do
5
+ before(:each) { @manager = Alias::Manager.new }
6
+
7
+ def expect_aliases(hash)
8
+ arr = Alias::Creators::AnyToInstanceMethodCreator.maps_config(hash)
9
+ Alias::Creators::AnyToInstanceMethodCreator.expects(:generates_aliases).with(arr).returns('')
10
+ end
11
+
12
+ def create_aliases(hash)
13
+ @manager.create_aliases(:any_to_instance_method, hash)
14
+ end
15
+
16
+ test "deletes invalid classes" do
17
+ expect_aliases 'String'=>{'String.to_s'=>'s'}
18
+ create_aliases 'String'=>{'String.to_s'=>'s'}, 'AnotherString'=>{'String.to_s'=>'s'}
19
+ end
20
+
21
+ test "deletes existing method aliases" do
22
+ expect_aliases 'String'=>{'Date.commercial'=>'s'}
23
+ create_aliases 'String'=>{'Date.civil'=>'strip', 'Date.commercial'=>'s'}
24
+ end
25
+
26
+ test "creates aliases" do
27
+ Kernel.eval %[
28
+ class ::SomeClass
29
+ def self.cap; 'itup'; end
30
+ end
31
+ module ::SomeModule; end
32
+ ]
33
+ create_aliases 'SomeModule'=>{'SomeClass.cap.to_sym'=>'c', 'SomeClass.cap.gsub'=>'gsub'}
34
+ obj = Object.new.extend SomeModule
35
+ SomeClass.cap.to_sym.should == obj.c
36
+ SomeClass.cap.gsub('cap','smack') == obj.gsub('cap','smack')
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,42 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper.rb')
2
+
3
+ class Alias::Creators::ClassMethodCreatorTest < Test::Unit::TestCase
4
+
5
+ context "ClassMethodCreator" do
6
+ before(:each) { @manager = Alias::Manager.new }
7
+ def expect_aliases(hash)
8
+ arr = Alias::Creators::ClassMethodCreator.maps_config(hash)
9
+ Alias::Creators::ClassMethodCreator.expects(:generates_aliases).with(arr).returns('')
10
+ end
11
+
12
+ def create_aliases(hash)
13
+ @manager.create_aliases(:class_method, hash)
14
+ end
15
+
16
+ test "deletes invalid class method keys" do
17
+ expect_aliases "Array"=>{}, "String"=>{'yaml_new'=>'yn'}
18
+ create_aliases 'String'=>{'yaml_new'=>'yn'},'Array'=>{'blah'=>'bl'}
19
+ end
20
+
21
+ test "deletes invalid classes" do
22
+ expect_aliases "String"=>{'yaml_new'=>'yn'}
23
+ create_aliases 'String'=>{'yaml_new'=>'yn'},'Blah'=>{'new'=>'n'}
24
+ end
25
+
26
+ test "deletes existing class method aliases" do
27
+ expect_aliases 'Date'=>{'valid_time?'=>'vt'}
28
+ create_aliases 'Date'=>{'civil_to_jd'=>'civil', 'valid_time?'=>'vt'}
29
+ end
30
+
31
+ test "creates class method aliases" do
32
+ Kernel.eval %[
33
+ class ::SampleClass
34
+ def self.cap; 'itup'; end
35
+ end
36
+ ]
37
+ hash1 = {'SampleClass'=>{:cap=>:capohow}, 'Array'=>{:blah=>:bl}}
38
+ @manager.create_aliases(:class_method, hash1)
39
+ SampleClass.capohow.should == SampleClass.cap
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,48 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper.rb')
2
+
3
+ class Alias::Creators::ClassToInstanceMethodCreatorTest < Test::Unit::TestCase
4
+ context "ClassToInstanceMethodCreator" do
5
+ before(:each) { @manager = Alias::Manager.new }
6
+
7
+ def expect_aliases(hash)
8
+ arr = Alias::Creators::ClassToInstanceMethodCreator.maps_config(hash)
9
+ Alias::Creators::ClassToInstanceMethodCreator.expects(:generates_aliases).with(arr).returns('')
10
+ end
11
+
12
+ def create_aliases(hash)
13
+ @manager.create_aliases(:class_to_instance_method, hash)
14
+ end
15
+
16
+ test "deletes invalid to classes" do
17
+ expect_aliases 'String'=>{'String.to_s'=>'s'}
18
+ create_aliases 'String'=>{'AnotherString.name'=>'n', 'String.to_s'=>'s'}
19
+ end
20
+
21
+ test "deletes invalid classes" do
22
+ expect_aliases 'String'=>{'String.to_s'=>'s'}
23
+ create_aliases 'String'=>{'String.to_s'=>'s'}, 'AnotherString'=>{'String.to_s'=>'s'}
24
+ end
25
+
26
+ test "deletes existing method aliases" do
27
+ expect_aliases 'String'=>{'Date.commercial'=>'s'}
28
+ create_aliases 'String'=>{'Date.civil'=>'strip', 'Date.commercial'=>'s'}
29
+ end
30
+
31
+ test "deletes invalid to methods" do
32
+ expect_aliases 'String'=>{'Date.civil'=>'c'}
33
+ create_aliases 'String'=>{'Date.civil'=>'c', 'Date.uncivil'=>'uc'}
34
+ end
35
+
36
+ test "creates aliases" do
37
+ Kernel.eval %[
38
+ class ::SampleClass
39
+ def self.cap; 'itup'; end
40
+ end
41
+ module ::SampleModule; end
42
+ ]
43
+ create_aliases 'SampleModule'=>{'SampleClass.cap'=>'c', 'Sampleclass.dap'=>'d'}
44
+ obj = Object.new.extend SampleModule
45
+ SampleClass.cap.should == obj.c
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,60 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper.rb')
2
+
3
+ class Alias::ConsoleTest < Test::Unit::TestCase
4
+ before(:all) { @console = Object.new.extend(Alias::Console) }
5
+
6
+ test "create_aliases doesn't save failed alias creation" do
7
+ capture_stderr { @console.create_aliases :blah, {} }
8
+ Alias.manager.instance_eval("@created_aliases").should == {}
9
+ end
10
+
11
+ test "create_aliases takes partial creator names" do
12
+ Alias.manager.expects(:create_aliases).with(:instance_method, {}, anything)
13
+ @console.create_aliases(:in, {})
14
+ end
15
+
16
+ test "search_aliases lists aliases if given nothing" do
17
+ Alias.manager.expects(:all_aliases)
18
+ @console.search_aliases
19
+ end
20
+
21
+ context "save_aliases" do
22
+ before(:all) { eval "module ::Bluh; def blah; end; end" }
23
+ before(:each) { Alias.manager.instance_eval("@created_aliases = nil") }
24
+
25
+ test "saves created aliases" do
26
+ hash = {"Bluh"=>{"blah"=>"bl"}}
27
+ @console.create_aliases :instance_method, hash
28
+ File.expects(:exists?).returns(false)
29
+ Alias.expects(:save_to_file).with("#{ENV['HOME']}/.alias.yml", {:aliases=>{:instance_method=>hash}}.to_yaml)
30
+ capture_stdout { @console.save_aliases }.should =~ /Save/
31
+ end
32
+
33
+ test "saves to given file" do
34
+ hash = {"Bluh"=>{"blah"=>"b"}}
35
+ @console.create_aliases :instance_method, hash
36
+ Alias.expects(:save_to_file).with("explicit", {:aliases=>{:instance_method=>hash}}.to_yaml)
37
+ capture_stdout { @console.save_aliases('explicit') }.should =~ /explicit/
38
+ end
39
+
40
+ test "prints message if nothing to save" do
41
+ capture_stdout { @console.save_aliases }.should =~ /Didn't save/
42
+ end
43
+
44
+ test "saves aliases to config/alias.yml if config/ exists" do
45
+ File.expects(:directory?).returns(true)
46
+ @console.create_aliases :instance_method, {"Bluh"=>{"blah"=>"b2"}}
47
+ Alias.expects(:save_to_file).with("config/alias.yml", anything)
48
+ capture_stdout { @console.save_aliases }
49
+ end
50
+
51
+ test "merges existing aliases with created aliases" do
52
+ hash = {"Bluh"=>{"blah"=>"b3"}}
53
+ @console.create_aliases :instance_method, hash
54
+ Alias.expects(:read_config_file).returns({:aliases=>{:instance_method=>{"Bluh"=>{"bling"=>"bl"}}}})
55
+ Alias.expects(:save_to_file).with("#{ENV['HOME']}/.alias.yml",
56
+ {:aliases=>{:instance_method=>{"Bluh"=>{'bling'=>'bl', 'blah'=>'b3'}}}}.to_yaml)
57
+ capture_stdout { @console.save_aliases }
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,38 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper.rb')
2
+
3
+ class Alias::Creators::ConstantCreatorTest < Test::Unit::TestCase
4
+ context "AliasConstantCreator" do
5
+ before(:each) { @manager = Alias::Manager.new }
6
+
7
+ def create_aliases(hash, options={})
8
+ @manager.create_aliases(:constant, hash, options)
9
+ end
10
+
11
+ def expect_aliases(hash)
12
+ arr = Alias::Creators::ConstantCreator.maps_config(hash)
13
+ Alias::Creators::ConstantCreator.expects(:generates_aliases).with(arr).returns('')
14
+ end
15
+
16
+ test "deletes existing aliases" do
17
+ expect_aliases "Array"=>'Ar'
18
+ create_aliases "Alias::Creators::ConstantCreator"=>"Alias::Creator", "Array"=>"Ar"
19
+ end
20
+
21
+ test "deletes existing aliases unless force option" do
22
+ h1 = {"Alias::Creators::ConstantCreator"=>"Alias::Creator", "Array"=>"Ar"}
23
+ expect_aliases h1
24
+ create_aliases h1, :force=>true
25
+ end
26
+
27
+ test "deletes invalid classes" do
28
+ eval "module ::Bling; end"
29
+ expect_aliases 'Array'=>'Ar'
30
+ create_aliases "Blah"=>"Bling", "Array"=>"Ar"
31
+ end
32
+
33
+ test "creates aliases" do
34
+ create_aliases 'Time'=>'T'
35
+ ::Time.should == ::T
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,29 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper.rb')
2
+
3
+ module Alias
4
+ class CreatorTest < Test::Unit::TestCase
5
+ context "invalid creator" do
6
+ before(:all) { eval "class Alias::TestCreator < Alias::Creator; end"}
7
+ test "raises AbstractMethodError if map not defined" do
8
+ assert_raises(Creator::AbstractMethodError) {
9
+ TestCreator.maps_config({})
10
+ }
11
+ end
12
+
13
+ test "raises AbstractMethodError if generate not defined" do
14
+ assert_raises(Creator::AbstractMethodError) {
15
+ TestCreator.generates_aliases([])
16
+ }
17
+ end
18
+
19
+ test "raises ArgumentError when no validator is given" do
20
+ assert_raises(ArgumentError) { TestCreator.valid :name }
21
+ end
22
+
23
+ test "prints error and deletes validator when invalid one is given" do
24
+ capture_stderr { TestCreator.valid(:name, :if=>:blah) }.should =~ /not set/
25
+ TestCreator.validators[:name].should == nil
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,41 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper.rb')
2
+
3
+ class Alias::Creators::InstanceMethodCreatorTest < Test::Unit::TestCase
4
+ context "InstanceMethodCreator" do
5
+ before(:each) { @manager = Alias::Manager.new }
6
+ def expect_aliases(hash)
7
+ arr = Alias::Creators::InstanceMethodCreator.maps_config(hash)
8
+ Alias::Creators::InstanceMethodCreator.expects(:generates_aliases).with(arr).returns('')
9
+ end
10
+
11
+ def create_aliases(hash)
12
+ @manager.create_aliases(:instance_method, hash)
13
+ end
14
+
15
+ test "deletes existing instance method aliases" do
16
+ expect_aliases "String"=>{"strip"=>"st"}
17
+ create_aliases 'String'=>{'strip'=>'st', 'chomp'=>'chop'}
18
+ end
19
+
20
+ test "deletes invalid classes" do
21
+ expect_aliases "String"=>{'strip','st'}
22
+ create_aliases "String"=>{'strip','st'}, 'Blah'=>{'map'=>'m'}
23
+ end
24
+
25
+ test "deletes invalid instance method keys" do
26
+ expect_aliases "Array"=>{}, "String"=>{"strip"=>"st"}
27
+ create_aliases 'String'=>{'strip'=>'st'},'Array'=>{'blah', 'bl'}
28
+ end
29
+
30
+ test "creates aliases" do
31
+ Kernel.eval %[
32
+ class ::SampleClass
33
+ def whoop; 'WHOOP'; end
34
+ end
35
+ ]
36
+ obj = SampleClass.new
37
+ create_aliases 'SampleClass'=>{:whoop=>:can_of_wass, :blah=>:bl}
38
+ SampleClass.new.whoop.should == SampleClass.new.can_of_wass
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,105 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper.rb')
2
+
3
+ class Alias::ManagerTest < Test::Unit::TestCase
4
+ before(:each) { @manager = Alias::Manager.new}
5
+
6
+ context "create_aliases" do
7
+ before(:all) { eval %[class Alias::Creators::ValidTestCreator < Alias::Creator; map { [] }; generate { ' '}; end]}
8
+ def create_aliases(options={})
9
+ @manager.create_aliases :valid_test, {}, options
10
+ end
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
104
+ end
105
+ end
@@ -0,0 +1,31 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'context' #gem install jeremymcanally-context -s http://gems.github.com
4
+ require 'mocha' #gem install mocha
5
+ require 'matchy' #gem install jeremymcanally-matchy -s http://gems.github.com
6
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
7
+ require 'alias'
8
+
9
+ class Test::Unit::TestCase
10
+ def capture_stdout(&block)
11
+ original_stdout = $stdout
12
+ $stdout = fake = StringIO.new
13
+ begin
14
+ yield
15
+ ensure
16
+ $stdout = original_stdout
17
+ end
18
+ fake.string
19
+ end
20
+
21
+ def capture_stderr(&block)
22
+ original_stderr = $stderr
23
+ $stderr = fake = StringIO.new
24
+ begin
25
+ yield
26
+ ensure
27
+ $stderr = original_stderr
28
+ end
29
+ fake.string
30
+ end
31
+ end
@@ -0,0 +1,42 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper.rb')
2
+
3
+ module Alias
4
+ class UtilTest < Test::Unit::TestCase
5
+ test "any_const_get fetches simple class" do
6
+ Util.any_const_get("Array").should == Array
7
+ end
8
+
9
+ test "any_const_get fetches nested class" do
10
+ eval "module ::Somemodule; class Someclass; end; end"
11
+ Util.any_const_get("Somemodule::Someclass").should == Somemodule::Someclass
12
+ end
13
+
14
+ test "any_const_get returns nil for nonexistent class" do
15
+ Util.any_const_get("NonexistentClass").should == nil
16
+ end
17
+
18
+ test "slice only returns valid keys given" do
19
+ Util.slice({:a=>1,:b=>2}, :a, :c).should == {:a=>1}
20
+ end
21
+
22
+ test "slice_off! returns given keys but takes them off existing hash" do
23
+ h = {:a=>1, :b=>2}
24
+ Util.slice_off!(h, :a, :c).should == {:a=>1}
25
+ h.should == {:b=>2}
26
+ end
27
+
28
+ test "camelize should uppercase non-underscored string" do
29
+ Util.camelize('man').should == 'Man'
30
+ end
31
+
32
+ test "camelize should camelize underscored string" do
33
+ Util.camelize('some_test').should == 'SomeTest'
34
+ end
35
+
36
+ test "make_shortest_aliases" do
37
+ eval "::Y = 'some value'"
38
+ expected_hash = {"Yo"=>"Y", "Man"=>"M", "Cool"=>"C", 'Yay'=>'Ya'}
39
+ Util.make_shortest_aliases(['Yo','Yay','Cool','Man']).should == expected_hash
40
+ end
41
+ end
42
+ end