impromptu 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. data/.document +5 -0
  2. data/.gitignore +21 -0
  3. data/LICENSE +34 -0
  4. data/README.rdoc +76 -0
  5. data/Rakefile +53 -0
  6. data/VERSION +1 -0
  7. data/impromptu.gemspec +106 -0
  8. data/lib/impromptu/autoload.rb +33 -0
  9. data/lib/impromptu/component.rb +124 -0
  10. data/lib/impromptu/component_set.rb +10 -0
  11. data/lib/impromptu/file.rb +220 -0
  12. data/lib/impromptu/folder.rb +151 -0
  13. data/lib/impromptu/impromptu.rb +81 -0
  14. data/lib/impromptu/ordered_set.rb +49 -0
  15. data/lib/impromptu/resource.rb +195 -0
  16. data/lib/impromptu/symbol.rb +43 -0
  17. data/lib/impromptu.rb +12 -0
  18. data/test/framework/copies/extra_klass2.rb +7 -0
  19. data/test/framework/copies/new_klass.rb +10 -0
  20. data/test/framework/copies/new_unseen.rb +7 -0
  21. data/test/framework/copies/original_klass.rb +10 -0
  22. data/test/framework/ext/extensions/blog.rb +6 -0
  23. data/test/framework/ext/extensions.rb +4 -0
  24. data/test/framework/lib/group/klass2.rb +4 -0
  25. data/test/framework/lib/klass.rb +10 -0
  26. data/test/framework/other/also.rb +8 -0
  27. data/test/framework/other/ignore.rb +2 -0
  28. data/test/framework/other/load.rb +2 -0
  29. data/test/framework/other/two.rb +14 -0
  30. data/test/framework/private/klass.rb +10 -0
  31. data/test/framework/test.components +23 -0
  32. data/test/helper.rb +10 -0
  33. data/test/test_autoload.rb +32 -0
  34. data/test/test_component.rb +133 -0
  35. data/test/test_component_set.rb +20 -0
  36. data/test/test_folder.rb +4 -0
  37. data/test/test_impromptu.rb +43 -0
  38. data/test/test_integration.rb +312 -0
  39. data/test/test_ordered_set.rb +93 -0
  40. data/test/test_resource.rb +186 -0
  41. data/test/test_symbol.rb +99 -0
  42. metadata +139 -0
