1
+ require File . expand_path ( File . dirname ( __FILE__ ) + '/../spec_helper' )
2
+ require "nokogiri"
3
+ require "om"
4
+
5
+ describe "OM::XML::Container" do
6
+
7
+ before ( :all ) do
8
+ class ContainerTest
9
+ include OM ::XML ::Container
10
+ end
11
+ end
12
+
13
+ before ( :each ) do
14
+ @container = ContainerTest . from_xml ( "<foo><bar>1</bar></foo>" )
15
+ end
16
+
17
+ it "should add .ng_xml accessor" do
18
+ @container . should respond_to ( :ng_xml )
19
+ @container . should respond_to ( :ng_xml= )
20
+ end
21
+
22
+ describe "new" do
23
+ it "should populate ng_xml with an instance of Nokogiri::XML::Document" do
24
+ @container . ng_xml . class . should == Nokogiri ::XML ::Document
25
+ end
26
+ end
27
+
28
+ describe '#xml_template' do
29
+ it "should return an empty xml document" do
30
+ ContainerTest . xml_template . to_xml . should == "<?xml version=\" 1.0\" ?>\n "
31
+ end
32
+ end
33
+
34
+ describe "#from_xml" do
35
+ it "should accept a String, parse it and store it in .ng_xml" do
36
+ Nokogiri ::XML ::Document . expects ( :parse ) . returns ( "parsed xml" )
37
+ container1 = ContainerTest . from_xml ( "<foo><bar>1</bar></foo>" )
38
+ container1 . ng_xml . should == "parsed xml"
39
+ end
40
+ it "should accept a File, parse it and store it in .ng_xml" do
41
+ file = fixture ( File . join ( "mods_articles" , "hydrangea_article1.xml" ) )
42
+ Nokogiri ::XML ::Document . expects ( :parse ) . returns ( "parsed xml" )
43
+ container1 = ContainerTest . from_xml ( file )
44
+ container1 . ng_xml . should == "parsed xml"
45
+ end
46
+ it "should accept Nokogiri nodes as input and leave them as-is" do
47
+ parsed_xml = Nokogiri ::XML ::Document . parse ( "<foo><bar>1</bar></foo>" )
48
+ container1 = ContainerTest . from_xml ( parsed_xml )
49
+ container1 . ng_xml . should == parsed_xml
50
+ end
51
+ it "should initialize from #xml_template if no xml is provided" do
52
+ ContainerTest . expects ( :xml_template ) . returns ( "fake template" )
53
+ ContainerTest . from_xml . ng_xml . should == "fake template"
54
+ end
55
+ end
56
+
57
+ describe ".to_xml" do
58
+ it "should call .ng_xml.to_xml" do
59
+ @container . ng_xml . expects ( :to_xml ) . returns ( "ng xml" )
60
+ @container . to_xml . should == "ng xml"
61
+ end
62
+
63
+ it 'should accept an optional Nokogiri::XML Document as an argument and insert its fields into that (mocked test)' do
64
+ doc = Nokogiri ::XML ::Document . parse ( "<test_xml/>" )
65
+ mock_new_node = mock ( "new node" )
66
+ doc . root . expects ( :add_child ) . with ( @container . ng_xml . root ) . returns ( mock_new_node )
67
+ result = @container . to_xml ( doc )
68
+ end
69
+
70
+ it 'should accept an optional Nokogiri::XML Document as an argument and insert its fields into that (functional test)' do
71
+ doc = Nokogiri ::XML ::Document . parse ( "<test_xml/>" )
72
+ @container . to_xml ( doc ) . should == "<?xml version=\" 1.0\" ?>\n <test_xml>\n <foo>\n <bar>1</bar>\n </foo>\n </test_xml>\n "
73
+ end
74
+
75
+ it 'should add to root of Nokogiri::XML::Documents, but add directly to the elements if a Nokogiri::XML::Node is passed in' do
76
+ mock_new_node = mock ( "new node" )
77
+ mock_new_node . stubs ( :to_xml ) . returns ( "foo" )
78
+
79
+ doc = Nokogiri ::XML ::Document . parse ( "<test_document/>" )
80
+ el = Nokogiri ::XML ::Node . new ( "test_element" , Nokogiri ::XML ::Document . new )
81
+ doc . root . expects ( :add_child ) . with ( @container . ng_xml . root ) . returns ( mock_new_node )
82
+ el . expects ( :add_child ) . with ( @container . ng_xml . root ) . returns ( mock_new_node )
83
+ @container . to_xml ( doc ) . should
84
+ @container . to_xml ( el )
85
+ end
86
+ end
87
+
88
+ end
0 commit comments