golang.org/x/tools@v0.21.0/internal/stack/stack_test.go (about)

     1  // Copyright 2020 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package stack_test
     6  
     7  import (
     8  	"bytes"
     9  	"strings"
    10  	"testing"
    11  
    12  	"golang.org/x/tools/internal/stack"
    13  )
    14  
    15  func TestProcess(t *testing.T) {
    16  	for _, test := range []struct{ name, input, expect string }{{
    17  		name:   `empty`,
    18  		input:  ``,
    19  		expect: ``,
    20  	}, {
    21  		name:  `no_frame`,
    22  		input: `goroutine 1 [running]:`,
    23  		expect: `
    24  [running]: $1
    25  
    26  1 goroutines, 1 unique
    27  `,
    28  	}, {
    29  		name: `one_frame`,
    30  		input: `
    31  goroutine 1 [running]:
    32  package.function(args)
    33  	file.go:10
    34  `,
    35  		expect: `
    36  [running]: $1
    37  file.go:10: function
    38  
    39  1 goroutines, 1 unique
    40  `,
    41  	}, {
    42  		name: `one_call`,
    43  		input: `
    44  goroutine 1 [running]:
    45  package1.functionA(args)
    46  	file1.go:10
    47  package2.functionB(args)
    48  	file2.go:20
    49  package3.functionC(args)
    50  	file3.go:30
    51  `,
    52  		expect: `
    53  [running]: $1
    54  file1.go:10: functionA
    55  file2.go:20: functionB
    56  file3.go:30: functionC
    57  
    58  1 goroutines, 1 unique
    59  `,
    60  	}, {
    61  		name: `two_call`,
    62  		input: `
    63  goroutine 1 [running]:
    64  package1.functionA(args)
    65  	file1.go:10
    66  goroutine 2 [running]:
    67  package2.functionB(args)
    68  	file2.go:20
    69  `,
    70  		expect: `
    71  [running]: $1
    72  file1.go:10: functionA
    73  
    74  [running]: $2
    75  file2.go:20: functionB
    76  
    77  2 goroutines, 2 unique
    78  `,
    79  	}, {
    80  		name: `merge_call`,
    81  		input: `
    82  goroutine 1 [running]:
    83  package1.functionA(args)
    84  	file1.go:10
    85  goroutine 2 [running]:
    86  package1.functionA(args)
    87  	file1.go:10
    88  `,
    89  		expect: `
    90  [running]: $1, $2
    91  file1.go:10: functionA
    92  
    93  2 goroutines, 1 unique
    94  `,
    95  	}, {
    96  		name: `alternating_call`,
    97  		input: `
    98  goroutine 1 [running]:
    99  package1.functionA(args)
   100  	file1.go:10
   101  goroutine 2 [running]:
   102  package2.functionB(args)
   103  	file2.go:20
   104  goroutine 3 [running]:
   105  package1.functionA(args)
   106  	file1.go:10
   107  goroutine 4 [running]:
   108  package2.functionB(args)
   109  	file2.go:20
   110  goroutine 5 [running]:
   111  package1.functionA(args)
   112  	file1.go:10
   113  goroutine 6 [running]:
   114  package2.functionB(args)
   115  	file2.go:20
   116  `,
   117  		expect: `
   118  [running]: $1, $3, $5
   119  file1.go:10: functionA
   120  
   121  [running]: $2, $4, $6
   122  file2.go:20: functionB
   123  
   124  6 goroutines, 2 unique
   125  `,
   126  	}, {
   127  		name: `sort_calls`,
   128  		input: `
   129  goroutine 1 [running]:
   130  package3.functionC(args)
   131  	file3.go:30
   132  goroutine 2 [running]:
   133  package2.functionB(args)
   134  	file2.go:20
   135  goroutine 3 [running]:
   136  package1.functionA(args)
   137  	file1.go:10
   138  `,
   139  		expect: `
   140  [running]: $3
   141  file1.go:10: functionA
   142  
   143  [running]: $2
   144  file2.go:20: functionB
   145  
   146  [running]: $1
   147  file3.go:30: functionC
   148  
   149  3 goroutines, 3 unique
   150  `,
   151  	}, {
   152  		name: `real_single`,
   153  		input: `
   154  panic: oops
   155  
   156  goroutine 53 [running]:
   157  golang.org/x/tools/internal/jsonrpc2_test.testHandler.func1(0x1240c20, 0xc000013350, 0xc0000133b0, 0x1240ca0, 0xc00002ab00, 0x3, 0x3)
   158  	/work/tools/internal/jsonrpc2/jsonrpc2_test.go:160 +0x74c
   159  golang.org/x/tools/internal/jsonrpc2.(*Conn).Run(0xc000204330, 0x1240c20, 0xc000204270, 0x1209570, 0xc000212120, 0x1242700)
   160  	/work/tools/internal/jsonrpc2/jsonrpc2.go:187 +0x777
   161  golang.org/x/tools/internal/jsonrpc2_test.run.func1(0x123ebe0, 0xc000206018, 0x123ec20, 0xc000206010, 0xc0002080a0, 0xc000204330, 0x1240c20, 0xc000204270, 0xc000212120)
   162  	/work/tools/internal/jsonrpc2/jsonrpc2_test.go:131 +0xe2
   163  created by golang.org/x/tools/internal/jsonrpc2_test.run
   164  	/work/tools/internal/jsonrpc2/jsonrpc2_test.go:121 +0x263
   165  FAIL    golang.org/x/tools/internal/jsonrpc2    0.252s
   166  FAIL
   167  `,
   168  		expect: `
   169  panic: oops
   170  
   171  [running]: $53
   172  /work/tools/internal/jsonrpc2/jsonrpc2_test.go:160: testHandler.func1
   173  /work/tools/internal/jsonrpc2/jsonrpc2.go:187:      (*Conn).Run
   174  /work/tools/internal/jsonrpc2/jsonrpc2_test.go:131: run.func1
   175  /work/tools/internal/jsonrpc2/jsonrpc2_test.go:121: run
   176  
   177  1 goroutines, 1 unique
   178  
   179  FAIL    golang.org/x/tools/internal/jsonrpc2    0.252s
   180  FAIL
   181  `,
   182  	}} {
   183  		t.Run(test.name, func(t *testing.T) {
   184  			buf := &bytes.Buffer{}
   185  			stack.Process(buf, strings.NewReader(test.input))
   186  			expect := strings.TrimSpace(test.expect)
   187  			got := strings.TrimSpace(buf.String())
   188  			if got != expect {
   189  				t.Errorf("got:\n%s\nexpect:\n%s", got, expect)
   190  			}
   191  		})
   192  	}
   193  }