@@ -0,0 +1,43 @@
1
+ # Extensions to all Symbols to make it easier to deal
2
+ # with namespaced, nested symbol names
3
+ class Symbol
4
+ # True if this symbol contains namespaces (is a nested
5
+ # symbol such as A::B).
6
+ def nested?
7
+ self.to_s.include? '::'
8
+ end
9
+
10
+ # True if this symbol contains no namespaces (is a root
11
+ # symbol such as A, in contrast to to A::B).
12
+ def unnested?
13
+ !self.nested?
14
+ end
15
+
16
+ # Split a symbol into its component names (A::B =>
17
+ # [:A, :B])
18
+ def nested_symbols
19
+ self.to_s.split('::').collect(&:to_sym)
20
+ end
21
+
22
+ # Retrieve the base (end or final) symbol name from this
23
+ # symbol (the Class or Module actually being referred to).
24
+ def base_symbol
25
+ self.nested_symbols.last
26
+ end
27
+
28
+ # Retrieve the root (first) symbol name from this symbol.
29
+ def root_symbol
30
+ self.nested_symbols.first
31
+ end
32
+
33
+ # Iterate through a namespaced symbol by visiting each
34
+ # name in turn, including its parent names. e.g calling
35
+ # on A::B::C would yield :A, :A::B, and :A::B::C
36
+ def each_namespaced_symbol
37
+ self.nested_symbols.inject([]) do |name, symbol|
38
+ name << symbol
39
+ yield name.join('::').to_sym
40
+ name
41
+ end
42
+ end
43
+ end
data/lib/impromptu.rb ADDED
@@ -0,0 +1,12 @@
1
+ require 'pathname'
2
+ require 'set'
3
+
4
+ require 'impromptu/symbol'
5
+ require 'impromptu/ordered_set'
6
+ require 'impromptu/component_set'
7
+ require 'impromptu/resource'
8
+ require 'impromptu/file'
9
+ require 'impromptu/folder'
10
+ require 'impromptu/component'
11
+ require 'impromptu/impromptu'
12
+ require 'impromptu/autoload'
@@ -0,0 +1,7 @@
1
+ module Framework
2
+ class Klass2
3
+ def self.new_method
4
+ true
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ module Framework
2
+ class Klass
3
+ def self.overriden_method
4
+ 3
5
+ end
6
+
7
+ def self.new_method
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,7 @@
1
+ module Framework
2
+ module Unseen
3
+ def self.test_method
4
+ true
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ module Framework
2
+ class Klass
3
+ def self.standard_method
4
+ end
5
+
6
+ def self.overriden_method
7
+ 1
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,6 @@
1
+ module Framework
2
+ module Extensions
3
+ class Blog
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,4 @@
1
+ module Framework
2
+ module Extensions
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Framework
2
+ class Klass2
3
+ end
4
+ end
@@ -0,0 +1,10 @@
1
+ module Framework
2
+ class Klass
3
+ def self.standard_method
4
+ end
5
+
6
+ def self.overriden_method
7
+ 1
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,8 @@
1
+ class OtherName
2
+ def self.overriden_method
3
+ 3
4
+ end
5
+
6
+ def self.one
7
+ end
8
+ end
@@ -0,0 +1,2 @@
1
+ class Ignore
2
+ end
@@ -0,0 +1,2 @@
1
+ class Load
2
+ end
@@ -0,0 +1,14 @@
1
+ module ModOne
2
+ end
3
+
4
+ module ModTwo
5
+ end
6
+
7
+ class OtherName
8
+ def self.overriden_method
9
+ 4
10
+ end
11
+
12
+ def self.two
13
+ end
14
+ end
@@ -0,0 +1,10 @@
1
+ module Framework
2
+ class Klass
3
+ def self.extension_method
4
+ end
5
+
6
+ def self.overriden_method
7
+ 2
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,23 @@
1
+ component 'framework' do
2
+ namespace :Framework
3
+ requires 'mathn'
4
+ folder 'lib', nested_namespaces: false
5
+ end
6
+
7
+ component 'framework.extensions' do
8
+ namespace :Framework
9
+ folder 'ext'
10
+ end
11
+
12
+ component 'other' do
13
+ folder 'other' do
14
+ file 'load.rb'
15
+ file 'also.rb', :provides => :OtherName
16
+ file 'two.rb', :provides => [:ModOne, :ModTwo, :OtherName]
17
+ end
18
+ end
19
+
20
+ component 'private' do
21
+ namespace :Framework
22
+ folder 'private'
23
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'impromptu'
8
+
9
+ class Test::Unit::TestCase
10
+ end
@@ -0,0 +1,32 @@
1
+ require 'helper'
2
+
3
+ class TestAutload < Test::Unit::TestCase
4
+ context "A component with a namespace and no resources" do
5
+ setup do
6
+ Impromptu.reset
7
+ Impromptu.define_components do
8
+ component 'test' do
9
+ namespace :Namespace
10
+ end
11
+ end
12
+ end
13
+
14
+ should "have a single component with a namespace" do
15
+ assert_equal 1, Impromptu.components.size
16
+ assert_equal :Namespace, Impromptu.components['test'].namespace
17
+ assert_not_nil Impromptu.root_resource.child(:Namespace)
18
+ end
19
+
20
+ should "be able to have the namespace loaded using the autoload extension" do
21
+ assert_nothing_raised do
22
+ ::Namespace
23
+ end
24
+ end
25
+
26
+ should "raise an appropriate exception when accessing a non existant resource" do
27
+ assert_raise NameError do
28
+ ::IDontExistSoRaiseAnException
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,133 @@
1
+ require 'helper'
2
+
3
+ class TestComponent < Test::Unit::TestCase
4
+ context "A new component" do
5
+ setup { @component = Impromptu::Component.new(nil, 'component') }
6
+ should "have a default path of the cwd" do
7
+ assert_equal Pathname.new('.').realpath, @component.base_path
8
+ end
9
+
10
+ should "respond to :requires" do
11
+ assert_respond_to @component, :requires
12
+ end
13
+
14
+ should "respond to :namespace" do
15
+ assert_respond_to @component, :namespace
16
+ end
17
+
18
+ should "respond to :folder" do
19
+ assert_respond_to @component, :folder
20
+ end
21
+
22
+ should "have a name" do
23
+ assert_not_nil @component.name
24
+ end
25
+
26
+ should "have no requirements" do
27
+ assert_equal 0, @component.requirements.size
28
+ end
29
+
30
+ should "have no folders" do
31
+ assert_equal 0, @component.folders.size
32
+ end
33
+
34
+ # ----------------------------------------
35
+ # Requirements
36
+ # ----------------------------------------
37
+ context "with two requirements" do
38
+ setup { @component.requires('gem', 'other') }
39
+ should "store two requirements" do
40
+ assert_equal 2, @component.requirements.size
41
+ end
42
+
43
+ context "and with two more, overlapping requirements" do
44
+ setup { @component.requires('another', 'gem') }
45
+ should "only store new requirements" do
46
+ assert_equal 3, @component.requirements.size
47
+ end
48
+ end
49
+ end
50
+
51
+ # ----------------------------------------
52
+ # Namespaces
53
+ # ----------------------------------------
54
+ context "with a namespace" do
55
+ setup { @component.namespace(:Framework) }
56
+ should "have a namespace" do
57
+ assert_equal :Framework, @component.namespace
58
+ end
59
+ end
60
+
61
+ # ----------------------------------------
62
+ # Folders
63
+ # ----------------------------------------
64
+ context "with a folder" do
65
+ setup { @component.folder(['test', 'framework']) }
66
+ should "have one folder" do
67
+ assert_equal 1, @component.folders.size
68
+ end
69
+
70
+ should "have a folder that is a folder" do
71
+ assert_instance_of Impromptu::Folder, @component.folders.first
72
+ end
73
+ end
74
+
75
+ # ----------------------------------------
76
+ # Freezing
77
+ # ----------------------------------------
78
+ should "be able to be frozen" do
79
+ assert_respond_to @component, :freeze
80
+ assert_respond_to @component, :frozen?
81
+ end
82
+
83
+ should "not be frozen by default" do
84
+ assert_equal false, @component.frozen?
85
+ end
86
+
87
+ context "which is frozen" do
88
+ setup { @component.freeze }
89
+ should "be frozen after calling freeze" do
90
+ assert_equal true, @component.frozen?
91
+ end
92
+
93
+ should "raise an exception when being modified" do
94
+ assert_raise RuntimeError do
95
+ @component.requires('ignored')
96
+ end
97
+ assert_raise RuntimeError do
98
+ @component.folder('ignored')
99
+ end
100
+ assert_raise RuntimeError do
101
+ @component.namespace(:Ignored)
102
+ end
103
+ end
104
+ end
105
+
106
+ # ----------------------------------------
107
+ # Loading dependencies
108
+ # ----------------------------------------
109
+ context "with some external requirements" do
110
+ setup { @component.requires 'matrix' }
111
+ should "ensure unloaded requirements are not already loaded" do
112
+ assert_raise NameError do
113
+ Matrix
114
+ end
115
+ end
116
+
117
+ should "load external requirements when requested" do
118
+ success = @component.load_external_dependencies
119
+ assert_equal true, success
120
+ assert_nothing_raised do
121
+ Matrix
122
+ end
123
+ end
124
+
125
+ should "only load requirements once" do
126
+ success = @component.load_external_dependencies
127
+ assert_equal true, success
128
+ success = @component.load_external_dependencies
129
+ assert_equal false, success
130
+ end
131
+ end
132
+ end
133
+ end
@@ -0,0 +1,20 @@
1
+ require 'helper'
2
+
3
+ class TestComponentSet < Test::Unit::TestCase
4
+ context "A component set with two components" do
5
+ setup do
6
+ @set = Impromptu::ComponentSet.new
7
+ @a = @set << Impromptu::Component.new(nil, 'a')
8
+ @b = @set << Impromptu::Component.new(nil, 'b')
9
+ end
10
+
11
+ should "have two components" do
12
+ assert_equal 2, @set.size
13
+ end
14
+
15
+ should "be able to find both components by name" do
16
+ assert_equal @a, @set['a']
17
+ assert_equal @b, @set['b']
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,4 @@
1
+ require 'helper'
2
+
3
+ class TestFolder < Test::Unit::TestCase
4
+ end
@@ -0,0 +1,43 @@
1
+ require 'helper'
2
+
3
+ class TestImpromptu < Test::Unit::TestCase
4
+ context "Impromptu" do
5
+ should "respond to root_resource" do
6
+ assert_respond_to Impromptu, :root_resource
7
+ end
8
+
9
+ should "respond to components" do
10
+ assert_respond_to Impromptu, :components
11
+ end
12
+
13
+ should "respond to reset" do
14
+ assert_respond_to Impromptu, :reset
15
+ end
16
+
17
+ should "respond to define_components" do
18
+ assert_respond_to Impromptu, :define_components
19
+ end
20
+
21
+ should "respond to parse_file" do
22
+ assert_respond_to Impromptu, :parse_file
23
+ end
24
+
25
+ should "respond to component" do
26
+ assert_respond_to Impromptu, :component
27
+ end
28
+
29
+ should "return a resource from root_resource" do
30
+ assert_instance_of Impromptu::Resource, Impromptu.root_resource
31
+ end
32
+
33
+ should "return a component set from components" do
34
+ assert_instance_of Impromptu::ComponentSet, Impromptu.components
35
+ end
36
+
37
+ should "rase an exception if a block is not supplied to define_components" do
38
+ assert_raise RuntimeError do
39
+ Impromptu.define_components
40
+ end
41
+ end
42
+ end
43
+ end