| 186 | | |
|---|
| 187 | | |
|---|
| 188 | | class XmlNodeTest < Test::Unit::TestCase |
|---|
| 189 | | def test_all |
|---|
| 190 | | xn = XmlNode.from_xml(%{<?xml version="1.0" encoding="UTF-8"?> |
|---|
| 191 | | <response success='true'> |
|---|
| 192 | | <page title='Ajax Summit' id='1133' email_address='ry87ib@backpackit.com'> |
|---|
| 193 | | <description>With O'Reilly and Adaptive Path</description> |
|---|
| 194 | | <notes> |
|---|
| 195 | | <note title='Hotel' id='1020' created_at='2005-05-14 16:41:11'> |
|---|
| 196 | | Staying at the Savoy |
|---|
| 197 | | </note> |
|---|
| 198 | | </notes> |
|---|
| 199 | | <tags> |
|---|
| 200 | | <tag name='Technology' id='4' /> |
|---|
| 201 | | <tag name='Travel' id='5' /> |
|---|
| 202 | | </tags> |
|---|
| 203 | | </page> |
|---|
| 204 | | </response> |
|---|
| 205 | | } |
|---|
| 206 | | ) |
|---|
| 207 | | assert_equal 'UTF-8', xn.node.document.encoding |
|---|
| 208 | | assert_equal '1.0', xn.node.document.version |
|---|
| 209 | | assert_equal 'true', xn['success'] |
|---|
| 210 | | assert_equal 'response', xn.node_name |
|---|
| 211 | | assert_equal 'Ajax Summit', xn.page['title'] |
|---|
| 212 | | assert_equal '1133', xn.page['id'] |
|---|
| 213 | | assert_equal "With O'Reilly and Adaptive Path", xn.page.description.node_value |
|---|
| 214 | | assert_equal nil, xn.nonexistent |
|---|
| 215 | | assert_equal "Staying at the Savoy", xn.page.notes.note.node_value.strip |
|---|
| 216 | | assert_equal 'Technology', xn.page.tags.tag[0]['name'] |
|---|
| 217 | | assert_equal 'Travel', xn.page.tags.tag[1][:name] |
|---|
| 218 | | matches = xn.xpath('//@id').map{ |id| id.to_i } |
|---|
| 219 | | assert_equal [4, 5, 1020, 1133], matches.sort |
|---|
| 220 | | matches = xn.xpath('//tag').map{ |tag| tag['name'] } |
|---|
| 221 | | assert_equal ['Technology', 'Travel'], matches.sort |
|---|
| 222 | | assert_equal "Ajax Summit", xn.page['title'] |
|---|
| 223 | | xn.page['title'] = 'Ajax Summit V2' |
|---|
| 224 | | assert_equal "Ajax Summit V2", xn.page['title'] |
|---|
| 225 | | assert_equal "Staying at the Savoy", xn.page.notes.note.node_value.strip |
|---|
| 226 | | xn.page.notes.note.node_value = "Staying at the Ritz" |
|---|
| 227 | | assert_equal "Staying at the Ritz", xn.page.notes.note.node_value.strip |
|---|
| 228 | | assert_equal '5', xn.page.tags.tag[1][:id] |
|---|
| 229 | | xn.page.tags.tag[1]['id'] = '7' |
|---|
| 230 | | assert_equal '7', xn.page.tags.tag[1]['id'] |
|---|
| 231 | | end |
|---|
| 232 | | |
|---|
| 233 | | |
|---|
| 234 | | def test_small_entry |
|---|
| 235 | | node = XmlNode.from_xml('<entry>hi</entry>') |
|---|
| 236 | | assert_equal 'hi', node.node_value |
|---|
| 237 | | end |
|---|
| 238 | | |
|---|
| 239 | | end |
|---|