/* MonkeyTalk - a cross-platform functional testing tool
Copyright (C) 2012 Gorilla Logic, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
package com.gorillalogic.monkeytalk.api.js.tools.tests;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.matchers.JUnitMatchers.containsString;
import static org.junit.matchers.JUnitMatchers.hasItem;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
import com.gorillalogic.monkeytalk.api.js.tools.JSHelper;
import com.gorillalogic.monkeytalk.utils.FileUtils;
import com.gorillalogic.monkeytalk.utils.TestHelper;
public class JSHelperTest extends TestHelper {
private ByteArrayOutputStream out;
@Before
public void before() {
out = new ByteArrayOutputStream();
System.setOut(new PrintStream(out));
}
@After
public void after() {
out = null;
}
@AfterClass
public static void afterClass() throws IOException {
cleanup();
}
@Test
public void testReservedWords() throws IOException {
assertThat(JSHelper.RESERVED_WORDS, hasItem("alert"));
}
@Test
public void testCopyAPI() throws IOException {
File dir = tempDir();
File api = new File(dir, "api.js");
JSHelper.copyAPI(api);
assertThat(api.exists(), is(true));
assertThat(api.isFile(), is(true));
String contents = FileUtils.readFile(api);
assertThat(contents, containsString("MonkeyTalkAPI.js generated by MonkeyTalk"));
}
@Test
public void testGenAPI() throws IOException {
File dir = tempDir();
JSHelper.genAPIAndLib(dir);
File libs = new File(dir, "libs");
assertThat(libs.exists(), is(true));
assertThat(libs.isDirectory(), is(true));
File api = new File(libs, "MonkeyTalkAPI.js");
assertThat(api.exists(), is(true));
assertThat(api.isFile(), is(true));
String js = FileUtils.readFile(api);
assertThat(js, containsString("MonkeyTalkAPI.js generated by MonkeyTalk"));
}
@Test
public void testGenMT() throws IOException {
File dir = tempDir();
File foo = tempScript("foo.mt", "Button FOO Tap", dir);
JSHelper.genJS(foo);
File fooJS = new File(dir, "foo.js");
assertThat(fooJS.exists(), is(true));
assertThat(fooJS.isFile(), is(true));
String js = FileUtils.readFile(fooJS);
assertThat(js, containsString("load(\"libs/MyProj.js\");"));
assertThat(js, containsString("MyProj.foo.prototype.run = function() {\n"));
assertThat(js, containsString("var app = this.app;\n"));
assertThat(js, containsString("app.button(\"FOO\").tap();\n"));
}
}