Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add some tests

  • Loading branch information...
commit 9c1982ccc66a55acdaaad5a88f8cbc23e93aa9c1 1 parent ace4bae
@technohippy authored
View
4 lib/red_grape/edge.rb
@@ -7,8 +7,8 @@ class Edge < PropertiedObject
def initialize(graph, id, source, target, label, opts={})
super graph, opts
@id = id
- @source = graph.vertex source
- @target = graph.vertex target
+ @source = source.is_a?(Vertex) ? source : graph.vertex(source)
+ @target = target.is_a?(Vertex) ? target : graph.vertex(target)
@source.add_out_edge self
@target.add_in_edge self
@label = label
View
42 lib/red_grape/graph.rb
@@ -1,3 +1,4 @@
+require 'stringio'
require 'red_grape/vertex'
require 'red_grape/vertex_group'
require 'red_grape/edge'
@@ -12,7 +13,11 @@ class Graph
class <<self
def load(filename)
- self.new.load filename
+ if filename =~ /^<\?xml/
+ self.new.load StringIO.new(filename)
+ else
+ self.new.load filename
+ end
end
end
@@ -32,6 +37,39 @@ def edge(id)
end
alias e edge
+ def add_vertex(id, v=nil)
+ if v
+ if v.is_a? Hash
+ v = Vertex.new self, id, v
+ end
+ else
+ if id.is_a? Hash
+ v = id
+ id = v[:id] || v['id']
+ else
+ v = id
+ id = v._id
+ end
+ end
+ raise ArgumentError.new 'invalid id' unless id == v._id
+
+ @vertices[id.to_s] = v
+ end
+
+ def add_edge(id, label, from, to)
+ edge = if id.is_a? Edge
+ id
+ else
+ id = id.to_s
+ from = self.vertex[from.to_s] unless from.is_a? Vertex
+ to = self.vertex[to.to_s] unless to.is_a? Vertex
+ add_vertex from unless self.vertex(from._id)
+ add_vertex to unless self.vertex(to._id)
+ Edge.new self, id, from, to, label
+ end
+ @edges[edge._id] = edge
+ end
+
def load(file, type=:xml)
file = File.open file if file.is_a? String
case type
@@ -77,10 +115,8 @@ def nodes(xml, elm)
xml.xpath(".//xmlns:#{elm}", NAMESPACES)
end
-=begin
def to_s
{:vertices => @vertices, :edges => @edges}.to_s
end
-=end
end
end
View
2  lib/red_grape/pipe/if_then_else_pipe.rb
@@ -13,7 +13,7 @@ def pass(obj, context)
else
context.eval({:it => obj}, &else_block)
end
- ret = ret.invoke if ret.is_a? Pipe
+ ret = ret.invoke if ret.is_a? Pipe::Base
if self.last?
ret
else
View
15 lib/red_grape/propertied_object.rb
@@ -1,3 +1,5 @@
+require 'red_grape/property_description'
+
module RedGrape
class PropertiedObject
def initialize(graph, opts={})
@@ -5,16 +7,27 @@ def initialize(graph, opts={})
@property = {}
@property_description = opts[:property_description] || {}
@property_description.each do |k ,v|
+ if v.is_a? Array
+ v = PropertyDescription.new *v
+ elsif v.is_a? Hash
+ v = PropertyDescription.new v[:name], v[:type], v[:default]
+ end
+ @property_description[k] = v
set_property k, v.default if v.has_default?
end
end
def set_property(kid, v)
desc = @property_description[kid]
- self[desc.name] = desc.convert(v) if desc.accessible? v
+ if desc.accessible? v
+ self[desc.name] = desc.convert(v)
+ else
+ raise ArgumentError.new "#{kid} should be #{desc.type}."
+ end
end
def []=(k, v)
+ # TODO: type check?
@property[k.to_s] = v
end
View
10 lib/red_grape/property_description.rb
@@ -9,13 +9,17 @@ def initialize(name, type, default=nil)
end
def accessible?(v)
- # TODO
- true
+ case type
+ when 'int', 'integer'
+ v.is_a? Integer
+ else
+ true
+ end
end
def convert(val)
case type
- when 'int'
+ when 'int', 'integer'
val.to_i
else
val
View
2  lib/red_grape/vertex.rb
@@ -42,7 +42,7 @@ def to_s
end
def method_missing(name, *args, &block)
- self[name.to_s] or raise NoMethodError.new(name)
+ self[name.to_s] or raise NoMethodError.new(name.to_s)
end
end
end
View
71 test/test_graph.rb
@@ -0,0 +1,71 @@
+require 'test/unit'
+require 'stringio'
+require 'red_grape'
+
+class GraphTest < Test::Unit::TestCase
+ def setup
+ @graph = RedGrape::Graph.new
+ end
+
+ def test_construct
+ graph = RedGrape::Graph.new
+ v1 = graph.add_vertex 1, name:'yasushi'
+ v2 = graph.add_vertex 2, name:'ando'
+ e12 = graph.add_edge 1, :fullname, v1, v2
+
+ assert_equal 1, v1._out_edges.size
+ assert_equal 0, v1._in_edges.size
+ assert_equal e12, v1._out_edges.first
+
+ assert_equal 0, v2._out_edges.size
+ assert_equal 1, v2._in_edges.size
+ assert_equal e12, v2._in_edges.first
+ end
+
+ def test_load
+ data = <<-EOS
+<?xml version="1.0" encoding="UTF-8"?>
+<graphml xmlns="http://graphml.graphdrawing.org/xmlns"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns
+ http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
+ <key id="weight" for="edge" attr.name="weight" attr.type="float"/>
+ <key id="name" for="node" attr.name="name" attr.type="string"/>
+ <graph id="G" edgedefault="directed">
+ <node id="1">
+ <data key="name">marko</data>
+ </node>
+ <node id="2">
+ <data key="name">vadas</data>
+ </node>
+ <node id="3">
+ <data key="name">lop</data>
+ </node>
+ <edge id="4" source="1" target="2" label="knows">
+ <data key="weight">0.5</data>
+ </edge>
+ <edge id="5" source="1" target="3" label="knows">
+ <data key="weight">1.0</data>
+ </edge>
+ </graph>
+</graphml>
+ EOS
+ graph = RedGrape::Graph.load data
+ assert_equal 'marko', graph.vertex(1).name
+ assert_equal 'vadas', graph.vertex(2).name
+ assert_equal 'lop', graph.vertex(3).name
+ assert_equal 'marko', graph.edge(4).source.name
+ assert_equal 'vadas', graph.edge(4).target.name
+ assert_equal 'marko', graph.edge(5).source.name
+ assert_equal 'lop', graph.edge(5).target.name
+
+ graph = RedGrape::Graph.load StringIO.new(data)
+ assert_equal 'marko', graph.vertex(1).name
+ assert_equal 'vadas', graph.vertex(2).name
+ assert_equal 'lop', graph.vertex(3).name
+ assert_equal 'marko', graph.edge(4).source.name
+ assert_equal 'vadas', graph.edge(4).target.name
+ assert_equal 'marko', graph.edge(5).source.name
+ assert_equal 'lop', graph.edge(5).target.name
+ end
+end
View
3  test/test_on_the_nature_of_pipes.rb
@@ -64,7 +64,8 @@ def test_side_effect
def test_if_then_else
#assert_equal ['vadas', ['ripple', 'lop']],
- # @graph.v(1).out('knows').if_then_else(proc{it.age < 30}, proc{it.name}, proc{it.out('created').name}).invoke.to_a
+ assert_equal ['vadas', 'ripple', 'lop'],
+ @graph.v(1).out('knows').if_then_else(proc{it.age < 30}, proc{it.name}, proc{it.out('created').name}).invoke.to_a
end
def test_back
View
42 test/test_propertied_object.rb
@@ -0,0 +1,42 @@
+require 'test/unit'
+require 'red_grape'
+require 'red_grape/propertied_object'
+require 'red_grape/property_description'
+
+class PropertiedObjectTest < Test::Unit::TestCase
+ def test_new
+ g = RedGrape::Graph.new
+ v = RedGrape::PropertiedObject.new g, property_description:{
+ name:RedGrape::PropertyDescription.new('name', 'string'),
+ sex:RedGrape::PropertyDescription.new('sex', 'string', 'male'),
+ age:RedGrape::PropertyDescription.new('age', 'integer')
+ }
+ desc = v.instance_eval '@property_description'
+ assert_equal 'name', desc[:name].name
+ assert_equal 'string', desc[:name].type
+ assert_nil desc[:name].default
+ assert_equal 'male', desc[:sex].default
+
+ v = RedGrape::PropertiedObject.new g, property_description:{
+ name:['name', 'string'],
+ sex:['sex', 'string', 'male'],
+ age:['age', 'integer']
+ }
+ desc = v.instance_eval '@property_description'
+ assert_equal 'name', desc[:name].name
+ assert_equal 'string', desc[:name].type
+ assert_nil desc[:name].default
+ assert_equal 'male', desc[:sex].default
+
+ v = RedGrape::PropertiedObject.new g, property_description:{
+ name:{name:'name', type:'string'},
+ sex:{name:'sex', type:'string', default:'male'},
+ age:{name:'age', type:'integer'}
+ }
+ desc = v.instance_eval '@property_description'
+ assert_equal 'name', desc[:name].name
+ assert_equal 'string', desc[:name].type
+ assert_nil desc[:name].default
+ assert_equal 'male', desc[:sex].default
+ end
+end
View
34 test/test_vertex.rb
@@ -0,0 +1,34 @@
+require 'test/unit'
+require 'red_grape'
+
+class VertexTest < Test::Unit::TestCase
+ def test_to_s
+ id = '12345'
+ g = RedGrape::Graph.new
+ v = RedGrape::Vertex.new g, id
+ assert_equal "v[#{id}]", v.to_s
+ end
+
+ def test_set_property
+ g = RedGrape::Graph.new
+ v = RedGrape::Vertex.new g, '1234', property_description:{
+ name:RedGrape::PropertyDescription.new('name', 'string'),
+ sex:RedGrape::PropertyDescription.new('sex', 'string', 'male'),
+ age:RedGrape::PropertyDescription.new('age', 'integer')
+ }
+ v.set_property :name, 'hello'
+ assert_equal 'hello', v[:name]
+ assert_equal 'male', v[:sex]
+ assert_raise(ArgumentError) {v.set_property :age, 'not integer'}
+ end
+
+ def test_method_missing
+ g = RedGrape::Graph.new
+ v = RedGrape::Vertex.new g, '12345'
+ v[:name] = 'yasushi'
+ assert_equal 'yasushi', v[:name]
+ assert_equal 'yasushi', v['name']
+ assert_equal 'yasushi', v.name
+ assert_raise(NoMethodError) {v.no_attr}
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.