package cn.lihongjie.coal.common;

import static org.junit.jupiter.api.Assertions.*;

import freemarker.template.TemplateException;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.*;

class FreeMakerUtilsTest {

    @Test
    void testIf() {

        Assertions.assertEquals(
                "t",
                FreeMakerUtils.render(
                                """
        <@if test=b>

        t
        </@if>

        """,
                                Map.of("b", true))
                        .trim());

        Assertions.assertThrows(
                TemplateException.class,
                () -> {
                    FreeMakerUtils.render(
                                    """
    <@if test=b>

    t
    </@if>

    """,
                                    Map.of("b", "1"))
                            .trim();
                });
    }

    @Test
    void testTrim() {

        Assertions.assertEquals(
                "where 1=1",
                FreeMakerUtils.render(
                                """
        <@trim prefix="where" prefixOverrides="AND |OR ">
        and 1=1
        </@trim>

        """,
                                Map.of("b", true))
                        .trim());

    }

    @Test
    void testWhere() {

        Assertions.assertEquals(
                "where 1=1",
                FreeMakerUtils.render(
                                """
        <@where >
        and 1=1
        </@where>

        """,
                                Map.of("b", true))
                        .trim());

    }

    @Test
    void testSet() {

        Assertions.assertEquals(
                "set a=1",
                FreeMakerUtils.render(
                                """
        <@set >
        ,a=1
        </@set>

        """,
                                Map.of("b", true))
                        .trim());

    }

    @Test
    void testObject() {
        Assertions.assertEquals(
                "set a=1",
                FreeMakerUtils.render(
                                """
        <@set >
        ,a=1
        </@set>

        """,
                                new Demo("1") )
                        .trim());



    }

    @Test
    void testForeach() {
        Assertions.assertEquals(
                """
where id in (        					1
,        					2
,        					3
,)
""".trim(),
                FreeMakerUtils.render(
                                """
       <@where>
               				<@foreach item="item" index="index" collection="ids"
               						 open="id in (" separator="," close=")" nullable="true">
               					${item}
               				</@foreach>
               		</@where>

        """,
                                Map.of("ids", List.of(1, 2, 3)))
                        .trim());
    }

    @Test
    void testForeach2() {
        Assertions.assertEquals(
                """
where id in (        					:item_0
,        					:item_1
,        					:item_2
,)
"""
                        .trim(),
                FreeMakerUtils.render(
                                """
       <@where>
               				<@foreach item="item" index="index" collection="ids"
               						 open="id in (" separator="," close=")" nullable="true">
               					:item_${index}
               				</@foreach>
               		</@where>

        """,
                                Map.of("ids", List.of(1, 2, 3)))
                        .trim());
    }

    record Demo(String k){

}
}
