codemodels 0.2.2-java → 0.2.3-java

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,71 @@
1
+ require 'test_helper'
2
+
3
+ class TestForeignNavigation < Test::Unit::TestCase
4
+
5
+ include CodeModels
6
+
7
+ class B < CodeModelsAstNode
8
+ has_attr 'id',String
9
+ contains_many_uni 'bs', B
10
+ end
11
+
12
+ class C < CodeModelsAstNode
13
+ has_attr 'id',String
14
+ contains_many_uni 'bs', B
15
+ end
16
+
17
+ class A < CodeModelsAstNode
18
+ has_attr 'id',String
19
+ contains_many_uni 'bs', B
20
+ end
21
+
22
+ def setup
23
+ @c1 = C.build 'c1'
24
+ @c2 = C.build 'c2'
25
+ @b1 = B.build 'b1'
26
+ @b2 = B.build 'b2'
27
+ @b3 = B.build 'b3'
28
+ @b4 = B.build 'b4'
29
+ @b5 = B.build 'b5'
30
+ @a1 = A.build 'a1'
31
+
32
+ @c1.addBs @b1
33
+ @b1.addBs @b2
34
+ @b1.addBs @b3
35
+ @b4.addBs @b5
36
+ @a1.addBs @b4
37
+ @a1.addForeign_asts @c1
38
+ @a1.addForeign_asts @c2
39
+ end
40
+
41
+ def test_all_children_also_foreign
42
+ assert_equal [@b4,@c1,@c2], @a1.all_children_also_foreign
43
+ assert_equal [@b2,@b3], @b1.all_children_also_foreign
44
+ assert_equal [], @b2.all_children_also_foreign
45
+ assert_equal [], @b3.all_children_also_foreign
46
+ assert_equal [@b5], @b4.all_children_also_foreign
47
+ assert_equal [], @b5.all_children_also_foreign
48
+ assert_equal [@b1], @c1.all_children_also_foreign
49
+ assert_equal [], @c2.all_children_also_foreign
50
+ end
51
+
52
+ def test_all_children_deep_also_foreign
53
+ assert_equal [@b4,@b5,@c1,@b1,@b2,@b3,@c2], @a1.all_children_deep_also_foreign
54
+ assert_equal [@b2,@b3], @b1.all_children_deep_also_foreign
55
+ assert_equal [], @b2.all_children_deep_also_foreign
56
+ assert_equal [], @b3.all_children_deep_also_foreign
57
+ assert_equal [@b5], @b4.all_children_deep_also_foreign
58
+ assert_equal [], @b5.all_children_deep_also_foreign
59
+ assert_equal [@b1,@b2,@b3], @c1.all_children_deep_also_foreign
60
+ assert_equal [], @c2.all_children_deep_also_foreign
61
+ end
62
+
63
+ def traverse_also_foreign
64
+ ids = []
65
+ CodeModels.traverse_also_foreign(@a1) do |n|
66
+ ids << n.id
67
+ end
68
+ assert_equal ['a1','b4','b5','c1','b1','b2','b3','c2'],ids
69
+ end
70
+
71
+ end
@@ -25,7 +25,7 @@ class MyDummyLanguageSpecificLogic
25
25
 
26
26
  end
27
27
 
28
- class MyMetaClass < RGen::MetamodelBuilder::MMBase
28
+ class MyMetaClass < CodeModelsAstNode
29
29
  has_many_attr 'a',String
30
30
  has_many_attr 'b',Float
31
31
  end
@@ -5,16 +5,30 @@ class TestLanguage < Test::Unit::TestCase
5
5
  include CodeModels
6
6
 
7
7
  class MyLanguage < Language
8
- def initialize
8
+ def initialize(my_parser)
9
9
  super('MyLanguage')
10
10
  @extensions << 'my1'
11
11
  @extensions << 'my2'
12
- @parser = 'p'
12
+ @parser = my_parser
13
+ end
14
+ end
15
+
16
+ class MyParser
17
+ attr_reader :invokations
18
+
19
+ def initialize
20
+ @invokations = []
21
+ end
22
+
23
+ def parse_file(path)
24
+ @invokations << path
13
25
  end
14
26
  end
15
27
 
16
28
  def setup
17
- CodeModels.register_language(MyLanguage.new)
29
+ @my_language = MyLanguage.new(MyParser.new)
30
+ 2.times {CodeModels.register_language(@my_language)}
31
+ @my_parser = CodeModels.registered_languages[0].parser
18
32
  end
19
33
 
