<commit_before># Copyright (c) 2017 The Regents of the University of Michigan.
# All Rights Reserved. Licensed according to the terms of the Revised
# BSD License. See LICENSE.txt for details.
from hamcrest import *
from hamcrest.core.base_matcher import BaseMatcher
import unittest

from .hamcrest import evaluates_to
from ..logtree import MutableTree

class TreeMatcher (BaseMatcher):

    def __init__ (self, expected_value):
        self.expected_value = expected_value

    def describe_to (self, description):
        description.append_text(
                self.expectation.format(repr(self.expected_value)))

class has_full_length (TreeMatcher):
    expectation = "a tree with a full length of {}"

    def _matches (self, item):
        return item.full_length() == self.expected_value

class iterates_into_list (TreeMatcher):
    expectation = "a tree that iterates into {}"

    def _matches (self, item):
        return list(item) == self.expected_value

class iterates_recursively_into_list (TreeMatcher):
    expectation = "a tree that iterates recursively into {}"

    def _matches (self, item):
        return list(item.walk()) == self.expected_value

class has_value (TreeMatcher):
    expectation = "a tree node with a value of {}"

    def _matches (self, item):
        return item.value == self.expected_value

class GivenEmptyTree:

    def setUp (self):
        self.tree = MutableTree()

    def set_value (self, value):
        self.tree.value = value

class TestGivenEmptyTree (GivenEmptyTree, unittest.TestCase):

    def test_evaluates_to_false (self):
        assert_that(self.tree, evaluates_to(False))

    def test_has_length_of_0 (self):
        assert_that(self.tree, has_length(0))

    def test_has_total_length_of_0 (self):
        assert_that(self.tree, has_full_length(0))

    def test_iterates_into_empty_list (self):
        assert_that(self.tree, iterates_into_list([]))

    def test_walk_iterates_into_empty_list (self):
        assert_that(self.tree, iterates_recursively_into_list([]))

    def test_has_no_first_item (self):
        assert_that(calling(lambda x: x[0]).with_args(self.tree),
                    raises(IndexError))

    def test_value_is_none (self):
        assert_that(self.tree, has_value(None))

    def test_can_modify_value (self):
        self.set_value("hello")
        assert_that(self.tree, has_value("hello"))

        self.set_value(235813)
        assert_that(self.tree, has_value(235813))

    def test_cannot_delete_value (self):
        assert_that(calling(delattr).with_args(self.tree, "value"),
                    raises(AttributeError))

    def test_when_value_changes_it_still_cannot_be_deleted (self):
        self.set_value("hello")
        assert_that(calling(delattr).with_args(self.tree, "value"),
                    raises(AttributeError))

class GivenTreeWithOneEmptyChild (unittest.TestCase):

    def setUp (self):
        self.tree = MutableTree()
        self.tree.insert(0, MutableTree())

    def test_has_length_of_1 (self):
        assert_that(self.tree, has_length(1))<commit_msg>Separate context for tree with 1 child too<commit_after># Copyright (c) 2017 The Regents of the University of Michigan.
# All Rights Reserved. Licensed according to the terms of the Revised
# BSD License. See LICENSE.txt for details.
from hamcrest import *
from hamcrest.core.base_matcher import BaseMatcher
import unittest

from .hamcrest import evaluates_to
from ..logtree import MutableTree

class TreeMatcher (BaseMatcher):

    def __init__ (self, expected_value):
        self.expected_value = expected_value

    def describe_to (self, description):
        description.append_text(
                self.expectation.format(repr(self.expected_value)))

class has_full_length (TreeMatcher):
    expectation = "a tree with a full length of {}"

    def _matches (self, item):
        return item.full_length() == self.expected_value

class iterates_into_list (TreeMatcher):
    expectation = "a tree that iterates into {}"

    def _matches (self, item):
        return list(item) == self.expected_value

class iterates_recursively_into_list (TreeMatcher):
    expectation = "a tree that iterates recursively into {}"

    def _matches (self, item):
        return list(item.walk()) == self.expected_value

class has_value (TreeMatcher):
    expectation = "a tree node with a value of {}"

    def _matches (self, item):
        return item.value == self.expected_value

class GivenEmptyTree:

    def setUp (self):
        self.tree = MutableTree()

    def set_value (self, value):
        self.tree.value = value

class GivenTreeWithOneEmptyChild (GivenEmptyTree):

    def setUp (self):
        super().setUp()
        self.tree.insert(0, MutableTree())

class TestEmptyTree (GivenEmptyTree, unittest.TestCase):

    def test_evaluates_to_false (self):
        assert_that(self.tree, evaluates_to(False))

    def test_has_length_of_0 (self):
        assert_that(self.tree, has_length(0))

    def test_has_total_length_of_0 (self):
        assert_that(self.tree, has_full_length(0))

    def test_iterates_into_empty_list (self):
        assert_that(self.tree, iterates_into_list([]))

    def test_walk_iterates_into_empty_list (self):
        assert_that(self.tree, iterates_recursively_into_list([]))

    def test_has_no_first_item (self):
        assert_that(calling(lambda x: x[0]).with_args(self.tree),
                    raises(IndexError))

    def test_value_is_none (self):
        assert_that(self.tree, has_value(None))

    def test_can_modify_value (self):
        self.set_value("hello")
        assert_that(self.tree, has_value("hello"))

        self.set_value(235813)
        assert_that(self.tree, has_value(235813))

    def test_cannot_delete_value (self):
        assert_that(calling(delattr).with_args(self.tree, "value"),
                    raises(AttributeError))

    def test_when_value_changes_it_still_cannot_be_deleted (self):
        self.set_value("hello")
        assert_that(calling(delattr).with_args(self.tree, "value"),
                    raises(AttributeError))

class TestTreeWithOneEmptyChild (GivenTreeWithOneEmptyChild,
                                 unittest.TestCase):

    def test_has_length_of_1 (self):
        assert_that(self.tree, has_length(1))
{'fraction_non_alphanumeric': 0.09577509529860229, 'fraction_numerical': 0.0077827191867852604, 'mean_word_length': 3.434507042253521, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '212640', 'n_tokens_mistral': 1976, 'n_tokens_neox': 1898, 'n_words': 449}