Esempio n. 1
0
    def test_fail(self):
        def detect(context):
            context.fail('abcde')

        with self.assertRaises(FormatRequirementsUnmet) as result:
            apply_format_detector(self._dataset_root, detect)

        self.assertEqual(result.exception.failed_alternatives, ('abcde', ))
Esempio n. 2
0
    def test_root_path(self):
        provided_root = None

        def detect(context):
            nonlocal provided_root
            provided_root = context.root_path

        apply_format_detector(self._dataset_root, detect)
        self.assertEqual(provided_root, self._dataset_root)
Esempio n. 3
0
    def test_require_files_failure(self):
        def detect(context):
            context.require_files('*.txt')

        with self.assertRaises(FormatRequirementsUnmet) as result:
            apply_format_detector(self._dataset_root, detect)

        self.assertEqual(len(result.exception.failed_alternatives), 1)
        self.assertIn('*.txt', result.exception.failed_alternatives[0])
Esempio n. 4
0
    def test_probe_text_file_failure_bad_file(self):
        def detect(context):
            with context.probe_text_file('foobar.txt', 'abcde'):
                pass

        with self.assertRaises(FormatRequirementsUnmet) as result:
            apply_format_detector(self._dataset_root, detect)

        self.assertEqual(result.exception.failed_alternatives,
                         ('foobar.txt: abcde', ))
Esempio n. 5
0
    def test_probe_text_file_success(self):
        with open(osp.join(self._dataset_root, 'foobar.txt'), 'w') as f:
            print('123', file=f)

        def detect(context):
            with context.probe_text_file('foobar.txt', 'abcde') as f:
                if next(f) != '123\n':
                    raise Exception

        apply_format_detector(self._dataset_root, detect)
Esempio n. 6
0
    def test_require_file_failure(self):
        with open(osp.join(self._dataset_root, 'foobar.txt'), 'w'):
            pass

        def detect(context):
            context.require_file('*/*')

        with self.assertRaises(FormatRequirementsUnmet) as result:
            apply_format_detector(self._dataset_root, detect)

        self.assertEqual(len(result.exception.failed_alternatives), 1)
        self.assertIn('*/*', result.exception.failed_alternatives[0])
Esempio n. 7
0
    def test_probe_text_file_nested_req(self):
        with open(osp.join(self._dataset_root, 'foobar.txt'), 'w'):
            pass

        def detect(context):
            with context.probe_text_file('foobar.txt', 'abcde'):
                context.fail('abcde')

        with self.assertRaises(FormatRequirementsUnmet) as result:
            apply_format_detector(self._dataset_root, detect)

        self.assertEqual(result.exception.failed_alternatives, ('abcde', ))
Esempio n. 8
0
    def test_require_file_success(self):
        with open(osp.join(self._dataset_root, 'foobar.txt'), 'w'):
            pass

        selected_file = None

        def detect(context):
            nonlocal selected_file
            selected_file = context.require_file('**/[fg]oo*.t?t')

        apply_format_detector(self._dataset_root, detect)

        self.assertEqual(selected_file, 'foobar.txt')
Esempio n. 9
0
    def test_require_any_failure(self):
        def detect(context):
            with context.require_any():
                with context.alternative():
                    context.fail('bad alternative 1')
                with context.alternative():
                    context.fail('bad alternative 2')

        with self.assertRaises(FormatRequirementsUnmet) as result:
            apply_format_detector(self._dataset_root, detect)

        self.assertEqual(result.exception.failed_alternatives,
                         ('bad alternative 1', 'bad alternative 2'))
Esempio n. 10
0
    def test_require_files_success(self):
        for stem in 'cba':
            with open(osp.join(self._dataset_root, f'{stem}.txt'), 'w'):
                pass

        selected_files = []

        def detect(context):
            selected_files.extend(
                context.require_files('*.txt', exclude_fnames='b.txt'))

        apply_format_detector(self._dataset_root, detect)

        self.assertEqual(selected_files, ['a.txt', 'c.txt'])
Esempio n. 11
0
    def test_require_file_exclude_fname_many(self):
        for ext in ('txt', 'lst'):
            with open(osp.join(self._dataset_root, f'foobar.{ext}'), 'w'):
                pass

        def detect(context):
            context.require_file('foobar.*', exclude_fnames=('*.txt', '*.lst'))

        with self.assertRaises(FormatRequirementsUnmet) as result:
            apply_format_detector(self._dataset_root, detect)

        self.assertEqual(len(result.exception.failed_alternatives), 1)
        self.assertIn('foobar.*', result.exception.failed_alternatives[0])
        self.assertIn('*.txt', result.exception.failed_alternatives[0])
        self.assertIn('*.lst', result.exception.failed_alternatives[0])
Esempio n. 12
0
    def test_require_any_success(self):
        alternatives_executed = set()

        def detect(context):
            nonlocal alternatives_executed
            with context.require_any():
                with context.alternative():
                    alternatives_executed.add(1)
                    context.fail('bad alternative 1')
                with context.alternative():
                    alternatives_executed.add(2)
                    # good alternative 2
                with context.alternative():
                    alternatives_executed.add(3)
                    context.fail('bad alternative 3')

        apply_format_detector(self._dataset_root, detect)

        self.assertEqual(alternatives_executed, {1, 2, 3})
Esempio n. 13
0
 def test_custom_confidence(self):
     result = apply_format_detector(self._dataset_root,
                                    lambda c: FormatDetectionConfidence.LOW)
     self.assertEqual(result, FormatDetectionConfidence.LOW)
Esempio n. 14
0
 def test_default_result(self):
     result = apply_format_detector(self._dataset_root, lambda c: None)
     self.assertEqual(result, FormatDetectionConfidence.MEDIUM)
Esempio n. 15
0
    def test_raise_unsupported(self):
        def detect(context):
            context.raise_unsupported()

        with self.assertRaises(FormatDetectionUnsupported):
            apply_format_detector(self._dataset_root, detect)