-- playlist/xspf tests: XSPF files written by Python's xml.etree.ElementTree (tools/fixtures/playlist.py) decode to
-- their entries; every track field, multiple locations, xml:base, relative / local locations, a prefixed namespace;
-- the writer round-trips (and escapes; its output was also checked by hand with ElementTree); errors (malformed XML,
-- another root); the api; the fuzz pass.
-- Fixtures: tools/fixtures/playlist.py.
local T = require("@tests/T")
local Xspf = require("@formats/playlist/xspf")
local Bytes = require("@formats/core/Bytes")
local Err = require("@formats/core/Err")
local F = require("@formats")
local Cases = require("@tests/gen/playlist_cases")
local t = T.suite("xspf")
local function decode(s: string, base: string?): (any, any)
return Xspf.decode(Bytes.reader(buffer.fromstring(s)), { baseUrl = base })
end
for _, c in Cases do
local pl, err = decode(c.xspf)
t:check(pl, c.name .. ": decode: " .. tostring(err))
t:eq(pl.title, c.title, c.name .. ": title")
t:eq(#pl.entries, #c.entries, c.name .. ": entries")
for i, e in c.entries do
local g = pl.entries[i]
t:check(g.url == e.url and g.title == e.title and g.duration == e.duration and g.playable, c.name .. ": entry " .. i)
end
local back = decode(buffer.tostring(Xspf.encode(pl) :: buffer))
t:eq(back.title, pl.title, c.name .. ": round trip title")
for i, e in pl.entries do
local g = back.entries[i]
t:check(g.url == e.url and g.title == e.title and g.duration == e.duration, c.name .. ": round trip " .. i)
end
end
t:note("%d ElementTree-written XSPF files decode to their entries and round-trip", #Cases)
do
local text = [[
My & list Me
a%20b.mp3http://mirror.example/a.mp3
AArtistAlbum1500
3cover.jpg
../b.mp3
no location
C:\Music\c.mp3
]]
local pl = decode(text, "http://ignored.example/")
t:eq(pl.title, "My & list", "title (entity, trimmed)")
t:eq(pl.meta.creator, "Me", "playlist creator")
t:eq(#pl.entries, 3, "a track without a location is skipped")
local a = pl.entries[1]
t:eq(a.url, "http://media.example/base/a%20b.mp3", "xml:base on the playlist")
t:check(a.title == "A" and a.creator == "Artist" and a.album == "Album" and a.duration == 1.5 and a.trackNum == 3 and a.image == "cover.jpg", "track fields")
t:check(a.locations and #a.locations == 2 and a.locations[2] == "http://mirror.example/a.mp3", "multiple locations")
t:eq(pl.entries[2].url, "http://other.example/b.mp3", "xml:base on a track")
t:check(not pl.entries[3].playable and pl.entries[3].reason == "local path", "local path")
t:check(#pl.warnings >= 2, "warnings for the missing location and the local path")
-- writer: escaping, extra fields, a plain entry list
local out = buffer.tostring(Xspf.encode({ { url = "http://x/a?b=1&c=2", title = "", duration = 2.5, creator = "C" } } :: any) :: buffer)
t:check(string.find(out, "http://x/a?b=1&c=2", 1, true) ~= nil and string.find(out, "<A & B>", 1, true) ~= nil, "escaping")
local back = decode(out)
t:check(back.entries[1].url == "http://x/a?b=1&c=2" and back.entries[1].title == "" and back.entries[1].duration == 2.5 and back.entries[1].creator == "C", "writer round trip")
-- errors
local _, e1 = decode("")
t:check(e1 and Err.is(e1), "malformed XML: " .. tostring(e1))
local _, e2 = decode("")
t:check(e2 and e2.code == Err.CORRUPT, "another root")
t:check(select(2, Xspf.encode(5 :: any)) ~= nil, "encode a number")
t:eq(Xspf.sniff(Bytes.reader(buffer.fromstring(Cases[1].xspf))), 0.95, "sniff")
t:eq(Xspf.sniff(Bytes.reader(buffer.fromstring(""))), 0, "sniff without the namespace")
-- the api
t:eq(F.detect(Cases[1].xspf), "xspf", "F.detect")
local la = F.playlist.load(Cases[1].xspf)
t:check(la and la.format == "xspf" and #la.entries == #Cases[1].entries, "F.playlist.load")
local m = F.convert(Cases[1].m3u, "xspf")
t:check(m and #decode(buffer.tostring(m)).entries == #Cases[1].entries, "F.convert m3u -> xspf")
end
local samples = {}
for _, c in Cases do
table.insert(samples, c.xspf)
end
t:fuzz("decode", function(b: buffer)
return Xspf.decode(Bytes.reader(b), { baseUrl = "http://h/a/" })
end, samples, { mutations = 20 })
t:done()