Skip to content

Add comprehensive unit tests and project documentation#191

Merged
llgcode merged 7 commits intomasterfrom
copilot/add-unit-tests-and-copilot-instructions
Feb 7, 2026
Merged

Add comprehensive unit tests and project documentation#191
llgcode merged 7 commits intomasterfrom
copilot/add-unit-tests-and-copilot-instructions

Conversation

Copy link
Contributor

Copilot AI commented Feb 7, 2026

Adds 117 unit tests and project documentation to establish baseline test coverage across core packages.

Test Coverage Added

Core (draw2d) - 82.0% coverage

  • matrix_test.go (26 tests): affine transformations, composition, inversion
  • path_test.go (19 tests): path construction, curves, arcs, subpaths

Base (draw2dbase) - 76.6% coverage

  • stack_gc_test.go (27 tests): GraphicContext state, transforms, save/restore
  • dasher_test.go (6 tests): dash patterns, state preservation
  • flattener_test.go (9 tests): path flattening, transforms
  • stroker_test.go (4 tests): line stroking, width handling

Backends

  • draw2dkit (10 tests): shapes, stroke/fill operations - 100% coverage
  • draw2dimg (16 tests): RGBA rendering, DPI, clear operations - 58.8% coverage

Documentation

.github/copilot-instructions.md documents:

  • Code style (headers, comments, naming conventions)
  • Test patterns (table-driven tests, t.TempDir(), issue references)
  • Architecture (backend structure, GraphicContext interface)
  • Coverage goals

All tests use standard testing package, no external dependencies.

Original prompt

Objective

Add comprehensive unit tests and a .github/copilot-instructions.md file to the draw2d project.

Part 1: Copilot Instructions File

Create .github/copilot-instructions.md with the following content exactly:

# Copilot Instructions for draw2d

## Project Overview

draw2d is a Go 2D vector graphics library with multiple backends:
- `draw2d` — Core package: interfaces (`GraphicContext`, `PathBuilder`), types (`Matrix`, `Path`), and font management
- `draw2dbase` — Base implementations shared across backends (`StackGraphicContext`, flattener, stroker, dasher)
- `draw2dimg` — Raster image backend (using freetype-go)
- `draw2dpdf` — PDF backend (using gofpdf)
- `draw2dsvg` — SVG backend
- `draw2dgl` — OpenGL backend
- `draw2dkit` — Drawing helpers (`Rectangle`, `Circle`, `Ellipse`, `RoundedRectangle`)
- `samples/` — Example drawings used as integration tests

## Language and Conventions

- All code, comments, commit messages, and documentation must be written in **English**.
- The project uses **Go 1.20+** (see `go.mod`).

## Code Style

### File Headers

