File: test-source-xml-parser.lua

package info (click to toggle)
grilo-plugins 0.3.18-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,348 kB
  • sloc: ansic: 52,656; xml: 2,879; python: 1,137; makefile: 9
file content (116 lines) | stat: -rw-r--r-- 3,286 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
--[[
 * Copyright (C) 2015 Victor Toso.
 *
 * Contact: Victor Toso <me@victortoso.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
--]]

---------------------------
-- Source initialization --
---------------------------

source = {
  id = "test-source-xml-parser",
  name = "Fake Source for XML Parser",
  description = "a source to test XML parser function",
  supported_keys = { "title" },
  supported_media = "all",
  resolve_keys = {
    ["type"] = "all",
    required = { "url" } ,
  },
  tags = { 'test', 'net:plaintext' },
}

---------------------------------
-- Handlers of Grilo functions --
---------------------------------

function grl_source_resolve()
  -- This source expects an url which will be fetched and converted
  -- to a table using grl.lua.xml.string_to_table().
  local req = grl.get_media_keys()
  if not req or not req.url or #req.url ~= 2 then
    grl.warning("resolve was called without metadata-key url")
    grl.callback()
    return
  end

  grl.fetch(req.url, fetch_url_cb)
end

-- feeds[1] is the xml to test
-- feeds[2] is a lua table with this xml, to compare
function fetch_url_cb(feeds)
  if not feeds or #feeds ~= 2 then
    grl.warning("failed to load xml")
    grl.callback()
    return
  end

  local xml = grl.lua.xml.string_to_table(feeds[1])
  local ref = load(feeds[2])()
  if not xml or not ref then
    grl.warning ("xml parser failed")
    grl.callback()
    return
  end

  if not test_table_contains(xml, ref) or
     not test_table_contains(ref, xml) then
    grl.warning("xml parser failed, results are not the same\n" ..
                "reference table of test:\n" .. grl.lua.inspect(ref) .. "\n" ..
                "table from xml parser:\n" .. grl.lua.inspect(xml))
    grl.callback()
    return
  end

  local media = { id = "success" }
  grl.callback(media, 0)
end

function test_table_contains(t, e)
  if type(t) ~= "table" or
     type(e) ~= "table" then
     return false
  end

  -- This is xml: keys are always strings
  for key, value in pairs(t) do
    if not e[key] then
      grl.debug ("table does not have key: " .. key)
      return false
    end

    if type(value) == "string" then
      if t[key] ~= e[key] then
        grl.debug ("values differ '" .. t[key] .. "' and '" .. e[key] .. "'")
        return false
      end
    elseif type(value) == "table" then
      if not test_table_contains(t[key], e[key]) or
         not test_table_contains(e[key], t[key]) then
         return false
      end
    else
      grl.warning("test not handling type: " .. type(value))
      return false
    end
  end
  return true
end