rxsd 0.2 → 0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,14 @@
1
+ # loads and runs all tests for the rxsd project
2
+ #
3
+ # Copyright (C) 2010 Mohammed Morsi <movitto@yahoo.com>
4
+ # Licensed under the AGPLv3+ http://www.gnu.org/licenses/agpl.txt
5
+
6
+ require 'rubygems'
7
+ require 'spec'
8
+
9
+ CURRENT_DIR=File.dirname(__FILE__)
10
+ $: << File.expand_path(CURRENT_DIR + "/../lib")
11
+
12
+ require 'rxsd'
13
+ include RXSD
14
+ include RXSD::XSD
@@ -0,0 +1,105 @@
1
+ # tests the translator module
2
+ #
3
+ # Copyright (C) 2010 Mohammed Morsi <movitto@yahoo.com>
4
+ # See COPYING for the License of this software
5
+
6
+ require File.dirname(__FILE__) + '/spec_helper'
7
+
8
+ describe "Translator" do
9
+
10
+ # FIXME test child_attributes on all XSD classes!
11
+
12
+ before(:each) do
13
+ @data = '<schema xmlns:xs="http://www.w3.org/2001/XMLSchema">' +
14
+ '<xs:simpleType name="MyStrType">'+
15
+ ' <xs:restriction base="xs:string" />' +
16
+ '</xs:simpleType>' +
17
+ '<xs:simpleType name="MyFArrType">'+
18
+ ' <xs:list itemType="xs:float" />' +
19
+ '</xs:simpleType>' +
20
+ '<xs:complexType id="ct1" name="MyType">' +
21
+ '<xs:complexContent id="cc1">' +
22
+ '<xs:extension id="e1" base="xs:string">' +
23
+ '<xs:attribute name="my_s" type="xs:string"/>' +
24
+ '<xs:attribute name="my_a" type="MyFArrType" />' +
25
+ '</xs:extension>' +
26
+ '</xs:complexContent>' +
27
+ '</xs:complexType>' +
28
+ '<xs:element name="Kaboom" type="MyStrType"/>' +
29
+ '<xs:element name="Foomanchu" type="xs:boolean"/>' +
30
+ '<xs:element name="MoMoney" type="MyType"/>' +
31
+ '</schema>'
32
+ end
33
+
34
+ it "should generate correct schema tags" do
35
+ schema = Parser.parse_xsd :raw => @data
36
+ tags = schema.tags
37
+ tags.size.should == 5
38
+ tags.has_key?("Kaboom").should be_true
39
+ tags.has_key?("Foomanchu").should be_true
40
+ tags.has_key?("MoMoney").should be_true
41
+ tags.has_key?("MoMoney:my_s").should be_true
42
+ tags.has_key?("MoMoney:my_a").should be_true
43
+ tags["Kaboom"].should_not be_nil
44
+ end
45
+
46
+ #def test_schema_all_builders
47
+ #end
48
+
49
+ it "should generate ruby classes" do
50
+ schema = Parser.parse_xsd :raw => @data
51
+ classes = schema.to :ruby_classes
52
+ classes.size.should == 6
53
+ classes.include?(XSDFloat).should be_true
54
+ classes.include?(Array).should be_true
55
+ classes.include?(String).should be_true
56
+ classes.include?(Boolean).should be_true
57
+ classes.include?(Kaboom).should be_true
58
+ classes.include?(MoMoney).should be_true
59
+ momoney = MoMoney.new
60
+ momoney.method(:my_s).should_not be_nil
61
+ momoney.method(:my_s=).should_not be_nil
62
+ momoney.method(:my_a).should_not be_nil
63
+ momoney.method(:my_a=).should_not be_nil
64
+ end
65
+
66
+ it "should generate ruby class definitions" do
67
+ schema = Parser.parse_xsd :raw => @data
68
+ classes = schema.to :ruby_definitions
69
+ classes.size.should == 6
70
+ classes.include?("class XSDFloat\nend").should be_true
71
+ classes.include?("class Array\nend").should be_true
72
+ classes.include?("class String\nend").should be_true
73
+ classes.include?("class Boolean\nend").should be_true
74
+ classes.include?("class Kaboom < String\nend").should be_true
75
+ classes.include?("class MoMoney < String\n" +
76
+ "attr_accessor :my_s\n" +
77
+ "attr_accessor :my_a\n" +
78
+ "end").should be_true
79
+ end
80
+
81
+ it "should generate ruby objects" do
82
+ schema = Parser.parse_xsd :raw => @data
83
+ classes = schema.to :ruby_classes
84
+
85
+ instance = '<Kaboom>yo</Kaboom>'
86
+ schema_instance = Parser.parse_xml :raw => instance
87
+ objs = schema_instance.to :ruby_objects, :schema => schema
88
+ objs.size.should == 1
89
+ objs.collect { |o| o.class }.include?(Kaboom).should be_true
90
+ objs.find { |o| o.class == Kaboom }.should == "yo"
91
+
92
+ instance = '<Foomanchu>true</Foomanchu>'
93
+ schema_instance = Parser.parse_xml :raw => instance
94
+ objs = schema_instance.to :ruby_objects, :schema => schema
95
+ objs.size.should == 1
96
+ objs[0].should == true
97
+
98
+ instance = '<MoMoney my_s="abc" />'
99
+ schema_instance = Parser.parse_xml :raw => instance
100
+ objs = schema_instance.to :ruby_objects, :schema => schema
101
+ objs.size.should == 1
102
+ objs.collect { |o| o.class }.include?(MoMoney).should be_true
103
+ objs.find { |o| o.class == MoMoney }.my_s.should == "abc"
104
+ end
105
+ end
@@ -0,0 +1,47 @@
1
+ # rxsd types tests
2
+ #
3
+ # Copyright (C) 2010 Mohammed Morsi <movitto@yahoo.com>
4
+ # See COPYING for the License of this software
5
+
6
+ require File.dirname(__FILE__) + '/spec_helper'
7
+
8
+ describe "RXSD Types" do
9
+
10
+ # FIXME DateTime
11
+
12
+ it "should convert string to/from bool" do
13
+ "true".to_b.should be_true
14
+ "false".to_b.should be_false
15
+ lambda {
16
+ "foobar".to_b
17
+ }.should raise_error(ArgumentError)
18
+
19
+ String.from_s("money").should == "money"
20
+ end
21
+
22
+ it "should convert bool to/from string" do
23
+ Boolean.from_s("true").should be_true
24
+ Boolean.from_s("false").should be_false
25
+ end
26
+
27
+ it "should convert char to/from string" do
28
+ Char.from_s("c").should == "c"
29
+ end
30
+
31
+ it "should convert int to/from string" do
32
+ XSDInteger.from_s("123").should == 123
33
+ end
34
+
35
+ it "should convert float to/from string" do
36
+ XSDFloat.from_s("4.25").should == 4.25
37
+ end
38
+
39
+ it "should convert array to/from string" do
40
+ arr = Array.from_s "4 9 50 123", XSDInteger
41
+ arr.size.should == 4
42
+ arr.include?(4).should be_true
43
+ arr.include?(9).should be_true
44
+ arr.include?(50).should be_true
45
+ arr.include?(123).should be_true
46
+ end
47
+ end
data/spec/xml_spec.rb ADDED
@@ -0,0 +1,189 @@
1
+ # tests the xml modules
2
+ #
3
+ # Copyright (C) 2010 Mohammed Morsi <movitto@yahoo.com>
4
+ # See COPYING for the License of this software
5
+
6
+ require File.dirname(__FILE__) + '/spec_helper'
7
+
8
+ describe "RXSD::XML" do
9
+
10
+ it "should provide root node given adapter and xml data" do
11
+ root_node = XML::Node.factory :backend => :libxml, :xml => "<schema/>"
12
+ root_node.is_a?(XML::LibXMLNode).should be_true
13
+ end
14
+
15
+ it "should return correct root node" do
16
+ child = MockXMLNode.new
17
+ parent = MockXMLNode.new
18
+ gp = MockXMLNode.new
19
+
20
+ child.root.should == child
21
+ child.test_parent = parent
22
+ child.root.should == parent
23
+ parent.root.should == parent
24
+ parent.test_parent = gp
25
+ child.root.should == gp
26
+ parent.root.should == gp
27
+ end
28
+
29
+ it "should instantiate all children of a specified class type from xml" do
30
+ child1 = MockXMLNode.new :name => MockXMLEntity.tag_name
31
+ child2 = MockXMLNode.new :name => MockXMLEntity.tag_name
32
+ child3 = MockXMLNode.new :name => "foobar"
33
+ parent = MockXMLNode.new
34
+ parent.children << child1 << child2 << child3
35
+
36
+ children = parent.children_objs(MockXMLEntity)
37
+ children.size.should == 2
38
+ children[0].class.should == MockXMLEntity
39
+ children[1].class.should == MockXMLEntity
40
+ end
41
+
42
+ it "should return value attributes of all children w/ specified name" do
43
+ child1 = MockXMLNode.new :name => MockXMLEntity.tag_name
44
+ child2 = MockXMLNode.new :name => MockXMLEntity.tag_name
45
+ child3 = MockXMLNode.new :name => MockXMLEntity.tag_name
46
+ child4 = MockXMLNode.new :name => "foobar"
47
+ parent = MockXMLNode.new
48
+ parent.children << child1 << child2 << child3
49
+
50
+ children = parent.child_values(MockXMLEntity.tag_name)
51
+ children.size.should == 3
52
+ children[0].should == 'pi'
53
+ children[1].should == 'pi'
54
+ children[2].should == 'pi'
55
+ end
56
+
57
+ end
58
+
59
+ describe "RXSD::LibXMLAdapter" do
60
+
61
+ before(:each) do
62
+ @test_xml =
63
+ "<schema xmlns:h='http://test.host/ns.xml' xmlns:a='aaa' >" +
64
+ "<entity some_attr='foo' another_attr='bar'><child child_attr='123' /></entity>" +
65
+ "<other_entity>some text</other_entity>" +
66
+ "</schema>"
67
+ end
68
+
69
+ it "should parse xml children" do
70
+ root = XML::LibXMLNode.xml_root(@test_xml)
71
+ root.children.size.should == 2
72
+ root.children.each { |c| c.class.should == XML::LibXMLNode }
73
+ root.children.collect { |c| c.name }.include?("entity").should be_true
74
+ root.children.collect { |c| c.name }.include?("other_entity").should be_true
75
+ root.children.collect { |c| c.name }.include?("foo_entity").should be_false
76
+
77
+ root.children[0].children.size.should == 1
78
+ root.children[1].children.size.should == 0
79
+ end
80
+
81
+ it "should parse xml names" do
82
+ root = XML::LibXMLNode.xml_root(@test_xml)
83
+ root.name.should == "schema"
84
+ root.children[0].name.should == "entity"
85
+ root.children[1].name.should == "other_entity"
86
+ root.children[0].children[0].name.should == "child"
87
+ end
88
+
89
+ it "should parse xml attributes" do
90
+ root = XML::LibXMLNode.xml_root(@test_xml)
91
+ root.children[0].attrs.should == {'some_attr' => 'foo', 'another_attr' => 'bar'}
92
+ root.children[0].children[0].attrs.should == {'child_attr' => '123' }
93
+ end
94
+
95
+ it "should identify and return parent" do
96
+ root = XML::LibXMLNode.xml_root(@test_xml)
97
+
98
+ root.parent?.should be_false
99
+ root.parent.should be_nil
100
+
101
+ root.children[0].parent?.should be_true
102
+ root.children[0].parent.should == root
103
+
104
+ root.children[1].parent?.should be_true
105
+ root.children[1].parent.should == root
106
+
107
+ root.children[0].children[0].parent?.should be_true
108
+ root.children[0].children[0].parent.should == root.children[0]
109
+ end
110
+
111
+ it "should identify text and return content" do
112
+ root = XML::LibXMLNode.xml_root(@test_xml)
113
+ root.children[0].text?.should be_false
114
+ root.children[1].text?.should be_true
115
+ root.children[0].children[0].text?.should be_false
116
+
117
+ root.children[1].content.should == "some text"
118
+ end
119
+
120
+ it "should return namespaces" do
121
+ root = XML::LibXMLNode.xml_root(@test_xml)
122
+ root.namespaces.size.should == 2
123
+ root.namespaces.collect { |ns| ns.to_s }.include?('h:http://test.host/ns.xml').should be_true
124
+ #root.children[0].namespaces.size.should == 0 # children share the namespace apparently
125
+ end
126
+
127
+ end
128
+
129
+
130
+ class MockXMLEntity
131
+ def self.tag_name
132
+ "mock_xml_entity"
133
+ end
134
+
135
+ def self.from_xml(node)
136
+ MockXMLEntity.new
137
+ end
138
+ end
139
+
140
+ class MockXMLNode < XML::Node
141
+ attr_accessor :tag_name
142
+
143
+ attr_accessor :test_attrs
144
+
145
+ attr_accessor :test_parent
146
+
147
+ attr_accessor :test_children
148
+
149
+ def initialize(args = {})
150
+ @tag_name = args[:name] if args.has_key? :name
151
+
152
+ @test_parent = nil
153
+ @test_children = []
154
+ @test_children += args[:children] if args.has_key? :children
155
+ end
156
+
157
+ def name
158
+ @tag_name
159
+ end
160
+
161
+ def attrs
162
+ {:str_attr => "foobar", :int_attr => 50, :float_attr => 1.2, 'value' => 'pi'}
163
+ end
164
+
165
+ def parent?
166
+ !@test_parent.nil?
167
+ end
168
+
169
+ def parent
170
+ @test_parent
171
+ end
172
+
173
+ def children
174
+ @test_children
175
+ end
176
+
177
+ def text?
178
+ false
179
+ end
180
+
181
+ def content
182
+ "contents"
183
+ end
184
+
185
+ def namespaces
186
+ []
187
+ end
188
+ end
189
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rxsd
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.2"
4
+ version: "0.3"
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mohammed Morsi
@@ -9,91 +9,81 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-09 00:00:00 -05:00
12
+ date: 2010-03-18 00:00:00 -04:00
13
13
  default_executable:
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
16
- name: libxml-ruby
17
- type: :runtime
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
20
- requirements:
21
- - - ">="
22
- - !ruby/object:Gem::Version
23
- version: 1.1.3
24
- version:
25
- - !ruby/object:Gem::Dependency
26
- name: activesupport
27
- type: :runtime
28
- version_requirement:
29
- version_requirements: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: 2.3.4
34
- version:
35
- description: RXSD is a library to translate xsd schemas and xml implementations into ruby classes/objects
14
+ dependencies: []
15
+
16
+ description: A library to translate xsd schemas and xml implementations into ruby classes/objects
36
17
  email: movitto@yahoo.com
