package org.rubyforge.debugcommons;

import org.rubyforge.debugcommons.RubyDebuggerProxy;
import org.rubyforge.debugcommons.model.IRubyBreakpoint;
import org.rubyforge.debugcommons.model.RubyFrame;
import org.rubyforge.debugcommons.model.RubyThreadInfo;
import org.rubyforge.debugcommons.model.RubyVariable;

/** Rather functional tests. */
public final class RubyDebugCommunicationTest extends CommonCommunicationTestBase {
    
    public RubyDebugCommunicationTest(final String name) {
        super(name);
        setDebuggerType(RubyDebuggerProxy.RUBY_DEBUG);
    }
    
    public void testComprehensive() throws Exception {
        final RubyDebuggerProxy proxy = prepareProxy(
                "s = Thread.new {", // 1
                "  a=5",            // 2
                "  x=6",            // 3
                "  puts 'x'",       // 4
                "}",                // 5
                "s.join",           // 6
                "b=10",             // 7
                "b=11");            // 8
        final IRubyBreakpoint[] breakpoints = new IRubyBreakpoint[] {
            new TestBreakpoint("test.rb", 3),
            new TestBreakpoint("test.rb", 7)
        };
        startDebugging(proxy, breakpoints, 1);
        
        // spawned thread suspended
        RubyThreadInfo[] ti = proxy.readThreadInfo();
        assertEquals("two thread", 2, ti.length);
        assertNotNull(suspendedThread);
        RubyFrame[] frames = suspendedThread.getFrames();
        assertEquals("one frames", 1, frames.length);
        RubyFrame frame = frames[0];
        assertEquals(3, frame.getLine());
        RubyVariable[] variables = frames[0].getVariables();
        assertEquals("a, b, s", 3, variables.length);
        assertEquals("a", variables[0].getName());
        assertEquals("b", variables[1].getName());
        assertEquals("s", variables[2].getName());
        RubyVariable inspected = frame.inspectExpression("a == b");
        assertEquals("eval_result", inspected.getName());
        assertEquals("false", inspected.getValue().getValueString());
        assertEquals("FalseClass", inspected.getValue().getReferenceTypeName());
        RubyVariable unknown = frame.inspectExpression("unknown_in_context");
        resumeSuspendedThread(proxy); // finish spawned thread
        
        // main thread suspended
        ti = proxy.readThreadInfo();
        assertEquals("two thread", 1, ti.length);
        assertNotNull(suspendedThread);
        frames = suspendedThread.getFrames();
        assertEquals("one frames", 1, frames.length);
        frame = frames[0];
        assertEquals(7, frame.getLine());
        variables = frame.getVariables();
        assertEquals("b, s", 2, variables.length);
        assertEquals("b", variables[0].getName());
        assertEquals("s", variables[1].getName());
        // there is a third variable 'x' for ruby 1.8.0
        waitForEvents(proxy, 1, new Runnable() {
            public void run() {
                try {
                    suspendedThread.stepOver();
                } catch (RubyDebuggerException e) {
                    throw new RuntimeException("Cannot stepOver", e);
                }
            }
        });
        frames = suspendedThread.getFrames();
        assertEquals("one frames", 1, frames.length);
        frame = frames[0];
        variables = frame.getVariables();
        assertEquals("b, s", 2, variables.length);
        assertEquals("b", variables[0].getName());
        assertEquals("s", variables[1].getName());
        resumeSuspendedThread(proxy); // finish main thread
    }
    
}

@@ -3590,10 +3590,1066 @@
  %7D%0A    %0A
+    /**%0A     * This test failed with ruby-debug-ide version &lt; 0.5. The debugger%0A     * backend just died.%0A     */%0A    public void testInspectionWithEqualityNotCountingOnNilParameter() throws Exception %7B%0A        final RubyDebuggerProxy proxy = prepareProxy(%0A                %22class A%22,%0A                %22  def ==(obj)%22,%0A                %22    obj.non_existent%22,%0A                %22  end%22,%0A                %22end%22,%0A                %22a = A.new%22,%0A                %22puts a%22);%0A        final IRubyBreakpoint%5B%5D breakpoints = new IRubyBreakpoint%5B%5D %7B%0A            new TestBreakpoint(%22test.rb%22, 7)%0A        %7D;%0A        startDebugging(proxy, breakpoints, 1);%0A        RubyFrame%5B%5D frames = suspendedThread.getFrames();%0A        assertEquals(%22one frames%22, 1, frames.length);%0A        RubyFrame frame = frames%5B0%5D;%0A        assertEquals(7, frame.getLine());%0A        RubyVariable%5B%5D variables = frames%5B0%5D.getVariables();%0A        assertEquals(%22a%22, 1, variables.length);%0A        resumeSuspendedThread(proxy); // finish main thread%0A        variables%5B0%5D.getValue().getVariables();%0A    %7D%0A    %0A
 %7D%0A

test for bug int ruby-debug-ide < 0.5 * fixed subtle bug in xml_printer.rb#print_variable which caused the debugger to die when == method was overrided and did not count on nil parameters