#! python
# -*- coding: utf-8 -*-
#
# Copyright 2015-2019European Commission (JRC);
# Licensed under the EUPL (the 'Licence');
# You may not use this work except in compliance with the Licence.
# You may obtain a copy of the Licence at: http://ec.europa.eu/idabc/eupl

import doctest
import sys
import unittest

from pandalone.components import Assembly, FuncComponent, Pstep
import pandalone.components
from pandalone.mappings import pmods_from_tuples
from tests._tutils import init_logging


log = init_logging(__name__)


class Doctest(unittest.TestCase):
    def test_doctests(self):
        failure_count, test_count = doctest.testmod(
            pandalone.components, optionflags=doctest.NORMALIZE_WHITESPACE
        )  # | doctest.ELLIPSIS)
        self.assertGreater(test_count, 0, (failure_count, test_count))
        self.assertEqual(failure_count, 0, (failure_count, test_count))


class TestComponents(unittest.TestCase):
    def test_assembly(self):
        def cfunc_f1(comp, value_tree):
            comp.pinp().A
            comp.pout().B

        def cfunc_f2(comp, value_tree):
            comp.pinp().B
            comp.pout().C

        ass = Assembly(FuncComponent(cfunc) for cfunc in [cfunc_f1, cfunc_f2])
        ass._build()
        self.assertEqual(list(ass._iter_validations()), [])
        self.assertEqual(ass._inp, ["f1/A", "f2/B"])
        self.assertEqual(ass._out, ["f1/B", "f2/C"])

        pmods = pmods_from_tuples([("f1", r"/root1"), ("f2", r"/root2")])
        ass._build(pmods)
        self.assertEqual(
            sorted(ass._inp + ass._out),
            ["/root1/A", "/root1/B", "/root2/B", "/root2/C"],
        )


if __name__ == "__main__":
    # import sys;sys.argv = ['', 'Test.testName']
    unittest.main()
