import mock
import unittest
import subprocess
from signals.main import run_main
from tests.utils import captured_stderr, captured_stdout


class MainTestCase(unittest.TestCase):
    def test_run_command(self):
        command = "python -m signals --schema ./tests/files/test_schema.json --generator ios " \
                  "--datamodels ./tests/files/ --projectname YetiProject --apiurl http://test.com/api/v1/"
        process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        output, error = process.communicate()
        self.assertEqual(error, "")
        self.assertIn("Finished generating your files!", output)

    def test_run_main(self):
        with captured_stderr() as error, captured_stdout() as out:
            run_main("./tests/files/test_schema.json", "ios", "./tests/files/", None, "YetiProject",
                     "http://test.com/api/v1/", False)
            self.assertEqual(error.getvalue(), "")
            self.assertIn("Finished generating your files!", out.getvalue())

    @mock.patch("signals.generators.ios.ios_generator.subprocess")
    def test_run_main_error(self, mock_subprocess):
        mock_subprocess.check_output.return_value = "Xcode.app"
        with captured_stdout() as out:
            run_main("./tests/files/test_schema.json", "ios", "./tests/files/", "./core/data/path", "YetiProject",
                     "http://test.com/api/v1/", False)
            self.assertIn("Must quit Xcode before writing to core data", out.getvalue())

@@ -72,16 +72,43 @@
 run_main
+, add_trailing_slash_to_api
 %0Afrom te
@@ -1532,24 +1532,537 @@
  data%22, out.getvalue())%0A
+%0A    def test_add_trailing_slash_to_api(self):%0A        url_no_slash = 'http://test.com'%0A        url_with_slash = add_trailing_slash_to_api(None, None, url_no_slash)%0A        # '/' has been added to the url string if it did not end in a slash%0A        self.assertEqual(url_with_slash, 'http://test.com/')%0A%0A        same_url = add_trailing_slash_to_api(None, None, url_with_slash)%0A        # '/' has not been added to the url string if it already ended in a slash%0A        self.assertEqual(same_url, 'http://test.com/')%0A

test for trailing_slash