37
18
  executables: []
38
19
 
39
20
  extensions: []
40
21
 
41
- extra_rdoc_files:
42
- - README
22
+ extra_rdoc_files: []
23
+
43
24
  files:
44
- - LICENSE
45
- - COPYING
46
25
  - bin/rxsd-test.rb
26
+ - bin/gen_ruby_definitions.rb
47
27
  - lib/rxsd.rb
48
- - lib/rxsd/builder.rb
49
- - lib/rxsd/builtin_types.rb
50
- - lib/rxsd/common.rb
51
- - lib/rxsd/exceptions.rb
52
- - lib/rxsd/libxml_adapter.rb
53
- - lib/rxsd/loader.rb
54
- - lib/rxsd/parser.rb
55
- - lib/rxsd/resolver.rb
56
- - lib/rxsd/translator.rb
57
28
  - lib/rxsd/xml.rb
58
- - lib/rxsd/builders/ruby_class.rb
59
- - lib/rxsd/builders/ruby_definition.rb
60
- - lib/rxsd/builders/ruby_object.rb
29
+ - lib/rxsd/resolver.rb
30
+ - lib/rxsd/common.rb
31
+ - lib/rxsd/xsd/group.rb
61
32
  - lib/rxsd/xsd/attribute_group.rb
33
+ - lib/rxsd/xsd/simple_content.rb
34
+ - lib/rxsd/xsd/list.rb
62
35
  - lib/rxsd/xsd/attribute.rb
