<commit_before>#
# Project:   retdec-python
# Copyright: (c) 2015 by <NAME> <<EMAIL>> and contributors
# License:   MIT, see the LICENSE file for more details
#

"""Tests for the :mod:`retdec.decompiler` module."""

import unittest
from unittest import mock

from retdec.decompiler import Decompilation
from retdec.decompiler import DecompilationPhase
from retdec.decompiler import Decompiler
from retdec.exceptions import DecompilationFailedError
from retdec.exceptions import InvalidValueError
from retdec.file import File
from tests.file_tests import AnyFile
from tests.resource_tests import ResourceTestsBase
from tests.resource_tests import WithDisabledWaitingInterval
from tests.resource_tests import WithMockedIO
from tests.service_tests import BaseServiceTests


class DecompilerTests(BaseServiceTests):
    """Tests for :class:`retdec.decompiler.Decompiler`."""

    def test_repr_returns_correct_value(self):
        decompiler = Decompiler(
            api_key='API-KEY',
            api_url='https://retdec.com/service/api/'
        )
        self.assertEqual(
            repr(decompiler),
            "<retdec.decompiler.Decompiler api_url='https://retdec.com/service/api'>"
        )


class DecompilerRunDecompilationTests(BaseServiceTests):
    """Tests for :func:`retdec.decompiler.Decompiler.run_decompilation()`."""

    def setUp(self):
        super().setUp()

        self.input_file = mock.MagicMock(spec_set=File)

        self.decompiler = Decompiler(api_key='KEY')

    def test_creates_api_connection_with_correct_url_and_api_key(self):
        self.decompiler.run_decompilation(input_file=self.input_file)

        self.APIConnectionMock.assert_called_once_with(
            'https://retdec.com/service/api/decompiler/decompilations',
            self.decompiler.api_key
        )

    def test_mode_is_set_to_c_when_not_given_and_file_name_ends_with_c(self):
        self.input_file.name = 'test.c'

        self.decompiler.run_decompilation(input_file=self.input_file)

        self.conn.send_post_request.assert_called_once_with(
            '',
            params={
                'mode': 'c',
                'generate_archive': False
            },
            files={'input': AnyFile()}
        )

    def test_mode_is_set_to_bin_when_not_given_and_file_name_does_not_end_with_c(self):
        self.input_file.name = 'test.exe'

        self.decompiler.run_decompilation(input_file=self.input_file)

        self.conn.send_post_request.assert_called_once_with(
            '',
            params={
                'mode': 'bin',
                'generate_archive': False
            },
            files={'input': AnyFile()}
        )

    def test_generate_archive_is_set_to_false_when_not_given(self):
        self.input_file.name = 'test.exe'

        self.decompiler.run_decompilation(
            input_file=self.input_file
        )

        self.conn.send_post_request.assert_called_once_with(
            '',
            params={
                'mode': 'bin',
                'generate_archive': False
            },
            files={'input': AnyFile()}
        )

    def test_generate_archive_is_set_to_true_when_given(self):
        self.input_file.name = 'test.exe'

        self.decompiler.run_decompilation(
            input_file=self.input_file,
            generate_archive=True
        )

        self.conn.send_post_request.assert_called_once_with(<commit_msg>Fix the name of a Decompiler test.<commit_after>#
# Project:   retdec-python
# Copyright: (c) 2015 by <NAME> <<EMAIL>> and contributors
# License:   MIT, see the LICENSE file for more details
#

"""Tests for the :mod:`retdec.decompiler` module."""

import unittest
from unittest import mock

from retdec.decompiler import Decompilation
from retdec.decompiler import DecompilationPhase
from retdec.decompiler import Decompiler
from retdec.exceptions import DecompilationFailedError
from retdec.exceptions import InvalidValueError
from retdec.file import File
from tests.file_tests import AnyFile
from tests.resource_tests import ResourceTestsBase
from tests.resource_tests import WithDisabledWaitingInterval
from tests.resource_tests import WithMockedIO
from tests.service_tests import BaseServiceTests


class DecompilerTests(BaseServiceTests):
    """Tests for :class:`retdec.decompiler.Decompiler`."""

    def test_repr_returns_correct_value(self):
        decompiler = Decompiler(
            api_key='API-KEY',
            api_url='https://retdec.com/service/api/'
        )
        self.assertEqual(
            repr(decompiler),
            "<retdec.decompiler.Decompiler api_url='https://retdec.com/service/api'>"
        )


class DecompilerRunDecompilationTests(BaseServiceTests):
    """Tests for :func:`retdec.decompiler.Decompiler.run_decompilation()`."""

    def setUp(self):
        super().setUp()

        self.input_file = mock.MagicMock(spec_set=File)

        self.decompiler = Decompiler(api_key='KEY')

    def test_creates_api_connection_with_correct_url_and_api_key(self):
        self.decompiler.run_decompilation(input_file=self.input_file)

        self.APIConnectionMock.assert_called_once_with(
            'https://retdec.com/service/api/decompiler/decompilations',
            self.decompiler.api_key
        )

    def test_mode_is_set_to_c_when_not_given_and_file_name_ends_with_c(self):
        self.input_file.name = 'test.c'

        self.decompiler.run_decompilation(input_file=self.input_file)

        self.conn.send_post_request.assert_called_once_with(
            '',
            params={
                'mode': 'c',
                'generate_archive': False
            },
            files={'input': AnyFile()}
        )

    def test_mode_is_set_to_bin_when_not_given_and_file_name_does_not_end_with_c(self):
        self.input_file.name = 'test.exe'

        self.decompiler.run_decompilation(input_file=self.input_file)

        self.conn.send_post_request.assert_called_once_with(
            '',
            params={
                'mode': 'bin',
                'generate_archive': False
            },
            files={'input': AnyFile()}
        )

    def test_generate_archive_is_set_to_false_when_not_given(self):
        self.input_file.name = 'test.exe'

        self.decompiler.run_decompilation(
            input_file=self.input_file
        )

        self.conn.send_post_request.assert_called_once_with(
            '',
            params={
                'mode': 'bin',
                'generate_archive': False
            },
            files={'input': AnyFile()}
        )

    def test_generate_archive_is_set_to_true_when_given_as_true(self):
        self.input_file.name = 'test.exe'

        self.decompiler.run_decompilation(
            input_file=self.input_file,
            generate_archive=True
        )

        self.conn.send_post_request.assert_called_once_with(
{'fraction_non_alphanumeric': 0.09363623125091015, 'fraction_numerical': 0.001164991990680064, 'mean_word_length': 2.8028792912513842, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2942138', 'n_tokens_mistral': 2096, 'n_tokens_neox': 2036, 'n_words': 317}