"""Basic Smorgasbord tests.
"""
import os
import tempfile

from nose.tools import ok_, eq_, raises    #with_setup

from unipath import FSPath as Path
from unipath.tools import dict2dir

from smorgasbord import Buffet, BuffetCached
from smorgasbord.plugins import Plugin
#from smorgasbord.plugins.breve_plugin import BrevePlugin
from smorgasbord.plugins.cheetah_plugin import CheetahPlugin
from smorgasbord.plugins.genshi_plugin import GenshiPlugin, GenshiTextPlugin
from smorgasbord.plugins.kid_plugin import KidPlugin
from smorgasbord.plugins.mako_plugin import MakoPlugin
#from smorgasbord.plugins.myghty_plugin import MyghtyPlugin
from smorgasbord.plugins.string_template_plugin import StringTemplatePlugin

class FixedBuffet(Buffet):
    def _get_available_engines(self):
        return {
            "string": StringTemplatePlugin,
            "mako": MakoPlugin,
            "cheetah": CheetahPlugin,
            "genshi": GenshiPlugin,
            "genshi-text": GenshiTextPlugin,
            "kid": KidPlugin,
            #"myghty": MyghtyPlugin,
            #"breve": BrevePlugin,
            }
        

#### Fixtures ####
class Fixture1(object):
    tree = {
        "dir1": {
            "index.html": "HTML index",
            "index.py": "PY index",
            "index.tmpl": "TMPL index",
            },
        "dir2": {
            "index.html": "HTML index",
            "index.py": "PY index",
            "index.tmpl": "TMPL index",
            }
        }

    def setUp(self):
        self.dir = dir = Path(tempfile.mkdtemp())
        dict2dir(self.dir, self.tree)
    
    def tearDown(self):
        self.dir.rmtree()

#### Fixture tests ####
class TestFixture1(Fixture1):
    def test_setup(self):
        assert Path(self.dir, "dir1", "index.html").exists()


#### Plugin base class tests ####
class TestBasePluginOptions(Fixture1):
    pass


class TestBasePluginFindTemplates(Fixture1):
    pass


#### Plugin tests ####
class _TestPlugin(object):
    plugin_name = None
    plugin_opts = {}
    template_filename = "template.html"
    source = "The quick brown fox jumped over the lazy ${what}."
    info = {"what": "dog"}
    expected = "The quick brown fox jumped over the lazy dog."

    def test1(self):
        #"""Fill the template and compare the result."""
        buffet = FixedBuffet()
        buffet.init_engine(self.plugin_name, naming="path", **self.plugin_opts)
        dir = Path(tempfile.mkdtemp())
        tpath = Path(dir, "template.html")
        f = open(tpath, "w")
        f.write(self.source)
        f.close()
        result = buffet.render(self.plugin_name, tpath, self.info)
        assert result == self.expected
        

class TestStringTemplatePlugin(_TestPlugin):
    plugin_name = "string"


class TestMakoPlugin(_TestPlugin):
    plugin_name = "mako"


class TestCheetahPlugin(_TestPlugin):
    plugin_name = "cheetah"


class TestGenshiTextPlugin(_TestPlugin):
    plugin_name = "genshi-text"


class TestGenshiPlugin(_TestPlugin):
    plugin_name = "genshi"
    source = "<html>%s</html>" % _TestPlugin.source
    expected = "<html>%s</html>" % _TestPlugin.expected


class TestKidPlugin(_TestPlugin):
    plugin_name = "kid"
    source = '<?xml version="1.0" encoding="utf-8"?>\n<html>%s</html>' % _TestPlugin.source
    expected = '<?xml version="1.0" encoding="utf-8"?>\n<html>%s</html>' % _TestPlugin.expected


#class TestBrevePlugin(_TestPlugin):
#    plugin_name = "breve"
#    source = '"The quick brown fox jumped over the lazy" + what + "."'


#class TestMyghtyPlugin(_TestPlugin):
#    plugin_name = "myghty"
#    source = "The quick brown fox jumped over the lazy <% what %>."

