コード例 #1
0
def test_improve_parent_node_not_title():
    expected = "Great Title!"

    root = TreeNode('something')
    child = TreeNode('title', root)

    improve_parent('Great Title!', child)
    assert root.value != expected
コード例 #2
0
ファイル: generate_doc.py プロジェクト: KPrasch/nucypher
def schema2rst(data: dict, excluded_key: str, tree_name: str):
    """Mimics parser.schema2rst method with string input parameters"""

    tree = TreeNode(tree_name)

    rst = RST_DIRECTIVES
    TreeNode.dict2tree(data, tree, excluded_key)
    rst += _traverse_bfs(tree, _node2rst)
    return rst
コード例 #3
0
def test_dict2tree_simple_dict():
    expected = TreeNode('Root')
    child_1 = TreeNode('value1: foo', expected)
    child_2 = TreeNode('value2: bar', expected)

    dictionary = {'value1': 'foo', 'value2': 'bar'}

    result = TreeNode.dict2tree(dictionary, None)

    assert result == expected
コード例 #4
0
def test_dict2tree_simple_dict_with_integers():
    expected = TreeNode('Root')
    child_1 = TreeNode('value1: 1', expected)
    child_2 = TreeNode('value2: 2', expected)

    dictionary = {'value1': 1, 'value2': 2}

    result = TreeNode.dict2tree(dictionary, None)

    assert result == expected
コード例 #5
0
def test_dict2tree_with_none_input_on_two_entries_gives_tree_with_two_child():
    expected = TreeNode('Root')
    child_1 = TreeNode('value1: foo', expected)
    child_2 = TreeNode('value2: bar', expected)

    dictionary = {'value1': 'foo', 'value2': 'bar'}

    result = TreeNode.dict2tree(dictionary, None)

    assert result == expected
コード例 #6
0
def test_dict2tree_dict_with_dict():
    expected = TreeNode('Root')
    child_1 = TreeNode('dict1', expected)
    child_2 = TreeNode('dict2', child_1)
    child_3 = TreeNode('value: foo', child_2)

    dictionary = {'dict1': {'dict2': {'value': 'foo'}}}

    result = TreeNode.dict2tree(dictionary, None)

    assert result == expected
コード例 #7
0
def test_dict2tree_list_of_list():
    expected = TreeNode('Root')
    child_1 = TreeNode('list', expected)
    child_intermediate = TreeNode(0, child_1)
    child_1_1 = TreeNode('value: foo', child_intermediate)

    dictionary = {'list': [[{'value': 'foo'}]]}

    result = TreeNode.dict2tree(dictionary, None)

    assert result == expected
コード例 #8
0
def test_dict2tree_two_entries_appends_two_child_to_given_tree():
    expected = TreeNode()
    child_1 = TreeNode('value1: foo', expected)
    child_2 = TreeNode('value2: bar', expected)

    dictionary = {'value1': 'foo', 'value2': 'bar'}

    result = TreeNode()
    result = TreeNode.dict2tree(dictionary, result)

    assert result == expected
コード例 #9
0
def test_search_in_parents_siblings_subtrees():
    expected = TreeNode('Searched')

    root = TreeNode('Root')
    sibling_1 = TreeNode('Sibling 1', root)
    searching_leaf = TreeNode('Searching', sibling_1)
    sibling_2 = TreeNode('Sibling 2', root)
    unrelated_leaf = TreeNode('Unrelated', sibling_2)
    searched = TreeNode('Searched', sibling_2)

    result = searching_leaf.search_in_parents_siblings_subtrees('Searched')

    assert result == expected
コード例 #10
0
def test_init_with_string():
    expected = "foo"
    node = TreeNode(expected)
    result = node.value

    assert result == expected
    assert node.lvl == 0
    assert node.children == []
    assert node.parent is None
コード例 #11
0
def test_next_id():
    expected = [1, 2, 3]

    TreeNode._ID = 0  # reset the id counter
    result = []
    for i in range(3):
        result.append(TreeNode._next_id())

    assert result == expected
コード例 #12
0
def schema2rst(schema_file, excluded_key):
    """
    Parse a json/yaml schema file into RST text.

    Example:
        with open("schema.json") as schema:
            print(jsonschema2rst(schema))

    Args:
        schema_file(file): a json or yaml schema file descriptor.

        excluded_key(string): csv containing schema's keywords to ignore

    Returns:
        string: a restructured-text string representing ``schema_file``
    """
    tree = TreeNode(
        os.path.basename(change_extension(schema_file.name, JSON_EXTENSION)))

    rst = RST_DIRECTIVES
    TreeNode.dict2tree(yaml.load(schema_file), tree, excluded_key)
    rst += _traverse_bfs(tree, _node2rst)
    return rst
コード例 #13
0
def test_eq_true():
    tree1 = TreeNode(1)
    sub_tree_1_1 = TreeNode(2, tree1)
    sub_tree_1_2 = TreeNode(3, tree1)

    tree2 = TreeNode(1)
    sub_tree_2_1 = TreeNode(2, tree2)
    sub_tree_2_2 = TreeNode(3, tree2)

    assert tree1 == tree2
コード例 #14
0
def test_eq_false():
    tree1 = TreeNode(1)
    sub_tree_1_1 = TreeNode(2, tree1)
    sub_tree_1_2 = TreeNode(4, tree1)

    tree2 = TreeNode(1)
    sub_tree_2_1 = TreeNode(2, tree2)
    sub_tree_2_2 = TreeNode(3, tree2)

    assert not tree1 == tree2
コード例 #15
0
def test_ancestors():
    tree = TreeNode(0)
    child_1 = TreeNode(1, tree)
    child_2 = TreeNode(2, child_1)
    child_3 = TreeNode(3, child_2)

    expected = [tree, child_1, child_2]

    result = child_3.ancestors()

    assert result == expected
コード例 #16
0
def test_is_leaf_false():
    parent = TreeNode('inner')
    TreeNode('leaf', parent)

    assert parent.is_leaf() is False
コード例 #17
0
def test_is_leaf_true():
    leaf = TreeNode('leaf')

    assert leaf.is_leaf() is True
コード例 #18
0
def test_init_without_val():
    expected = ''
    result = TreeNode().value

    assert result == expected
コード例 #19
0
def test_init_with_integer():
    expected = '1'
    result = TreeNode(1).value

    assert result == expected
コード例 #20
0
def test_init_with_none():
    expected = 'None'
    result = TreeNode(None).value

    assert result == expected
コード例 #21
0
def test_dict2tree_none_dict_gives_just_a_node():
    expected = TreeNode('Root')
    result = TreeNode.dict2tree(None, None)

    assert result == expected
コード例 #22
0
def test_ancestors_empty_list_expected():
    expected = []
    result = TreeNode().ancestors()

    assert result == expected
コード例 #23
0
def test_init_with_int():
    expected = "1"
    result = TreeNode(1).value

    assert result == expected
コード例 #24
0
def test_dict2tree_none_parent_gives_tree_with_standard_root_value():
    expected = TreeNode('Root')
    result = TreeNode.dict2tree({}, None)

    assert result == expected