20
34
  def test_my_language
@@ -22,7 +36,23 @@ def test_my_language
22
36
  l = CodeModels.registered_languages[0]
23
37
  assert_equal 'MyLanguage',l.name
24
38
  assert_equal ['my1','my2'],l.extensions
25
- assert_equal 'p',l.parser
39
+ assert l.parser.is_a?(MyParser)
40
+ end
41
+
42
+ def test_can_parse?
43
+ assert_equal true, @my_language.can_parse?('pippo.my1')
44
+ assert_equal false, @my_language.can_parse?('pippo.else')
45
+ end
46
+
47
+ def test_parse_file_registered_language
48
+ CodeModels.parse_file('pippo.my1')
49
+ assert_equal ['pippo.my1'],@my_parser.invokations
50
+ end
51
+
52
+ def test_parse_file_unregistered_language
53
+ assert_raise NoLanguageRegistered do
54
+ CodeModels.parse_file('pippo.else')
55
+ end
26
56
  end
27
57
 
28
58
  end
@@ -0,0 +1,102 @@
1
+ require 'test_helper'
2
+
3
+ class TestMetamodel < Test::Unit::TestCase
4
+
5
+ include CodeModels
6
+
7
+ def setup
8
+ ea1_pos = SourcePosition.new
9
+ ea1_pos.begin_line = 10
10
+ ea1_pos.begin_column = 8
11
+
12
+ ea2_pos = SourcePosition.new
13
+ ea2_pos.begin_line = 3
14
+ ea2_pos.begin_column = 4
15
+
16
+ ea3_pos = SourcePosition.new
17
+ ea3_pos.begin_line = 1
18
+ ea3_pos.begin_column = 2
19
+
20
+ @fa1 = FileArtifact.new
21
+
22
+ @ea1 = EmbeddedArtifact.new
23
+ @ea1.host_artifact = @fa1
24
+ @ea1.position_in_host = ea1_pos
25
+
26
+ @ea2 = EmbeddedArtifact.new
27
+ @ea2.host_artifact = @ea1
28
+ @ea2.position_in_host = ea2_pos
29
+
30
+ @ea3 = EmbeddedArtifact.new
31
+ @ea3.host_artifact = @ea2
32
+ @ea3.position_in_host = ea3_pos
33
+ end
34
+
35
+ def test_file_artifact_absolute_start
36
+ sp = SourcePoint.new 1,1
37
+ assert_equal sp,@fa1.absolute_start
38
+ end
39
+
40
+ def test_embedded_artifact_absolute_start
41
+ sp = SourcePoint.new 10,8
42
+ assert_equal sp,@ea1.absolute_start
43
+ end
44
+
45
+ def test_embedded_artifact_indirect_absolute_start
46
+ sp = SourcePoint.new 12,4
47
+ assert_equal sp,@ea2.absolute_start
48
+ end
49
+
50
+ def test_embedded_artifact_indirect_absolute_start_on_line_1
51
+ sp = SourcePoint.new 12,5
52
+ assert_equal sp,@ea3.absolute_start
53
+ end
54
+
55
+ def test_file_artifact_point_to_absolute
56
+ p1 = SourcePoint.new 5,7
57
+ p2 = SourcePoint.new 5,7
58
+ assert_equal p2,@fa1.point_to_absolute(p1)
59
+ end
60
+
61
+ def test_embedded_artifact_point_to_absolute
62
+ p1 = SourcePoint.new 5,7
63
+ p2 = SourcePoint.new 14,7
64
+ assert_equal p2,@ea1.point_to_absolute(p1)
65
+ p3 = SourcePoint.new 1,7
66
+ p4 = SourcePoint.new 10,14
67
+ assert_equal p4,@ea1.point_to_absolute(p3)
68
+ end
69
+
70
+ def test_embedded_artifact_indirect_point_to_absolute
71
+ p1 = SourcePoint.new 5,7
72
+ p2 = SourcePoint.new 16,7
73
+ assert_equal p2,@ea2.point_to_absolute(p1)
74
+ p3 = SourcePoint.new 1,7
75
+ p4 = SourcePoint.new 12,10
76
+ assert_equal p4,@ea2.point_to_absolute(p3)
77
+ end
78
+
79
+ def test_embedded_artifact_position_to_absolute
80
+ p1b = SourcePoint.new 1,8
81
+ p1e = SourcePoint.new 12,7
82
+ pos1 = SourcePosition.new p1b,p1e
83
+ p2b = SourcePoint.new 10,15
84
+ p2e = SourcePoint.new 21,7
85
+ pos2 = SourcePosition.new p2b,p2e
86
+ assert_equal pos2,@ea1.position_to_absolute(pos1)
87
+ end
88
+
89
+ def test_source_point_begin_point_assignment_with_point
90
+ p = SourcePoint.new 7,8
91
+ si = SourceInfo.new
92
+ si.begin_point = p
93
+ assert_equal SourcePoint.new(7,8),si.position.begin_point
94
+ end
95
+
96
+ def test_source_point_end_point_assignment_with_hash
97
+ si = SourceInfo.new
98
+ si.end_point = {line:7,column:8}
99
+ assert_equal SourcePoint.new(7,8),si.position.end_point
100
+ end
101
+
102
+ end
@@ -0,0 +1,12 @@
1
+ require 'test_helper'
2
+
3
+ class TestModelBuilding < Test::Unit::TestCase
4
+
5
+ include CodeModels
6
+
7
+ def test_file_mapper
8
+ fm = FileMapper.new('dir_a','dir_b','java','xml')
9
+ assert_equal "dir_b/abc/def/pippo.xml",fm.map('dir_a/abc/def/pippo.java')
10
+ end
11
+
12
+ end
@@ -2,21 +2,21 @@ require 'test_helper'
2
2
 
