All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.searls.jasmine.coffee.CoffeeScript Maven / Gradle / Ivy

Go to download

A JavaScript unit test plugin that processes JavaScript sources and Jasmine specs, prepares test runner HTML files, executes Jasmine specs headlessly with HtmlUnit, and produces JUnit XML reports

There is a newer version: 3.0-beta-02
Show newest version
package com.github.searls.jasmine.coffee;

import com.gargoylesoftware.htmlunit.MockWebConnection;
import com.gargoylesoftware.htmlunit.ScriptResult;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.github.searls.jasmine.io.IOUtilsWrapper;
import org.apache.commons.lang3.StringEscapeUtils;

import java.io.IOException;
import java.util.Collections;
import java.util.Map;
import java.util.WeakHashMap;

public class CoffeeScript {

  private static Map cache = Collections.synchronizedMap(new WeakHashMap());

  private final ThreadLocal htmlPage = new ThreadLocal() {
    @Override
    protected HtmlPage initialValue() {
      MockWebConnection webConnection = new MockWebConnection();
      WebClient webClient = new WebClient();
      webClient.setWebConnection(webConnection);
      try {
        HtmlPage page = webClient.getPage(WebClient.URL_ABOUT_BLANK);
        page.executeJavaScript(ioUtilsWrapper.toString("/vendor/js/coffee-script.js"));
        return page;
      } catch (IOException e) {
        throw new RuntimeException(e);
      }
    }
  };

  private final IOUtilsWrapper ioUtilsWrapper = new IOUtilsWrapper();

  public String compile(String coffee) throws IOException {
    String escapedCoffee = StringEscapeUtils.escapeEcmaScript(coffee);
    return cache.containsKey(escapedCoffee) ? cache.get(escapedCoffee) : compileAndCache(escapedCoffee);
  }

  private String compileAndCache(String input) {
    ScriptResult scriptResult = htmlPage.get().executeJavaScript(String.format("CoffeeScript.compile(\"%s\");", input));
    String result = (String) scriptResult.getJavaScriptResult();
    cache.put(input, result);
    return result;
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy