55 "strings"
66
77 uv "github.com/charmbracelet/ultraviolet"
8- "github.com/charmbracelet/x/ansi"
98 "github.com/codecrafters-io/grep-tester/virtual_terminal"
109 "github.com/dustin/go-humanize/english"
1110)
@@ -18,30 +17,29 @@ import (
1817// This one is involved only with error detection and propagation
1918// Highlighting assertion is responsible for using this and building proper error
2019type screenStateComparator struct {
21- successLogs []string
22- highlightingIsTurnedOn bool
20+ partialSuccessLogs []string
2321}
2422
2523// ComparisonError represents an error found during screen state comparison
2624type ComparisonError struct {
27- RowIdx int
28- ColumnIdx int
29- ExpectedCell * uv. Cell
30- ActualCell * uv. Cell
31- ErrorString error
32- SuccessLogs []string
25+ RowIdx int
26+ ColumnIdx int
27+ ExpectedANSICode string
28+ ACtualANSICode string
29+ ErrorString error
30+ PartialSuccessLogs []string
3331}
3432
3533func newScreenStateComparator () * screenStateComparator {
3634 return & screenStateComparator {}
3735}
3836
39- func (c * screenStateComparator ) resetSuccessLogs () {
40- c .successLogs = []string {}
37+ func (c * screenStateComparator ) resetPartialSuccessLogs () {
38+ c .partialSuccessLogs = []string {}
4139}
4240
43- func (c * screenStateComparator ) addSuccessLog (successLog string ) {
44- c .successLogs = append (c .successLogs , successLog )
41+ func (c * screenStateComparator ) addPartialSuccessLog (successLog string ) {
42+ c .partialSuccessLogs = append (c .partialSuccessLogs , successLog )
4543}
4644
4745func (c * screenStateComparator ) CompareHighlighting (expected , actual * virtual_terminal.ScreenState ) * ComparisonError {
@@ -51,25 +49,25 @@ func (c *screenStateComparator) CompareHighlighting(expected, actual *virtual_te
5149 for rowIdx := range cursorPosition .RowIndex + 1 {
5250
5351 // Compare upto the column before in which the cursor is present
54- columnsCount := expected .GetColumnsCount ()
52+ maxColumnsCount := expected .GetColumnsCount ()
5553 if cursorPosition .RowIndex == rowIdx {
56- columnsCount = cursorPosition .ColumnIndex
54+ maxColumnsCount = cursorPosition .ColumnIndex
5755 }
5856
5957 // Compare cells
60- for columnIdx := range columnsCount {
58+ for columnIdx := range maxColumnsCount {
6159
6260 expectedCell := expected .MustGetCellAtPosition (rowIdx , columnIdx )
6361 actualCell := actual .MustGetCellAtPosition (rowIdx , columnIdx )
6462
6563 if err := c .compareCells (expectedCell , actualCell ); err != nil {
6664 return & ComparisonError {
67- RowIdx : rowIdx ,
68- ColumnIdx : columnIdx ,
69- ExpectedCell : expectedCell ,
70- ActualCell : actualCell ,
71- ErrorString : err ,
72- SuccessLogs : c . successLogs ,
65+ RowIdx : rowIdx ,
66+ ColumnIdx : columnIdx ,
67+ ErrorString : err ,
68+ PartialSuccessLogs : c . partialSuccessLogs ,
69+ ExpectedANSICode : expectedCell . Style . String () ,
70+ ACtualANSICode : actualCell . Style . String () ,
7371 }
7472 }
7573 }
@@ -79,13 +77,8 @@ func (c *screenStateComparator) CompareHighlighting(expected, actual *virtual_te
7977}
8078
8179func (c * screenStateComparator ) compareCells (expected , actual * uv.Cell ) error {
82- // Reset for each comparison
83- c .resetSuccessLogs ()
84-
85- // If a single cell is found which should be highlighted, which means highlighting is turned on for this run
86- if expected .Style .Fg == ansi .Red && expected .Style .Attrs == uv .AttrBold {
87- c .highlightingIsTurnedOn = true
88- }
80+ // Reset for each cell
81+ c .resetPartialSuccessLogs ()
8982
9083 var firstError error
9184
@@ -112,7 +105,7 @@ func (c *screenStateComparator) checkFgColor(expectedCell, actualCell *uv.Cell)
112105 return fmt .Errorf ("Expected %s, got %s" , getFgColorName (expectedCell .Style .Fg ), getFgColorName (actualCell .Style .Fg ))
113106 }
114107
115- c .addSuccessLog (fmt .Sprintf ("✓ Color is %s" , getFgColorName (expectedCell .Style .Fg )))
108+ c .addPartialSuccessLog (fmt .Sprintf ("✓ Color is %s" , getFgColorName (expectedCell .Style .Fg )))
116109 return nil
117110}
118111
@@ -124,14 +117,14 @@ func (c *screenStateComparator) checkBoldAttr(expectedCell, actualCell *uv.Cell)
124117 if expectedBold {
125118 return fmt .Errorf ("Expected character to be bold (ANSI code 01), was not bold" )
126119 } else {
127- return fmt .Errorf ("Expected character to not be bold, was bold" )
120+ return fmt .Errorf ("Expected character to not be bold, was bold (ANSI code 01) " )
128121 }
129122 }
130123
131124 if expectedBold {
132- c .addSuccessLog ("✓ Bold attribute is present" )
125+ c .addPartialSuccessLog ("✓ Bold attribute is present" )
133126 } else {
134- c .addSuccessLog ("✓ Bold attribute is not present" )
127+ c .addPartialSuccessLog ("✓ Bold attribute is not present" )
135128 }
136129
137130 return nil
0 commit comments