3
3
  class TestRgenExt < Test::Unit::TestCase
4
4
 
5
- class C < RGen::MetamodelBuilder::MMBase
5
+ class C < CodeModels::CodeModelsAstNode
6
6
  has_attr 'id',Integer
7
7
  end
8
8
 
9
- class D < RGen::MetamodelBuilder::MMBase
9
+ class D < CodeModels::CodeModelsAstNode
10
10
  has_attr 'id',Integer
11
11
  contains_one_uni 'c', C
12
12
  end
13
13
 
14
- class B < RGen::MetamodelBuilder::MMBase
14
+ class B < CodeModels::CodeModelsAstNode
15
15
  has_attr 'id',Integer
16
16
  contains_many_uni 'ds', D
17
17
  end
18
18
 
19
- class A < RGen::MetamodelBuilder::MMBase
19
+ class A < CodeModels::CodeModelsAstNode
20
20
  has_attr 'id',Integer
21
21
  contains_many_uni 'bs', B
22
22
  contains_one_uni 'c', C
@@ -1,15 +1,28 @@
1
1
  require 'test_helper'
2
2
 
3
- class TestSerializationRgen < Test::Unit::TestCase
3
+ class TestSerialization < Test::Unit::TestCase
4
4
 
5
- class Person < RGen::MetamodelBuilder::MMBase
5
+ class Person < CodeModelsAstNode
6
6
  has_attr 'name', String
7
7
  end
8
8
 
9
+ class Street < CodeModelsAstNode
10
+ has_attr 'name', String
11
+ end
12
+
13
+ class Address < CodeModelsAstNode
14
+ contains_one_uni 'street', Street
15
+ has_attr 'number', Integer
16
+ end
17
+
18
+ class StupidPhoneBook < CodeModelsAstNode
19
+ has_many_attr 'numbers', String
20
+ end
21
+
9
22
  def test_to_model_with_single_obj
10
23
  p = Person.build 'pippo'
11
24
  m = Serialization.rgenobject_to_model(p)
12
-
25
+
13
26
  assert_equal 1,m['root']['id']
14
27
  assert_equal 0,m['external_elements'].count
15
28
  end
@@ -21,19 +34,6 @@ class TestSerializationRgen < Test::Unit::TestCase
21
34
  assert_equal 'pippo',m['root']['attr_name']
22
35
  end
23
36
 
24
- class Street < RGen::MetamodelBuilder::MMBase
25
- has_attr 'name', String
26
- end
27
-
28
- class Address < RGen::MetamodelBuilder::MMBase
29
- contains_one_uni 'street', Street
30
- has_attr 'number', Integer
31
- end
32
-
33
- class StupidPhoneBook < RGen::MetamodelBuilder::MMBase
34
- has_many_attr 'numbers', String
35
- end
36
-
37
37
  def test_to_model_with_rel_cont_single
38
38
  a = Address.build number: 11
39
39
  a.street = Street.build 'via Cassini'
@@ -44,11 +44,11 @@ class TestSerializationRgen < Test::Unit::TestCase
44
44
  assert_equal 'via Cassini',street_serialized['attr_name']
45
45
  end
46
46
 
47
- class Street < RGen::MetamodelBuilder::MMBase
47
+ class Street < CodeModelsAstNode
48
48
  has_attr 'name', String
