# test_pamhyr.py -- Pamhyr
# Copyright (C) 2023  INRAE
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

# -*- coding: utf-8 -*-

import os
import unittest
import tempfile

from tools import parse_command_line


class ToolsCMDParserTestCase(unittest.TestCase):
    def test_trivial(self):
        cmd = "foo"
        expect = ["foo"]

        res = parse_command_line(cmd)

        for i, s in enumerate(expect):
            self.assertEqual(res[i], s)

    def test_unix_simple(self):
        cmd = "/foo/bar a -b -c"
        expect = ["/foo/bar", "a", '-b', "-c"]

        res = parse_command_line(cmd)

        for i, s in enumerate(expect):
            self.assertEqual(res[i], s)

    def test_unix_quoted(self):
        cmd = "\"/foo/bar\" -a -b -c"
        expect = ["/foo/bar", "-a", '-b', "-c"]

        res = parse_command_line(cmd)

        for i, s in enumerate(expect):
            self.assertEqual(res[i], s)

    def test_unix_quoted_with_space(self):
        cmd = "\"/foo/bar baz\" -a -b -c"
        expect = ["/foo/bar baz", "-a", '-b', "-c"]

        res = parse_command_line(cmd)

        for i, s in enumerate(expect):
            self.assertEqual(res[i], s)

    def test_unix_quoted_args(self):
        cmd = "/foo/bar -a -b -c=\"baz\""
        expect = ["/foo/bar", "-a", '-b', "-c=\"baz\""]

        res = parse_command_line(cmd)

        for i, s in enumerate(expect):
            self.assertEqual(res[i], s)

    def test_unix_quoted_args_with_space(self):
        cmd = "/foo/bar -a -b -c=\"baz bazz\""
        expect = ["/foo/bar", "-a", '-b', "-c=\"baz bazz\""]

        res = parse_command_line(cmd)

        for i, s in enumerate(expect):
            self.assertEqual(res[i], s)

    def test_windows_prog_files(self):
        cmd = "\"C:\\Program Files (x86)\foo\bar\" a -b -c"
        expect = ["C:\\Program Files (x86)\foo\bar", "a", '-b', "-c"]

        res = parse_command_line(cmd)

        for i, s in enumerate(expect):
            self.assertEqual(res[i], s)

    def test_windows_prog_files_args(self):
        cmd = "\"C:\\Program Files (x86)\foo\bar\" a -b=\"baz bazz\" -c"
        expect = [
            "C:\\Program Files (x86)\foo\bar",
            "a", '-b=\"baz bazz\"', "-c"
        ]

        res = parse_command_line(cmd)

        for i, s in enumerate(expect):
            self.assertEqual(res[i], s)
