alias 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,60 +1,63 @@
1
1
  require File.join(File.dirname(__FILE__), 'test_helper.rb')
2
2
 
3
- class Alias::ConsoleTest < Test::Unit::TestCase
4
- before(:all) { @console = Object.new.extend(Alias::Console) }
3
+ describe "Console" do
4
+ before { @console = nil }
5
+ def console
6
+ console ||= Object.new.extend(Console)
7
+ end
5
8
 
6
- test "create_aliases doesn't save failed alias creation" do
7
- capture_stderr { @console.create_aliases :blah, {} }
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
- test "create_aliases takes partial creator names" do
14
+ it "create_aliases takes partial creator names" do
12
15
  Alias.manager.expects(:create_aliases).with(:instance_method, {}, anything)
13
- @console.create_aliases(:in, {})
16
+ console.create_aliases(:in, {})
14
17
  end
15
18
 
16
- test "search_aliases lists aliases if given nothing" do
19
+ it "search_aliases lists aliases if given nothing" do
17
20
  Alias.manager.expects(:all_aliases)
18
- @console.search_aliases
21
+ console.search_aliases
19
22
  end
20
23
 
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
+ 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
- test "saves created aliases" do
28
+ it "saves created aliases" do
26
29
  hash = {"Bluh"=>{"blah"=>"bl"}}
27
- @console.create_aliases :instance_method, hash
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 { @console.save_aliases }.should =~ /Save/
33
+ capture_stdout { console.save_aliases }.should =~ /Save/
31
34
  end
32
35
 
33
- test "saves to given file" do
36
+ it "saves to given file" do
34
37
  hash = {"Bluh"=>{"blah"=>"b"}}
35
- @console.create_aliases :instance_method, hash
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 { @console.save_aliases('explicit') }.should =~ /explicit/
40
+ capture_stdout { console.save_aliases('explicit') }.should =~ /explicit/
38
41
  end
39
42
 
40
- test "prints message if nothing to save" do
41
- capture_stdout { @console.save_aliases }.should =~ /Didn't save/
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
- test "saves aliases to config/alias.yml if config/ exists" do
47
+ it "saves aliases to config/alias.yml if config/ exists" do
45
48
  File.expects(:directory?).returns(true)
46
- @console.create_aliases :instance_method, {"Bluh"=>{"blah"=>"b2"}}
49
+ console.create_aliases :instance_method, {"Bluh"=>{"blah"=>"b2"}}
47
50
  Alias.expects(:save_to_file).with("config/alias.yml", anything)
48
- capture_stdout { @console.save_aliases }
51
+ capture_stdout { console.save_aliases }
49
52
  end
50
53
 
51
- test "merges existing aliases with created aliases" do
54
+ it "merges existing aliases with created aliases" do
52
55
  hash = {"Bluh"=>{"blah"=>"b3"}}
53
- @console.create_aliases :instance_method, hash
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 { @console.save_aliases }
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
- class Alias::Creators::ConstantCreatorTest < Test::Unit::TestCase
4
- context "AliasConstantCreator" do
5
- before(:each) { @manager = Alias::Manager.new }
3
+ describe "AliasConstantCreator" do
4
+ before { @manager = Manager.new }
6
5
 
7
- def create_aliases(hash, options={})
8
- @manager.create_aliases(:constant, hash, options)
9
- end
6
+ def create_aliases(hash, options={})
7
+ @manager.create_aliases(:constant, hash, options)
8
+ end
10
9
 
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
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
- 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
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
- test "deletes invalid classes" do
28
- eval "module ::Bling; end"
29
- expect_aliases 'Array'=>'Ar'
30
- create_aliases "Blah"=>"Bling", "Array"=>"Ar"
31
- end
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
- test "creates aliases" do
34
- create_aliases 'Time'=>'T'
35
- ::Time.should == ::T
36
- end
32
+ it "creates aliases" do
33
+ create_aliases 'Time'=>'T'
34
+ ::Time.should == ::T
37
35
  end
38
36
  end
@@ -1,29 +1,25 @@
1
1
  require File.join(File.dirname(__FILE__), 'test_helper.rb')
2
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
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
- test "raises AbstractMethodError if generate not defined" do
14
- assert_raises(Creator::AbstractMethodError) {
15
- TestCreator.generates_aliases([])
16
- }
17
- end
11
+ it "raises AbstractMethodError if generate not defined" do
12
+ should.raise(Creator::AbstractMethodError) {
13
+ TestCreator.generates_aliases([])
14
+ }
15
+ end
18
16
 
19
- test "raises ArgumentError when no validator is given" do
20
- assert_raises(ArgumentError) { TestCreator.valid :name }
21
- end
17
+ it "raises ArgumentError when no validator is given" do
18
+ should.raise(ArgumentError) { TestCreator.valid :name }
19
+ end
22
20
 
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
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
- 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
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
- 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
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
- test "deletes invalid classes" do
21
- expect_aliases "String"=>{'strip','st'}
22
- create_aliases "String"=>{'strip','st'}, 'Blah'=>{'map'=>'m'}
23
- end
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
- 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
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
- 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
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
@@ -1,105 +1,106 @@
1
1
  require File.join(File.dirname(__FILE__), 'test_helper.rb')
2
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
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