import pytest


def check_passed(spec, testdir):
    opt, passed_result = spec
    rec = testdir.inline_run("-a", opt)
    passed, skipped, fail = rec.listoutcomes()
    passed = [x.nodeid.split("::")[-1] for x in passed]
    assert len(passed) == len(passed_result)
    assert set(passed) == set(passed_result)


def test_arg(testdir):
    result = testdir.runpytest("--help")
    result.stdout.fnmatch_lines("*-a ATTREXPR*")


def test_config(testdir):
    config = testdir.parseconfig()
    assert config.getoption('attrexpr') == ''

    config = testdir.parseconfig('-a', 'attr1')
    assert config.getoption('attrexpr') == 'attr1'

    config = testdir.parseconfig('-a', 'attr1==val1 and attr2==True')
    assert config.getoption('attrexpr') == 'attr1==val1 and attr2==True'

    config = testdir.parseconfig('-a', 'attr1==val1 and attr2==True',
                                 '-k', 'somethingelse')
    assert config.getoption('attrexpr') == 'attr1==val1 and attr2==True'


@pytest.mark.parametrize("spec", [
    ("xyz", ("test_one",)),
    ("xyz and xyz2", ()),
    ("xyz2", ("test_two",)),
    ("xyz or xyz2", ("test_one", "test_two"),)
])
def test_functions(spec, testdir):
    testdir.makepyfile("""
        def test_one():
            pass
        test_one.xyz = "xyz"

        def test_two():
            pass
        test_two.xyz2 = "xyz2"
    """)
    return check_passed(spec, testdir)


@pytest.mark.parametrize("spec", [
    ("xyz", ("test_one",)),
    ("xyz and xyz2", ()),
    ("xyz2", ("test_two",)),
    ("xyz or xyz2", ("test_one", "test_two")),
    ("xyz3", ("test_three",)),
    ("xyz or xyz2 or xyz3", ("test_one", "test_two", "test_three")),
])
def test_classes(spec, testdir):
    testdir.makepyfile("""
        import unittest
        class OneTest(unittest.TestCase):
            def test_one(self):
                pass
        OneTest.xyz = "xyz"

        class TwoTest(unittest.TestCase):
            def test_two(self):
                pass
        TwoTest.xyz2 = "xyz2"

        class ThreeTest(unittest.TestCase):
            def test_three(self):
                pass
            test_three.xyz3 = "xyz3"
    """)
    return check_passed(spec, testdir)


@pytest.mark.parametrize("spec", [
    ("xyz=='xyz'", ("test_one",)),
    ("xyz=='xyz2'", ()),
    ("xyz=='xyz2' or xyz2", ("test_two",)),
    ("xyz=='xyz2' and xyz2=='xyz'", ()),
    ("xyz=='xyz' and xyz2=='xyz2'", ()),
])
def test_conditionals(spec, testdir):
    testdir.makepyfile("""
        import unittest
        class OneTest(unittest.TestCase):
            def test_one(self):
                pass
            xyz = "xyz"

        class TwoTest(unittest.TestCase):
            def test_two(self):
                pass
            xyz2 = "xyz2"
    """)
    return check_passed(spec, testdir)

@@ -1981,16 +1981,12 @@
     
-TwoTest.
+    
 xyz2

Include test for class attrib syntax