#!/usr/bin/env python
# -*- coding: utf-8 mode: python -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8

__all__ = ('suite',
           )

import os
from os import path

from base import TC
from utools.base.config import ConfigFile, ShConfigFile #, PListFile

class ClassTest(TC):
    def tf(self):
        eq = self.failUnlessEqual
        ist = self.failUnless
        isf = self.failIf
        ex = self.failUnlessRaises
        return eq, ist, isf, ex

    def get_file(self, name):
        return path.join(path.split(__file__)[0], name)

    def test_config(self):
        eq, _, _, _ = self.tf()
        cf = ConfigFile(self.get_file("test.config"))
        eq("value0", cf.get("var0"))
        eq("value1", cf.get("var1"))
        eq("value2", cf.get("var2"))
        eq("\\\nvalue30\nvalue31\n", cf.get("var3"))
        eq("\nvalue40\nvalue41", cf.get("var4"))
        eq("value50:value51", cf.get("var5"))
        eq("  value60  :  value61  ", cf.get("var6"))
        eq("$X ${X} \\\" \\\\ \\$ \\`", cf.get("var7"))

        eq(["\\", "value30", "value31", ""], cf.get_list("var3"))
        eq(["", "value40", "value41"], cf.get_list("var4"))
        eq(["value50", "value51"], cf.get_paths("var5"))
        eq(["  value60  ", "  value61  "], cf.get_paths("var6"))

        eq(["\\", "value30", "value31"], cf.get_list("var3", True))
        eq(["value40", "value41"], cf.get_list("var4", True))
        eq(["value50", "value51"], cf.get_paths("var5", True))
        eq(["value60", "value61"], cf.get_paths("var6", True))

    def test_sh_config(self):
        eq, _, _, _ = self.tf()

        if os.environ.has_key("X"): del os.environ["X"]
        cf = ShConfigFile(self.get_file("test.sh_config"))
        eq("value0", cf.get("var0"))
        eq("value1", cf.get("var1"))
        eq("value2", cf.get("var2"))
        eq("value30\nvalue31\n", cf.get("var3"))
        eq("\nvalue40\nvalue41", cf.get("var4"))
        eq("value50:value51", cf.get("var5"))
        eq("  value60  :  value61  ", cf.get("var6"))
        eq("  \" \\ $ \\`", cf.get("var7"))

        eq(["value30", "value31", ""], cf.get_list("var3"))
        eq(["", "value40", "value41"], cf.get_list("var4"))
        eq(["value50", "value51"], cf.get_paths("var5"))
        eq(["  value60  ", "  value61  "], cf.get_paths("var6"))

        eq(["value30", "value31"], cf.get_list("var3", True))
        eq(["value40", "value41"], cf.get_list("var4", True))
        eq(["value50", "value51"], cf.get_paths("var5", True))
        eq(["value60", "value61"], cf.get_paths("var6", True))

        os.environ["X"] = "valueX"
        cf = ShConfigFile(self.get_file("test.sh_config"))
        eq("valueX valueX \" \\ $ \\`", cf.get("var7"))

    def test_plist(self):
        #XXX test PListFile
        pass

def suite():
    from base import TestLoader
    return TestLoader().loadTestsFromTestCase(ClassTest)

if __name__ == '__main__':
    from base import main
    main()
