Esempio n. 1
0
class TestInstanceMatcher(Test):
    """Tests for Instance matcher
    """

    def instance_match(self, clazz, attrs=None, obj=None, should_match=True):
        match_with = obj if obj is not None else clazz()
        assert_val = Instance(clazz, attrs).match(match_with)

        if not should_match:
            assert_val = not assert_val
            msg = "{0!r} matched Instance({0.__class__.__qualname__}, {1})"
        else:
            msg = "{0!r} didn't match Instance({0.__class__.__qualname__}, {1})"  # noqa
        assert assert_val, msg.format(match_with, attrs)

    def test_instance_match(self):
        mis_match_class = MultiAttrClass()
        mis_match_class.int += 1
        yield from self.yield_tests(self.instance_match, [
            [BasicClass],
            [MultiAttrClass, MultiAttrClass.value_dict],  # noqa
            [BasicClass, None, object(), False],
            [MultiAttrClass, MultiAttrClass.value_dict, mis_match_class, False]  # noqa
        ])


if __name__ == '__main__':
    from py2c.tests import runmodule
    runmodule()
Esempio n. 2
0
                                       CouldNotWriteSourceError, "")

    def test_lists_only_passed_file_name(self):
        fsh = FileSourceHandler("magic.py")

        files = fsh.get_files()
        assert inspect.isgenerator(files), (
            "FileSourceHandler.get_files should return a generator")

        assert_equal(list(files), ["magic.py"])

    def test_gets_correct_source_correctly(self):
        file_name = self.get_temporary_file_name()
        self.write_to_file(file_name, "Hello World!")

        fsh = FileSourceHandler(file_name)
        assert_equal(fsh.get_source(file_name), "Hello World!")

    def test_writes_source_correctly(self):
        file_name = self.get_temporary_file_name()

        fsh = FileSourceHandler(file_name)
        fsh.write_source(file_name, "Hello World!")

        assert_equal(self.read_from_file(file_name), "Hello World!")


if __name__ == '__main__':
    from py2c.tests import runmodule
    runmodule()
Esempio n. 3
0
        {"description": "a name starting with dot", "args": [".invalid"]},
        {"description": "a dotted name starting with dot", "args": [".invalid.name"]},
        {"description": "a name with spaces", "args": ["invalid name"]},
        {"description": "a non-string name", "args": [1200]},
    ], prefix="raises error when registering invalid option: ")
    def test_raises_error_registering_invalid_option(self, name):
        try:
            Configuration().register_option(name)
        except Exception:
            pass
        else:
            self.fail("Should not have registered name: {}".format(name))

    def test_does_reset_options_to_default_value_correctly(self):
        self.config.register_option("option_name", "Yo!")
        # Sanity check
        assert_equal(self.config.get_option("option_name"), "Yo!")

        self.config.set_option("option_name", "Hi!")
        # Sanity check
        assert_equal(self.config.get_option("option_name"), "Hi!")

        self.config.reset()
        # Real purpose of this test
        assert_equal(self.config.get_option("option_name"), "Yo!")

if __name__ == '__main__':
    from py2c.tests import runmodule

    runmodule(capture=False)