From 875e650865586b795af73373af288148ec231c49 Mon Sep 17 00:00:00 2001 From: TinWoodman92 Date: Wed, 5 Jul 2023 21:25:34 -0500 Subject: Moved tests into package. --- tests/__init__.py | 0 tests/test_binary_conversion.py | 58 +++++++++++++++++++++++++++++++++++++ tests/test_evaluate_expression.py | 60 +++++++++++++++++++++++++++++++++++++++ tests/test_evaluate_expression.r | 16 +++++++++++ tests/test_import_variable.py | 49 ++++++++++++++++++++++++++++++++ tests/test_import_variable.r | 8 ++++++ 6 files changed, 191 insertions(+) create mode 100644 tests/__init__.py create mode 100644 tests/test_binary_conversion.py create mode 100644 tests/test_evaluate_expression.py create mode 100644 tests/test_evaluate_expression.r create mode 100644 tests/test_import_variable.py create mode 100644 tests/test_import_variable.r (limited to 'tests') diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_binary_conversion.py b/tests/test_binary_conversion.py new file mode 100644 index 0000000..5b28f4c --- /dev/null +++ b/tests/test_binary_conversion.py @@ -0,0 +1,58 @@ +import unittest +from dialoguer.binary_conversion import bin_conv + +# some of these test currently fail because bytes are reversed. +# the binary conversions work with R's tendancy to reverse the sockets i/o +# perhaps we can put reversals in this test +class TestBinaryConversion(unittest.TestCase): + + @classmethod + def setUpClass(cls): + pass + + @classmethod + def tearDownClass(cls): + pass + + def setUp(self): + pass + + def tearDown(self): + pass + + def test_convert_to_binary(self): + bin_1 = b'\x00\x00\x00\x00\x00\x00\x01\x00' + bin_2 = bin_conv(2) + self.assertEqual(bin_1, bin_2) + + def test_convert_from_binary(self): + bin_1 = b'\x00\x01\x00\x00\x00\x00\x00\x00' + var_1 = 2 + var_2 = bin_conv(bin_1, type(var_1)) + self.assertEqual(var_1, var_2) + + def test_integer_conversion(self): + for i in range(1000): + bin_i = bin_conv(i) + bin_i = bytes(reversed(bin_i)) + var_i = bin_conv(bin_i, type(i)) + self.assertEqual(i, var_i) + + def test_string_conversion(self): + str_base = 'abcdefghijklmnopqrstuvwxyz!@#$%^&*()' + str_base += str_base.upper() + for i in range(1000): + str_i = str(i) + bin_i = bin_conv(str_i) + print(bin_i, type(bin_i)) + var_i = bin_conv(bin_i, type(str_i)) + self.assertEqual(str_i, var_i) + for s in str_base: + bin_s = bin_conv(s) + bin_s = bytes(reversed(bin_s)) + var_s = bin_conv(bin_s, type(s)) + self.assertEqual(s, var_s) + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_evaluate_expression.py b/tests/test_evaluate_expression.py new file mode 100644 index 0000000..b5389e8 --- /dev/null +++ b/tests/test_evaluate_expression.py @@ -0,0 +1,60 @@ +import unittest +import dialoguer + + +class TestEvaluateExpression(unittest.TestCase): + + # @classmethod + # def setUpClass(cls): + # source_file_path = __file__.replace('.py', '.r') + # cls.src_fil_r = dialoguer.Dialogue(source_file_path) + + # @classmethod + # def tearDownClass(cls): + # cls.src_fil_r.close() + + def setUp(self): + source_file_path = __file__.replace('.py', '.r') + self.src_fil_r = dialoguer.Dialogue(source_file_path) + + def tearDown(self): + self.src_fil_r.close() + + def test_built_in_method(self): + res_1 = self.src_fil_r.evaluate_expression('sum', 1, 2) + self.assertEqual(res_1, 3) + + def test_defined_method(self): + res_1 = self.src_fil_r.evaluate_expression('test_method', 1, 2) + self.assertEqual(res_1, 3) + + def test_kwargs(self): + kwargs = { + 'arg_1': 2, + 'arg_2': 3 + } + res_1 = self.src_fil_r.evaluate_expression('test_kwargs', **kwargs) + self.assertEqual(res_1, 6) + + def test_args_and_kwargs(self): + args = [2, 3] + kwargs = { + 'kwarg_1': 2, + 'kwarg_2': 3 + } + res_1 = self.src_fil_r.evaluate_expression('test_args_and_kwargs', *args, **kwargs) + res_ctrl = args[0] * args[1] * kwargs['kwarg_1'] * kwargs['kwarg_2'] + self.assertEqual(res_ctrl, res_1) + + def test_args_and_default_kwargs(self): + args = [2, 3] + kwargs = { + 'kwarg_2': 3 + } + res_1 = self.src_fil_r.evaluate_expression('test_args_and_kwargs', *args, **kwargs) + res_ctrl = args[0] * args[1] * 1 * kwargs['kwarg_2'] + self.assertEqual(res_ctrl, res_1) + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_evaluate_expression.r b/tests/test_evaluate_expression.r new file mode 100644 index 0000000..f9e657a --- /dev/null +++ b/tests/test_evaluate_expression.r @@ -0,0 +1,16 @@ +test_method <- function(arg_1, arg_2) { + result <- arg_1 + arg_2 + return(result) +} + +test_kwargs <- function(arg_1 = 1, arg_2 = 1) { + result <- arg_1 * arg_2 + return(result) +} + +test_args_and_kwargs <- function(arg_1, arg_2, kwarg_1 = 1, kwarg_2 = 3) { + result <- arg_1 * arg_2 + result <- result * kwarg_1 + result <- result * kwarg_2 + return(result) +} diff --git a/tests/test_import_variable.py b/tests/test_import_variable.py new file mode 100644 index 0000000..c8ac5ed --- /dev/null +++ b/tests/test_import_variable.py @@ -0,0 +1,49 @@ +import unittest +import dialoguer + + +class TestImportVariable(unittest.TestCase): + + # @classmethod + # def setUpClass(cls): + # source_file_path = __file__.replace('.py', '.r') + # cls.src_fil_r = dialoguer.Dialogue(source_file_path) + + # @classmethod + # def tearDownClass(cls): + # cls.src_fil_r.close() + + def setUp(self): + source_file_path = __file__.replace('.py', '.r') + self.src_fil_r = dialoguer.Dialogue(source_file_path) + + def tearDown(self): + self.src_fil_r.close() + + def test_import_string(self): + msg_1 = self.src_fil_r.import_variable('msg_1') + msg_2 = self.src_fil_r.import_variable('msg_2') + self.assertEqual(msg_1, 'Initializing Client - R') + self.assertEqual(msg_2, 'Initialized Client - R') + + def test_import_integer(self): + int_1 = self.src_fil_r.import_variable('int_1') + int_2 = self.src_fil_r.import_variable('int_2') + self.assertEqual(int_1, 3) + self.assertEqual(int_2, 155) + + def test_import_boolean(self): + chk_1 = self.src_fil_r.import_variable('chk_1') + chk_2 = self.src_fil_r.import_variable('chk_2') + self.assertEqual(chk_1, True) + self.assertEqual(chk_2, False) + + def test_import_double(self): + dbl_1 = self.src_fil_r.import_variable('dbl_1') + dbl_2 = self.src_fil_r.import_variable('dbl_2') + self.assertEqual(dbl_1, 3) + self.assertEqual(dbl_2, 12.345) + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_import_variable.r b/tests/test_import_variable.r new file mode 100644 index 0000000..f35ff9c --- /dev/null +++ b/tests/test_import_variable.r @@ -0,0 +1,8 @@ +msg_1 <- "Initializing Client - R" +msg_2 <- "Initialized Client - R" +int_1 <- as.integer(3) +int_2 <- as.integer(155) +dbl_1 <- 3 +dbl_2 <- 12.345 +chk_1 <- TRUE +chk_2 <- FALSE -- cgit v1.2.3