package rhogenwizard.sdk.task;

import junit.framework.TestCase;

import org.junit.Test;

public class RubyCodeExecTaskTest extends TestCase
{
    @Test
    public void testHelloWorld()
    {
        RubyCodeExecTask task = new RubyCodeExecTask("puts \"Hello, World!\"");
        task.run();
        assertTrue(task.isOk());
        assertEquals(0, task.getExitValue());
        assertEquals("", task.getError());
        assertEquals("Hello, World!\n", task.getOutput());
    }

    @Test
    public void testArithmetics()
    {
        RubyCodeExecTask task = new RubyCodeExecTask("puts 1 + 2");
        task.run();
        assertTrue(task.isOk());
        assertEquals(0, task.getExitValue());
        assertEquals("", task.getError());
        assertEquals("3\n", task.getOutput());
    }

    @Test
    public void testSeveralStatementsInline()
    {
        RubyCodeExecTask task = new RubyCodeExecTask("a = 4; b = 5; puts a + b");
        task.run();
        assertTrue(task.isOk());
        assertEquals(0, task.getExitValue());
        assertEquals("", task.getError());
        assertEquals("9\n", task.getOutput());
    }

    @Test
    public void testSeveralStatements()
    {
        RubyCodeExecTask task = new RubyCodeExecTask("h = 'Hello'", "w = 'World'", "puts h + ', ' + w + '!'");
        task.run();
        assertTrue(task.isOk());
        assertEquals(0, task.getExitValue());
        assertEquals("", task.getError());
        assertEquals("Hello, World!\n", task.getOutput());
    }

    @Test
    public void testUndefinedError()
    {
        RubyCodeExecTask task = new RubyCodeExecTask("a");
        task.run();
        assertFalse(task.isOk());
        assertEquals(1, task.getExitValue());
        assertEquals("-e:1: undefined local variable or method `a' for main:Object (NameError)\n",
            task.getError());
        assertEquals("", task.getOutput());
    }

    @Test
    public void testSyntaxError()
    {
        RubyCodeExecTask task = new RubyCodeExecTask("def");
        task.run();
        assertFalse(task.isOk());
        assertEquals(1, task.getExitValue());
        assertEquals("-e:1: syntax error, unexpected $end\n", task.getError());
        assertEquals("", task.getOutput());
    }

    @Test
    public void testPartialSuccess()
    {
        RubyCodeExecTask task = new RubyCodeExecTask("puts 'The \\'a\\' value is'", "puts a");
        task.run();
        assertFalse(task.isOk());
        assertEquals(1, task.getExitValue());
        assertEquals("-e:2: undefined local variable or method `a' for main:Object (NameError)\n",
            task.getError());
        assertEquals("The 'a' value is\n", task.getOutput());
    }
}

@@ -435,32 +435,34 @@
 (%22Hello, World!%5C
+r%5C
 n%22, task.getOutp
@@ -2683,10 +2683,457 @@
 ;%0A    %7D%0A
+    %0A    @Test%0A    public void testMultyLineCode()%0A    %7B%0A        RubyCodeExecTask task = new RubyCodeExecTask(%22puts 'The %5C%5C'a%5C%5C' value is'%5Cnputs a%22);%0A        task.run();%0A        assertFalse(task.isOk());%0A        assertEquals(1, task.getExitValue());%0A        assertEquals(%22-e:2: undefined local variable or method %60a' for main:Object (NameError)%5Cn%22,%0A            task.getError());%0A        assertEquals(%22The 'a' value is%5Cn%22, task.getOutput());%0A    %7D%0A
 %7D%0A

add multyline test