import unittest
import makesite


class RenderTest(unittest.TestCase):

    def test_simple_substitution(self):
        """Test basic template variable substitution."""
        template = "Hello {{ name }}!"
        result = makesite.render(template, name="World")
        self.assertEqual(result, "Hello World!")

    def test_multiple_variables(self):
        """Test substitution of multiple variables."""
        template = "{{ greeting }} {{ name }}, welcome to {{ site }}!"
        result = makesite.render(template, greeting="Hello", name="Alice", site="Blog")
        self.assertEqual(result, "Hello Alice, welcome to Blog!")

    def test_same_variable_multiple_times(self):
        """Test that the same variable can be used multiple times."""
        template = "{{ word }} {{ word }} {{ word }}"
        result = makesite.render(template, word="echo")
        self.assertEqual(result, "echo echo echo")

    def test_missing_variable_unchanged(self):
        """Test that missing variables are left unchanged."""
        template = "Hello {{ name }}, your score is {{ score }}"
        result = makesite.render(template, name="Bob")
        self.assertEqual(result, "Hello Bob, your score is {{ score }}")

    def test_no_variables(self):
        """Test template with no variables."""
        template = "Just plain text without variables"
        result = makesite.render(template)
        self.assertEqual(result, "Just plain text without variables")

    def test_variable_with_whitespace(self):
        """Test variables with different whitespace patterns."""
        template = "{{name}} {{ name }} {{  name  }}"
        result = makesite.render(template, name="Test")
        self.assertEqual(result, "Test Test Test")

    def test_numeric_values(self):
        """Test substitution with numeric values."""
        template = "Price: {{ price }}, Quantity: {{ qty }}"
        result = makesite.render(template, price=19.99, qty=5)
        self.assertEqual(result, "Price: 19.99, Quantity: 5")

    def test_empty_string_value(self):
        """Test substitution with empty string."""
        template = "Start {{ middle }} End"
        result = makesite.render(template, middle="")
        self.assertEqual(result, "Start  End")

    def test_special_characters_in_value(self):
        """Test substitution with special characters."""
        template = "Content: {{ text }}"
        result = makesite.render(template, text="<html> & 'quotes' \"double\"")
        self.assertEqual(result, "Content: <html> & 'quotes' \"double\"")

    def test_multiline_template(self):
        """Test template spanning multiple lines."""
        template = """<h1>{{ title }}</h1>
<p>{{ content }}</p>
<footer>{{ author }}</footer>"""
        result = makesite.render(template, title="Post", content="Text", author="Alice")
        expected = """<h1>Post</h1>
<p>Text</p>
<footer>Alice</footer>"""
        self.assertEqual(result, expected)

    def test_nested_braces_not_matched(self):
        """Test that nested braces are not mistakenly matched."""
        template = "Code: {{ code }}"
        result = makesite.render(template, code="{key: value}")
        self.assertEqual(result, "Code: {key: value}")

    def test_variable_name_with_underscore(self):
        """Test variable names with underscores."""
        template = "{{ first_name }} {{ last_name }}"
        result = makesite.render(template, first_name="John", last_name="Doe")
        self.assertEqual(result, "John Doe")

    def test_extra_parameters_ignored(self):
        """Test that extra parameters are ignored."""
        template = "Hello {{ name }}"
        result = makesite.render(template, name="Alice", unused="value", extra=123)
        self.assertEqual(result, "Hello Alice")

    def test_boolean_values(self):
        """Test substitution with boolean values."""
        template = "Active: {{ active }}, Disabled: {{ disabled }}"
        result = makesite.render(template, active=True, disabled=False)
        self.assertEqual(result, "Active: True, Disabled: False")

    def test_none_value(self):
        """Test substitution with None value."""
        template = "Value: {{ value }}"
        result = makesite.render(template, value=None)
        self.assertEqual(result, "Value: None")
