11import * as assert from 'assert' ;
22import * as fs from 'fs' ;
33import * as path from 'path' ;
4+ import { stringify } from 'querystring' ;
45import * as sinon from 'sinon' ;
56import { DebugClient } from 'vscode-debugadapter-testsupport' ;
67import { DebugProtocol } from 'vscode-debugprotocol' ;
@@ -296,6 +297,35 @@ suite('Go Debug Adapter', function () {
296297
297298 teardown ( ( ) => dc . stop ( ) ) ;
298299
300+ /**
301+ * Helper function to assert that a variable has a particular value.
302+ * This should be called when the program is stopped.
303+ *
304+ * The following requests are issued by this function to determine the
305+ * value of the variable:
306+ * 1. threadsRequest
307+ * 2. stackTraceRequest
308+ * 3. scopesRequest
309+ * 4. variablesRequest
310+ */
311+ async function assertVariableValue ( name : string , val : string ) : Promise < void > {
312+ const threadsResponse = await dc . threadsRequest ( ) ;
313+ assert ( threadsResponse . success ) ;
314+ const stackTraceResponse = await dc . stackTraceRequest ( { threadId : threadsResponse . body . threads [ 0 ] . id } ) ;
315+ assert ( stackTraceResponse . success ) ;
316+ const scopesResponse = await dc . scopesRequest ( { frameId : stackTraceResponse . body . stackFrames [ 0 ] . id } ) ;
317+ assert ( scopesResponse . success ) ;
318+ const variablesResponse = await dc . variablesRequest ( {
319+ variablesReference : scopesResponse . body . scopes [ 0 ] . variablesReference
320+ } ) ;
321+ assert ( variablesResponse . success ) ;
322+ // Locate the variable with the matching name.
323+ const i = variablesResponse . body . variables . findIndex ( ( v ) => v . name === name ) ;
324+ assert ( i >= 0 ) ;
325+ // Check that the value of name is val.
326+ assert . strictEqual ( variablesResponse . body . variables [ i ] . value , val ) ;
327+ }
328+
299329 suite ( 'basic' , ( ) => {
300330
301331 test ( 'unknown request should produce error' , ( done ) => {
@@ -526,17 +556,7 @@ suite('Go Debug Adapter', function () {
526556
527557 ] ) . then ( ( ) =>
528558 // The program is stopped at the breakpoint, check to make sure 'i == 1'.
529- dc . threadsRequest ( ) . then ( ( threadsResponse ) =>
530- dc . stackTraceRequest ( { threadId : threadsResponse . body . threads [ 0 ] . id } ) . then ( ( stackTraceResponse ) =>
531- dc . scopesRequest ( { frameId : stackTraceResponse . body . stackFrames [ 0 ] . id } ) . then ( ( scopesResponse ) =>
532- dc . variablesRequest ( { variablesReference : scopesResponse . body . scopes [ 0 ] . variablesReference } )
533- . then ( ( variablesResponse ) => {
534- assert . strictEqual ( variablesResponse . body . variables [ 0 ] . name , 'i' ) ;
535- assert . strictEqual ( variablesResponse . body . variables [ 0 ] . value , '2' ) ;
536- } )
537- )
538- )
539- )
559+ assertVariableValue ( 'i' , '2' )
540560 ) ;
541561 } ) ;
542562
@@ -558,17 +578,7 @@ suite('Go Debug Adapter', function () {
558578
559579 return dc . hitBreakpoint ( debugConfig , location ) . then ( ( ) =>
560580 // The program is stopped at the breakpoint, check to make sure 'i == 0'.
561- dc . threadsRequest ( ) . then ( ( threadsResponse ) =>
562- dc . stackTraceRequest ( { threadId : threadsResponse . body . threads [ 0 ] . id } ) . then ( ( stackTraceResponse ) =>
563- dc . scopesRequest ( { frameId : stackTraceResponse . body . stackFrames [ 0 ] . id } ) . then ( ( scopesResponse ) =>
564- dc . variablesRequest ( { variablesReference : scopesResponse . body . scopes [ 0 ] . variablesReference } )
565- . then ( ( variablesResponse ) => {
566- assert . strictEqual ( variablesResponse . body . variables [ 0 ] . name , 'i' ) ;
567- assert . strictEqual ( variablesResponse . body . variables [ 0 ] . value , '0' ) ;
568- } )
569- )
570- )
571- )
581+ assertVariableValue ( 'i' , '0' )
572582 ) . then ( ( ) =>
573583 // Add a condition to the breakpoint, and make sure it runs until 'i == 2'.
574584 dc . setBreakpointsRequest ( {
@@ -581,17 +591,7 @@ suite('Go Debug Adapter', function () {
581591 dc . assertStoppedLocation ( 'breakpoint' , location )
582592 ] ) . then ( ( ) =>
583593 // The program is stopped at the breakpoint, check to make sure 'i == 2'.
584- dc . threadsRequest ( ) . then ( ( threadsResponse ) =>
585- dc . stackTraceRequest ( { threadId : threadsResponse . body . threads [ 0 ] . id } ) . then ( ( stackTraceResponse ) =>
586- dc . scopesRequest ( { frameId : stackTraceResponse . body . stackFrames [ 0 ] . id } ) . then ( ( scopesResponse ) =>
587- dc . variablesRequest ( { variablesReference : scopesResponse . body . scopes [ 0 ] . variablesReference } )
588- . then ( ( variablesResponse ) => {
589- assert . strictEqual ( variablesResponse . body . variables [ 0 ] . name , 'i' ) ;
590- assert . strictEqual ( variablesResponse . body . variables [ 0 ] . value , '2' ) ;
591- } )
592- )
593- )
594- )
594+ assertVariableValue ( 'i' , '2' )
595595 )
596596 )
597597 ) ;
@@ -630,17 +630,7 @@ suite('Go Debug Adapter', function () {
630630
631631 ] ) . then ( ( ) =>
632632 // The program is stopped at the breakpoint, check to make sure 'i == 2'.
633- dc . threadsRequest ( ) . then ( ( threadsResponse ) =>
634- dc . stackTraceRequest ( { threadId : threadsResponse . body . threads [ 0 ] . id } ) . then ( ( stackTraceResponse ) =>
635- dc . scopesRequest ( { frameId : stackTraceResponse . body . stackFrames [ 0 ] . id } ) . then ( ( scopesResponse ) =>
636- dc . variablesRequest ( { variablesReference : scopesResponse . body . scopes [ 0 ] . variablesReference } )
637- . then ( ( variablesResponse ) => {
638- assert . strictEqual ( variablesResponse . body . variables [ 0 ] . name , 'i' ) ;
639- assert . strictEqual ( variablesResponse . body . variables [ 0 ] . value , '2' ) ;
640- } )
641- )
642- )
643- )
633+ assertVariableValue ( 'i' , '2' )
644634 ) . then ( ( ) =>
645635 // Remove the breakpoint condition, and make sure the program runs until 'i == 3'.
646636 dc . setBreakpointsRequest ( {
@@ -653,17 +643,7 @@ suite('Go Debug Adapter', function () {
653643 dc . assertStoppedLocation ( 'breakpoint' , location )
654644 ] ) . then ( ( ) =>
655645 // The program is stopped at the breakpoint, check to make sure 'i == 3'.
656- dc . threadsRequest ( ) . then ( ( threadsResponse ) =>
657- dc . stackTraceRequest ( { threadId : threadsResponse . body . threads [ 0 ] . id } ) . then ( ( stackTraceResponse ) =>
658- dc . scopesRequest ( { frameId : stackTraceResponse . body . stackFrames [ 0 ] . id } ) . then ( ( scopesResponse ) =>
659- dc . variablesRequest ( { variablesReference : scopesResponse . body . scopes [ 0 ] . variablesReference } )
660- . then ( ( variablesResponse ) => {
661- assert . strictEqual ( variablesResponse . body . variables [ 0 ] . name , 'i' ) ;
662- assert . strictEqual ( variablesResponse . body . variables [ 0 ] . value , '3' ) ;
663- } )
664- )
665- )
666- )
646+ assertVariableValue ( 'i' , '3' )
667647 )
668648 )
669649 ) ;
0 commit comments