36
+ - lib/rxsd/xsd/restriction.rb
63
37
  - lib/rxsd/xsd/choice.rb
64
- - lib/rxsd/xsd/complex_content.rb
65
- - lib/rxsd/xsd/complex_type.rb
66
38
  - lib/rxsd/xsd/element.rb
39
+ - lib/rxsd/xsd/complex_type.rb
40
+ - lib/rxsd/xsd/complex_content.rb
67
41
  - lib/rxsd/xsd/extension.rb
68
- - lib/rxsd/xsd/group.rb
69
- - lib/rxsd/xsd/list.rb
70
- - lib/rxsd/xsd/restriction.rb
71
42
  - lib/rxsd/xsd/schema.rb
72
- - lib/rxsd/xsd/sequence.rb
73
- - lib/rxsd/xsd/simple_content.rb
74
43
  - lib/rxsd/xsd/simple_type.rb
75
- - README
44
+ - lib/rxsd/xsd/sequence.rb
45
+ - lib/rxsd/translator.rb
46
+ - lib/rxsd/loader.rb
47
+ - lib/rxsd/parser.rb
48
+ - lib/rxsd/libxml_adapter.rb
49
+ - lib/rxsd/builtin_types.rb
50
+ - lib/rxsd/builders/ruby_object.rb
51
+ - lib/rxsd/builders/ruby_definition.rb
52
+ - lib/rxsd/builders/ruby_class.rb
53
+ - lib/rxsd/exceptions.rb
54
+ - lib/rxsd/builder.rb
55
+ - COPYING
56
+ - LICENSE
57
+ - Rakefile
58
+ - README.rdoc
59
+ - spec/spec_helper.rb
60
+ - spec/translator_spec.rb
61
+ - spec/resolver_spec.rb
62
+ - spec/parser_spec.rb
63
+ - spec/types_spec.rb
64
+ - spec/loader_spec.rb
65
+ - spec/xml_spec.rb
66
+ - spec/builder_spec.rb
76
67
  has_rdoc: true
