graphs 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,149 @@
1
+ #! /usr/bin/env ruby
2
+ # -*- coding: UTF-8 -*-
3
+
4
+ module JSONUtils
5
+ def self.get_sample_graph
6
+ @@json
7
+ end
8
+
9
+ @@json = <<EOJSON
10
+ {
11
+ "nodes" : [
12
+ { "label": "foo" }, { "label": "bar" }
13
+ ],
14
+ "edges" : [
15
+ { "node1": "bar", "node2": "foo" }
16
+ ]
17
+ }
18
+ EOJSON
19
+
20
+ @@json.gsub!(/\s+/, '')
21
+ end
22
+
23
+ class JSON_Graph_test < Test::Unit::TestCase
24
+
25
+ # == Graph#to_json == #
26
+
27
+ def test_empty_graph_to_json
28
+ g = Graph.new
29
+ empty_json = '{"nodes":[],"edges":[]}'
30
+ assert_equal(empty_json, g.to_json)
31
+ end
32
+
33
+ def test_sample_graph_to_json
34
+ json = JSONUtils::get_sample_graph
35
+ g = JSONGraph::parse(json)
36
+ assert_equal(json, g.to_json)
37
+ end
38
+
39
+ # == Graph#write('….json') == #
40
+
41
+ def test_empty_graph_write_json
42
+ g = Graph.new
43
+
44
+ f = Tempfile.new([ 'foo', '.json' ])
45
+ f.close
46
+
47
+ g.write(f.path)
48
+ g2 = JSONGraph.load(f.path)
49
+
50
+ assert_equal(g, g2)
51
+ f.unlink
52
+ end
53
+
54
+ def setup
55
+ if File.exists? '/tmp/_graph_test.json'
56
+ File.delete '/tmp/_graph_test.json'
57
+ end
58
+ end
59
+
60
+ end
61
+
62
+ class JSON_test < Test::Unit::TestCase
63
+
64
+ # == JSON::parse == #
65
+
66
+ def test_parse_empty_graph
67
+ g = JSONGraph::parse('')
68
+
69
+ assert_equal([], g.nodes)
70
+ assert_equal([], g.edges)
71
+ end
72
+
73
+ def test_parse_empty_graph_with_nodes_list
74
+
75
+ s = '{"nodes":[]}'
76
+
77
+ g = JSONGraph::parse(s)
78
+
79
+ assert_equal([], g.nodes)
80
+ assert_equal([], g.edges)
81
+ end
82
+
83
+ def test_parse_empty_graph_with_nodes_and_edges_lists
84
+ s = '{"nodes":[],"edges":[]}'
85
+ g = JSONGraph::parse(s)
86
+
87
+ assert_equal([], g.nodes)
88
+ assert_equal([], g.edges)
89
+ end
90
+
91
+ def test_parse_one_node_no_edge
92
+ s = '{"nodes":[{"label":"foo"}]}'
93
+ g = JSONGraph::parse(s)
94
+
95
+ assert_equal(1, g.nodes.length)
96
+ assert_equal('foo', g.nodes[0]['label'])
97
+ assert_equal([], g.edges)
98
+ end
99
+
100
+ def test_parse_sample_graph
101
+ g = JSONGraph::parse(JSONUtils::get_sample_graph)
102
+
103
+ assert_equal(2, g.nodes.length)
104
+ assert_equal(1, g.edges.length)
105
+
106
+ assert_equal('foo', g.nodes[0]['label'])
107
+ assert_equal('bar', g.nodes[1]['label'])
108
+
109
+ assert_equal('bar', g.edges[0]['node1'])
110
+ assert_equal('foo', g.edges[0]['node2'])
111
+
112
+ end
113
+
114
+ # == JSON::unparse == #
115
+
116
+ def test_unparse_empty_graph
117
+ g = Graph.new
118
+
119
+ s = JSONGraph::unparse(g)
120
+
121
+ assert_equal('{"nodes":[],"edges":[]}', s)
122
+ end
123
+
124
+ def test_unparse_one_node
125
+ g = Graph.new([{
126
+ 'label' => 'hello',
127
+ 'foo' => 42
128
+ }])
129
+ s = JSONGraph::unparse(g)
130
+ assert_equal('{"nodes":[{"label":"hello","foo":42}],"edges":[]}', s)
131
+ end
132
+
133
+ def test_unparse_one_edge
134
+ g = Graph.new([], [{
135
+ 'label' => 'hello',
136
+ 'foo' => 42
137
+ }])
138
+ s = JSONGraph::unparse(g)
139
+ assert_equal('{"nodes":[],"edges":[{"label":"hello","foo":42}]}', s)
140
+ end
141
+
142
+ def test_unparse_sample_graph
143
+ g1 = JSONGraph::parse(JSONUtils::get_sample_graph)
144
+ g2 = JSONGraph::parse(JSONGraph::unparse(g1))
145
+
146
+ assert_equal(g1, g2)
147
+ end
148
+
149
+ end
@@ -0,0 +1,141 @@
1
+ #! /usr/bin/env ruby
2
+ # -*- coding: UTF-8 -*-
3
+
4
+ class Node_test < Test::Unit::TestCase
5
+
6
+ def setup
7
+ @@empty = Graph::Node.new
8
+ @@alice = Graph::Node.new('label' => 'Alice')
9
+
10
+ # Alice ----> Bob
11
+ # ↑ ↑
12
+ # | |
13
+ # Oscar -------'
14
+ @@sample_graph = Graph.new(
15
+ [
16
+ { 'label' => 'Alice' },
17
+ { 'label' => 'Bob' },
18
+ { 'label' => 'Oscar' }
19
+ ],
20
+ [
21
+ { 'node1' => 'Alice', 'node2' => 'Bob' },
22
+ { 'node1' => 'Oscar', 'node2' => 'Alice'},
23
+ { 'node1' => 'Oscar', 'node2' => 'Bob'}
24
+ ]
25
+ )
26
+
27
+ end
28
+
29
+ def test_create_graph_with_node_objects
30
+ g1 = Graph.new([
31
+ Graph::Node.new('label' => 'Foo')
32
+ ])
33
+
34
+ g2 = Graph.new([
35
+ { 'label' => 'Foo' }
36
+ ])
37
+
38
+ assert_equal(g2, g1)
39
+ end
40
+
41
+ def test_node_attrs
42
+ a = @@alice
43
+
44
+ a['foo'] = 'bar'
45
+
46
+ assert_equal('Alice', @@alice['label'])
47
+ assert_equal('bar', @@alice['foo'])
48
+ assert_equal(nil, @@alice['fooo'])
49
+ end
50
+
51
+ def test_node_degree_by_label
52
+ assert_equal(2, @@sample_graph.degree_of('Alice'))
53
+ assert_equal(2, @@sample_graph.degree_of('Oscar'))
54
+ assert_equal(2, @@sample_graph.degree_of('Bob'))
55
+ assert_equal(0, @@sample_graph.degree_of('not found'))
56
+ end
57
+
58
+ def test_node_degree_by_object
59
+ assert_equal(2, @@sample_graph.degree_of(@@alice))
60
+ end
61
+
62
+ def test_node_in_degree_by_label
63
+ assert_equal(1, @@sample_graph.in_degree_of('Alice'))
64
+ assert_equal(2, @@sample_graph.in_degree_of('Bob'))
65
+ assert_equal(0, @@sample_graph.in_degree_of('Oscar'))
66
+ assert_equal(0, @@sample_graph.in_degree_of('not found'))
67
+ end
68
+
69
+ def test_node_in_degree_by_object
70
+ assert_equal(1, @@sample_graph.in_degree_of(@@alice))
71
+ end
72
+
73
+ def test_node_out_degree_by_label
74
+ assert_equal(1, @@sample_graph.out_degree_of('Alice'))
75
+ assert_equal(0, @@sample_graph.out_degree_of('Bob'))
76
+ assert_equal(2, @@sample_graph.out_degree_of('Oscar'))
77
+ assert_equal(0, @@sample_graph.out_degree_of('not found'))
78
+ end
79
+
80
+ def test_node_out_degree_by_object
81
+ assert_equal(1, @@sample_graph.out_degree_of(@@alice))
82
+ end
83
+
84
+ def test_node_label_attr
85
+ assert_equal('Alice', @@alice.label)
86
+ end
87
+
88
+ def test_node_update
89
+
90
+ n = Graph::Node.new
91
+
92
+ assert_equal(true, n.update({}).is_a?(Graph::Node))
93
+ end
94
+
95
+ def test_node_init_with_another_node
96
+
97
+ n = Graph::Node.new({ :foo => 'bar' })
98
+
99
+ assert_equal( n, Graph::Node.new(n) )
100
+
101
+ end
102
+
103
+ end
104
+
105
+ class NodeArray_test < Test::Unit::TestCase
106
+
107
+ def test_nodearray_push_node
108
+
109
+ n = Graph::Node.new({ :foo => 42 })
110
+ na = Graph::NodeArray.new([])
111
+
112
+ na.push(n)
113
+
114
+ assert_equal(n, na[0])
115
+
116
+ end
117
+
118
+ def test_nodearray_push_hash
119
+
120
+ n = { :foo => 42 }
121
+ na = Graph::NodeArray.new([])
122
+
123
+ na.push(n)
124
+
125
+ assert_equal(Graph::Node.new(n), na[0])
126
+
127
+ end
128
+
129
+ def test_nodearray_push_no_node_nor_hash
130
+
131
+ na = Graph::NodeArray.new([])
132
+
133
+ assert_raise(TypeError) do
134
+
135
+ na.push(42)
136
+
137
+ end
138
+
139
+ end
140
+
141
+ end
@@ -0,0 +1,24 @@
1
+ #! /usr/bin/env ruby
2
+ # -*- coding: UTF-8 -*-
3
+
4
+ require 'coveralls'
5
+ Coveralls.wear!
6
+
7
+ require 'test/unit'
8
+ require 'simplecov'
9
+ require 'tempfile'
10
+
11
+ test_dir = File.expand_path( File.dirname(__FILE__) )
12
+
13
+ SimpleCov.formatter = Coveralls::SimpleCov::Formatter
14
+ SimpleCov.start { add_filter '/tests/' }
15
+
16
+ require_relative '../lib/graph'
17
+ require_relative '../lib/graphs/gdf'
18
+ require_relative '../lib/graphs/json'
19
+
20
+ for t in Dir.glob( File.join( test_dir, '*_tests.rb' ) )
21
+ require t
22
+ end
23
+
24
+ exit Test::Unit::AutoRunner.run
metadata ADDED
@@ -0,0 +1,137 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: graphs
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Baptiste Fontaine
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain:
11
+ - |
12
+ -----BEGIN CERTIFICATE-----
13
+ MIIDaDCCAlCgAwIBAgIBATANBgkqhkiG9w0BAQUFADA9MRAwDgYDVQQDDAdiYXRp
14
+ Zm9uMRUwEwYKCZImiZPyLGQBGRYFeWFob28xEjAQBgoJkiaJk/IsZAEZFgJmcjAe
15
+ Fw0xNDA4MjQxMTM5NTJaFw0xNTA4MjQxMTM5NTJaMD0xEDAOBgNVBAMMB2JhdGlm
16
+ b24xFTATBgoJkiaJk/IsZAEZFgV5YWhvbzESMBAGCgmSJomT8ixkARkWAmZyMIIB
17
+ IjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAn3uOgWl+FwXIjDdCay28i6cK
18
+ FxHWhHoS/mH9pkXzSGVctEKP2fulie6MkIvrLCP5M6TpByeaBjcJjZPadrou1FIc
19
+ Yc/O14jYjaKTqfMxpzgNfGzDdBgBo0QZ9rcHjORetdIZdUSDaZjPtI1aGS6eBMsh
20
+ W2X6GxL4UQ1kH0Lyg7iPYAH5RHnD3+G+S28iOPFfRLFzm4fwJp1k7URiiSyOHTDp
21
+ B0ZehKKrW/ibCaRMYp2VoCamcim4de1VA6CTOaYSShueqThE18n1HM6aprihziyM
22
+ 04yIpo80/unO6JxlsUFdBjsb5d7oJSqPJ6/OfcFnyXa/VRm+Ed9d6PTwZvL7YwID
23
+ AQABo3MwcTAJBgNVHRMEAjAAMAsGA1UdDwQEAwIEsDAdBgNVHQ4EFgQUWyH3xMx1
24
+ 8I5NLncgNEC0ZlDRKegwGwYDVR0RBBQwEoEQYmF0aWZvbkB5YWhvby5mcjAbBgNV
25
+ HRIEFDASgRBiYXRpZm9uQHlhaG9vLmZyMA0GCSqGSIb3DQEBBQUAA4IBAQAaTVya
26
+ CGgojxBwUoadCCIsFheGsXvSFhikkXYNXy0VxEYr8BaTfGwzYh9c9T5N+Y5Mu5MW
27
+ WegqwIwRhIu6Rg7huqJ7TK50pVDF0yrZcsxvWjOfd3clblBHjKGQx5Mbu7LVNGKE
28
+ +QNdTAwYVTAA8wXHpxk200cHb9xz4e9ANpb4lonGuPz8jKmb/A7Z1M5QD6zStG8l
29
+ sTlVAhA/LZiC9gL9LtW8Iq7o7xRFhxNPKWHu6JVThH9i16eli+JignOJbGna7C40
30
+ QnOQb8zHyNL+gq2m/mnZGrSehx+6AujokjOfHbmivYMfDATOQQx0eIBI18IhacZm
31
+ 42WxhhIV2bwDtd77
32
+ -----END CERTIFICATE-----
33
+ date: 2014-08-24 00:00:00.000000000 Z
34
+ dependencies:
35
+ - !ruby/object:Gem::Dependency
36
+ name: simplecov
37
+ requirement: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ~>
40
+ - !ruby/object:Gem::Version
41
+ version: '0.7'
42
+ type: :development
43
+ prerelease: false
44
+ version_requirements: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ~>
47
+ - !ruby/object:Gem::Version
48
+ version: '0.7'
49
+ - !ruby/object:Gem::Dependency
50
+ name: rake
51
+ requirement: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ~>
54
+ - !ruby/object:Gem::Version
55
+ version: '10.1'
56
+ type: :development
57
+ prerelease: false
58
+ version_requirements: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ~>
61
+ - !ruby/object:Gem::Version
62
+ version: '10.1'
63
+ - !ruby/object:Gem::Dependency
64
+ name: test-unit
65
+ requirement: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: '2.5'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ~>
75
+ - !ruby/object:Gem::Version
76
+ version: '2.5'
77
+ - !ruby/object:Gem::Dependency
78
+ name: coveralls
79
+ requirement: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ~>
82
+ - !ruby/object:Gem::Version
83
+ version: '0.7'
84
+ type: :development
85
+ prerelease: false
86
+ version_requirements: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ~>
89
+ - !ruby/object:Gem::Version
90
+ version: '0.7'
91
+ description: Provide functions to (un)parse GDF/JSON files and generate graphs
92
+ email: batifon@yahoo.fr
93
+ executables: []
94
+ extensions: []
95
+ extra_rdoc_files: []
96
+ files:
97
+ - lib/graph.rb
98
+ - lib/graphs/gdf.rb
99
+ - lib/graphs/json.rb
100
+ - tests/edge_tests.rb
101
+ - tests/gdf_tests.rb
102
+ - tests/graph_tests.rb
103
+ - tests/json_tests.rb
104
+ - tests/node_tests.rb
105
+ - tests/tests.rb
106
+ homepage: https://github.com/bfontaine/Graphs.rb
107
+ licenses:
108
+ - MIT
109
+ metadata: {}
110
+ post_install_message:
111
+ rdoc_options: []
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - '>='
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - '>='
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubyforge_project:
126
+ rubygems_version: 2.2.1
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: Utilities to manipulate graph files
130
+ test_files:
131
+ - tests/edge_tests.rb
132
+ - tests/gdf_tests.rb
133
+ - tests/graph_tests.rb
134
+ - tests/json_tests.rb
135
+ - tests/node_tests.rb
136
+ - tests/tests.rb
137
+ has_rdoc: