# encoding: utf-8

"""
.. codeauthor:: Tsuyoshi Hombashi <gogogo.vm@gmail.com>
"""

import pytest
from subprocrunner import SubprocessRunner
import thutils


@pytest.fixture
def device_option(request):
    return request.config.getoption("--device")


class Test_tcshow(object):
    """
    Tests of in this class are inappropriate for Travis CI.
    Execute following command at the local environment  when running tests:
      python setup.py test --addopts --runxfail
    """

    @pytest.mark.xfail
    def test_normal(self, device_option):
        command = " ".join([
            "tcset",
            "--device", device_option,
            "--delay", "10",
            "--delay-distro", "2",
            "--loss", "0.01",
            "--rate", "0.25M",
            "--network", "192.168.0.10",
            "--port", "8080",
        ])
        assert SubprocessRunner(command).run() == 0

        command = " ".join([
            "tcset",
            "--device", device_option,
            "--delay", "1",
            "--loss", "0.02",
            "--rate", "500K",
            "--direction", "incoming",
        ])
        assert SubprocessRunner(command).run() == 0

        command = " ".join([
            "tcshow",
            "--device", device_option,
        ])
        proc = SubprocessRunner(command).popen()
        stdout, _stderr = proc.communicate()
        assert thutils.loader.JsonLoader.loads(stdout) == thutils.loader.JsonLoader.loads("""{
    "eth0": {
        "outgoing": {
            "network=192.168.0.10/32, port=8080": {
                "delay": "10.0", 
                "loss": "0.01", 
                "rate": "250K", 
                "delay-distro": "2.0"
            }, 
            "network=0.0.0.0/0": {}
        }, 
        "incoming": {
            "network=0.0.0.0/0": {
                "delay": "1.0", 
                "loss": "0.02", 
                "rate": "500K"
            }
        }
    }
}
""")

        assert SubprocessRunner("tcdel --device " + device_option).run() == 0

@@ -77,16 +77,29 @@
 m%3E%0A%22%22%22%0A%0A
+import json%0A%0A
 import p
@@ -1394,101 +1394,87 @@
 ert 
-thutils.loader.JsonLoader.loads(stdout) == thutils.loader.JsonLoader
+json.loads(stdout) == json
 .loads(
-%22%22%22%7B
 %0A    
-%22eth0%22: %7B
+        %22%7B%22 + device_option + %22: %7B%22 + %22%22%22
 %0A   
@@ -1931,17 +1931,16 @@
 %0A    %7D%0A%7D
-%0A
 %22%22%22)%0A%0A  

Update JSON parsing method