77
- homepage: http://projects.morsi.org/RXSD
68
+ homepage: http://morsi.org/projects/RXSD
78
69
  licenses: []
79
70
 
80
71
  post_install_message:
81
- rdoc_options:
82
- - --inline-source
83
- - --charset=UTF-8
72
+ rdoc_options: []
73
+
84
74
  require_paths:
85
75
  - lib
86
76
  required_ruby_version: !ruby/object:Gem::Requirement
87
77
  requirements:
88
78
  - - ">="
89
79
  - !ruby/object:Gem::Version
90
- version: "0"
80
+ version: 1.8.1
91
81
  version:
92
82
  required_rubygems_version: !ruby/object:Gem::Requirement
93
83
  requirements:
94
84
  - - ">="
95
85
  - !ruby/object:Gem::Version
96
- version: 1.3.1
86
+ version: 1.3.3
97
87
  version:
98
88
  requirements: []
99
89
 
@@ -101,6 +91,6 @@ rubyforge_project:
101
91
  rubygems_version: 1.3.5
102
92
  signing_key:
103
93
  specification_version: 3
104
- summary: RXSD is a library to translate xsd schemas and xml implementations into ruby classes/objects
94
+ summary: A library to translate xsd schemas and xml implementations into ruby classes/objects
105
95
  test_files: []
106
96