Skip to content

Commit 162378f

Browse files
authored
refactor: testing formatting using Go (#652)
1 parent 3468719 commit 162378f

4 files changed

Lines changed: 288 additions & 86 deletions

File tree

.github/workflows/go.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,6 @@ jobs:
2323

2424
- name: Test
2525
run: go test -v ./...
26+
27+
- name: Test Race
28+
run: go test -race -v ./...

SCC-OUTPUT-REPORT.html

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
<tbody><tr>
1414
<th>Go</th>
1515
<th>27</th>
16-
<th>23152</th>
17-
<th>1447</th>
16+
<th>23414</th>
17+
<th>1456</th>
1818
<th>439</th>
19-
<th>21266</th>
20-
<th>1383</th>
21-
<th>449516</th>
22-
<th>6306</th>
19+
<th>21519</th>
20+
<th>1423</th>
21+
<th>456460</th>
22+
<th>6380</th>
2323
</tr><tr>
2424
<td>processor/constants.go</td>
2525
<td></td>
@@ -30,6 +30,16 @@
3030
<td>0</td>
3131
<td>211234</td>
3232
<td>2066</td>
33+
</tr><tr>
34+
<td>processor/formatters_test.go</td>
35+
<td></td>
36+
<td>1797</td>
37+
<td>162</td>
38+
<td>4</td>
39+
<td>1631</td>
40+
<td>169</td>
41+
<td>42734</td>
42+
<td>486</td>
3343
</tr><tr>
3444
<td>processor/workers_test.go</td>
3545
<td></td>
@@ -40,16 +50,6 @@
4050
<td>287</td>
4151
<td>32293</td>
4252
<td>525</td>
43-
</tr><tr>
44-
<td>processor/formatters_test.go</td>
45-
<td></td>
46-
<td>1535</td>
47-
<td>153</td>
48-
<td>4</td>
49-
<td>1378</td>
50-
<td>129</td>
51-
<td>35790</td>
52-
<td>406</td>
5353
</tr><tr>
5454
<td>processor/formatters.go</td>
5555
<td></td>
@@ -294,15 +294,15 @@
294294
<tfoot><tr>
295295
<th>Total</th>
296296
<th>27</th>
297-
<th>23152</th>
298-
<th>1447</th>
297+
<th>23414</th>
298+
<th>1456</th>
299299
<th>439</th>
300-
<th>21266</th>
301-
<th>1383</th>
302-
<th>449516</th>
303-
<th>6306</th>
300+
<th>21519</th>
301+
<th>1423</th>
302+
<th>456460</th>
303+
<th>6380</th>
304304
</tr>
305305
<tr>
306-
<th colspan="9">Estimated Cost to Develop (organic) $669,368<br>Estimated Schedule Effort (organic) 11.81 months<br>Estimated People Required (organic) 5.04<br></th>
306+
<th colspan="9">Estimated Cost to Develop (organic) $677,732<br>Estimated Schedule Effort (organic) 11.86 months<br>Estimated People Required (organic) 5.08<br></th>
307307
</tr></tfoot>
308308
</table></body></html>

processor/formatters_test.go

Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
package processor
44

55
import (
6+
"io"
7+
"os"
68
"slices"
79
"strings"
810
"testing"
@@ -1372,6 +1374,34 @@ func TestToHTML(t *testing.T) {
13721374
if !strings.Contains(res, `<html lang="en">`) {
13731375
t.Error("Expected to have HTML wrapper")
13741376
}
1377+
1378+
if !strings.Contains(res, "<th>Language</th>") {
1379+
t.Error("html Language check failed")
1380+
}
1381+
if !strings.Contains(res, "<th>Files</th>") {
1382+
t.Error("html Files check failed")
1383+
}
1384+
if !strings.Contains(res, "<th>Lines</th>") {
1385+
t.Error("html Lines check failed")
1386+
}
1387+
if !strings.Contains(res, "<th>Blank</th>") {
1388+
t.Error("html Blank check failed")
1389+
}
1390+
if !strings.Contains(res, "<th>Comment</th>") {
1391+
t.Error("html Comment check failed")
1392+
}
1393+
if !strings.Contains(res, "<th>Code</th>") {
1394+
t.Error("html Code check failed")
1395+
}
1396+
if !strings.Contains(res, "<th>Complexity</th>") {
1397+
t.Error("html Complexity check failed")
1398+
}
1399+
if !strings.Contains(res, "<th>Bytes</th>") {
1400+
t.Error("html Bytes check failed")
1401+
}
1402+
if !strings.Contains(res, "<th>Uloc</th>") {
1403+
t.Error("html Uloc check failed")
1404+
}
13751405
}
13761406

13771407
func TestToHTMLTable(t *testing.T) {
@@ -1533,3 +1563,235 @@ func TestGetCSVFilesSortFunc(t *testing.T) {
15331563
}
15341564
}
15351565
}
1566+
1567+
func TestToCSVFilesHeader(t *testing.T) {
1568+
inputChan := make(chan *FileJob, 1000)
1569+
inputChan <- &FileJob{
1570+
Language: "Go",
1571+
Filename: "bbbb.go",
1572+
Extension: "go",
1573+
Location: "./",
1574+
Bytes: 1000,
1575+
Lines: 1000,
1576+
Code: 1000,
1577+
Comment: 1000,
1578+
Blank: 1000,
1579+
Complexity: 1000,
1580+
WeightedComplexity: 1000,
1581+
Binary: false,
1582+
}
1583+
inputChan <- &FileJob{
1584+
Language: "Go",
1585+
Filename: "aaaa.go",
1586+
Extension: "go",
1587+
Location: "./",
1588+
Bytes: 1000,
1589+
Lines: 1000,
1590+
Code: 1000,
1591+
Comment: 1000,
1592+
Blank: 1000,
1593+
Complexity: 1000,
1594+
WeightedComplexity: 1000,
1595+
Binary: false,
1596+
}
1597+
close(inputChan)
1598+
res := toCSVFiles(inputChan)
1599+
header, _, _ := strings.Cut(res, "\n")
1600+
const expected = "Language,Provider,Filename,Lines,Code,Comments,Blanks,Complexity,Bytes,ULOC"
1601+
if header != expected {
1602+
t.Errorf("check toCSVFiles header failed, expected: %v, got: %v", expected, header)
1603+
}
1604+
}
1605+
1606+
func TestToCSVStreamHeader(t *testing.T) {
1607+
inputChan := make(chan *FileJob, 1000)
1608+
inputChan <- &FileJob{
1609+
Language: "Go",
1610+
Filename: "bbbb.go",
1611+
Extension: "go",
1612+
Location: "./",
1613+
Bytes: 1000,
1614+
Lines: 1000,
1615+
Code: 1000,
1616+
Comment: 1000,
1617+
Blank: 1000,
1618+
Complexity: 1000,
1619+
WeightedComplexity: 1000,
1620+
Binary: false,
1621+
}
1622+
inputChan <- &FileJob{
1623+
Language: "Go",
1624+
Filename: "aaaa.go",
1625+
Extension: "go",
1626+
Location: "./",
1627+
Bytes: 1000,
1628+
Lines: 1000,
1629+
Code: 1000,
1630+
Comment: 1000,
1631+
Blank: 1000,
1632+
Complexity: 1000,
1633+
WeightedComplexity: 1000,
1634+
Binary: false,
1635+
}
1636+
close(inputChan)
1637+
1638+
originStdout := os.Stdout
1639+
t.Cleanup(func() {
1640+
os.Stdout = originStdout
1641+
})
1642+
r, w, err := os.Pipe()
1643+
if err != nil {
1644+
t.Fatal(err)
1645+
}
1646+
os.Stdout = w
1647+
go func() {
1648+
toCSVStream(inputChan)
1649+
_ = w.Close()
1650+
}()
1651+
output, err := io.ReadAll(r)
1652+
if err != nil {
1653+
t.Fatal(err)
1654+
}
1655+
1656+
header, _, _ := strings.Cut(string(output), "\n")
1657+
const expected = "Language,Provider,Filename,Lines,Code,Comments,Blanks,Complexity,Bytes,Uloc"
1658+
if header != expected {
1659+
t.Errorf("check toCSVStream header failed, expected: %v, got: %v", expected, header)
1660+
}
1661+
}
1662+
1663+
func TestToJSONKeys(t *testing.T) {
1664+
inputChan := make(chan *FileJob, 1000)
1665+
inputChan <- &FileJob{
1666+
Language: "Go",
1667+
Filename: "bbbb.go",
1668+
Extension: "go",
1669+
Location: "./",
1670+
Bytes: 1000,
1671+
Lines: 1000,
1672+
Code: 1000,
1673+
Comment: 1000,
1674+
Blank: 1000,
1675+
Complexity: 1000,
1676+
WeightedComplexity: 1000,
1677+
Binary: false,
1678+
}
1679+
inputChan <- &FileJob{
1680+
Language: "Go",
1681+
Filename: "aaaa.go",
1682+
Extension: "go",
1683+
Location: "./",
1684+
Bytes: 1000,
1685+
Lines: 1000,
1686+
Code: 1000,
1687+
Comment: 1000,
1688+
Blank: 1000,
1689+
Complexity: 1000,
1690+
WeightedComplexity: 1000,
1691+
Binary: false,
1692+
}
1693+
close(inputChan)
1694+
1695+
res := toJSON(inputChan)
1696+
if !strings.Contains(res, `"Name":`) {
1697+
t.Error("JSON Name check failed")
1698+
}
1699+
if !strings.Contains(res, `"Files":`) {
1700+
t.Error("JSON Files check failed")
1701+
}
1702+
if !strings.Contains(res, `"Lines":`) {
1703+
t.Error("JSON Lines check failed")
1704+
}
1705+
if !strings.Contains(res, `"Blank":`) {
1706+
t.Error("JSON Blank check failed")
1707+
}
1708+
if !strings.Contains(res, `"Comment":`) {
1709+
t.Error("JSON Comment check failed")
1710+
}
1711+
if !strings.Contains(res, `"Code":`) {
1712+
t.Error("JSON Code check failed")
1713+
}
1714+
if !strings.Contains(res, `"Complexity":`) {
1715+
t.Error("JSON Complexity check failed")
1716+
}
1717+
if !strings.Contains(res, `"Bytes":`) {
1718+
t.Error("JSON Bytes check failed")
1719+
}
1720+
if !strings.Contains(res, `"ULOC":`) {
1721+
t.Error("JSON Uloc check failed")
1722+
}
1723+
}
1724+
1725+
func TestToJSON2Keys(t *testing.T) {
1726+
inputChan := make(chan *FileJob, 1000)
1727+
inputChan <- &FileJob{
1728+
Language: "Go",
1729+
Filename: "bbbb.go",
1730+
Extension: "go",
1731+
Location: "./",
1732+
Bytes: 1000,
1733+
Lines: 1000,
1734+
Code: 1000,
1735+
Comment: 1000,
1736+
Blank: 1000,
1737+
Complexity: 1000,
1738+
WeightedComplexity: 1000,
1739+
Binary: false,
1740+
}
1741+
inputChan <- &FileJob{
1742+
Language: "Go",
1743+
Filename: "aaaa.go",
1744+
Extension: "go",
1745+
Location: "./",
1746+
Bytes: 1000,
1747+
Lines: 1000,
1748+
Code: 1000,
1749+
Comment: 1000,
1750+
Blank: 1000,
1751+
Complexity: 1000,
1752+
WeightedComplexity: 1000,
1753+
Binary: false,
1754+
}
1755+
close(inputChan)
1756+
1757+
res := toJSON2(inputChan)
1758+
if !strings.Contains(res, `"Name":`) {
1759+
t.Error("JSON2 Name check failed")
1760+
}
1761+
if !strings.Contains(res, `"Files":`) {
1762+
t.Error("JSON2 Files check failed")
1763+
}
1764+
if !strings.Contains(res, `"Lines":`) {
1765+
t.Error("JSON2 Lines check failed")
1766+
}
1767+
if !strings.Contains(res, `"Blank":`) {
1768+
t.Error("JSON2 Blank check failed")
1769+
}
1770+
if !strings.Contains(res, `"Comment":`) {
1771+
t.Error("JSON2 Comment check failed")
1772+
}
1773+
if !strings.Contains(res, `"Code":`) {
1774+
t.Error("JSON2 Code check failed")
1775+
}
1776+
if !strings.Contains(res, `"Complexity":`) {
1777+
t.Error("JSON2 Complexity check failed")
1778+
}
1779+
if !strings.Contains(res, `"Bytes":`) {
1780+
t.Error("JSON2 Bytes check failed")
1781+
}
1782+
if !strings.Contains(res, `"ULOC":`) {
1783+
t.Error("JSON2 Uloc check failed")
1784+
}
1785+
if !strings.Contains(res, `"languageSummary":`) {
1786+
t.Error("JSON2 languageSummary check failed")
1787+
}
1788+
if !strings.Contains(res, `"estimatedCost":`) {
1789+
t.Error("JSON2 estimatedCost check failed")
1790+
}
1791+
if !strings.Contains(res, `"estimatedScheduleMonths":`) {
1792+
t.Error("JSON2 estimatedScheduleMonths check failed")
1793+
}
1794+
if !strings.Contains(res, `"estimatedPeople":`) {
1795+
t.Error("JSON2 estimatedPeople check failed")
1796+
}
1797+
}

0 commit comments

Comments
 (0)