package com.mumux.androidtesting.actions; import org.json.JSONException; import org.junit.Assert; import org.junit.Test; import java.io.FileWriter; import java.io.IOException; import java.util.Arrays; import java.util.Collections; import static org.hamcrest.CoreMatchers.is; public class ActionParserTest { @Test public void testIsActionAvailable() { Assert.assertNull(ActionParser.getAction("XXX")); Assert.assertNotNull(ActionParser.getAction("PLANE")); } @Test public void testParseTokens() { Assert.assertThat(ActionParser.parseTokens("HOME"), is(Collections.singletonList("HOME"))); Assert.assertThat(ActionParser.parseTokens("KILL_APP myapp"), is(Arrays.asList("KILL_APP", "myapp"))); Assert.assertThat(ActionParser.parseTokens("PRESS \"suivi conso\""), is(Arrays.asList("PRESS", "suivi conso"))); Assert.assertThat(ActionParser.parseTokens("INPUT \"this is the id\" \"this is the message\""), is(Arrays.asList("INPUT", "this is the id", "this is the message"))); Assert.assertThat(ActionParser.parseTokens("KILL_APP myapp"), is(Arrays.asList("KILL_APP", "myapp"))); Assert.assertThat(ActionParser.parseTokens("PRESS MY_BUTTON # This is Comment"), is(Arrays.asList("PRESS", "MY_BUTTON"))); //Assert.assertThat(ActionParser.parseTokens("PRESS \"#BUTTON\""), is(Arrays.asList("PRESS", "#BUTTON"))); // TODO } @Test public void testParseAction() throws Exception { try { ActionParser.parseAction("XXX"); throw new Exception("Should not happen!"); } catch (ActionParseException e) { Assert.assertEquals("Action XXX not found", e.getMessage()); } try { ActionParser.parseAction("PLANE"); throw new Exception("Should not happen!"); } catch (ActionParseException e) { Assert.assertEquals("Invalid number of arguments for action PLANE", e.getMessage()); } try { ActionParser.parseAction("PLANE XXX"); throw new Exception("Should not happen!"); } catch (ActionParseException e) { //Assert.assertEquals("Invalid value XXX for ON_OFF", e.getMessage()); Assert.assertEquals("Invalid value XXX - should be 'on' or 'off'", e.getMessage()); } Action action = ActionParser.parseAction("PLANE on"); Assert.assertEquals("PLANE", action.getName()); Assert.assertArrayEquals("testing values", new String[]{"on"}, action.getStringValues()); Assert.assertFalse(action.isOptional()); //action = ActionParser.parseAction("PRESS MY_BUTTON # This is Comment"); //Assert.assertEquals("PRESS", action.getName()); //Assert.assertEquals(1, action.getArguments().length); //Assert.assertEquals("MY_BUTTON", action.getArguments()[0].getStringValue()); //action = ActionParser.parseAction("PRESS \"#BUTTON\""); } @Test public void testAllActionInJson() throws JSONException, IOException { String OUTPUT_FILE = "build/actions.json"; FileWriter file = new FileWriter(OUTPUT_FILE); file.write(ActionParser.getJson().toString()); file.close(); } }