courgette 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 57adf4c4137fcf9342cc36a2da2d18bae46adff3
4
- data.tar.gz: c7d05925b78857a07c8765859e9c945dbcffd3e8
3
+ metadata.gz: 38ea2fcd95ca4875051e13099c9bd2588d27ee5a
4
+ data.tar.gz: eda2e173085c058bff814cfb19a963a6e099e0d8
5
5
  SHA512:
6
- metadata.gz: 17e68370df6b304fdf7973b9f969e53d5bc9f30764a2eebdf0b33442b48bfddf6d4b40a81bcb4bd35679448e23a1962e222688339fc1797670d75943f732d5a4
7
- data.tar.gz: bf96fc9d079926ea905030564e21a12f2543fbe546249056c53c85565deed8d05ac56e49b608e68d2fc3750009480a258506e7968aa8f5cd67aa5a6640e0ff2c
6
+ metadata.gz: 5b842597ac23ddca376de1f97de42d4d405831a9264f32342bfedc991613d28ee9dc45cebc461b443e820644ab495e5052c2dc32635727e842aa772aacd5914f
7
+ data.tar.gz: 0716c611805a0657751446a40897a61c4bb74aae132a1177583e13be135e9d5c45a7764118623740705939cc874c408e8cc1cb5d21cdf0cc62341af967a284f0
File without changes
data/courgette.gemspec CHANGED
@@ -25,4 +25,6 @@ Gem::Specification.new do |spec|
25
25
  spec.add_development_dependency "bundler", "~> 1.3"
26
26
  spec.add_development_dependency "rake"
27
27
  spec.add_development_dependency "minitest"
28
+ spec.add_development_dependency "mocha"
29
+ spec.add_development_dependency "simplecov"
28
30
  end
@@ -1,18 +1,22 @@
1
1
  require 'courgette/scope'
2
2
 
3
3
  module Courgette
4
- class Capturer
4
+ class Captured
5
5
  def initialize
6
6
  @references = []
7
7
  @definitions = []
8
8
  end
9
9
 
10
- def capture sexpr
11
- iterate sexpr, []
10
+ def add_definition definition
11
+ @definitions << definition
12
+ end
13
+
14
+ def add_reference reference, context
15
+ @references << Reference.new(reference, context)
12
16
  end
13
17
 
14
18
  def references
15
- @references.uniq! { |reference| reference.to_a }
19
+ @references.uniq! { |ref| ref.to_a }
16
20
  @references
17
21
  end
18
22
 
@@ -23,13 +27,32 @@ module Courgette
23
27
 
24
28
  private
25
29
  Reference = Struct.new :name, :context
30
+ end
31
+
32
+ class Capturer
33
+ def initialize observer = nil
34
+ @captured = observer || Captured.new
35
+ end
36
+
37
+ def capture sexpr
38
+ iterate sexpr, []
39
+ end
40
+
41
+ def references
42
+ @captured.references
43
+ end
26
44
 
45
+ def definitions
46
+ @captured.definitions
47
+ end
48
+
49
+ private
27
50
  def add_definition definition
28
- @definitions << definition.flatten
51
+ @captured.add_definition definition.flatten
29
52
  end
30
53
 
31
54
  def add_reference context, reference
32
- @references << Reference.new(reference, context)
55
+ @captured.add_reference reference, context
33
56
  end
34
57
 
35
58
  def scope sexpr
@@ -63,7 +86,6 @@ module Courgette
63
86
  iterate_many sexpr[3..-1], context
64
87
  end
65
88
 
66
-
67
89
  def iterate sexpr, context
68
90
  return unless sexpr.is_a? Enumerable
69
91
 
@@ -9,7 +9,11 @@ module Courgette
9
9
  def convert filename
10
10
  contents = File.read filename
11
11
 
12
- @parser.parse contents
12
+ begin
13
+ x = @parser.parse contents
14
+ rescue Racc::ParseError => e
15
+ $stderr.puts "Error parsing #{filename}: #{e} (file ignored)"
16
+ end
13
17
  end
14
18
  end
15
19
  end
@@ -5,15 +5,21 @@ module Courgette
5
5
  end
6
6
 
7
7
  def resolve reference
8
- definitions.find do |definition|
9
- match_name?(definition, reference) &&
10
- match_scope?(definition, reference)
8
+ matches(reference).max do |x, y|
9
+ x.length <=> y.length
11
10
  end
12
11
  end
13
12
 
14
13
  private
15
14
  attr_reader :definitions
16
15
 