49
49
  end
50
50
 
51
- class CityMap < RGen::MetamodelBuilder::MMBase
51
+ class CityMap < CodeModelsAstNode
52
52
  contains_many_uni 'streets', Street
53
53
  end
54
54
 
@@ -71,7 +71,7 @@ class TestSerializationRgen < Test::Unit::TestCase
71
71
  spb.addNumbers '+39 389 4561234'
72
72
 
73
73
  assert_equal({
74
- "type"=>"TestSerializationRgen::StupidPhoneBook",
74
+ "type"=>"TestSerialization::StupidPhoneBook",
75
75
  "id"=>1,
76
76
  "attr_numbers"=>[
77
77
  "+49 0176 12345678",
metadata CHANGED
@@ -2,14 +2,14 @@
2
2
  name: codemodels
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.2.2
5
+ version: 0.2.3
6
6
  platform: java
7
7
  authors:
8
8
  - Federico Tomassetti
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-08-27 00:00:00.000000000 Z
12
+ date: 2013-10-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
@@ -104,29 +104,30 @@ files:
104
104
  - Rakefile
105
105
  - codemodels.gemspec
106
106
  - lib/codemodels.rb
107
- - lib/codemodels/foreign_ast.rb
108
107
  - lib/codemodels/info_extraction.rb
109
108
  - lib/codemodels/language.rb
110
109
  - lib/codemodels/metamodel.rb
111
110
  - lib/codemodels/model_building.rb
112
111
  - lib/codemodels/monkey_patching.rb
112
+ - lib/codemodels/navigation.rb
113
113
  - lib/codemodels/parsing.rb
114
- - lib/codemodels/query_serialized.rb
115
114
  - lib/codemodels/rgen_ext.rb
116
115
  - lib/codemodels/serialization.rb
116
+ - lib/codemodels/source_info.rb
117
117
  - lib/codemodels/version.rb
118
118
  - test/data/node_setCompleted.json
119
- - test/test_foreign_ast.rb
119
+ - test/test_foreign_navigation.rb
120
120
  - test/test_helper.rb
121
121
  - test/test_info_extraction.rb
122
122
  - test/test_language.rb
123
+ - test/test_metamodel.rb
124
+ - test/test_model_building.rb
123
125
  - test/test_monkey_patching.rb
124
- - test/test_query_serialized.rb
125
126
  - test/test_rgen_ext.rb
126
127
  - test/test_serialization.rb
127
128
  homepage: https://github.com/ftomassetti/codemodels
128
129
  licenses:
129
- - APACHE 2
130
+ - Apache v2
130
131
  post_install_message:
131
132
  rdoc_options: []
132
133
  require_paths:
@@ -157,11 +158,12 @@ specification_version: 3
157
158
  summary: Library to build models of code
158
159
  test_files:
159
160
  - test/data/node_setCompleted.json
160
- - test/test_foreign_ast.rb
161
+ - test/test_foreign_navigation.rb
161
162
  - test/test_helper.rb
162
163
  - test/test_info_extraction.rb
163
164
  - test/test_language.rb
165
+ - test/test_metamodel.rb
166
+ - test/test_model_building.rb
164
167
  - test/test_monkey_patching.rb
165
- - test/test_query_serialized.rb
166
168
  - test/test_rgen_ext.rb
167
169
  - test/test_serialization.rb
@@ -1,11 +0,0 @@
1
- require 'rgen/metamodel_builder'
2
-
3
- module CodeModels
4
-
5
- def self.enable_foreign_asts(clazz)
6
- clazz.class_eval do
7
- contains_many_uni 'foreign_asts', CodeModelsAstNode
8
- end
9
- end
10
-
11
- end
@@ -1,81 +0,0 @@
1
- # This module permits to manipulate Objects serialized
2
- # as Hash
3
-
4
- module CodeModels
5
-
6
- module QuerySerialized
7
-
8
- def self.rel_conts(root)
9
- root.keys.select {|k| k.start_with? 'relcont_'}
10
- end
11
-
12
- def self.rel_non_conts(root)
13
- root.keys.select {|k| k.start_with? 'relnoncont_'}
14
- end
15
-
16
- def self.attrs(root)
17
- root.keys.select {|k| k.start_with? 'attr_'}
18
- end
19
-
20
- def self.print_tree(root,depth=0)
21
- traverse(root) do |n,d|
22
- s = ""
23
- d.times { s = s + " " }
24
- s = s + n['type'] if n
25
- s = s + '<NIL>' unless n
26
- puts s
27
- end
28
- end
29
-
30
- def self.values(root,feat)
31
- raw = root[feat]
32
- return [] if raw==nil
33
- return raw if raw.is_a? Array
34
- return [raw]
35
- end
36
-
37
- def self.traverse(root,depth=0,&op)
38
- return traverse(root['root'],depth,&op) if root and (root.key? 'root')
39
- op.call(root,depth)
40
- return unless root
41
- rel_conts(root).each do |r|
42
- if root[r].is_a? Array
43
- root[r].each do |c|
44
- raise "expected an object but it is a #{c.class} (relation: #{r})" unless c.is_a? Hash
45
- traverse(c,depth+1,&op)
46
- end
47
- else
48
- traverse(root[r],depth+1,&op)
49
- end
50
- end
51
- end
52
-
53
- # the set of values appearing in the object and its children
54
- def self.collect_values(el)
55
- values = Set.new
56
- rel_conts(el).each do |r|
57
- values(el,r).each {|c| values.merge(collect_values(c))}
58
- end
59
- attrs(el).each do |a|
60
- values(el,a).each {|v| values.add(v)}
61
- end
62
- values
63
- end
64
-
65
- # a counting map values appearing in the object and its children
66
- def self.collect_values_with_count(el)
67
- values = Hash.new {|h,k| h[k]=0}
68
- rel_conts(el).each do |r|
69
- values(el,r).each do |ch|
70
- collect_values_with_count(ch).each {|v,count| values[v]+=count}
71
- end
72
- end
73
- attrs(el).each do |a|
74
- values(el,a).each {|v| values[v]+=1 }
75
- end
76
- values
77
- end
78
-
79
- end
80
-
81
- end
@@ -1,28 +0,0 @@
1
- require 'test_helper'
2
-
3
- class TestForeignAst < Test::Unit::TestCase
4
-
5
- include CodeModels
6
-
7
- class MyLanguageAstNodeA < CodeModelsAstNode
8
- has_attr 'id', Integer
9
- end
10
-
11
- class MyLanguageAstNodeB < CodeModelsAstNode
12
- has_attr 'id', Integer
13
- end
14
-
15
- def setup
16
- end
17
-
18
- def test_without_foreign_ast
19
- assert_equal 0, MyLanguageAstNodeA.ecore.eAllReferences.count, "No Refs expected but they are: #{MyLanguageAstNodeA.ecore.eAllReferences.name}"
20
- end
21
-
22
- def test_with_foreign_ast
23
- CodeModels.enable_foreign_asts(MyLanguageAstNodeB)
24
- assert_equal 1, MyLanguageAstNodeB.ecore.eAllReferences.count
25
- assert_equal 'foreign_asts',MyLanguageAstNodeB.ecore.eAllReferences[0].name
26
- end
27
-
28
- end
@@ -1,40 +0,0 @@
1
- require 'test_helper'
2
-
3
- class TestQuerySerialized < Test::Unit::TestCase
4
-
5
- def test_rel_conts_on_complex_node
6
- set_completed = JSON.parse(IO.read(File.dirname(__FILE__)+'/data/node_setCompleted.json'))
7
-
8
- assert_equal [
9
- 'relcont_layoutInformations',
10
- 'relcont_typeReference',
11
- 'relcont_arrayDimensionsBefore',
12
- 'relcont_arrayDimensionsAfter',
13
- 'relcont_typeParameters',
14
- 'relcont_parameters',
15
- 'relcont_exceptions',
16
- 'relcont_annotationsAndModifiers',
17
- 'relcont_statements'],
18
- CodeModels::QuerySerialized.rel_conts(set_completed)
19
- end
20
-
21
- def test_rel_nonconts_on_complex_node
22
- set_completed = JSON.parse(IO.read(File.dirname(__FILE__)+'/data/node_setCompleted.json'))
23
-
24
- assert_equal [
25
- 'relnoncont_getterFor',
26
- 'relnoncont_setterFor'],
27
- CodeModels::QuerySerialized.rel_non_conts(set_completed)
28
- end
29
-
30
- def test_attrs_on_complex_node
31
- set_completed = JSON.parse(IO.read(File.dirname(__FILE__)+'/data/node_setCompleted.json'))
32
-
33
- assert_equal [
34
- 'attr_name',
35
- 'attr_getter',
36
- 'attr_setter'],
37
- CodeModels::QuerySerialized.attrs(set_completed)
38
- end
39
-
40
- end