Source files include a copyright header and creation date:
```go
// Copyright 2010 The draw2d Authors. All rights reserved.
// created: 21/11/2010 by Laurent Le Goff

New files should follow the same pattern with the current date and author name.

Comments

  • Exported types, functions, and methods must have GoDoc comments.
  • Comments should start with the name of the thing being documented:
    // Rectangle draws a rectangle using a path between (x1,y1) and (x2,y2)
    func Rectangle(path draw2d.PathBuilder, x1, y1, x2, y2 float64) {
  • Package comments go in the main source file or a doc.go file.

Naming

  • Follow standard Go naming conventions (camelCase for unexported, PascalCase for exported).
  • Backend packages are named draw2d<backend> (e.g., draw2dimg, draw2dpdf).
  • The GraphicContext struct in each backend embeds *draw2dbase.StackGraphicContext.

Error Handling

  • Functions that can fail return error as the last return value.
  • Do not silently ignore errors — log or return them.

Testing

Structure

  • Unit tests go alongside the source file they test (e.g., matrix_test.go tests matrix.go).
  • Integration/sample tests live in samples_test.go, draw2dpdf/samples_test.go, etc.
  • Test output files go in the output/ directory (generated, not committed).

Writing Tests

  • Use the standard testing package only — no external test frameworks.
  • Use table-driven tests where multiple inputs share the same logic:
    tests := []struct {
        name string
        // ...
    }{
        {"case1", ...},
        {"case2", ...},
    }
    for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) { ... })
    }
  • Tests must not depend on external resources (fonts, network) unless testing that specific integration.
  • For image-based tests, use image.NewRGBA(image.Rect(0, 0, w, h)) as the canvas.
  • Use t.TempDir() for any file output in tests.
  • Reference GitHub issue numbers in regression test comments:
    // Test related to issue #95: DashVertexConverter state preservation

Running Tests

go test ./...
go test -cover ./... | grep -v "no test"

Test Coverage Goals

  • Every exported function and method should have at least one unit test.
  • Core types (Matrix, Path, StackGraphicContext) should have thorough coverage.
  • Backend-specific operations (Stroke, Fill, FillStroke, Clear) should verify pixel output where possible.
  • Known bugs in the issue tracker should have corresponding regression tests.

Documentation

  • When adding or changing public API, update the GoDoc comments accordingly.
  • When fixing a bug, add a comment referencing the issue number.
  • If a change affects behavior described in README.md or package READMEs, update them.
  • The samples/ directory serves as living documentation — keep samples working after changes.

Architecture Notes

  • All backends implement the draw2d.GraphicContext interface defined in gc.go.
  • draw2dbase.StackGraphicContext provides the common state management (colors, transforms, font, path). Backends embed it and override rendering methods (Stroke, Fill, FillStroke, string drawing, etc.).
  • The draw2dkit helpers operate on draw2d.PathBuilder, not GraphicContext, making them backend-agnostic.
  • Matrix is a [6]float64 affine transformation matrix. Coordinate system follows the HTML Canvas 2D Context conventions.

## Part 2: Unit Tests

Add the following test files. Do NOT modify any existing files. All tests must pass with `go test ./...`.

### 2.1 `matrix_test.go` (root package `draw2d`)

Unit tests for `Matrix` type in `matrix.go`. Tests to include:

- `TestNewIdentityMatrix` — verify `IsIdentity()` returns true, equals `Matrix{1, 0, 0, 1, 0, 0}`
- `TestNewTranslationMatrix` — verify `GetTranslation()` returns correct values, `IsTranslation()`...

</details>



<!-- START COPILOT CODING AGENT SUFFIX -->

*This pull request was created from Copilot chat.*
>

<!-- START COPILOT CODING AGENT TIPS -->
---

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more [Copilot coding agent tips](https://gh.io/copilot-coding-agent-tips) in the docs.

- Created .github/copilot-instructions.md with project guidelines
- Added matrix_test.go with 26 Matrix tests (82% coverage)
- Added path_test.go with 19 Path tests
- Added draw2dbase/stack_gc_test.go with 30 StackGraphicContext tests
- Added draw2dkit/draw2dkit_extended_test.go with 10 additional tests (100% coverage)
- Added draw2dimg/gc_test.go with 17 image backend tests
- Added draw2dbase/dasher_test.go with 6 DashVertexConverter tests
- Added draw2dbase/flattener_test.go with 10 flattener tests
- Added draw2dbase/stroker_test.go with 4 LineStroker tests
- All tests pass with go test ./... (except draw2dgl which requires OpenGL)

Co-authored-by: llgcode <524083+llgcode@users.noreply.github.com>
Copilot AI changed the title [WIP] Add comprehensive unit tests and copilot instructions file Add comprehensive unit tests and Copilot instructions Feb 7, 2026
Copilot AI requested a review from llgcode February 7, 2026 09:09
Copilot AI and others added 2 commits February 7, 2026 09:36
- Added known_issues_test.go with tests for GitHub issues #155, #139, #171, #181
- Added draw2dimg/known_issues_test.go for backend-specific issues
- Added performance_bench_test.go for performance benchmarking (issue #147)
- Added KNOWN_ISSUES_TESTS.md documenting all failing tests and their purpose

FAILING TESTS (demonstrating real bugs):
- TestIssue155_SetLineCapButtCap: Line caps don't render differently
- TestIssue155_SetLineCapSquareCap: Line caps don't render differently
- TestIssue139_PDFVerticalFlip: Y-axis flip doesn't work in PDF backend
- draw2dimg/TestIssue155_LineCapVisualDifference: Line caps identical

These tests intentionally FAIL to show current implementation limitations
as requested. They document known bugs from open GitHub issues.

Co-authored-by: llgcode <524083+llgcode@users.noreply.github.com>
Co-authored-by: llgcode <524083+llgcode@users.noreply.github.com>
Copilot AI changed the title Add comprehensive unit tests and Copilot instructions Add failing tests for known bugs to expose implementation limits Feb 7, 2026
Copilot AI changed the title Add failing tests for known bugs to expose implementation limits Remove failing test commits - handled by separate PR Feb 7, 2026
Copilot AI changed the title Remove failing test commits - handled by separate PR Add comprehensive unit tests and project documentation Feb 7, 2026
@llgcode llgcode marked this pull request as ready for review February 7, 2026 10:12
@llgcode llgcode merged commit 91c1aac into master Feb 7, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants