Debian Codegraph @

test/test_nuxt.py

raw
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3
4 # Copyright 2026 Mike Fährmann
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License version 2 as
8 # published by the Free Software Foundation.
9
10 import os
11 import sys
12 import unittest
13
14 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
15 from gallery_dl import text, dt # noqa E402
16 from gallery_dl.extractor.utils import nuxt # noqa E402
17
18
19 TEST_CASES_EQ = (
20 ("int" , [-42], -42),
21 ("str" , ["foo!!!"], "foo!!!"),
22 ("Object", [{"foo": 1, "x-y": 2}, "bar", "z"], {"foo": "bar", "x-y": "z"}),
23 ("Array" , [[1, 2, 3], "a", "b", "c"], ["a", "b", "c"]),
24 ("Array (empty)" , [[]], []),
25 ("Array (sparse)" , [[-2, 1, -2], "b"], [None, "b", None]),
26 ("str (repetition)" , [[1, 1], "astring"], ["astring", "astring"]),
27 ("None (repetition)", [[1, 1], None], [None, None]),
28 ("dict (repetition)", [[1, 1], {}], [{}, {}]),
29 ("cross-realm POJO" , [{}], {}),
30 ("Object without prototype", [["null"]], {}),
31
32 ("Number", [["Object", 42]], 42),
33 ("String", [["Object", "var"]], "var"),
34 ("RegExp", [["RegExp", "regexp", "gim"]], text.re_compile("regexp")),
35 ("Date" , [["Date", "2001-09-09T01:46:40.000Z"]], dt.datetime(2001, 9, 9, 1, 46, 40)), # noqa: E501
36 ("Set" , [["Set", 1, 2, 3], 1, 2, 3], [1, 2, 3]),
37 ("Map" , [["Map", 1, 2], "a", "b"], [["a", "b"]]),
38 ("BigInt", [["BigInt", "1"]], 1),
39 ("Uint8Array" , [["Uint8Array", "AQID"]], [1, 2, 3]),
40 ("ArrayBuffer", [["ArrayBuffer", "AQID"]], [1, 2, 3]),
41 )
42
43 TEST_CASES_IS = (
44 ("bool" , [True], True),
45 ("Boolean" , [["Object", False]], False),
46 ("null" , [None], None),
47 )
48
49 TEST_CASES_ERR = (
50 ("empty string", "" , IndexError, "string index out of range"),
51 ("number" , 42 , TypeError , "'int' object is not subscriptable"),
52 ("boolean" , True, TypeError , "'bool' object is not subscriptable"),
53 ("null" , None, TypeError , "'NoneType' object is not subscriptable"), # noqa: E501
54 ("object" , {} , KeyError , "0"),
55 ("empty array" , [] , IndexError, "list index out of range"),
56 ("Python negative indexing",
57 [[1, 2, 3, 4, 5, 6, 7, -7], 1, 2, 3, 4, 5, 6, 7],
58 IndexError, "invalid index: -7"),
59 )
60
61
62 class TestDevalue(unittest.TestCase):
63 def test_nuxt_resolve_equals(self):
64 for name, input, expected in TEST_CASES_EQ:
65 self.assertEqual(nuxt.resolve(input), expected, name)
66
67 def test_nuxt_resolve_is(self):
68 for name, input, expected in TEST_CASES_IS:
69 self.assertIs(nuxt.resolve(input), expected, name)
70
71 def test_nuxt_resolve_err(self):
72 for name, input, exc, msg in TEST_CASES_ERR:
73 with self.assertLogs("nuxt") as log_info:
74 nuxt.resolve(input)
75 self.assertEqual(len(log_info.output), 1, name)
76 self.assertRegex(log_info.output[0],
77 f"^ERROR:nuxt:{exc.__name__}: .*{msg}", name)
78
79 def test_nuxt_resolve_cyclical(self):
80 name = "Map (cyclical)"
81 result = nuxt.resolve([["Map", 1, 0], "self"])
82 self.assertEqual(result[0][0], "self", name)
83 self.assertIs(result, result[0][1], name)
84
85 name = "Set (cyclical)"
86 result = nuxt.resolve([["Set", 0, 1], 42])
87 self.assertEqual(result[1], 42, name)
88 self.assertIs(result, result[0], name)
89
90 result = nuxt.resolve([[0]])
91 self.assertIs(result, result[0], "Array (cyclical)")
92
93 name = "Object (cyclical)"
94 result = nuxt.resolve([{"self": 0}])
95 self.assertIs(result, result["self"], name)
96
97 name = "Object with null prototype (cyclical)"
98 result = nuxt.resolve([["null", "self", 0]])
99 self.assertIs(result, result["self"], name)
100
101 name = "Objects (cyclical)"
102 result = nuxt.resolve([[1, 2], {"second": 2}, {"first": 1}])
103 self.assertIs(result[0], result[1]["first"], name)
104 self.assertIs(result[1], result[0]["second"], name)
105
106
107 if __name__ == "__main__":
108 unittest.main()