GoPLS Viewer

Home|gopls/cmd/cover/testdata/test.go
1// Copyright 2013 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// This program is processed by the cover command, and then testAll is called.
6// The test driver in main.go can then compare the coverage statistics with expectation.
7
8// The word LINE is replaced by the line number in this file. When the file is executed,
9// the coverage processing has changed the line numbers, so we can't use runtime.Caller.
10
11package main
12
13const anything = 1e9 // Just some unlikely value that means "we got here, don't care how often"
14
15func testAll() {
16    testSimple()
17    testBlockRun()
18    testIf()
19    testFor()
20    testRange()
21    testSwitch()
22    testTypeSwitch()
23    testSelect1()
24    testSelect2()
25    testPanic()
26    testEmptySwitches()
27}
28
29// The indexes of the counters in testPanic are known to main.go
30const panicIndex = 3
31
32// This test appears first because the index of its counters is known to main.go
33func testPanic() {
34    defer func() {
35        recover()
36    }()
37    check(LINE1)
38    panic("should not get next line")
39    check(LINE0// this is GoCover.Count[panicIndex]
40    // The next counter is in testSimple and it will be non-zero.
41    // If the panic above does not trigger a counter, the test will fail
42    // because GoCover.Count[panicIndex] will be the one in testSimple.
43}
44
45func testSimple() {
46    check(LINE1)
47}
48
49func testIf() {
50    if true {
51        check(LINE1)
52    } else {
53        check(LINE0)
54    }
55    if false {
56        check(LINE0)
57    } else {
58        check(LINE1)
59    }
60    for i := 0i < 3i++ {
61        if checkVal(LINE3i) <= 2 {
62            check(LINE3)
63        }
64        if checkVal(LINE3i) <= 1 {
65            check(LINE2)
66        }
67        if checkVal(LINE3i) <= 0 {
68            check(LINE1)
69        }
70    }
71    for i := 0i < 3i++ {
72        if checkVal(LINE3i) <= 1 {
73            check(LINE2)
74        } else {
75            check(LINE1)
76        }
77    }
78    for i := 0i < 3i++ {
79        if checkVal(LINE3i) <= 0 {
80            check(LINE1)
81        } else if checkVal(LINE2i) <= 1 {
82            check(LINE1)
83        } else if checkVal(LINE1i) <= 2 {
84            check(LINE1)
85        } else if checkVal(LINE0i) <= 3 {
86            check(LINE0)
87        }
88    }
89    if func(ab intbool { return a < b }(34) {
90        check(LINE1)
91    }
92}
93
94func testFor() {
95    for i := 0i < 10; func() { i++; check(LINE10) }() {
96        check(LINE10)
97    }
98}
99
100func testRange() {
101    for _f := range []func(){
102        func() { check(LINE1) },
103    } {
104        f()
105        check(LINE1)
106    }
107}
108
109func testBlockRun() {
110    check(LINE1)
111    {
112        check(LINE1)
113    }
114    {
115        check(LINE1)
116    }
117    check(LINE1)
118    {
119        check(LINE1)
120    }
121    {
122        check(LINE1)
123    }
124    check(LINE1)
125}
126
127func testSwitch() {
128    for i := 0i < 5; func() { i++; check(LINE5) }() {
129        switch i {
130        case 0:
131            check(LINE1)
132        case 1:
133            check(LINE1)
134        case 2:
135            check(LINE1)
136        default:
137            check(LINE2)
138        }
139    }
140}
141
142func testTypeSwitch() {
143    var x = []interface{}{12.0"hi"}
144    for _v := range x {
145        switch func() { check(LINE3) }(); v.(type) {
146        case int:
147            check(LINE1)
148        case float64:
149            check(LINE1)
150        case string:
151            check(LINE1)
152        case complex128:
153            check(LINE0)
154        default:
155            check(LINE0)
156        }
157    }
158}
159
160func testSelect1() {
161    c := make(chan int)
162    go func() {
163        for i := 0i < 1000i++ {
164            c <- i
165        }
166    }()
167    for {
168        select {
169        case <-c:
170            check(LINEanything)
171        case <-c:
172            check(LINEanything)
173        default:
174            check(LINE1)
175            return
176        }
177    }
178}
179
180func testSelect2() {
181    c1 := make(chan int1000)
182    c2 := make(chan int1000)
183    for i := 0i < 1000i++ {
184        c1 <- i
185        c2 <- i
186    }
187    for {
188        select {
189        case <-c1:
190            check(LINE1000)
191        case <-c2:
192            check(LINE1000)
193        default:
194            check(LINE1)
195            return
196        }
197    }
198}
199
200// Empty control statements created syntax errors. This function
201// is here just to be sure that those are handled correctly now.
202func testEmptySwitches() {
203    check(LINE1)
204    switch 3 {
205    }
206    check(LINE1)
207    switch i := (interface{})(3).(int); i {
208    }
209    check(LINE1)
210    c := make(chan int)
211    go func() {
212        check(LINE1)
213        c <- 1
214        select {}
215    }()
216    <-c
217    check(LINE1)
218}
219
MembersX
testSelect2
anything
testIf.i
testFor.i
testSelect1.c
testSelect1.BlockStmt.i
testSelect2.c2
testEmptySwitches
testEmptySwitches.c
testPanic
testSimple
testRange
testTypeSwitch.RangeStmt_2785.v
testIf
testBlockRun
testTypeSwitch
testSwitch
testSwitch.i
testSelect1
testSelect2.c1
testAll
panicIndex
testFor
testRange.RangeStmt_2256.f
testSelect2.i
Members
X