File size: 1,406 Bytes
6e4ef92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import pytest

from pytube.exceptions import HTMLParseError
from pytube.parser import parse_for_object


def test_invalid_start():
    with pytest.raises(HTMLParseError):
        parse_for_object('test = {}', r'invalid_regex')


def test_parse_simple_empty_object():
    result = parse_for_object('test = {}', r'test\s*=\s*')
    assert result == {}


def test_parse_longer_empty_object():
    test_html = """test = {


    }"""
    result = parse_for_object(test_html, r'test\s*=\s*')
    assert result == {}


def test_parse_empty_object_with_trailing_characters():
    test_html = 'test = {};'
    result = parse_for_object(test_html, r'test\s*=\s*')
    assert result == {}


def test_parse_simple_object():
    test_html = 'test = {"foo": [], "bar": {}};'
    result = parse_for_object(test_html, r'test\s*=\s*')
    assert result == {
        'foo': [],
        'bar': {}
    }


def test_parse_context_closer_in_string_value():
    test_html = 'test = {"foo": "};"};'
    result = parse_for_object(test_html, r'test\s*=\s*')
    assert result == {
        'foo': '};'
    }


def test_parse_object_requiring_ast():
    invalid_json = '{"foo": "bar",}'
    test_html = f'test = {invalid_json}'
    with pytest.raises(json.decoder.JSONDecodeError):
        json.loads(invalid_json)
    result = parse_for_object(test_html, r'test\s*=\s*')
    assert result == {
        'foo': 'bar'
    }