<commit_before>/*
 * Copyright (C) 2013 The Minium Authors
 *
 * 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 com.vilt.minium.script;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;

import java.io.File;
import java.net.URL;
import java.util.List;

import org.mozilla.javascript.FunctionObject;
import org.mozilla.javascript.NativeFunction;
import org.testng.annotations.Test;

import com.vilt.minium.prefs.AppPreferences;

public class MiniumScriptEngineTest {

   @Test
   public void testEngineWebElementDrivers() throws Exception {
      // given
      MiniumScriptEngine engine = new MiniumScriptEngine(WebElementsDriverFactory.instance());

      // when
      Object firefoxDriver = engine.eval("firefoxDriver");
      Object load = engine.eval("load");

      // then
      assertThat(firefoxDriver, instanceOf(NativeFunction.class));
      assertThat(load, instanceOf(FunctionObject.class));
   }

   @Test
   public void testEngineDefaultConst() throws Exception {
       // given
       MiniumScriptEngine engine = new MiniumScriptEngine();

       // when
       Object firefoxDriver = engine.eval("firefoxDriver");

       // then
       assertThat(firefoxDriver, instanceOf(NativeFunction.class));
   }

   @Test
   public void testEnginePut() throws Exception {
      // given
      MiniumScriptEngine engine = new MiniumScriptEngine();
      engine.put("foo", "bar");

      // when
      Object result = engine.eval("foo");

      // then
      assertThat(result, instanceOf(String.class));
      assertThat((String) result, equalTo("bar"));
   }

   @Test
   public void testNativeJavaObjects() throws Exception {
       // given
       MiniumScriptEngine engine = new MiniumScriptEngine();

       // when
       Object result = engine.eval("java.lang.Integer.valueOf(1)");

       // then
       assertThat(result, instanceOf(Integer.class));
   }

   @Test
   public void testRunScript() throws Exception {
       // given
       MiniumScriptEngine engine = new MiniumScriptEngine();

       // when
       URL resource = getClass().getClassLoader().getResource("test.js");
       engine.run(new File(resource.getFile()));

       // then
       assertThat((String) engine.get("foo"), equalTo("bar"));
   }

   @Test
   public void testModulePathURIs() throws Exception {
       // given
       System.setProperty("basedir", "c:\\minium-app");
       MiniumScriptEngine engine = new MiniumScriptEngine(WebElementsDriverFactory.instance(), new AppPreferences());

       // when
       List<String> modulePathURIs = engine.getModulePathURIs();

       // then
       assertThat(modulePathURIs, containsInAnyOrder("file:/c:/minium-app/modules", "http://www.lodash.com"));
   }
}<commit_msg>Test on Travis failed because of UNIX path, trying to solve it (doesn't affect release)<commit_after>/*
 * Copyright (C) 2013 The Minium Authors
 *
 * 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 com.vilt.minium.script;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;

import java.io.File;
import java.net.URL;
import java.util.List;

import org.mozilla.javascript.FunctionObject;
import org.mozilla.javascript.NativeFunction;
import org.openqa.selenium.Platform;
import org.testng.annotations.Test;

import com.vilt.minium.prefs.AppPreferences;

public class MiniumScriptEngineTest {

   @Test
   public void testEngineWebElementDrivers() throws Exception {
      // given
      MiniumScriptEngine engine = new MiniumScriptEngine(WebElementsDriverFactory.instance());

      // when
      Object firefoxDriver = engine.eval("firefoxDriver");
      Object load = engine.eval("load");

      // then
      assertThat(firefoxDriver, instanceOf(NativeFunction.class));
      assertThat(load, instanceOf(FunctionObject.class));
   }

   @Test
   public void testEngineDefaultConst() throws Exception {
       // given
       MiniumScriptEngine engine = new MiniumScriptEngine();

       // when
       Object firefoxDriver = engine.eval("firefoxDriver");

       // then
       assertThat(firefoxDriver, instanceOf(NativeFunction.class));
   }

   @Test
   public void testEnginePut() throws Exception {
      // given
      MiniumScriptEngine engine = new MiniumScriptEngine();
      engine.put("foo", "bar");

      // when
      Object result = engine.eval("foo");

      // then
      assertThat(result, instanceOf(String.class));
      assertThat((String) result, equalTo("bar"));
   }

   @Test
   public void testNativeJavaObjects() throws Exception {
       // given
       MiniumScriptEngine engine = new MiniumScriptEngine();

       // when
       Object result = engine.eval("java.lang.Integer.valueOf(1)");

       // then
       assertThat(result, instanceOf(Integer.class));
   }

   @Test
   public void testRunScript() throws Exception {
       // given
       MiniumScriptEngine engine = new MiniumScriptEngine();

       // when
       URL resource = getClass().getClassLoader().getResource("test.js");
       engine.run(new File(resource.getFile()));

       // then
       assertThat((String) engine.get("foo"), equalTo("bar"));
   }

   @Test
   public void testModulePathURIs() throws Exception {
       // given
       System.setProperty("basedir", Platform.getCurrent().is(Platform.WINDOWS) ? "c:\\minium-app" : "/opt/minium-app");
       MiniumScriptEngine engine = new MiniumScriptEngine(WebElementsDriverFactory.instance(), new AppPreferences());

       // when
       List<String> modulePathURIs = engine.getModulePathURIs();

       // then
       String modules = Platform.getCurrent().is(Platform.WINDOWS) ? "file:/c:/minium-app/modules" : "file:/opt/minium-app/modules";
       assertThat(modulePathURIs, containsInAnyOrder(modules, "http://www.lodash.com"));
   }
}
{'fraction_non_alphanumeric': 0.10933295567200113, 'fraction_numerical': 0.002549213992352358, 'mean_word_length': 3.3378378378378377, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '142506', 'n_tokens_mistral': 1939, 'n_tokens_neox': 1891, 'n_words': 507}