16
+ def matches reference
17
+ definitions.select do |definition|
18
+ match_name?(definition, reference) &&
19
+ match_scope?(definition, reference)
20
+ end
21
+ end
22
+
17
23
  def match_name? definition, reference
18
24
  definition.last == reference.name.last
19
25
  end
@@ -2,8 +2,8 @@ require 'courgette/name_resolution'
2
2
 
3
3
  module Courgette
4
4
  class ReferenceToDependency
5
- def initialize definitions
6
- @resolver = Courgette::NameResolution.new definitions
5
+ def initialize definitions, resolver = nil
6
+ @resolver = resolver || Courgette::NameResolution.new(definitions)
7
7
  end
8
8
 
9
9
  def transform reference
@@ -1,4 +1,4 @@
1
1
  module Courgette
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
4
4
 
@@ -0,0 +1,109 @@
1
+ require 'simplecov'
2
+ require 'minitest/autorun'
3
+ require 'courgette/capturer'
4
+ require 'mocha/setup'
5
+
6
+ class TestCourgetteCaptured < MiniTest::Unit::TestCase
7
+ def setup
8
+ @object = Courgette::Captured.new
9
+ end
10
+
11
+ def test_definition
12
+ @object.add_definition [:A]
13
+
14
+ assert_equal [:A], @object.definitions.first
15
+ end
16
+
17
+ def test_reference
18
+ @object.add_reference "name", "context"
19
+
20
+ assert_equal "name", @object.references.first.name
21
+ assert_equal "context", @object.references.first.context
22
+ end
23
+
24
+ def test_duplicate_definitions
25
+ @object.add_definition [:A]
26
+ @object.add_definition [:A]
27
+
28
+ assert_equal 1, @object.definitions.count
29
+ end
30
+
31
+ def test_duplicate_references
32
+ @object.add_reference "name", "context"
33
+ @object.add_reference "name", "context"
34
+
35
+ assert_equal 1, @object.references.count
36
+ end
37
+ end
38
+
39
+ class TestCourgetteCapturer < MiniTest::Unit::TestCase
40
+ def setup
41
+ @observer = mock()
42
+ @object = Courgette::Capturer.new @observer
43
+ end
44
+
45
+ def test_references_delegation
46
+ @observer.expects(:references).once
47
+
48
+ @object.references
49
+ end
50
+
51
+ def test_definitions_delegation
52
+ @observer.expects(:definitions).once
53
+
54
+ @object.definitions
55
+ end
56
+
57
+ def test_class_definition
58
+ @observer.expects(:add_definition).with([:X]).once
59
+
60
+ @object.capture [:class, :X]
61
+ end
62
+
63
+ def test_module_definition
64
+ @observer.expects(:add_definition).with([:M]).once
65
+
66
+ @object.capture [:module, :M]
67
+ end
68
+
69
+ def test_class_inside_module
70
+ @observer.expects(:add_definition).with([:M]).once
71
+ @observer.expects(:add_definition).with([:M, :C]).once
72
+
73
+ @object.capture [:module, :M, [:class, :C]]
74
+ end
75
+
76
+ def test_constant_declaration
77
+ @observer.expects(:add_definition).with([:C]).once
78
+
79
+ @object.capture [:cdecl, :C, [:lit, 1]]
80
+ end
81
+
82
+ def test_scoped_definition
83
+ @observer.expects(:add_definition).with([:M, :C]).once
84
+
85
+ @object.capture [:module, [:colon2, [:const, :M], :C]]
86
+ end
87
+
88
+ def test_reference
89
+ @observer.expects(:add_reference).with([:C], []).once
90
+
91
+ @object.capture [:const, :C]
92
+ end
93
+
94
+ def test_class_call
95
+ @observer.expects(:add_reference).with([:C], []).once
96
+
97
+ @object.capture [:call, [:const, :C], :new]
98
+ end
99
+
100
+ def test_reference_in_method
101
+ @observer.expects(:add_reference).with([:C], []).once
102
+
103
+ @object.capture [:defn, :bananas, [], [:const, :C]]
104
+ end
105
+
106
+ def test_others
107
+ @object.capture [:if, [:lit, 42]]
108
+ end
109
+ end
@@ -1,3 +1,4 @@
1
+ require 'simplecov'
1
2
  require 'minitest/autorun'
2
3
  require 'courgette/name_resolution'
3
4
 
@@ -69,4 +70,40 @@ class TestCourgetteNameResolution < MiniTest::Unit::TestCase
69
70
  ref = FakeReference.new [:Name], [[:Potato, :Scoped]]
70
71
  assert_equal nil, @resolver.resolve(ref)
71
72
  end
