SetNextIpTests.cs 14.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.WebAssembly.Diagnostics;
using Newtonsoft.Json.Linq;
using System.IO;
using Xunit;
11
using Xunit.Abstractions;
12 13 14

namespace DebuggerTests;

15
public class SetNextIpTests : DebuggerTests
16
{
17 18 19
    public SetNextIpTests(ITestOutputHelper testOutput) : base(testOutput)
    {}

20
    [ConditionalFact(nameof(RunningOnChrome))]
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
    public async Task SetAndCheck()
    {
        async Task CheckLocalsAsync(JToken locals, int c, int d, int e, bool f)
        {
            CheckNumber(locals, "c", c);
            CheckNumber(locals, "d", d);
            CheckNumber(locals, "e", e);
            await CheckBool(locals, "f", f);
        }
        var bp = await SetBreakpoint("dotnet://debugger-test.dll/debugger-test.cs", 9, 8);

        //calling invoke_add twice to check if the breakpoint continue working after calling SetNextIP
        var pause_location = await EvaluateAndCheck(
            "window.setTimeout(function() { invoke_add(); invoke_add(); }, 1);",
            "dotnet://debugger-test.dll/debugger-test.cs", 9, 8,
            "IntAdd"
        );
        var top_frame = pause_location["callFrames"][0]["functionLocation"];
        await SetNextIPAndCheck(top_frame["scriptId"].Value<string>(), "dotnet://debugger-test.dll/debugger-test.cs", 12, 8, "IntAdd",
            locals_fn: async (locals) => await CheckLocalsAsync(locals, 0, 0, 0, false));
        await StepAndCheck(StepKind.Over, "dotnet://debugger-test.dll/debugger-test.cs", 13, 8, "IntAdd",
            locals_fn: async (locals) => await CheckLocalsAsync(locals, 0, 0, 0, true));
        await SetNextIPAndCheck(top_frame["scriptId"].Value<string>(), "dotnet://debugger-test.dll/debugger-test.cs", 9, 8, "IntAdd",
            locals_fn: async (locals) => await CheckLocalsAsync(locals, 0, 0, 0, true));
        await StepAndCheck(StepKind.Over, "dotnet://debugger-test.dll/debugger-test.cs", 10, 8, "IntAdd",
            locals_fn: async (locals) => await CheckLocalsAsync(locals, 30, 0, 0, true));
        await SetNextIPAndCheck(top_frame["scriptId"].Value<string>(), "dotnet://debugger-test.dll/debugger-test.cs", 11, 8, "IntAdd",
            locals_fn: async (locals) => await CheckLocalsAsync(locals, 30, 0, 0, true));
        await StepAndCheck(StepKind.Over, "dotnet://debugger-test.dll/debugger-test.cs", 12, 8, "IntAdd",
            locals_fn: async (locals) => await CheckLocalsAsync(locals, 30, 0, 10, true));

        //to check that after moving the execution pointer to the same line that there is already
        //a breakpoint, the breakpoint continue working
        pause_location = await StepAndCheck(StepKind.Resume,
            "dotnet://debugger-test.dll/debugger-test.cs", 9, 8,
            "IntAdd");
    }

59
    [ConditionalFact(nameof(RunningOnChrome))]
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    public async Task OutsideTheCurrentMethod()
    {
        var bp = await SetBreakpoint("dotnet://debugger-test.dll/debugger-test.cs", 9, 8);

        var pause_location = await EvaluateAndCheck(
            "window.setTimeout(function() { invoke_add(); }, 1);",
            "dotnet://debugger-test.dll/debugger-test.cs", 9, 8,
            "IntAdd");
        var top_frame = pause_location["callFrames"][0]["functionLocation"];
        await SetNextIPAndCheck(top_frame["scriptId"].Value<string>(), "dotnet://debugger-test.dll/debugger-test.cs", 20, 8, "IntAdd",
        expected_error: true);
        await StepAndCheck(StepKind.Over, "dotnet://debugger-test.dll/debugger-test.cs", 10, 8, "IntAdd",
        locals_fn: async (locals) =>
            {
                CheckNumber(locals, "c", 30);
                CheckNumber(locals, "d", 0);
                CheckNumber(locals, "e", 0);
                await CheckBool(locals, "f", false);
            });
    }

81
    [ConditionalFact(nameof(RunningOnChrome))]
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
    public async Task AsyncMethod()
    {
        var debugger_test_loc = "dotnet://debugger-test.dll/debugger-test.cs";

        await SetBreakpoint(debugger_test_loc, 140, 12);

        var pause_location = await EvaluateAndCheck(
            "window.setTimeout(function() { invoke_async_method_with_await(); }, 1);",
            debugger_test_loc, 140, 12, "MoveNext",
            locals_fn: async (locals) =>
            {
                CheckNumber(locals, "li", 0);
                CheckNumber(locals, "i", 42);
                await CheckString(locals, "ls", null);
            }
        );
        var top_frame = pause_location["callFrames"][0]["functionLocation"];
        await SetNextIPAndCheck(top_frame["scriptId"].Value<string>(), "dotnet://debugger-test.dll/debugger-test.cs", 141, 12, "MoveNext",
        locals_fn: async (locals) =>
            {
                CheckNumber(locals, "li", 0);
                CheckNumber(locals, "i", 42);
                await CheckString(locals, "ls", null);
            });
        await StepAndCheck(StepKind.Over, "dotnet://debugger-test.dll/debugger-test.cs", 142, 12, "MoveNext",
        locals_fn: async (locals) =>
            {
                CheckNumber(locals, "li", 0);
                CheckNumber(locals, "i", 42);
                await CheckString(locals, "ls", "string from jstest");
            });
    }

115
    [ConditionalFact(nameof(RunningOnChrome))]
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
    public async Task Lambda()
    {
        var debugger_test_loc = "dotnet://debugger-test.dll/debugger-async-test.cs";

        await SetBreakpoint(debugger_test_loc, 77, 12);
        var pause_location = await EvaluateAndCheck(
        "window.setTimeout(function() { invoke_static_method('[debugger-test] DebuggerTests.AsyncTests.ContinueWithTests:RunAsync'); })",
        debugger_test_loc, 77, 12, "MoveNext",
        locals_fn: async (locals) =>
        {
            await CheckString(locals, "str", "foobar");
            await CheckValueType(locals, "code", "System.Threading.Tasks.TaskStatus", description: "Created");
            await CheckValueType(locals, "dt0", "System.DateTime", description: "1/1/0001 12:00:00 AM");
        });
        var top_frame = pause_location["callFrames"][0]["functionLocation"];
        await SetNextIPAndCheck(top_frame["scriptId"].Value<string>(), "dotnet://debugger-test.dll/debugger-async-test.cs", 79, 16, "MoveNext",
        locals_fn: async (locals) =>
            {
                await CheckString(locals, "str", "foobar");
                await CheckValueType(locals, "code", "System.Threading.Tasks.TaskStatus", description: "Created");
                await CheckValueType(locals, "dt0", "System.DateTime", description: "1/1/0001 12:00:00 AM");
            });
        await StepAndCheck(StepKind.Over, "dotnet://debugger-test.dll/debugger-async-test.cs", 80, 16, "MoveNext",
        locals_fn: async (locals) =>
            {
                await CheckString(locals, "str", "foobar");
                await CheckValueType(locals, "code", "System.Threading.Tasks.TaskStatus", description: "Created");
                await CheckDateTime(locals, "dt0", new DateTime(3412, 4, 6, 8, 0, 2));
            });
        await SetNextIPAndCheck(top_frame["scriptId"].Value<string>(), "dotnet://debugger-test.dll/debugger-async-test.cs", 91, 16, "MoveNext",
        locals_fn: async (locals) =>
            {
                await CheckString(locals, "str", "foobar");
                await CheckValueType(locals, "code", "System.Threading.Tasks.TaskStatus", description: "Created");
                await CheckDateTime(locals, "dt0", new DateTime(3412, 4, 6, 8, 0, 2));
            });
        await StepAndCheck(StepKind.Over, "dotnet://debugger-test.dll/debugger-async-test.cs", 92, 12, "MoveNext",
        locals_fn: async (locals) =>
            {
                await CheckString(locals, "str", "foobar");
                await CheckValueType(locals, "code", "System.Threading.Tasks.TaskStatus", description: "Created");
                await CheckDateTime(locals, "dt0", new DateTime(3412, 4, 6, 8, 0, 2));
            });
        }

161
    [ConditionalFact(nameof(RunningOnChrome))]
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
    public async Task Lambda_InvalidLocation()
    {
        var debugger_test_loc = "dotnet://debugger-test.dll/debugger-async-test.cs";

        await SetBreakpoint(debugger_test_loc, 77, 12);
        var pause_location = await EvaluateAndCheck(
        "window.setTimeout(function() { invoke_static_method('[debugger-test] DebuggerTests.AsyncTests.ContinueWithTests:RunAsync'); })",
        debugger_test_loc, 77, 12, "MoveNext",
        locals_fn: async (locals) =>
        {
            await CheckString(locals, "str", "foobar");
            await CheckValueType(locals, "code", "System.Threading.Tasks.TaskStatus", description: "Created");
            await CheckValueType(locals, "dt0", "System.DateTime", description: "1/1/0001 12:00:00 AM");
        });
        var top_frame = pause_location["callFrames"][0]["functionLocation"];
        await SetNextIPAndCheck(top_frame["scriptId"].Value<string>(), "dotnet://debugger-test.dll/debugger-async-test.cs", 92, 8, "MoveNext",
        expected_error: true);

        await StepAndCheck(StepKind.Over, "dotnet://debugger-test.dll/debugger-async-test.cs", 79, 16, "MoveNext",
        locals_fn: async (locals) =>
            {
                await CheckString(locals, "str", "foobar");
                await CheckValueType(locals, "code", "System.Threading.Tasks.TaskStatus", description: "RanToCompletion");
                await CheckValueType(locals, "dt0", "System.DateTime", description: "1/1/0001 12:00:00 AM");
            },
        times: 2);
    }

190
    [ConditionalFact(nameof(RunningOnChrome))]
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
    public async Task Lambda_ToNestedLambda()
    {
        var debugger_test_loc = "dotnet://debugger-test.dll/debugger-async-test.cs";

        await SetBreakpoint(debugger_test_loc, 77, 12);
        var pause_location = await EvaluateAndCheck(
        "window.setTimeout(function() { invoke_static_method('[debugger-test] DebuggerTests.AsyncTests.ContinueWithTests:RunAsync'); })",
        debugger_test_loc, 77, 12, "MoveNext",
        locals_fn: async (locals) =>
        {
            await CheckString(locals, "str", "foobar");
            await CheckValueType(locals, "code", "System.Threading.Tasks.TaskStatus", description: "Created");
            await CheckValueType(locals, "dt0", "System.DateTime", description: "1/1/0001 12:00:00 AM");
        });
        var top_frame = pause_location["callFrames"][0]["functionLocation"];

        await SetNextIPAndCheck(top_frame["scriptId"].Value<string>(), "dotnet://debugger-test.dll/debugger-async-test.cs", 88, 20, "MoveNext",
        expected_error: true);
        
        await StepAndCheck(StepKind.Over, "dotnet://debugger-test.dll/debugger-async-test.cs", 79, 16, "MoveNext",
        locals_fn: async (locals) =>
            {
                await CheckString(locals, "str", "foobar");
                await CheckValueType(locals, "code", "System.Threading.Tasks.TaskStatus", description: "RanToCompletion");
                await CheckValueType(locals, "dt0", "System.DateTime", description: "1/1/0001 12:00:00 AM");
            },
        times: 2);
        }

220
    [ConditionalFact(nameof(RunningOnChrome))]
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
    public async Task Lambda_ToNestedSingleLineLambda_Invalid()
    {
        var debugger_test_loc = "dotnet://debugger-test.dll/debugger-async-test.cs";

        await SetBreakpoint(debugger_test_loc, 77, 12);
        var pause_location = await EvaluateAndCheck(
        "window.setTimeout(function() { invoke_static_method('[debugger-test] DebuggerTests.AsyncTests.ContinueWithTests:RunAsync'); })",
        debugger_test_loc, 77, 12, "MoveNext",
        locals_fn: async (locals) =>
        {
            await CheckString(locals, "str", "foobar");
            await CheckValueType(locals, "code", "System.Threading.Tasks.TaskStatus", description: "Created");
            await CheckValueType(locals, "dt0", "System.DateTime", description: "1/1/0001 12:00:00 AM");
        });
        var top_frame = pause_location["callFrames"][0]["functionLocation"];

        await SetNextIPAndCheck(top_frame["scriptId"].Value<string>(), "dotnet://debugger-test.dll/debugger-async-test.cs", 91, 58, "MoveNext",
        expected_error: true);
        
        await StepAndCheck(StepKind.Over, "dotnet://debugger-test.dll/debugger-async-test.cs", 79, 16, "MoveNext",
        locals_fn: async (locals) =>
            {
                await CheckString(locals, "str", "foobar");
                await CheckValueType(locals, "code", "System.Threading.Tasks.TaskStatus", description: "RanToCompletion");
                await CheckValueType(locals, "dt0", "System.DateTime", description: "1/1/0001 12:00:00 AM");
            },
        times: 2);
    }

250
    [ConditionalFact(nameof(RunningOnChrome))]
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
    public async Task Lambda_ToNestedSingleLineLambda_Valid()
    {
        var debugger_test_loc = "dotnet://debugger-test.dll/debugger-async-test.cs";

        await SetBreakpoint(debugger_test_loc, 77, 12);
        var pause_location = await EvaluateAndCheck(
        "window.setTimeout(function() { invoke_static_method('[debugger-test] DebuggerTests.AsyncTests.ContinueWithTests:RunAsync'); })",
        debugger_test_loc, 77, 12, "MoveNext",
        locals_fn: async (locals) =>
        {
            await CheckString(locals, "str", "foobar");
            await CheckValueType(locals, "code", "System.Threading.Tasks.TaskStatus", description: "Created");
            await CheckValueType(locals, "dt0", "System.DateTime", description: "1/1/0001 12:00:00 AM");
        });
        var top_frame = pause_location["callFrames"][0]["functionLocation"];

        await SetNextIPAndCheck(top_frame["scriptId"].Value<string>(), "dotnet://debugger-test.dll/debugger-async-test.cs", 91, 16, "MoveNext",
        locals_fn: async (locals) =>
            {
                await CheckString(locals, "str", "foobar");
                await CheckValueType(locals, "code", "System.Threading.Tasks.TaskStatus", description: "Created");
                await CheckValueType(locals, "dt0", "System.DateTime", description: "1/1/0001 12:00:00 AM");
            });
        
        await StepAndCheck(StepKind.Over, "dotnet://debugger-test.dll/debugger-async-test.cs", 92, 12, "MoveNext",
        locals_fn: async (locals) =>
            {
                await CheckString(locals, "str", "foobar");
                await CheckValueType(locals, "code", "System.Threading.Tasks.TaskStatus", description: "Created");
                await CheckValueType(locals, "dt0", "System.DateTime", description: "1/1/0001 12:00:00 AM");
            });
    }
283
}