<commit_before>/*
 * Copyright 2012-2013 Institut National des Sciences Appliquées de Lyon (INSA-Lyon)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package gololang;

import org.testng.annotations.Test;

import java.lang.invoke.MethodHandle;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.TreeMap;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

public class TemplateEngineTest {

  @Test
  public void simple_string() throws Throwable {
    TemplateEngine engine = new TemplateEngine();
    MethodHandle tpl = engine.compile("Plop!");
    assertThat((String) tpl.invoke(null), is("Plop!"));
  }

  @Test
  public void simple_value() throws Throwable {
    TemplateEngine engine = new TemplateEngine();
    MethodHandle tpl = engine.compile("<%= params: getOrElse(\"a\", \"n/a\")%>!");
    assertThat((String) tpl.invoke(Collections.emptyMap()), is("n/a!"));
    assertThat((String) tpl.invoke(new TreeMap<String, String>() {
      {
        put("a", "Plop!");
      }
    }), is("Plop!!"));
  }

  @Test
  public void simple_repeat() throws Throwable {
    TemplateEngine engine = new TemplateEngine();
    String template = "<% foreach (i in range(0, 3)) { %>a<% } %>";
    MethodHandle tpl = engine.compile(template);
    assertThat((String) tpl.invoke(null), is("aaa"));
  }

  @Test
  public void render_people() throws Throwable {
    HashMap<String, Object> params = new HashMap<String, Object>() {
      {
        put("people", Arrays.asList("Julien", "<NAME>", "<NAME>"));
      }
    };
    String template = "People:\n" +
        "<% foreach (p in params: get(\"people\")) { %>- <%= p %>\n" +
        "<% } %>\n";
    TemplateEngine engine = new TemplateEngine();
    MethodHandle tpl = engine.compile(template);
    assertThat((String) tpl.invoke(params), is(
        "People:\n" +
        "- Julien\n" +
        "- <NAME>\n" +
        "- <NAME>\n\n"));
  }

  @Test
  public void with_params() throws Throwable {
    TemplateEngine engine = new TemplateEngine();
    String template = "<%@params foo, bar %>=<%= foo + bar %>";
    MethodHandle tpl = engine.compile(template);
    assertThat(tpl.type().parameterCount(), is(2));
    assertThat((String) tpl.invoke(1, 2), is("=3"));
  }

  @Test
  public void with_imports() throws Throwable {
    TemplateEngine engine = new TemplateEngine();
    String template = "<%@import java.lang.Math %><%= max(1, 2) %>";
    MethodHandle tpl = engine.compile(template);
    assertThat((String) tpl.invoke(null), is("2"));
  }
}
<commit_msg>Test failing when bootstrapping Golo<commit_after>/*
 * Copyright 2012-2013 Institut National des Sciences Appliquées de Lyon (INSA-Lyon)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package gololang;

import org.testng.SkipException;
import org.testng.annotations.Test;

import java.lang.invoke.MethodHandle;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.TreeMap;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

public class TemplateEngineTest {

  @Test
  public void simple_string() throws Throwable {
    TemplateEngine engine = new TemplateEngine();
    MethodHandle tpl = engine.compile("Plop!");
    assertThat((String) tpl.invoke(null), is("Plop!"));
  }

  @Test
  public void simple_value() throws Throwable {
    if (System.getenv("golo.bootstrapped") == null) {
      throw new SkipException("Golo is in a bootstrap build execution");
    }
    TemplateEngine engine = new TemplateEngine();
    MethodHandle tpl = engine.compile("<%= params: getOrElse(\"a\", \"n/a\")%>!");
    assertThat((String) tpl.invoke(Collections.emptyMap()), is("n/a!"));
    assertThat((String) tpl.invoke(new TreeMap<String, String>() {
      {
        put("a", "Plop!");
      }
    }), is("Plop!!"));
  }

  @Test
  public void simple_repeat() throws Throwable {
    TemplateEngine engine = new TemplateEngine();
    String template = "<% foreach (i in range(0, 3)) { %>a<% } %>";
    MethodHandle tpl = engine.compile(template);
    assertThat((String) tpl.invoke(null), is("aaa"));
  }

  @Test
  public void render_people() throws Throwable {
    HashMap<String, Object> params = new HashMap<String, Object>() {
      {
        put("people", Arrays.asList("Julien", "<NAME>", "<NAME>"));
      }
    };
    String template = "People:\n" +
        "<% foreach (p in params: get(\"people\")) { %>- <%= p %>\n" +
        "<% } %>\n";
    TemplateEngine engine = new TemplateEngine();
    MethodHandle tpl = engine.compile(template);
    assertThat((String) tpl.invoke(params), is(
        "People:\n" +
        "- Julien\n" +
        "- Mr Bean\n" +
        "- <NAME>\n\n"));
  }

  @Test
  public void with_params() throws Throwable {
    TemplateEngine engine = new TemplateEngine();
    String template = "<%@params foo, bar %>=<%= foo + bar %>";
    MethodHandle tpl = engine.compile(template);
    assertThat(tpl.type().parameterCount(), is(2));
    assertThat((String) tpl.invoke(1, 2), is("=3"));
  }

  @Test
  public void with_imports() throws Throwable {
    TemplateEngine engine = new TemplateEngine();
    String template = "<%@import java.lang.Math %><%= max(1, 2) %>";
    MethodHandle tpl = engine.compile(template);
    assertThat((String) tpl.invoke(null), is("2"));
  }
}

{'fraction_non_alphanumeric': 0.1491885143570537, 'fraction_numerical': 0.006554307116104869, 'mean_word_length': 3.5811293781272338, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3678', 'n_tokens_mistral': 1947, 'n_tokens_neox': 1908, 'n_words': 553}