eim-xml-0.0.4/0000775000175000017500000000000011613235354012724 5ustar uwabamiuwabamieim-xml-0.0.4/Rakefile.utirake0000644000175000017500000002023111613235354016030 0ustar uwabamiuwabami# Utility for Rake # # Copyright (C) 2008, KURODA Hiraku # You can redistribute it and/or modify it under GPL3. require "rake/clean" require "rake/testtask" require "rdoc/task" require "rake/contrib/rubyforgepublisher" require "rubygems/package_task" class UtiRake include Rake::DSL def self.setup(opt={}, &proc) ur = new ur.setup(opt, &proc) rescue puts $!.class, $!.message, $!.backtrace end attr_reader :opt, :spec_proc, :cucumber_proc, :rdoc_proc, :gemspec_proc, :package_proc, :rcov_spec_proc def setup(opt={}, &proc) @opt = opt directory "external" CLEAN << "coverage" << "coverage.spec" << "coverage.cuke" << "doc" CLOBBER << "external" instance_eval(&proc) if proc if need_spec? begin @rspec2_flg = false require "rspec/core/rake_task" @rspec2_flg = true rescue LoadError require "spec/rake/spectask" end define_spec_task end if need_cucumber? require "cucumber/rake/task" define_cucumber_task end define_rdoc_task define_rcov_task define_package_task define_here_dependency define_alias_task if @alias_task end def rspec2?; @rspec2_flg; end def spec_task rspec2? ? RSpec::Core::RakeTask : Spec::Rake::SpecTask end def need_spec? File.directory?("spec") end def need_cucumber? File.directory?("features") end def spec(&proc) @spec_proc = proc end def cucumber(&proc) @cucumber_proc = proc end def rdoc(&proc) @rdoc_proc = proc end def gemspec(&proc) @gemspec_proc = proc end def rcov_spec(&proc) @rcov_spec_proc = proc end def package(&proc) @package_proc = proc end def no_here(task) @no_here_task = task end def no_here_task @no_here_task || "spec:lump" end def alias_task @alias_task = true end def hg(cmd) sh "hg #{cmd.to_s}" end def external(base_url, *libs) libs = libs.first if libs.first.is_a?(Array) namespace :external do directory "external/lib" libs.each do |lib| libdir = "external/#{lib}" file libdir => "external/lib" do if File.exist?("../#{lib}") Dir.chdir("external") do ln_s "../../#{lib}", "./", :force=>true end end hg "clone #{File.join(base_url, lib)} #{libdir}" unless File.exist?(libdir) Dir["#{libdir}/lib/*"].each do |f| base = File.basename(f) cd "external/lib" do ln_s "../#{lib}/lib/#{base}", "./" end end end if File.exist?(libdir) Dir["#{libdir}/lib/*"].each do |f| base = File.basename(f) file "external/lib/#{base}" => "external/lib" do cd "external/lib" do ln_s "../#{lib}/lib/#{base}", "./" end end task :setup => "external/lib/#{base}" end end desc "Setup external libraries" task :setup=>libdir end task :rake => :setup do libs.each do |lib| Dir.chdir "external/#{lib}" do sh "rake" end end end end @external = true end def external?; @external; end def define_rdoc_task Rake::RDocTask.new(:rdoc) do |rdoc| rdoc.options << "-S" rdoc.options << "-w" << "3" rdoc.options << "-c" << "UTF-8" rdoc.rdoc_files.include("lib/**/*.rb") rdoc_proc.call(rdoc) if rdoc_proc end task :doc do remove_entry_secure "doc" if File.directory?("doc") sh "rdoc -S -w 3 -c UTF-8 -d -x external" end end def define_package_task spec = Gem::Specification.new do |s| s.platform = Gem::Platform::RUBY s.files = FileList["Rakefile*", "lib/**/*", "spec/**/*"] s.version = "0.0.0.noversion" gemspec_proc.call(s) if gemspec_proc end gem = Gem::PackageTask.new(spec) do |t| t.need_tar_gz = true package_proc.call(t) if package_proc end task "utirake:copy_for_package" do mv "Rakefile.utirake", "Rakefile.utirake_#{$$}" cp "external/utirake/utirake.rb", "Rakefile.utirake" end file gem.package_dir_path => "utirake:copy_for_package" task :gem do rm "Rakefile.utirake" mv "Rakefile.utirake_#{$$}", "Rakefile.utirake" end end def publish(project_name, user_id) task :publish => "rdoc" do yield if block_given? Rake::RubyForgePublisher.new(project_name, user_id).upload end end FILE_SORT = lambda{|a, b| File.mtime(a)<=>File.mtime(b)} def spec_files @spec_files ||= FileList["./spec/**/*_spec.rb"].sort(&FILE_SORT).reverse end def set_spec_opts(spec) spec.verbose = false if rspec2? spec.rspec_opts ||= [] spec.rspec_opts << "-c" spec.rspec_opts << "-I" << "." << "-I" << "./lib" << "-I" << "./external/lib" else spec.spec_opts << "-c" spec.libs << "." << "./lib" << "./external/lib" end end def define_spec_task task :spec => "spec:apart" namespace :spec do spec_files.each do |f| desc "" spec_task.new(:apart) do |s| if rspec2? s.pattern = f else s.spec_files = FileList[f] end set_spec_opts(s) spec_proc.call(s) if spec_proc end end task(:apart).comment = "Run all specs separately" desc "Run all specs in a lump" spec_task.new(:lump) do |s| s.spec_files = spec_files unless rspec2? set_spec_opts(s) spec_proc.call(s) if spec_proc end desc "Run all specs to profile" spec_task.new(:profile) do |s| set_spec_opts(s) if rspec2? s.rspec_opts << "-p" else s.spec_opts << "-f" << "profile" end end `grep -sRn '#[[:space:]]*here\\([[:space:]]\\|$\\)' --include='*.rb' spec`.split(/\n/).map{|l| next nil unless l=~/\A(.*?):(\d+):/ [$1, $2.to_i] }.compact.sort{|a, b| FILE_SORT.call(a[0], b[0])}.reverse.each do |file, line| desc "" spec_task.new(:here) do |s| set_spec_opts(s) if rspec2? s.pattern = file s.rspec_opts << "-l#{line}" else s.spec_files = [file] s.spec_opts << "-l#{line}" end spec_proc.call(s) if spec_proc end end task :no_here => no_here_task end end def set_cucumber_opts(task) task.libs << "." cucumber_proc.call(task) if cucumber_proc end def define_cucumber_task Cucumber::Rake::Task.new do |t| set_cucumber_opts(t) end unless `grep -sRn '^[[:space:]]*@here$' features`.empty? Cucumber::Rake::Task.new("cucumber:here") do |t| t.cucumber_opts = %w[--tags @here] set_cucumber_opts(t) end end task "cucumber:no_here" => :cucumber end def define_here_dependency unless Rake::Task.task_defined?("spec:here") || Rake::Task.task_defined?("cucumber:here") task "spec:here" => "spec:no_here" if need_spec? && !Rake::Task.task_defined?("spec:here") task "cucumber:here" => "cucumber:no_here" if need_cucumber? && !Rake::Task.task_defined?("cucumber:here") end task("spec:here").comment = "Run spec only marked '# here'" task("cucumber:here").comment = "only tagged '@here'" end def rcov_opts(t, aggregation) t.rcov_opts ||= [] t.rcov_opts << "-I" << "./spec:./lib:./external/lib" t.rcov_opts << "--exclude" << "gems\/,features\/,external\/" t.rcov_opts << "--aggregate" << "coverage.data" if aggregation t.rcov = true end def define_rcov_each_task(aggregation) if need_spec? spec_task.new do |t| t.verbose = false rcov_opts(t, aggregation) if rspec2? t.rcov_opts << "-o" << "coverage.spec" unless aggregation else set_spec_opts(t) t.spec_files = spec_files t.rcov_dir = "coverage.spec" unless aggregation end rcov_spec_proc.call(t) if rcov_spec_proc end else task "spec" end if need_cucumber? Cucumber::Rake::Task.new do |t| set_cucumber_opts(t) rcov_opts(t, aggregation) t.rcov_opts << "-o" << "coverage.cuke" unless aggregation end else task "cucumber" end end def define_rcov_task namespace :rcov do define_rcov_each_task(false) end desc "Run specs and Cucumber using RCov" task "rcov:lump" do rm "coverage.data" if File.exist?("coverage.data") ns = namespace do define_rcov_each_task(true) end ns.tasks.each do |t| t.invoke end rm "coverage.data" end task "rcov:all" => %w[rcov:spec rcov:cucumber rcov:lump] end def define_alias_task if Rake::Task.task_defined?("spec:apart") task :apart => "spec:apart" task :lump => "spec:lump" task :here => "spec:here" task :profile => "spec:profile" end task :here => "cucumber:here" if Rake::Task.task_defined?("cucumber:here") end end eim-xml-0.0.4/spec/0000775000175000017500000000000011613235354013656 5ustar uwabamiuwabamieim-xml-0.0.4/spec/formatter_spec.rb0000644000175000017500000001242511613235354017222 0ustar uwabamiuwabamirequire "eim_xml/formatter" require "eim_xml/dsl" describe EimXML::Formatter do describe ".write" do it "should return output object" do s = "" EimXML::Formatter.write(EimXML::Element.new(:e), :out=>s).should be_equal(s) end it "should return string when destination not given" do r = EimXML::Formatter.write(EimXML::Element.new(:e)) r.should be_kind_of(String) end describe "should return formatted elements string" do def write(*arg) EimXML::Formatter.write(*arg) end it "(element not have content)"do write(EimXML::DSL.element(:e)).should == "\n" end it "(empty element which has attributes)" do r = (write(EimXML::DSL.element(:e, :a1=>"v1", :a2=>"v2")) =~ %r[]) r.should_not be_nil [$1, $2].sort.should == ["a1='v1'", "a2='v2'"] end it "(element in element)" do e = EimXML::DSL.element(:e) do element(:s) end write(e).should == < EOT end it "(elements in element)" do e = EimXML::DSL.element(:e) do element(:s1) element(:s2) end write(e).should == < EOT end it "(comment in element)" do e = EimXML::DSL.element(:e) do comment("multi\nline\n pre-indented\n comment") end write(e).should == < EOT end it "(string in element)" do e = EimXML::Element.new(:e) e.add("string") write(e).should == "\n string\n\n" esc = "&<>'\"" esc = "&<>\n'"" write(EimXML::Element.new(:e, :a=>"&<>\n'\"").add("&<>\n'\"")).should == "\n &<>\n '"\n\n" write(EimXML::Element.new(:e, :a=>"&<>\n'\"").add(EimXML::PCString.new("&<>\n'\"", true))).should == "\n &<>\n '\"\n\n" end it "(multi-line string in element)" do e = EimXML::Element.new(:e) e.add("multi\nline") write(e).should == < multi line EOT end describe "(preserve spaces" do it "name of element" do e = EimXML::DSL.element(:e) do element(:pre1) do element(:sub1).add("text") element(:sub2) end element(:pre2).add("multi\nline\ntext") element(:sub1).add("text") end s = < text multi line text text EOT write(e, :preservers=>[:pre1, :pre2]).should == s end it "class of element" do m = Module.new class m::Pre < EimXML::Element def initialize(n=nil) super(n||"pre") end end class m::P1 < m::Pre def initialize(n=nil) super(n||"p1") end end class m::P2 < m::P1 def initialize super("p2") end end e = EimXML::Element.new(:e) e << m::Pre.new.add("text\nwith\nnewline") e << m::Pre.new("dummy").add("t\nn") e << m::P1.new.add("t1\nn") e << m::P2.new.add("t2\nn") e << m::Pre.new.add(EimXML::Element.new(:s).add("t\ns")) e << m::P2.new.add(EimXML::Element.new(:s).add("t\ns2")) e << EimXML::Element.new(:s).add(EimXML::Element.new(:s).add("t\ns")) s = <
text
with
newline
t n t1 n t2 n
t
s
t s2 t s EOT write(e, :preservers=>[m::Pre]).should == s end end it "(all)" do s = < text2 text32 multi-line text sub52 sub54-1 sub54-2 EOT e = EimXML::DSL.element(:base) do element(:sub1) element(:sub2).add("text2") element(:sub3, :a1=>"v1") do element(:sub31) element(:sub32).add("text32") end element(:sub4).add("multi-line\ntext") element(:sub5) do element(:sub51) add("sub52") element(:sub53) add("sub54-1\nsub54-2") end end write(e).should == s end end end end describe EimXML::Formatter::ElementWrapper do before do @out = "" @opt = {:out=>@out, :preservers=>[], :a=>10, :b=>20} @formatter = EimXML::Formatter.new(@opt) @m = Module.new class @m::Wrapper < EimXML::Formatter::ElementWrapper def initialize(mocks) @mocks = mocks end def contents(option) @mocks end end @mocks = [mock(:m1).as_null_object, mock(:m2).as_null_object] @wrapper = @m::Wrapper.new(@mocks) @xml = EimXML::Element.new(:e) do |e| e << @wrapper end end describe "#each" do it "will give options from formatter" do @wrapper.should_receive(:contents).with(:a=>10, :b=>20).and_return([]) @formatter.write(@xml) end it "yield result of contents" do @mocks.each_with_index do |mock, index| mock.should_receive(:to_s).and_return("m#{index}") end @formatter.write(@xml) @out.should == "\n m0\n m1\n\n" end it "raise error when subclass of ElementWrapper is not implement #contents" do class @m::Wrapper2 < EimXML::Formatter::ElementWrapper; end @xml << @m::Wrapper2.new expect{@formatter.write(@xml)}.to raise_error(NoMethodError) end end end eim-xml-0.0.4/spec/dsl_spec.rb0000644000175000017500000001200711613235354015775 0ustar uwabamiuwabamirequire "eim_xml/dsl" module Module.new::M include EimXML EDSL = EimXML::DSL describe EimXML::DSL do it "scope is in instance of DSL" do outer = inner = nil e3 = e2 = nil block_executed = false e = EDSL.element(:out, :k1=>"v1") do outer = self e2 = element(:in, :k2=>"v2") do block_executed = true inner = self e3 = element(:deep) end end block_executed.should == true outer.should be_kind_of(EDSL) inner.should be_kind_of(EDSL) outer.should be_equal(inner) e.name.should == :out e[:k1].should == "v1" e[0].name.should == :in e[0][:k2].should == "v2" e[0][0].name.should == :deep e2.should be_equal(e[0]) e3.should be_equal(e[0][0]) end it "#comment" do Comment.should_receive(:new).with("comment").and_return(:success) EDSL.comment("comment").should == :success end it "#import_variables" do d = EDSL.new o = Object.new o.instance_variable_set("@v1", 1) o.instance_variable_set("@v2", "2") o.instance_variable_set("@_v3", :t) o.instance_variable_set("@__v4", 4) o.instance_variable_set("@_container", :t) orig_c = d.instance_variable_get("@_container") d.import_variables(o).should be_equal(d) d.instance_variable_get("@_container").should == orig_c d.instance_variables.map(&:to_s).sort.should == ["@v1", "@v2", "@__v4"].sort d.instance_variable_get("@v1").should == 1 d.instance_variable_get("@v2").should == "2" d.instance_variable_get("@__v4").should == 4 end describe "#_push" do before do m = Module.new class m::D < EimXML::DSL def call_push(c) _push(c) do element(:e) end end def exec element(:e) do element(:f) end end end @D = m::D end it "should return given container" do a = [] @D.new.call_push(a).should be_equal(a) a.should == [EimXML::Element.new(:e)] @D.new.exec.should == EimXML::Element.new(:e).add(EimXML::Element.new(:f)) end end end describe "Subclass of BaseDSL" do class DSL1 < EimXML::BaseDSL register([EimXML::Element, "call"]) register(Hash) register(String, Array, Object) end it "register" do lambda{EDSL.call(:dummy)}.should raise_error(NoMethodError) lambda{BaseDSL.call(:dummy)}.should raise_error(NoMethodError) lambda{DSL1.element(:dummy)}.should raise_error(NoMethodError) DSL1.call(:dummy).should be_kind_of(Element) DSL1.hash.should be_kind_of(Hash) DSL1.string.should be_kind_of(String) DSL1.array.should be_kind_of(Array) DSL1.object.should be_kind_of(Object) end end describe EimXML::OpenDSL do it "scope of block is one of outside" do @scope_checker_variable = 1 block_executed = false d = OpenDSL.new do |d| block_executed = true d.should be_kind_of(OpenDSL) d.container.should be_nil d.element(:base, :key1=>"v1") do @scope_checker_variable.should == 1 self.should_not be_kind_of(Element) d.container.should be_kind_of(Element) d.container.should == Element.new(:base, :key1=>"v1") d.element(:sub, :key2=>"v2") do d.container.should be_kind_of(Element) d.container.should == Element.new(:sub, :key2=>"v2") end d.element(:sub2).should == Element.new(:sub2) end end block_executed.should be_true end it "DSL methods return element" do d = OpenDSL.new d.container.should be_nil r = d.element(:base, :key1=>"v1") do d.element(:sub, :key2=>"v2") end r.should == EDSL.element(:base, :key1=>"v1") do element(:sub, :key2=>"v2") end end it "DSL method's block given instance of OpenDSL" do e = OpenDSL.new.element(:base) do |d| d.should be_kind_of(OpenDSL) d.container.name.should == :base d.element(:sub) do |d2| d2.should be_equal(d) end end e.should == EDSL.element(:base) do element(:sub) end end it "ensure reset container when error raised" do OpenDSL.new do |d| begin d.element(:base) do begin d.element(:sub) do raise "OK" end rescue RuntimeError => e raise unless e.message=="OK" d.container.name.should == :base raise end end rescue RuntimeError => e raise unless e.message=="OK" d.container.should == nil end end end it "respond to add" do r = OpenDSL.new.element(:base) do |d| d.add "text" d.element(:sub) do s = Element.new(:sub) s.add("sub text") d.add("sub text").should == s end end r.should == EDSL.element(:base) do add "text" element(:sub) do add "sub text" end end end it "respond to <<" do r = OpenDSL.new.element(:base) do |d| b = Element.new(:base) b << "text" << "next" (d << "text" << "next").should == b end r.should == EDSL.element(:base) do add "text" add "next" end end it "can call directly element method" do r = OpenDSL.element(:base) do |d| d.element(:sub) d.element(:sub2) end r.should == EDSL.element(:base) do element(:sub) element(:sub2) end end end end eim-xml-0.0.4/spec/eim_xml_spec.rb0000644000175000017500000002753011613235354016654 0ustar uwabamiuwabamirequire "eim_xml/dsl" module Module.new::M include EimXML EDSL = EimXML::DSL describe PCString do it ".encode" do PCString.encode("<>\"'&").should == "<>"'&" PCString.encode("&test;").should == "&test;" PCString.encode("&").should == "&amp;" PCString.encode(:'sym&').should == "sym&" end it ".new" do PCString.new("&").encoded_string.should == "&" PCString.new("&", true).encoded_string.should == "&" pcs = PCString.new(:'sym&') pcs.encoded_string.should == "sym&" pcs.src.should == :'sym&' end describe ".[]" do it "should return itself when given object is a PCString" do pcs = PCString.new("s") PCString[pcs].should equal(pcs) end it "should return PCString.new(obj) if given obj is not a PCString" do o = "str" r = PCString[o] r.is_a?(EimXML::PCString) r.src.should == o end end it "#==" do PCString.new("str").should == PCString.new("str") PCString.new("&").should == "&" PCString.new("&", true).should_not == "&" PCString.new("&", true).should == PCString.new("&", true) PCString.new("&").should == PCString.new("&", true) end describe "#write_to" do before do @pc = PCString.new("&") end it "should return encoded string" do @pc.write_to.should == "&amp;" end it "should return given destination" do s = "" @pc.write_to(s).should be_equal(s) end end end describe Comment do it ".new should raise error if given string include '--'" do lambda{Comment.new("--")}.should raise_error(ArgumentError) end describe "#write_to" do it "should return comment with markup" do Comment.new("flat comment").write_to.should == "" Comment.new("multi-line\ncomment").write_to.should == "" Comment.new("&").write_to.should == "" end it "should return given destination" do s = "" Comment.new("dummy").write_to(s).should be_equal(s) end end end describe Element do class Dummy < Element def chgname(name) self.name = name end end it "#name" do e = Element.new("el") e.name.should == :el lambda{e.name="changed"}.should raise_error(NoMethodError) d = Dummy.new("el1") d.name.should == :el1 d.chgname(:el2) d.name.should == :el2 d.chgname("el3") d.name.should == :el3 end it "#attributes should return hash whose keys are Symbol" do e = Element.new("el", "a1"=>"v1", :a2=>"v2", "a3"=>nil) e.name.should == :el e.attributes.should == {:a1=>"v1", :a2=>"v2", :a3=>nil} end it "#[]" do e = Element.new(:el, :attr=>"value") e << "test" e[:attr].should == "value" e[0].should == "test" end it "#add_attribute" do e = Element.new("el") e.add_attribute("key_str", "value1") e.add_attribute(:key_sym, "value2") e.attributes.should == {:key_str=>"value1", :key_sym=>"value2"} e.add_attribute(:nil, nil) e.attributes.should == {:key_str=>"value1", :key_sym=>"value2", :nil=>nil} end it "#del_attribute" do e = Element.new("el", {:a1=>"v1", :a2=>"v2"}) e.del_attribute("a1") e.attributes.should == {:a2=>"v2"} e.del_attribute(:a2) e.attributes.should == {} end it "#contents" do sub = Element.new("sub") e = Element.new("el") << "String1" << "String2" << sub e.contents.should == ["String1", "String2", sub] end it "#add" do e = Element.new("el").add(Element.new("sub")) e.should be_kind_of(Element) e.name.should == :el e = Element.new("el") e.add(Element.new("sub1")) e.add([Element.new("sub2").add("text"), "string"]) e.contents.should == [Element.new("sub1"), Element.new("sub2").add("text"), "string"] e = Element.new("el") e.add(nil) e.contents.size.should == 0 e = Element.new("el").add(:symbol) e.contents.should == [:symbol] e.to_s.should == "symbol" e = Element.new("super") << Element.new("sub") e.name.should == :super e.contents.should == [Element.new("sub")] end describe "#write_to" do it "should return flatten string" do Element.new("e").write_to.should == "" e = Element.new("super") e << Element.new("sub") e.write_to.should == "" e << Element.new("sub2") e.write_to.should == "" e = Element.new("super") << "content1" s = Element.new("sub") s << "content2" e << s e.write_to.should == "content1content2" e = Element.new("el") e.attributes["a1"] = "v1" e.attributes["a2"] = "'\"<>&" s = e.write_to s.should =~ /\A]*) \/>\z/ s.should =~ /a1='v1'/ s.should =~ /a2=''"<>&'/ end it "should return string without attribute whose value is nil or false" do s = EimXML::Element.new("e", :attr1=>"1", :attr2=>true, :attr3=>nil, :attr4=>false).write_to re = /\A\z/ s.should match(re) s =~ /\A\z/ [[$1, $2], [$3, $4]].sort.should == [["1", "1"], ["2", "true"]] end it "should return same string whenever name of element given with string or symbol" do sym = Element.new(:tag, :attr=>"value") str_name = Element.new("tag", :attr=>"value") str_attr = Element.new(:tag, "attr"=>"value") str_name.write_to.should == sym.write_to str_attr.write_to.should == sym.write_to end end it "encode special characters" do e = Element.new("el") << "&\"'<>" e << PCString.new("&\"'<>", true) e.attributes["key"] = PCString.new("&\"'<>", true) e.to_s.should == %['>&"'<>&\"'<>] end it "#dup" do e = Element.new("el") e.attributes["key"] = "value" e << "String" e << "Freeze".freeze s = Element.new("sub") s.attributes["subkey"] = "subvalue" e << s f = e.dup f.attributes.object_id.should == e.attributes.object_id f.contents.object_id.should == e.contents.object_id f.to_s.should == e.to_s end it "#clone" do e = Element.new("el") e.attributes["key"] = "value" e << "String" e << "Freeze".freeze s = Element.new("sub") s.attributes["subkey"] = "subvalue" e << s f = e.clone f.attributes.object_id.should == e.attributes.object_id f.contents.object_id.should == e.contents.object_id f.to_s.should == e.to_s end it "#==" do e1 = Element.new("el") e1.attributes["key"] = "value" s = Element.new("sub") s << "String" e1 << s e2 = e1.dup e2.should == e1 e3 = Element.new("e") e3.attributes["key"] = "value" s = Element.new("sub") s << "String" e3 << s e3.should_not == e1 e3 = Element.new("e") e3.attributes["k"] = "value" s = Element.new("sub") s << "String" e3 << s e3.should_not == e1 e3 = Element.new("e") e3.attributes["key"] = "v" s = Element.new("sub") s << "String" e3 << s e3.should_not == e1 e3 = Element.new("e") e3.attributes["key"] = "value" s = Element.new("sub") s << "S" e3 << s e3.should_not == e1 e3 = Element.new("e") e3.attributes["key"] = "value" s = Element.new("s") s << "String" e3 << s e3.should_not == e1 "string".should_not == e1 end describe ".new" do it "should convert name of attributes to Symbol" do e = Element.new(:e, "a"=>"v") e.attributes.keys.should == [:a] e[:a].should == "v" end it "with block" do base = nil e = Element.new("base") do |b| b["attr"]="value" b << Element.new("sub") base = b end base.object_id.should == e.object_id e2 = Element.new("base", :attr=>"value") e2 << Element.new("sub") e2.should == e e = Element.new("base") do |e| e <<= Element.new("sub1") do |e| e <<= Element.new("sub12") end e <<= Element.new("sub2") end base = Element.new("base") sub1 = Element.new("sub1") sub1 << Element.new("sub12") sub2 = Element.new("sub2") base << sub1 << sub2 e.should == base end end it "#match" do e = Element.new(:tag, :attr=>"value") e.match(:tag).should be_true e.match(:tag, :attr=>"value").should be_true e.match(:t).should be_false e.match(:tag, :attr2=>"value").should be_false e.match(:tag, :attr=>"value2").should be_false e.match(:tag, :attr=>/val/).should be_true e.match(Element.new(:tag)).should be_true e.match(Element.new(:tag, :attr=>"value")).should be_true e.match(Element.new(:tag, :attr=>/alu/)).should be_true e.match(Element.new(:t)).should be_false e.match(Element.new(:tag, :attr2=>"value")).should be_false e.match(Element.new(:tag, :attr=>"value2")).should be_false e.match(Element.new(:tag, :attr=>/aul/)).should be_false e.match(Element.new(:tag, :attr=>PCString.new("value"))).should be_true Element.new(:tag, :attr=>PCString.new("value")).should match(e) e.match(Element.new(:tag, :attr=>nil)).should be_false e.match(Element.new(:tag, :nonattr=>nil)).should be_true (!!e.match(/ag/)).should be_true (!!e.match(/elem/)).should be_false e.match(Element).should be_true e.match(Dummy).should be_false e.match(String).should be_false e = Element.new(:element) e << Element.new(:sub) e << "text" e.match(EDSL.element(:element){element(:sub)}).should be_true e.match(EDSL.element(:element){element(:other)}).should be_false e.match(EDSL.element(:element){add("text")}).should be_true e.match(EDSL.element(:element){add("other")}).should be_false e.match(EDSL.element(:element){add(/ex/)}).should be_true e.match(EDSL.element(:element){add(/th/)}).should be_false e.match(EDSL.element(:element){add(/sub/)}).should be_false e = Element.new(:t, :a=>"&") e.should match(Element.new(:t, :a=>"&")) e.should match(Element.new(:t, :a=>PCString.new("&", true))) e.should match(Element.new(:t, :a=>PCString.new("&"))) Element.new(:t, "a"=>"v").should match(Element.new(:t, :a=>"v")) end it "#=~" do e = Element.new(:tag, :attr=>"value", :a2=>"v2") e.should =~ :tag e.should =~ Element.new(:tag) e.should =~ Element.new(:tag, :a2=>"v2") e.should =~ Element.new(:tag, :attr=>/alu/) e.should =~ Element.new(:tag, :attr=>PCString.new("value")) e.should_not =~ :t e.should_not =~ Element.new(:t) e.should_not =~ Element.new(:tag, :attr=>/aul/) e = Element.new(:t, :a=>"&") e.should =~ Element.new(:t, :a=>"&") e.should =~ Element.new(:t, :a=>PCString.new("&", true)) e.should =~ Element.new(:t, :a=>PCString.new("&")) end %w[has? has_element? include?].each do |method| it "##{method}" do e = Element.new(:base) do |b| b <<= Element.new(:sub) do |s| s <<= Element.new(:deep) do |d| d << "text" d << PCString.new("&", true) d << "<" end end b <<= Element.new(:sub, :attr=>"value") end e.send(method, :sub).should be_true e.send(method, :sub, :attr=>"value").should be_true e.send(method, :sub, :attr=>"value", :attr2=>"").should be_false e.send(method, :deep).should be_true e.send(method, String).should be_true e.send(method, PCString).should be_true d = Element.new(:deep) d << "text" d << PCString.new("&", true) d << "<" e.send(method, d).should be_true d = Element.new(:deep) d << PCString.new("text", true) d << "&" d << PCString.new("<", true) e.send(method, d).should be_true end end it "#find" do s1 = Element.new(:sub) d = Element.new(:deep) d << "3rd" s1 << "2nd" << d s2 = Element.new(:sub, :attr=>"value") e = Element.new(:base) e << "1st" << s1 << s2 e.find(:deep).should be_kind_of(Element) e.find(:deep).name.should == :found e.find(:deep).contents.should == [d] e.find(:sub).contents.should == [s1, s2] e.find(//).contents.should == [e, s1, d, s2] e.find(:sub, :attr=>"value").contents.should == [s2] e.find(String).contents.should == ["1st", "2nd", "3rd"] end end end eim-xml-0.0.4/spec/xhtml_spec.rb0000644000175000017500000003233111613235354016351 0ustar uwabamiuwabamirequire "stringio" require "eim_xml/xhtml/dsl" module Module.new::M include EimXML include EimXML::XHTML XDSL = XHTML::DSL describe XHTML do it "DSL.base_ should raise NoMethodError" do lambda{XDSL.base_}.should raise_error(NoMethodError) end it "HTML" do h = HTML.new(:attr=>"value") h.should == Element.new(:html, :attr=>"value") h = HTML.new do |e| e <<= Element.new(:sub) end h2 = HTML.new h2 << Element.new(:sub) h2.should == h lambda{EimXML::DSL.html}.should raise_error(NoMethodError) XDSL.html(:key=>"v").should == HTML.new(:key=>"v") OpenDSL.html(:key=>"v").should == HTML.new(:key=>"v") h = HTML.new h.write_to.should == "" h.prefix='' h.write_to.should == %[\n] end it "HEAD" do HEAD.new.name.should == :head XDSL.head.should be_kind_of(HEAD) OpenDSL.head.should be_kind_of(HEAD) end it "META" do META.new.name.should == :meta XDSL.meta.should be_kind_of(META) OpenDSL.meta.should be_kind_of(META) end it "LINK" do LINK.new.name.should == :link XDSL.link.should be_kind_of(LINK) OpenDSL.link.should be_kind_of(LINK) end it "STYLE" do STYLE.new.name.should == :style XDSL.style.should be_kind_of(STYLE) OpenDSL.style.should be_kind_of(STYLE) end it "IMG" do IMG.new.name.should == :img XDSL.img.should be_kind_of(IMG) OpenDSL.img.should be_kind_of(IMG) end it "SCRIPT" do SCRIPT.new.name.should == :script XDSL.script.should be_kind_of(SCRIPT) OpenDSL.script.should be_kind_of(SCRIPT) end it "TITLE" do TITLE.new.name.should == :title XDSL.title.should be_kind_of(TITLE) OpenDSL.title.should be_kind_of(TITLE) end it "BODY" do BODY.new.name.should == :body XDSL.body.should be_kind_of(BODY) OpenDSL.body.should be_kind_of(BODY) end it "PRE" do PRE.new.name.should == :pre XDSL.pre.should be_kind_of(PRE) OpenDSL.pre.should be_kind_of(PRE) end it "Hn" do h1 = Hn.new(1) h6 = Hn.new(6) h1.name.should == :h1 h1.should be_kind_of(H1) h6.name.should == :h6 h6.should be_kind_of(H6) lambda{Hn.new(7)}.should raise_error(ArgumentError) lambda{Hn.new(0)}.should raise_error(ArgumentError) h = Hn.new(1, :key=>:value) do |hn| hn << "test" end h[:key].should == :value h[0].should == "test" [ [H1, XDSL.h1, OpenDSL.h1], [H2, XDSL.h2, OpenDSL.h2], [H3, XDSL.h3, OpenDSL.h3], [H4, XDSL.h4, OpenDSL.h4], [H5, XDSL.h5, OpenDSL.h5], [H6, XDSL.h6, OpenDSL.h6] ].each do |klass, dsl, od| dsl.should be_kind_of(klass) od.should be_kind_of(klass) end end it "P" do P.new.name.should == :p XDSL.p.should be_kind_of(P) OpenDSL.p.should be_kind_of(P) end it "A" do A.new.name.should == :a XDSL.a.should be_kind_of(A) OpenDSL.a.should be_kind_of(A) end it "EM" do EM.new.name.should == :em XDSL.em.should be_kind_of(EM) OpenDSL.em.should be_kind_of(EM) end it "STRONG" do STRONG.new.name.should == :strong XDSL.strong.should be_kind_of(STRONG) OpenDSL.strong.should be_kind_of(STRONG) end it "DIV" do DIV.new.name.should == :div XDSL.div.should be_kind_of(DIV) OpenDSL.div.should be_kind_of(DIV) end it "SPAN" do SPAN.new.name.should == :span XDSL.span.should be_kind_of(SPAN) OpenDSL.span.should be_kind_of(SPAN) end it "UL" do UL.new.name.should == :ul XDSL.ul.should be_kind_of(UL) OpenDSL.ul.should be_kind_of(UL) end it "OL" do OL.new.name.should == :ol XDSL.ol.should be_kind_of(OL) OpenDSL.ol.should be_kind_of(OL) end it "LI" do LI.new.name.should == :li XDSL.li.should be_kind_of(LI) OpenDSL.li.should be_kind_of(LI) end it "DL" do DL.new.name.should == :dl XDSL.dl.should be_kind_of(DL) OpenDSL.dl.should be_kind_of(DL) end it "DT" do DT.new.name.should == :dt XDSL.dt.should be_kind_of(DT) OpenDSL.dt.should be_kind_of(DT) end it "DD" do DD.new.name.should == :dd XDSL.dd.should be_kind_of(DD) OpenDSL.dd.should be_kind_of(DD) end it "TABLE" do TABLE.new.name.should == :table XDSL.table.should be_kind_of(TABLE) OpenDSL.table.should be_kind_of(TABLE) end it "CAPTION" do CAPTION.new.name.should == :caption XDSL.caption.should be_kind_of(CAPTION) OpenDSL.caption.should be_kind_of(CAPTION) end it "TR" do TR.new.name.should == :tr XDSL.tr.should be_kind_of(TR) OpenDSL.tr.should be_kind_of(TR) end it "TH" do TH.new.name.should == :th XDSL.th.should be_kind_of(TH) OpenDSL.th.should be_kind_of(TH) end it "TD" do TD.new.name.should == :td XDSL.td.should be_kind_of(TD) OpenDSL.td.should be_kind_of(TD) end it "FORM" do FORM.new.name.should == :form XDSL.form.should be_kind_of(FORM) OpenDSL.form.should be_kind_of(FORM) FORM.new.should_not include(HIDDEN) end it "FORM.new should be able to receive CGI::Session object and set random token" do s = mock("session") h = {} s.should_receive(:[]).any_number_of_times{|k| h[k]} s.should_receive(:[]=).any_number_of_times{|k, v| h[k]=v} f = FORM.new(:session=>s) h["token"].size.should == 40 h["token"].should =~ /\A[0-9a-f]{40}\z/ f.should include(HIDDEN.new(:name=>"token", :value=>h["token"])) s = mock("session") h = {} s.should_receive(:[]).any_number_of_times{|k| h[k]} s.should_receive(:[]=).any_number_of_times{|k, v| h[k]=v} f = FORM.new(:session=>s, :session_name=>"random_key") h["token"].should be_nil h["random_key"].size.should == 40 h["random_key"].should =~ /\A[0-9a-f]{40}\z/ f.should include(HIDDEN.new(:name=>"random_key", :value=>h["random_key"])) s = mock("session") h = {} s.should_receive(:[]).any_number_of_times{|k| h[k]} s.should_receive(:[]=).any_number_of_times{|k, v| h[k]=v} FORM.new(:session=>s) token = s["token"] FORM.new(:session=>s).should include(HIDDEN.new(:name=>"token", :value=>token)) s["token"].should == token end it "TEXTAREA" do TEXTAREA.new(:name=>"item").should == Element.new(:textarea, :name=>"item") TEXTAREA.new(:name=>:item).should == Element.new(:textarea, :name=>:item) TEXTAREA.new(:name=>"item", :class=>"cv").should == Element.new(:textarea, :name=>"item", :class=>"cv") t = XDSL.textarea(:name=>"t") t.should be_kind_of(TEXTAREA) t[:name].should == "t" t = OpenDSL.textarea(:name=>"t") t.should be_kind_of(TEXTAREA) t[:name].should == "t" end it "BUTTON" do BUTTON.new.should == Element.new(:button) end it "INPUT" do INPUT.new(:type=>:test, :name=>:item, :value=>"v").should == Element.new(:input, :type=>:test, :name=>:item, :value=>"v") INPUT.new(:type=>"test", :name=>"item", :value=>"v").should == Element.new(:input, :type=>"test", :name=>"item", :value=>"v") INPUT.new(:type=>:test, :name=>:item, :value=>"v", :class=>"c").should == Element.new(:input, :type=>:test, :name=>:item, :value=>"v", :class=>"c") INPUT.new(:type=>:submit, :value=>"v").should == Element.new(:input, :type=>:submit, :value=>"v") INPUT.new(:type=>:submit, :name=>"item").should == Element.new(:input, :type=>:submit, :name=>"item") i = XDSL.input(:type=>:dummy, :name=>:n, :value=>:v) i.should be_kind_of(INPUT) i.should =~ INPUT.new(:type=>:dummy, :name=>:n, :value=>:v) i = OpenDSL.input(:type=>:dummy, :name=>:n, :value=>:v) i.should be_kind_of(INPUT) i.should == INPUT.new(:type=>:dummy, :name=>:n, :value=>:v) end it "HIDDEN" do HIDDEN.new(:name=>"item", :value=>"v").should == Element.new(:input, :type=>:hidden, :name=>"item", :value=>"v") HIDDEN.new(:name=>:item, :value=>"v").should == Element.new(:input, :type=>:hidden, :name=>:item, :value=>"v") HIDDEN.new(:name=>:item, :value=>"v", :class=>"c").should == Element.new(:input, :type=>:hidden, :name=>:item, :value=>"v", :class=>"c") h = XDSL.hidden(:name=>:n, :value=>:v) h.should be_kind_of(HIDDEN) h.should =~ HIDDEN.new(:name=>:n, :value=>:v) h = OpenDSL.hidden(:name=>:n, :value=>:v) h.should be_kind_of(HIDDEN) h.should == HIDDEN.new(:name=>:n, :value=>:v) end it "SUBMIT" do SUBMIT.new.should == Element.new(:button, :type=>:submit) SUBMIT.new(:value=>"OK").should == Element.new(:button, :type=>:submit, :value=>"OK") SUBMIT.new(:value=>"OK", :class=>"c").should == Element.new(:button, :type=>:submit, :value=>"OK", :class=>"c") opt = {:value=>"v", :name=>"n"} opt2 = opt.dup SUBMIT.new(opt2) opt2.should == opt s = XDSL.submit s.should be_kind_of(SUBMIT) s.should =~ SUBMIT.new (!s[:name]).should be_true (!s[:value]).should be_true s = XDSL.submit(:name=>:s, :value=>:v) s[:name].should == :s s[:value].should == :v s = OpenDSL.submit s.should be_kind_of(SUBMIT) s.should == SUBMIT.new s[:name].should be_nil s[:value].should be_nil s = OpenDSL.submit(:name=>:s, :value=>:v) s.should == SUBMIT.new(:name=>:s, :value=>:v) end it "TEXT" do TEXT.new(:name=>:item).should == Element.new(:input, :type=>:text, :name=>:item) TEXT.new(:name=>"item").should == Element.new(:input, :type=>:text, :name=>"item") TEXT.new(:name=>:item, :value=>"txt").should == Element.new(:input, :type=>:text, :name=>:item, :value=>"txt") TEXT.new(:name=>:item, :value=>"txt", :class=>"c").should == Element.new(:input, :type=>:text, :name=>:item, :value=>"txt", :class=>"c") t = XDSL.text(:name=>:n, :value=>:v) t.should be_kind_of(TEXT) t.should =~ TEXT.new(:name=>:n, :value=>:v) t = OpenDSL.text(:name=>:n, :value=>:v) t.should be_kind_of(TEXT) t.should == TEXT.new(:name=>:n, :value=>:v) end it "PASSWORD" do PASSWORD.new(:name=>:item).should == Element.new(:input, :type=>:password, :name=>:item) PASSWORD.new(:name=>"item").should == Element.new(:input, :type=>:password, :name=>"item") PASSWORD.new(:name=>:item, :value=>"txt").should == Element.new(:input, :type=>:password, :name=>:item, :value=>"txt") PASSWORD.new(:name=>:item, :value=>"txt", :class=>"c").should == Element.new(:input, :type=>:password, :name=>:item, :value=>"txt", :class=>"c") t = XDSL.password(:name=>:n, :value=>:v) t.should be_kind_of(PASSWORD) t.should =~ PASSWORD.new(:name=>:n, :value=>:v) t = OpenDSL.password(:name=>:n, :value=>:v) t.should be_kind_of(PASSWORD) t.should == PASSWORD.new(:name=>:n, :value=>:v) end it "FILE" do FILE.new(:name=>:foo).should == Element.new(:input, :type=>:file, :name=>:foo) XDSL.file(:name=>:foo).should =~ FILE.new(:name=>:foo) OpenDSL.file(:name=>:foo).should == FILE.new(:name=>:foo) end it "SELECT" do SELECT.new(:name=>:foo).should == Element.new(:select, :name=>:foo) XDSL.select(:name=>:foo).should == SELECT.new(:name=>:foo) OpenDSL.select(:name=>:foo).should == SELECT.new(:name=>:foo) end it "OPTION" do OPTION.new(:value=>:bar, :selected=>true){|e| e << "TEXT"}.should == Element.new(:option, :value=>:bar, :selected=>true){|e| e.add("TEXT")} XDSL.option(:value=>:bar).should == OPTION.new(:value=>:bar) OpenDSL.option(:value=>:bar).should == OPTION.new(:value=>:bar) end it "BR" do BR.new.name.should == :br XDSL.br.should be_kind_of(BR) OpenDSL.br.should be_kind_of(BR) end it "HR" do HR.new.name.should == :hr XDSL.hr.should be_kind_of(HR) OpenDSL.hr.should be_kind_of(HR) end end describe EimXML::XHTML::OpenDSL do it "replace EimXML::XHTML::DSL" do e = EimXML::XHTML::OpenDSL.html do |d| d.head do d.title.add "Title" end d.body do d.h1.add "Sample" d.p do d.add "text" d.add "next" end end end e.should == EimXML::XHTML::DSL.html do head do title.add "Title" end body do h1.add "Sample" p do add "text" add "next" end end end end end describe EimXML::XHTML::Formatter do describe "#write" do it "should set :preservers=>PRESERVE_SPACES to default option" do e = EimXML::XHTML::HTML.new EimXML::Formatter.should_receive(:write).with(e, :preservers=>EimXML::XHTML::PRESERVE_SPACES, :opt=>:dummy) EimXML::XHTML::Formatter.write(e, :opt=>:dummy) end it "should return string" do h = EimXML::XHTML::DSL.html do head do style.add("style\ntext") script.add("script\ntext") end body do div.add("text\nin\ndiv") pre.add("pre\nt") h1.add("1\nt") h2.add("2\nt") h3.add("3\nt") h4.add("4\nt") h5.add("5\nt") h6.add("6\nt") p.add("p\nt") a.add("a\nt") em.add("e\nt") strong.add("s\nt") span.add("sp\nt") li.add("li\nt") dt.add("dt\nt") dd.add("dd\nt") caption.add("c\nt") th.add("th\nt") td.add("td\nt") button.add("button\nt") end end s = <
text in div
pre
t

1 t

2 t

3 t

4 t

5 t
6 t

p t

a t e t s t sp t
  • li t
  • dt t
    dd t
    c t th t td t EOT EimXML::XHTML::Formatter.write(h).should == s end end end end eim-xml-0.0.4/spec/assertions_test.rb0000644000175000017500000000124711613235354017436 0ustar uwabamiuwabami# Test for eim_xml/assertions.rb # # Copyright (C) 2006, KURODA Hiraku # You can redistribute it and/or modify it under GPL2. $:.unshift "#{File.dirname(File.dirname(File.expand_path(__FILE__)))}/lib" require "test/unit" require "eim_xml/assertions" class EimXMLAssertionsTest < Test::Unit::TestCase include EimXML include EimXML::Assertions def test_assert_has e = Element.new(:tag) do |e| e <<= Element.new(:sub) end assert_nothing_raised do assert_has(:sub, e) end a = assert_raises(Test::Unit::AssertionFailedError) do assert_has(:no, e) end assert(!a.backtrace.any?{ |i| i=~/eim_xml\/assertions\.rb/ }) end end eim-xml-0.0.4/spec/parser_spec.rb0000644000175000017500000000566211613235354016520 0ustar uwabamiuwabamirequire "eim_xml/parser" module Module.new::M include EimXML describe Parser do def parse(src) Parser.new(src).parse end it "'parser' method for test" do s = " " parse(s).should == Parser.new(s).parse end it "#parse with empty element" do parse("").should == Element.new("e") parse("").should == Element.new("e") parse(%[]).should == Element.new("e", :key=>"value") parse(%[]).should == Element.new("e", :key=>"value") parse(%[]).should == Element.new("e", :key=>"value") parse(%[]).should == Element.new("e", :key=>"value") parse(%[]).should == Element.new("e", :key=>"value", :key2=>"value2") parse(%[]).should == Element.new("e", :key=>"value", :key2=>"value2") s = " " p = Parser.new(s) p.parse.should == PCString.new(" ") p.parse.should == Element.new("e1") p.parse.should == PCString.new(" ") p.parse.should == Element.new("e2") end it "#parse with nonempty element" do parse("").should == Element.new("super") << Element.new("sub") parse("").should == Element.new("out") << Element.new("in") lambda{parse("")}.should raise_error(ParseError, "End tag mismatched.") lambda{parse("<>")}.should raise_error(ParseError, "Syntax error.") end it "#parse with string" do e = parse("string&") e.should be_kind_of(PCString) e.to_s.should == "string&" e = parse(" string & ") e.should be_kind_of(PCString) e.to_s.should == " string & " e = Element.new("e") e << PCString.new(" string ") parse(" string ").should == e e = Element.new("e") e << PCString.new("string") parse("string").should == e end it "#parse escaped characters" do e = parse("&"'<>") e.to_s.should == "&"'<>" e.src.should == "&\"'<>" end it "#parse with holding space" do s = " string with space\n" e = Element.new("e") e << PCString.new(" string with space\n") parse(s).should == e parse(s).to_s.should == s s = " string with space\n" e = Element.new("ns:e") e << PCString.new(" string with space\n") parse(s).should == e parse(s).to_s.should == s s = " string without space string with space string with space 2 " oa = Element.new("a") << PCString.new(" string without space ") b = Element.new("b") b << PCString.new(" string with space ") ia = Element.new("a") ia << PCString.new(" string with space 2 ") b << ia b << PCString.new(" ") oa << b oa << PCString.new(" ") parse(s).should == oa parse(s).to_s.should == s s = "" a = Element.new("a") b = Element.new("b") a << b parse(s).should == a parse(s).to_s.should == "" end end end eim-xml-0.0.4/metadata.yml0000664000175000017500000000267611613235354015242 0ustar uwabamiuwabami--- !ruby/object:Gem::Specification name: eim_xml version: !ruby/object:Gem::Version hash: 23 prerelease: segments: - 0 - 0 - 4 version: 0.0.4 platform: ruby authors: - KURODA Hiraku autorequire: bindir: bin cert_chain: [] date: 2011-07-19 00:00:00 Z dependencies: [] description: email: hiraku@hinet.mydns.jp executables: [] extensions: [] extra_rdoc_files: [] files: - Rakefile.utirake - Rakefile - lib/eim_xml/matcher.rb - lib/eim_xml/xhtml.rb - lib/eim_xml/dsl.rb - lib/eim_xml/parser.rb - lib/eim_xml/formatter.rb - lib/eim_xml/xhtml/dsl.rb - lib/eim_xml/assertions.rb - lib/eim_xml/formatter/element_wrapper.rb - lib/eim_xml.rb - spec/assertions_test.rb - spec/formatter_spec.rb - spec/eim_xml_spec.rb - spec/dsl_spec.rb - spec/xhtml_spec.rb - spec/parser_spec.rb homepage: http://eimxml.rubyforge.org/ licenses: [] post_install_message: rdoc_options: [] require_paths: - lib required_ruby_version: !ruby/object:Gem::Requirement none: false requirements: - - ">=" - !ruby/object:Gem::Version hash: 3 segments: - 0 version: "0" required_rubygems_version: !ruby/object:Gem::Requirement none: false requirements: - - ">" - !ruby/object:Gem::Version hash: 25 segments: - 1 - 3 - 1 version: 1.3.1 requirements: [] rubyforge_project: eimxml rubygems_version: 1.8.5 signing_key: specification_version: 3 summary: Easy IMplemented XML test_files: [] eim-xml-0.0.4/lib/0000775000175000017500000000000011613235354013472 5ustar uwabamiuwabamieim-xml-0.0.4/lib/eim_xml/0000775000175000017500000000000011613235354015124 5ustar uwabamiuwabamieim-xml-0.0.4/lib/eim_xml/assertions.rb0000644000175000017500000000066511613235354017650 0ustar uwabamiuwabamirequire "eim_xml" module EimXML::Assertions def assert_has(expect, element, message="") message << "\n" unless message.size==0 message << "<#{element}> doesn't have\n<#{expect.inspect}>" assert_block(message) do element.has?(expect) end rescue Test::Unit::AssertionFailedError=>e bt = e.backtrace.find_all do |b| b !~ /#{Regexp.escape(__FILE__)}/ end raise Test::Unit::AssertionFailedError, e.message, bt end end eim-xml-0.0.4/lib/eim_xml/xhtml/0000775000175000017500000000000011613235354016260 5ustar uwabamiuwabamieim-xml-0.0.4/lib/eim_xml/xhtml/dsl.rb0000644000175000017500000000042511613235354017366 0ustar uwabamiuwabamirequire "eim_xml/dsl" require "eim_xml/xhtml" module EimXML::XHTML class DSL < EimXML::BaseDSL end class OpenDSL < EimXML::OpenDSL end constants.each do |c| v = const_get(c) if v.is_a?(Class) && /_$/ !~ v.name DSL.register v OpenDSL.register v end end end eim-xml-0.0.4/lib/eim_xml/dsl.rb0000644000175000017500000000374311613235354016240 0ustar uwabamiuwabami# Easy IMplementation of XML # # Copyright (C) 2006,2008, KURODA Hiraku # You can redistribute it and/or modify it under GPL2. # require "eim_xml" module EimXML class BaseDSL def add(v) @_container << v end alias << add def import_variables(src) src.instance_variables.each do |v| instance_variable_set(v, src.instance_variable_get(v)) unless v=~/\A@_[^_]/ end self end def _build(klass, *arg, &proc) e = klass.new(*arg) @_container << e if @_container if proc oc = @_container @_container = e begin instance_eval(&proc) ensure @_container = oc end end e end private :_build def _push(container) oc = @_container @_container = container begin yield if block_given? container ensure @_container = oc end end private :_push def self.register(*args) args.each do |klass, name| name ||= klass.name.downcase[/(?:.*\:\:)?(.*)$/, 1] eval("def #{name}(*a, &p);_build(#{klass}, *a, &p);end", binding) eval("def self.#{name}(*a, &p);new.#{name}(*a, &p);end", binding) end end end class DSL < BaseDSL end class OpenDSL def _build(klass, *arg, &proc) e = klass.new(*arg) oc = @_container oc << e if oc.is_a?(Element) @_container = e begin proc.call(self) if proc e ensure @_container = oc end end private :_build def self.register_base(dsl, binding, *args) args.each do |klass, name| name ||= klass.name.downcase[/(?:.*\:\:)?(.*)$/, 1] eval("def #{name}(*a, &p);_build(#{klass}, *a, &p);end", binding) eval("def self.#{name}(*a, &p);self.new.#{name}(*a, &p);end", binding) end end def self.register(*args) register_base(self, binding, *args) end def initialize @_container = nil yield(self) if block_given? end def add(v) @_container.add(v) end alias :<< :add def container; @_container; end end DSL.register Element, Comment OpenDSL.register Element, Comment end eim-xml-0.0.4/lib/eim_xml/matcher.rb0000644000175000017500000000071511613235354017075 0ustar uwabamiuwabamirequire "eim_xml" module EimXML::Matchers class HaveContent def initialize(expected) @expected = expected end def matches?(target) @target = target @target.has?(@expected) end def failure_message "expected #{@target.inspect} must have #{@expected}, but not." end def negative_failure_message "expected #{@target.inspect} must not have #{@expected}, but has." end end def have(expected) HaveContent.new(expected) end end eim-xml-0.0.4/lib/eim_xml/parser.rb0000644000175000017500000000312111613235354016740 0ustar uwabamiuwabami# XML parser for EimXML # # Copyright (C) 2006, KURODA Hiraku # You can redistribute it and/or modify it under GPL2. # require "eim_xml" require "strscan" module EimXML class ParseError < StandardError end class Parser attr_reader :scanner module RE EMPTY_ELEMENT = /<([^>]*?)\/>/ START_TAG = /<([^>]*?([^\/>]\s*))>/ END_TAG = /<\/(\S+?)\s*>/ ATTRIBUTE = /\s+([^=\s]+)\s*=\s*('(.*?)'|"(.*?)")/m STRING = /[^<]+/ end def initialize(src) @scanner = StringScanner.new(src) @scanner.scan(/\s*<\?.*?\?>\s*/) end def parse if @scanner.scan(RE::EMPTY_ELEMENT) parse_empty_element elsif @scanner.scan(RE::START_TAG) parse_start_tag elsif @scanner.scan(RE::STRING) parse_string else nil end end def parse_tag s = StringScanner.new(@scanner[1]) e = Element.new(s.scan(/\S+/)) e[s[1]] = s[3] ? s[3] : s[4] while s.scan(RE::ATTRIBUTE) e end protected :parse_tag def parse_empty_element parse_tag end protected :parse_empty_element def parse_start_tag e = parse_tag until @scanner.scan(RE::END_TAG) c = parse raise ParseError.new("Syntax error.") unless c e << c end raise ParseError.new("End tag mismatched.") unless @scanner[1].to_sym==e.name e end protected :parse_start_tag def parse_string s = @scanner[0] s = s.gsub(/&(amp|quot|apos|lt|gt);/) do case $1 when "amp" "&" when "quot" '"' when "apos" "'" when "lt" "<" when "gt" ">" end end PCString.new(s) end protected :parse_string end end eim-xml-0.0.4/lib/eim_xml/formatter.rb0000644000175000017500000000403011613235354017447 0ustar uwabamiuwabamirequire "eim_xml" module EimXML class Formatter attr_reader :out def self.write(element, opt={}) opt = {:out=>""}.merge(opt) new(opt).write(element) opt[:out] end def initialize(opt) @out = opt[:out] @preservers = opt[:preservers] @preserve_space = false @indent_string = " " @indent_depth = 0 @option = opt.dup.tap{|h| [:out, :preservers].each{|k| h.delete(k)}} end def write(src) case src when ElementWrapper write_wrapper(src) when Comment write_comment(src) when Element write_element(src) when PCString write_pcstring(src) else write_string(src.to_s) end end def indent(&proc) @indent_depth += 1 proc.call ensure @indent_depth -= 1 end def preserve_space_element?(elm) @preservers && @preservers.any? do |e| case e when Symbol e==elm.name when Class e===elm end end end def write_indent out << @indent_string*@indent_depth unless @preserve_space end def write_newline out << "\n" unless @preserve_space end def write_comment(c) write_indent c.write_to(out) write_newline end def write_contents_of(elm) flag = @preserve_space @preserve_space = true if preserve_space_element?(elm) write_newline indent do elm.contents.each do |c| write(c) end end write_indent ensure @preserve_space = flag end def write_element(elm) write_indent out << "<" elm.name_and_attributes(out) case elm.contents.size when 0 out << " />" write_newline else out << ">" write_contents_of(elm) out << "" write_newline end end def write_pcstring(pcs) pcs.encoded_string.each_line do |l| write_indent out << l end write_newline end def write_string(str) PCString.encode(str).each_line do |l| write_indent out << l end write_newline end def write_wrapper(wrapper) wrapper.each(@option) do |i| write(i) end end end end require "eim_xml/formatter/element_wrapper" eim-xml-0.0.4/lib/eim_xml/formatter/0000775000175000017500000000000011613235354017127 5ustar uwabamiuwabamieim-xml-0.0.4/lib/eim_xml/formatter/element_wrapper.rb0000644000175000017500000000016711613235354022647 0ustar uwabamiuwabamiclass EimXML::Formatter class ElementWrapper def each(option, &proc) contents(option).each(&proc) end end end eim-xml-0.0.4/lib/eim_xml/xhtml.rb0000644000175000017500000000647211613235354016614 0ustar uwabamiuwabamirequire "eim_xml" require "eim_xml/formatter" module EimXML::XHTML module DocType XHTML_MATHML = %[] end class Base_ < EimXML::Element end class HTML < Base_ attr_accessor :prefix module NameSpace XHTML = "http://www.w3.org/1999/xhtml" end def initialize(attributes={}) super(:html, attributes) end def write_to(out="") out << @prefix << "\n" if @prefix super end end class Simple_ < Base_ def initialize(attributes={}) super(self.class.name[/.*::(.*)/, 1].downcase.to_sym, attributes) end end class PreserveSpace_ < Base_ def initialize(name={}, attributes={}) if name.is_a?(Hash) super(self.class.name[/.*::(.*)/, 1].downcase.to_sym, name) else super(name, attributes) end end end class HEAD < Simple_; end class META < Simple_; end class LINK < Simple_; end class IMG < Simple_; end class STYLE < PreserveSpace_; end class SCRIPT < PreserveSpace_; end class TITLE < Simple_; end class BODY < Simple_; end class PRE < PreserveSpace_; end class FORM < Simple_ def initialize(attributes={}) if attributes if s = attributes.delete(:session) name = attributes.delete(:session_name) || "token" require "digest/sha1" token = s[name] ||= Digest::SHA1.hexdigest("#{$$}#{Time.now}#{rand}") end end super add(HIDDEN.new(:name=>name, :value=>token)) if token end end class H1 < PreserveSpace_; end class H2 < PreserveSpace_; end class H3 < PreserveSpace_; end class H4 < PreserveSpace_; end class H5 < PreserveSpace_; end class H6 < PreserveSpace_; end class P < PreserveSpace_; end class A < PreserveSpace_; end class EM < PreserveSpace_; end class STRONG < PreserveSpace_; end class DIV < Simple_; end class SPAN < PreserveSpace_; end class UL < Simple_; end class OL < Simple_; end class LI < PreserveSpace_; end class DL < Simple_; end class DT < PreserveSpace_; end class DD < PreserveSpace_; end class TABLE < Simple_; end class CAPTION < PreserveSpace_; end class TR < Simple_; end class TH < PreserveSpace_; end class TD < PreserveSpace_; end class BR < Simple_; end class HR < Simple_; end class SELECT < Simple_; end class OPTION < Simple_; end module Hn def self.new(level, attr={}, &proc) raise ArgumentError unless 1<=level && level<=6 klass = EimXML::XHTML.const_get("H#{level}") klass.new(attr, &proc) end end class TEXTAREA < PreserveSpace_; end class INPUT < Base_ def initialize(opt={}) super(:input, opt) end end class BUTTON < PreserveSpace_ def initialize(opt={}) super(:button, opt) end end class SUBMIT < BUTTON def initialize(opt={}) super(opt.merge(:type=>:submit)) end end class HIDDEN < INPUT def initialize(opt={}) super(opt.merge(:type=>:hidden)) end end class TEXT < INPUT def initialize(opt={}) super(opt.merge(:type=>:text)) end end class PASSWORD < INPUT def initialize(opt={}) super(opt.merge(:type=>:password)) end end class FILE < INPUT def initialize(opt={}) super(opt.merge(:type=>:file)) end end PRESERVE_SPACES = [PreserveSpace_] class Formatter < EimXML::Formatter def self.write(element, opt={}) EimXML::Formatter.write(element, opt.merge(:preservers=>PRESERVE_SPACES)) end end end eim-xml-0.0.4/lib/eim_xml.rb0000644000175000017500000000776711613235354015470 0ustar uwabamiuwabami# Easy IMplementation of XML # # Copyright (C) 2006, KURODA Hiraku # You can redistribute it and/or modify it under GPL2. # module EimXML XML_DECLARATION = %[] class PCString attr_reader :encoded_string, :src alias to_s encoded_string def self.encode(s) s.to_s.gsub(/[&\"\'<>]/) do |m| case m when "&" "&" when '"' """ when "'" "'" when "<" "<" when ">" ">" end end end def self.[](obj) obj.is_a?(PCString) ? obj : PCString.new(obj) end def initialize(s, encoded=false) @src = s @encoded_string = encoded ? s : PCString.encode(s) end def ==(other) other.is_a?(PCString) ? @encoded_string==other.encoded_string : self==PCString.new(other) end def write_to(out="") out << encoded_string end end class Comment def initialize(text) raise ArgumentError, "Can not include '--'" if text =~ /--/ @text = text end def write_to(out="") out << "" end end class Element attr_reader :name, :attributes, :contents NEST = " " def initialize(name, attributes={}) @name = name.to_sym @attributes = Hash.new @contents = Array.new attributes.each do |k, v| @attributes[k.to_sym] = v end yield(self) if block_given? end def name=(new_name) @name = new_name.to_sym end protected :name= def add(v) case v when nil when Array v.each{|i| self.add(i)} else @contents << v end self end alias << add def name_and_attributes(out="") out << "#{@name}" @attributes.each do |k, v| next unless v out << " #{k}='#{PCString===v ? v : PCString.encode(v.to_s)}'" end end def write_to(out = "") out << "<" name_and_attributes(out) if @contents.empty? out << " />" else out << ">" @contents.each do |c| case c when Element c.write_to(out) when PCString out << c.to_s else out << PCString.encode(c.to_s) end end out << "" end out end alias :to_s :write_to alias :inspect :to_s def ==(xml) return false unless xml.is_a?(Element) @name==xml.name && @attributes==xml.attributes && @contents==xml.contents end def add_attribute(key, value) @attributes[key.to_sym] = value end alias []= add_attribute def [](key) if key.is_a?(Fixnum) @contents[key] else @attributes[key.to_sym] end end def del_attribute(key) @attributes.delete(key.to_sym) end def pcstring_contents @contents.select{|c| c.is_a?(String)||c.is_a?(PCString)}.map{|c| c.is_a?(String) ? PCString.new(c) : c} end def match(obj, attr=nil) return match(Element.new(obj, attr)) if attr return obj=~@name.to_s if obj.is_a?(Regexp) return @name==obj if obj.is_a?(Symbol) return is_a?(obj) if obj.is_a?(Module) raise ArgumentError unless obj.is_a?(Element) return false unless @name==obj.name obj.attributes.all? do |k, v| (v.nil? && !@attributes.include?(k)) || (@attributes.include?(k) && (v.is_a?(Regexp) ? v =~ @attributes[k] : PCString[v] == PCString[@attributes[k]])) end and obj.contents.all? do |i| case i when Element has_element?(i) when String pcstring_contents.include?(PCString.new(i)) when PCString pcstring_contents.include?(i) when Regexp @contents.any?{|c| c.is_a?(String) and i=~c} end end end alias :=~ :match def has?(obj, attr=nil) return has?(Element.new(obj, attr)) if attr @contents.any? do |i| if i.is_a?(Element) i.match(obj) || i.has?(obj) else obj.is_a?(Module) && i.is_a?(obj) end end end alias has_element? has? alias include? has? def find(obj, dst=Element.new(:found)) return find(Element.new(obj, dst)) if dst.is_a?(Hash) dst << self if match(obj) @contents.each do |i| case when i.is_a?(Element) i.find(obj, dst) when obj.is_a?(Module) && i.is_a?(obj) dst << i end end dst end end end eim-xml-0.0.4/Rakefile0000644000175000017500000000076411613235354014376 0ustar uwabamiuwabamiload "Rakefile.utirake" VER = "0.0.4" UtiRake.setup do rdoc do |t| t.title = "Easy IMplemented XML" t.main = "README" t.rdoc_files.include(FileList["lib/**/*.rb", "README"]) end gemspec do |s| s.name = "eim_xml" s.summary = "Easy IMplemented XML" s.author = "KURODA Hiraku" s.email = "hiraku@hinet.mydns.jp" s.homepage = "http://eimxml.rubyforge.org/" s.rubyforge_project = "eimxml" s.version = VER end publish("eimxml", "hiraku") alias_task end task :default => :spec