From b39cfcc54f96f3f60331cba7d9979c777c9e2f60 Mon Sep 17 00:00:00 2001 From: Siddharth Koli Date: Sat, 21 Dec 2024 00:14:25 +0530 Subject: [PATCH 1/5] Fix divide by 0 in case of 0 contributions --- ascii/generator.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ascii/generator.go b/ascii/generator.go index cba3457..bbf63a7 100644 --- a/ascii/generator.go +++ b/ascii/generator.go @@ -60,7 +60,10 @@ func GenerateASCII(contributionGrid [][]types.ContributionDay, username string, if day.ContributionCount == -1 { asciiGrid[dayIdx][weekIdx] = FutureBlock } else { - normalized := float64(day.ContributionCount) / float64(maxContributions) + normalized := 0.0 + if maxContributions != 0 { + normalized = float64(day.ContributionCount) / float64(maxContributions) + } asciiGrid[dayIdx][weekIdx] = getBlock(normalized, dayIdx, nonZeroCount) } } From 460d4057709e97f050853c94f509a7a8e2180b8f Mon Sep 17 00:00:00 2001 From: Siddharth Koli Date: Sat, 21 Dec 2024 00:15:44 +0530 Subject: [PATCH 2/5] Improve readability of conditions --- main.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index e815810..9addd7e 100644 --- a/main.go +++ b/main.go @@ -190,8 +190,9 @@ func generateSkyline(startYear, endYear int, targetUser string, full bool) error lines := strings.Split(asciiArt, "\n") gridStart := 0 for i, line := range lines { - if strings.Contains(line, string(ascii.EmptyBlock)) || - strings.Contains(line, string(ascii.FoundationLow)) { + containsEmptyBlock := strings.Contains(line, string(ascii.EmptyBlock)) + containsFoundationLow := strings.Contains(line, string(ascii.FoundationLow)) + isNotOnlyEmptyBlocks := strings.Trim(line, string(ascii.EmptyBlock)) != "" gridStart = i break } From 64066af916f865432e47a6bf260a673396c8860b Mon Sep 17 00:00:00 2001 From: Siddharth Koli Date: Sat, 21 Dec 2024 00:16:07 +0530 Subject: [PATCH 3/5] Add condition to skip over empty blocks (when -f is passed) --- main.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/main.go b/main.go index 9addd7e..79ce61d 100644 --- a/main.go +++ b/main.go @@ -193,6 +193,8 @@ func generateSkyline(startYear, endYear int, targetUser string, full bool) error containsEmptyBlock := strings.Contains(line, string(ascii.EmptyBlock)) containsFoundationLow := strings.Contains(line, string(ascii.FoundationLow)) isNotOnlyEmptyBlocks := strings.Trim(line, string(ascii.EmptyBlock)) != "" + + if (containsEmptyBlock || containsFoundationLow) && isNotOnlyEmptyBlocks { gridStart = i break } From 3e4f27b3beb0880160240bfe26ce8aeeb0a45866 Mon Sep 17 00:00:00 2001 From: Chris Reddington <791642+chrisreddington@users.noreply.github.com> Date: Sat, 28 Dec 2024 14:07:38 +0000 Subject: [PATCH 4/5] Add tests for GenerateASCII function with zero contributions --- ascii/generator_test.go | 60 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/ascii/generator_test.go b/ascii/generator_test.go index a0ed580..758e4cb 100644 --- a/ascii/generator_test.go +++ b/ascii/generator_test.go @@ -97,3 +97,63 @@ func TestGetBlock(t *testing.T) { }) } } + +// TestGenerateASCIIZeroContributions tests the GenerateASCII function with zero contributions. +// It verifies that the skyline consists of empty blocks and appropriately handles the header. +func TestGenerateASCIIZeroContributions(t *testing.T) { + tests := []struct { + name string + includeHeader bool + }{ + { + name: "Zero contributions without header", + includeHeader: false, + }, + { + name: "Zero contributions with header", + includeHeader: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + // Create a test grid with zero contributions + grid := makeTestGrid(3, 7) + for i := range grid { + for j := range grid[i] { + grid[i][j].ContributionCount = 0 + } + } + + // Generate ASCII art + result, err := GenerateASCII(grid, "testuser", 2023, tt.includeHeader) + if err != nil { + t.Fatalf("GenerateASCII() returned an error: %v", err) + } + + lines := strings.Split(result, "\n") + + // Determine the starting line of the skyline + skylineStart := 0 + if tt.includeHeader { + // Assuming HeaderTemplate has a fixed number of lines + headerLines := strings.Count(HeaderTemplate, "\n") + skylineStart = headerLines + 1 // +1 for the additional newline after header + } + + // Verify the skyline has at least 7 lines + if len(lines) < skylineStart+7 { + t.Fatalf("Expected at least %d lines for skyline, got %d", skylineStart+7, len(lines)) + } + + // Check each line of the skyline for empty blocks + for i := skylineStart; i < skylineStart+7; i++ { + for _, ch := range lines[i] { + if ch != EmptyBlock { + t.Errorf("Expected empty block in skyline, got '%c' on line %d", ch, i+1) + } + } + } + }) + } +} From 10a6ff539a49e96036962270803f0b66aef89aad Mon Sep 17 00:00:00 2001 From: Chris Reddington <791642+chrisreddington@users.noreply.github.com> Date: Sat, 28 Dec 2024 14:34:34 +0000 Subject: [PATCH 5/5] Refactor TestGenerateASCIIZeroContributions to include footer handling in the test cases --- ascii/generator_test.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/ascii/generator_test.go b/ascii/generator_test.go index 0352c36..4ee56c9 100644 --- a/ascii/generator_test.go +++ b/ascii/generator_test.go @@ -121,19 +121,19 @@ func TestGetBlock(t *testing.T) { } // TestGenerateASCIIZeroContributions tests the GenerateASCII function with zero contributions. -// It verifies that the skyline consists of empty blocks and appropriately handles the header. +// It verifies that the skyline consists of empty blocks and appropriately handles the header and footer. func TestGenerateASCIIZeroContributions(t *testing.T) { tests := []struct { - name string - includeHeader bool + name string + includeHeaderAndFooter bool }{ { - name: "Zero contributions without header", - includeHeader: false, + name: "Zero contributions without header", + includeHeaderAndFooter: false, }, { - name: "Zero contributions with header", - includeHeader: true, + name: "Zero contributions with header", + includeHeaderAndFooter: true, }, } @@ -148,7 +148,7 @@ func TestGenerateASCIIZeroContributions(t *testing.T) { } // Generate ASCII art - result, err := GenerateASCII(grid, "testuser", 2023, tt.includeHeader) + result, err := GenerateASCII(grid, "testuser", 2023, tt.includeHeaderAndFooter, tt.includeHeaderAndFooter) if err != nil { t.Fatalf("GenerateASCII() returned an error: %v", err) } @@ -157,7 +157,7 @@ func TestGenerateASCIIZeroContributions(t *testing.T) { // Determine the starting line of the skyline skylineStart := 0 - if tt.includeHeader { + if tt.includeHeaderAndFooter { // Assuming HeaderTemplate has a fixed number of lines headerLines := strings.Count(HeaderTemplate, "\n") skylineStart = headerLines + 1 // +1 for the additional newline after header