73
+
74
+ def test_ambiguous_reference_from_specific_context
75
+ definitions = [[:Potato, :Name], [:Name]]
76
+ create definitions
77
+
78
+ ref = FakeReference.new [:Name], [[:Potato]]
79
+
80
+ assert_equal [:Potato, :Name], @resolver.resolve(ref)
81
+ end
82
+
83
+ def test_ambiguous_reference_from_specific_context_different_creation_order
84
+ definitions = [[:Name], [:Potato, :Name]]
85
+ create definitions
86
+
87
+ ref = FakeReference.new [:Name], [[:Potato]]
88
+
89
+ assert_equal [:Potato, :Name], @resolver.resolve(ref)
90
+ end
91
+
92
+ def test_ambiguous_reference_from_global_context
93
+ definitions = [[:Potato, :Name], [:Name]]
94
+ create definitions
95
+
96
+ ref = FakeReference.new [:Name], []
97
+
98
+ assert_equal [:Name], @resolver.resolve(ref)
99
+ end
100
+
101
+ def test_ambiguous_reference_from_global_context_different_creation_order
102
+ definitions = [[:Name], [:Potato, :Name]]
103
+ create definitions
104
+
105
+ ref = FakeReference.new [:Name], []
106
+
107
+ assert_equal [:Name], @resolver.resolve(ref)
108
+ end
72
109
  end
@@ -0,0 +1,41 @@
1
+ require 'simplecov'
2
+ require 'minitest/autorun'
3
+ require 'courgette/reference_to_dependency'
4
+
5
+ class FakeResolver < Struct.new(:value)
6
+ def resolve reference
7
+ value
8
+ end
9
+ end
10
+
11
+ class TestCourgetteReferenceToDependency < MiniTest::Unit::TestCase
12
+ class FakeReference < Struct.new(:context)
13
+ end
14
+
15
+ def test_unknown_reference
16
+ @converter = Courgette::ReferenceToDependency.new [], FakeResolver.new(nil)
17
+ reference = FakeReference.new
18
+
19
+ dependency = @converter.transform reference
20
+
21
+ assert_nil dependency
22
+ end
23
+
24
+ def test_known_reference
25
+ @converter = Courgette::ReferenceToDependency.new [], FakeResolver.new(42)
26
+ reference = FakeReference.new []
27
+
28
+ dependency = @converter.transform reference
29
+
30
+ assert_equal 42, dependency.reference
31
+ end
32
+
33
+ def test_referrer
34
+ @converter = Courgette::ReferenceToDependency.new [], FakeResolver.new(42)
35
+ reference = FakeReference.new [[:X], [:Y]]
36
+
37
+ dependency = @converter.transform reference
38
+
39
+ assert_equal [:X, :Y], dependency.referrer
40
+ end
41
+ end
@@ -1,3 +1,4 @@
1
+ require 'simplecov'
1
2
  require 'minitest/autorun'
2
3
  require 'courgette/scope'
3
4
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: courgette
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hugo Peixoto
@@ -94,17 +94,45 @@ dependencies:
94
94
  - - '>='
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: mocha
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: simplecov
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
97
125
  description: Static ruby dependency analyser
98
126
  email:
99
127
  - hugo.peixoto@gmail.com
100
128
  executables:
101
- - courgette.rb
129
+ - courgette
102
130
  extensions: []
103
131
  extra_rdoc_files: []
104
132
  files:
105
133
  - Gemfile
106
134
  - Rakefile
107
- - bin/courgette.rb
135
+ - bin/courgette
108
136
  - courgette.gemspec
109
137
  - lib/courgette.rb
110
138
  - lib/courgette/capturer.rb
@@ -119,7 +147,9 @@ files:
119
147
  - lib/courgette/reference_to_dependency.rb
120
148
  - lib/courgette/scope.rb
121
149
  - lib/courgette/version.rb
150
+ - test/courgette/test_capturer.rb
122
151
  - test/courgette/test_name_resolution.rb
152
+ - test/courgette/test_reference_to_dependency.rb
123
153
  - test/courgette/test_scope.rb
124
154
  homepage: https://github.com/hugopeixoto/courgette
125
155
  licenses:
@@ -146,6 +176,8 @@ signing_key:
146
176
  specification_version: 4
147
177
  summary: Courgette analyses a set of files, and calculates a dependency graph
148
178
  test_files:
179
+ - test/courgette/test_capturer.rb
149
180
  - test/courgette/test_name_resolution.rb
181
+ - test/courgette/test_reference_to_dependency.rb
150
182
  - test/courgette/test_scope.rb
151
183
  has_rdoc: