|
3 | 3 | package processor |
4 | 4 |
|
5 | 5 | import ( |
| 6 | + "io" |
| 7 | + "os" |
6 | 8 | "slices" |
7 | 9 | "strings" |
8 | 10 | "testing" |
@@ -1372,6 +1374,34 @@ func TestToHTML(t *testing.T) { |
1372 | 1374 | if !strings.Contains(res, `<html lang="en">`) { |
1373 | 1375 | t.Error("Expected to have HTML wrapper") |
1374 | 1376 | } |
| 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 | + } |
1375 | 1405 | } |
1376 | 1406 |
|
1377 | 1407 | func TestToHTMLTable(t *testing.T) { |
@@ -1533,3 +1563,235 @@ func TestGetCSVFilesSortFunc(t *testing.T) { |
1533 | 1563 | } |
1534 | 1564 | } |
1535 | 1565 | } |
| 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