test/test_repo.rb in vpim-0.658 vs test/test_repo.rb in vpim-0.695

- old
+ new

@@ -1,25 +1,14 @@ #!/usr/bin/env ruby require 'vpim/repo' -require 'vpim/agent/calendars' -require 'test/unit' +require 'test/common' -require 'pp' - -module Enumerable - def count - self.inject(0){|i,_| i + 1} - end -end - class TestRepo < Test::Unit::TestCase Apple3 = Vpim::Repo::Apple3 Directory = Vpim::Repo::Directory - Agent = Vpim::Agent - Path = Agent::Path - NotFound = Agent::NotFound + Uri = Vpim::Repo::Uri def setup @testdir = Dir.getwd + "/test" #File.dirname($0) doesn't work with rcov :-( @caldir = @testdir + "/calendars" @eventsz = Dir[@caldir + "/**/*.ics"].size @@ -59,100 +48,63 @@ assert_equal(@eventsz, repo.count) _test_each(repo, 1) end - def assert_is_text_calendar(text) - lines = text.split("\n") - lines = lines.first, lines.last - assert_equal("BEGIN:VCALENDAR", lines.first.upcase, lines) - assert_equal("END:VCALENDAR", lines.last.upcase, lines) - end + def test_uri + caldata = open('test/calendars/weather.calendar/Events/1205042405-0-0.ics').read - def test_agent_calendars - repo = Apple3.new(@caldir) - rest = Agent::Calendars.new(repo) + server = data_on_port(caldata, 9876) + begin + c = Uri::Calendar.new("http://localhost:9876") + assert_equal(caldata, c.encode) - out1, form = rest.get(Path.new("http://host/here", "/here")) - assert_equal("text/html", form) - #puts(out1) + repo = Uri.new("http://localhost:9876") - out1, form = rest.get(Path.new("http://host/here/weather%2fLeavenworth", "/here")) - assert_equal("text/html", form) - #puts(out1) + assert_equal(1, repo.count) - out2, form = rest.get(Path.new("http://host/here/weather%2fLeavenworth/calendar", "/here")) - assert_equal("text/calendar", form) - assert_is_text_calendar(out2) + _test_each(repo, 1) + ensure + server.kill + end + end - #assert_equal(out1, out2) - - assert_raise(Vpim::Agent::NotFound) do - rest.get(Path.new("http://host/here/weather%2fLeavenworth/an_unknown_protocol", "/here")) + def test_uri_invalid + assert_raises(ArgumentError) do + Uri.new("url") end - assert_raise(Vpim::Agent::NotFound) do - rest.get(Path.new("http://host/here/no_such_calendar", "/here")) + assert_raises(ArgumentError) do + c = Uri::Calendar.new("url://localhost") end - - assert_equal(["","/","/"], Vpim::Agent::Path.split_path("/%2F/%2F")) - assert_equal(["/","/"], Vpim::Agent::Path.split_path("%2F/%2F")) - assert_equal(["calendars", "weather/Leavenworth"], - Vpim::Agent::Path.split_path("calendars/weather%2FLeavenworth")) + assert_raises(ArgumentError) do + c = Uri::Calendar.new("https://localhost") + end end - def test_agent_calendar_atom - repo = Apple3.new(@caldir) - rest = Agent::Calendars.new(repo) - - out, form = rest.get(Path.new("http://host/here/weather%2fLeavenworth/atom", "/here")) - assert_equal("application/atom+xml", form) - #pp out - #assert_is_atom(out) - end - - def _test_path_shift(url, shifts) - # last shift should be a nil - shifts << nil - - # presence or absence of a trailing / should not affect shifting - ["", "/"].each do |trailer| - path = Path.new(url + trailer) - shifts.each do |_| - assert_equal(_, path.shift) - end + def test_uri_unreachable + assert_raises(SocketError) do + r = Uri.new("http://example.example") + c = r.find{true} + c.events{} end + assert_raises(Errno::ECONNREFUSED) do + r = Uri.new("http://rubyforge.org:81") + c = r.find{true} + c.events{} + end + assert_raises(StandardError, Vpim::InvalidEncodingError) do + r = Uri.new("http://rubyforge.org/lua-rocks-my-world") + c = r.find{true} + c.events{} + end end - def test_path_shift - _test_path_shift("http://host.ex", []) - _test_path_shift("http://host.ex/a", ["a"]) - _test_path_shift("http://host.ex/a/b", ["a", "b"]) - _test_path_shift("http://host.ex/a/b/c", ["a", "b", "c"]) - end - - def _test_path_prefix(base, parts, shifts, prefix) - path = Path.new(base+parts.join("/")) - shifts.times{ path.shift } - assert_equal(prefix, path.prefix) - end - - def test_path_prefix - _test_path_prefix("http://host.ex/", [], 0, "/") - _test_path_prefix("http://host.ex/", ["a"], 0, "/") - _test_path_prefix("http://host.ex/", ["a"], 1, "/") - _test_path_prefix("http://host.ex/", ["a"], 2, "/a/") - _test_path_prefix("http://host.ex/", ["a"], 3, "/a/") - _test_path_prefix("http://host.ex/", ["a", "b"], 0, "/") - _test_path_prefix("http://host.ex/", ["a", "b"], 1, "/") - _test_path_prefix("http://host.ex/", ["a", "b"], 2, "/a/") - _test_path_prefix("http://host.ex/", ["a", "b"], 3, "/a/b/") - end - - def test_atomize - repo = Apple3.new(@caldir) - cal = repo.find{true} - a = Vpim::Agent::Atomize.new(cal) - assert( a.get(Path.new("http://example.com/path"))) + def test_uri_unparseable + assert_raises(Vpim::InvalidEncodingError) do + r = Uri.new("http://example.com:80") + c = r.find{true} + c.events{} + end end end