| 1 | # encoding: utf-8 |
|---|
| 2 | %w[ ../ext ./ext ../lib ./lib ].each { |lp| $: << lp } |
|---|
| 3 | |
|---|
| 4 | require 'fastxml' |
|---|
| 5 | |
|---|
| 6 | describe FastXml::Node, ' functionality' do |
|---|
| 7 | before(:all) do |
|---|
| 8 | data_raw = open( "./test_data/hasno_feed.xml" ) |
|---|
| 9 | @data_ary = data_raw.readlines |
|---|
| 10 | data_raw.close |
|---|
| 11 | @data_str = @data_ary.join('') |
|---|
| 12 | @doc = FastXml::Doc.new( @data_str ) |
|---|
| 13 | @node = @doc.root |
|---|
| 14 | end |
|---|
| 15 | |
|---|
| 16 | it 'should be able to handle xpath searches' do |
|---|
| 17 | @node.should respond_to( :search ) |
|---|
| 18 | @node.search( '/feed' ).length.should >= 1 |
|---|
| 19 | end |
|---|
| 20 | |
|---|
| 21 | it 'should support hpricot-style xpath queries using the / operator' do |
|---|
| 22 | @node.should respond_to( '/' ) |
|---|
| 23 | (@node/"feed").length.should >= 1 |
|---|
| 24 | (@node/"feed").length.should == @node.search( '/feed' ).length |
|---|
| 25 | end |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | it 'should provide a children accessor' do |
|---|
| 29 | @node.should respond_to( :children ) |
|---|
| 30 | @node.children.should_not be_nil |
|---|
| 31 | end |
|---|
| 32 | |
|---|
| 33 | it 'should provide a children accessor that includes Enumerable' do |
|---|
| 34 | @node.children.kind_of?( Enumerable ) |
|---|
| 35 | @node.children.respond_to?( :each ).should == true |
|---|
| 36 | @node.children.respond_to?( :first ).should == true |
|---|
| 37 | @node.children.respond_to?( :last ).should == true |
|---|
| 38 | @node.children.respond_to?( :length ).should == true |
|---|
| 39 | end |
|---|
| 40 | |
|---|
| 41 | it 'should provide a name accessor' do |
|---|
| 42 | @node.should respond_to( :name ) |
|---|
| 43 | @node.name.should_not be_nil |
|---|
| 44 | @node.name.kind_of?(String).should == true |
|---|
| 45 | @node.name.length.should > 0 |
|---|
| 46 | end |
|---|
| 47 | |
|---|
| 48 | it 'should provide a to_s method' do |
|---|
| 49 | @node.should respond_to( :to_s ) |
|---|
| 50 | @node.to_s.should_not be_nil |
|---|
| 51 | @node.to_s.kind_of?(String).should == true |
|---|
| 52 | @node.to_s.length.should > 0 |
|---|
| 53 | end |
|---|
| 54 | |
|---|
| 55 | it 'should provide valid nodes from searches' do |
|---|
| 56 | entries = @node.search( '//entry' ) |
|---|
| 57 | entries.should_not be_nil |
|---|
| 58 | entries.length.should > 0 |
|---|
| 59 | entries.first.should_not be_nil |
|---|
| 60 | entries.first.to_s.should_not be_nil |
|---|
| 61 | end |
|---|
| 62 | |
|---|
| 63 | it 'should provide an inspect method' do |
|---|
| 64 | @node.should respond_to( :inspect ) |
|---|
| 65 | @node.inspect.should_not be_nil |
|---|
| 66 | @node.inspect.kind_of?(String) |
|---|
| 67 | @node.inspect.length.should > 0 |
|---|
| 68 | end |
|---|
| 69 | |
|---|
| 70 | it 'should provide an attribute accessor method named attr' do |
|---|
| 71 | @node.should respond_to( 'attr' ) |
|---|
| 72 | @node.attr.should respond_to( '[]' ) |
|---|
| 73 | @node.attr[:ab].should be_nil |
|---|
| 74 | @node.attr["ab"].should be_nil |
|---|
| 75 | end |
|---|
| 76 | |
|---|
| 77 | it 'should provide an attribute mutator method that responds to symbols' do |
|---|
| 78 | @node.should respond_to( 'attr' ) |
|---|
| 79 | @node.attr.should respond_to( '[]' ) |
|---|
| 80 | @node.attr[:ab] = "test" |
|---|
| 81 | @node.attr[:ab].should == "test" |
|---|
| 82 | @node.attr["ab"].should == "test" |
|---|
| 83 | @node.attr["ab"].kind_of?(String).should == true |
|---|
| 84 | end |
|---|
| 85 | |
|---|
| 86 | it 'should provide a next method' do |
|---|
| 87 | @node.should respond_to( :next ) |
|---|
| 88 | first_chld = @node.children.first |
|---|
| 89 | first_chld.next.should_not be_nil |
|---|
| 90 | end |
|---|
| 91 | |
|---|
| 92 | it 'should provide a prev method' do |
|---|
| 93 | @node.should respond_to( :prev ) |
|---|
| 94 | first_chld = @node.children.first |
|---|
| 95 | first_chld.prev.should be_nil |
|---|
| 96 | first_chld.next.prev.should_not be_nil |
|---|
| 97 | end |
|---|
| 98 | |
|---|
| 99 | it 'should provide an xpath method returning the xpath to the node' do |
|---|
| 100 | @node.should respond_to( :xpath ) |
|---|
| 101 | @node.xpath.should_not be_nil |
|---|
| 102 | @node.xpath.kind_of?(String).should == true |
|---|
| 103 | end |
|---|
| 104 | |
|---|
| 105 | it 'should provide a parent method' do |
|---|
| 106 | @node.should respond_to( :parent ) |
|---|
| 107 | @node.parent.should_not be_nil |
|---|
| 108 | @node.parent.kind_of?(FastXml::Node).should == true |
|---|
| 109 | end |
|---|
| 110 | |
|---|
| 111 | it 'should provide an inner_xml method' do |
|---|
| 112 | @node.should respond_to( :inner_xml ) |
|---|
| 113 | @node.inner_xml.should_not be_nil |
|---|
| 114 | @node.inner_xml.kind_of?(String).should == true |
|---|
| 115 | end |
|---|
| 116 | |
|---|
| 117 | it 'should provide a children_of_type method' do |
|---|
| 118 | @node.should respond_to( :children_of_type ) |
|---|
| 119 | end |
|---|
| 120 | |
|---|
| 121 | it 'should provide an at method' do |
|---|
| 122 | @node.should respond_to( :at ) |
|---|
| 123 | @node.at( "entry" ).should_not be_nil |
|---|
| 124 | end |
|---|
| 125 | |
|---|
| 126 | it 'should provide a content accessor' do |
|---|
| 127 | @node.should respond_to( :content ) |
|---|
| 128 | @node.content.should_not be_nil |
|---|
| 129 | @node.content.kind_of?(String).should == true |
|---|
| 130 | end |
|---|
| 131 | |
|---|
| 132 | it 'should provide a content mutator' do |
|---|
| 133 | @node.should respond_to( :content= ) |
|---|
| 134 | @node.content = "test" |
|---|
| 135 | @node.content.should_not be_nil |
|---|
| 136 | @node.content.should == "test" |
|---|
| 137 | @node.content.kind_of?(String).should == true |
|---|
| 138 | end |
|---|
| 139 | end |
|---|