from linglish.grammar import parse import unittest class ParseTellTests(unittest.TestCase): def do_test_tell(self, text, expected): out = parse("command", text) out2 = {} for k,v in out.items(): if v: out2[k] = v self.assertEqual(out2, expected) def test_basic(self): self.do_test_tell("tell foo to bar", { "cmd": "tell", "application": "foo", "action": "bar" }) def test_with1str(self): self.do_test_tell("tell foo to bar with 'baz'", { "cmd": "tell", "application": "foo", "action": "bar", "params": [{'type':'string', 'value':'baz'}] }) def test_with1var(self): self.do_test_tell("tell foo to bar with baz", { "cmd": "tell", "application": "foo", "action": "bar", "params": [{'type':'var', 'name':'baz'}] }) def test_withmanystr(self): self.do_test_tell("tell foo to bar with 'baz', 'quux', 'quibble'", { "cmd": "tell", "application": "foo", "action": "bar", "params": [{'type':'string', 'value':'baz'}, {'type':'string', 'value':'quux'}, {'type':'string', 'value':'quibble'}] }) def test_withmany_nospace(self): self.do_test_tell("tell foo to bar with 'baz','quux','quibble'", { "cmd": "tell", "application": "foo", "action": "bar", "params": [{'type':'string', 'value':'baz'}, {'type':'string', 'value':'quux'}, {'type':'string', 'value':'quibble'}] }) def test_withmany_linebreak(self): self.do_test_tell("tell foo to bar with 'baz',\n'quux',\n'quibble'", { "cmd": "tell", "application": "foo", "action": "bar", "params": [{'type':'string', 'value':'baz'}, {'type':'string', 'value':'quux'}, {'type':'string', 'value':'quibble'}] }) def test_possessive(self): self.do_test_tell("tell foo's bar to baz", { "cmd": "tell", "application": "foo", "obj": "bar", "action": "baz" }) def test_possessive_withmany_linebreak(self): self.do_test_tell("tell foo's bar\nto baz\nwith 'quux'\n,\n'wibble'", { "cmd": "tell", "application": "foo", "obj": "bar", "action": "baz", "params": [{'type':'string', 'value':'quux'}, {'type':'string', 'value':'wibble'}] }) def test_via(self): self.do_test_tell("tell foo via bar to baz", { "cmd": "tell", "application": "foo", "interface": "bar", "action": "baz" }) def test_via_possessive(self): self.do_test_tell("tell foo's bar via baz to quux", { "cmd": "tell", "application": "foo", "obj": "bar", "interface": "baz", "action": "quux" }) def test_via_possessive_withmany(self): self.do_test_tell("tell foo's bar via baz to quux with 'wibble', 'flaps'", { "cmd": "tell", "application": "foo", "obj": "bar", "interface": "baz", "action": "quux", "params": [{'type':'string', 'value':'wibble'}, {'type':'string', 'value':'flaps'}] }) def test_buses(self): self.do_test_tell("tell foo on Session to bar", { "cmd": "tell", "application": "foo", "action": "bar", "bus": "Session" }) self.do_test_tell("tell foo on the Session bus to bar", { "cmd": "tell", "application": "foo", "action": "bar", "bus": "Session" }) self.do_test_tell("tell foo on System to bar", { "cmd": "tell", "application": "foo", "action": "bar", "bus": "System" }) self.do_test_tell("tell foo on the System bus to bar", { "cmd": "tell", "application": "foo", "action": "bar", "bus": "System" }) def test_as1(self): self.do_test_tell("tell foo to bar as baz", { "cmd": "tell", "application": "foo", "action": "bar", "retvals": [{"type": "var", "name": "baz"}] }) def test_asmany(self): self.do_test_tell("tell foo to bar as baz, quux", { "cmd": "tell", "application": "foo", "action": "bar", "retvals": [{"type": "var", "name": "baz"}, {"type": "var", "name": "quux"}] }) class ParseWaitTests(unittest.TestCase): def do_test_wait(self, text, expected): out = parse("command", text) out2 = {} for k,v in out.items(): if v: out2[k] = v self.assertEqual(out2, expected) def test_basic(self): self.do_test_wait("wait for foo's bar", { "cmd": "wait", "application": "foo", "signal": "bar", "wait_count": 1 }) def test_repeat(self): self.do_test_wait("wait repeatedly for foo's bar", { "cmd": "wait", "application": "foo", "signal": "bar", "wait_count": -1 }) def test_count(self): self.do_test_wait("wait 8 times for foo's bar", { "cmd": "wait", "application": "foo", "signal": "bar", "wait_count": 8 }) def test_as1(self): self.do_test_wait("wait for foo's bar as baz", { "cmd": "wait", "application": "foo", "signal": "bar", "wait_count": 1, "retvals": [{"type": "var", "name": "baz"}] }) def test_asmany(self): self.do_test_wait("wait for foo's bar as baz, quux", { "cmd": "wait", "application": "foo", "signal": "bar", "wait_count": 1, "retvals": [{"type": "var", "name": "baz"}, {"type": "var", "name": "quux"}] }) def test_tobe_value(self): self.do_test_wait("wait for foo's bar to be 'quux'", { "cmd": "wait", "application": "foo", "signal": "bar", "wait_count": 1, "required_value": {"type": "string", "value": "quux"} }) def test_tobe_name(self): self.do_test_wait("wait for foo's bar to be False", { "cmd": "wait", "application": "foo", "signal": "bar", "wait_count": 1, "required_value": {"type": "var", "name": "False"} }) class ParseAskTests(unittest.TestCase): def do_test_ask(self, text, expected): out = parse("command", text) out2 = {} for k,v in out.items(): if v: out2[k] = v self.assertEqual(out2, expected) def test_basic(self): self.do_test_ask("ask foo for bar", { "cmd": "ask", "application": "foo", "prop": "bar" }) def test_object(self): self.do_test_ask("ask foo's bar for baz", { "cmd": "ask", "application": "foo", "prop": "baz", "obj": "bar" }) class ParseRunTests(unittest.TestCase): def do_test_run(self, text, expected): out = parse("command", text) out2 = {} for k,v in out.items(): if v: out2[k] = v self.assertEqual(out2, expected) def test_basic(self): self.do_test_run("run 'something'", { "cmd": "run", "runnee": "something" }) class ParseScriptTests(unittest.TestCase): def do_test_script(self, text, expectedlist): outlist = parse("script", text) for out, expected in zip(outlist, expectedlist): out2 = {} for k,v in out.items(): if v: out2[k] = v self.assertEqual(out2, expected) def test_basic(self): self.do_test_script("ask foo for bar tell baz to quux", [ {"cmd": "ask", "application": "foo", "prop": "bar"}, {"cmd": "tell", "application": "baz", "action": "quux"} ] ) if __name__ == '__main__': unittest.main()