Compare commits
8 Commits
b24793bce6
...
multi-labe
| Author | SHA1 | Date | |
|---|---|---|---|
|
cf626cf797
|
|||
|
e008cc72fb
|
|||
|
45eceb7e7a
|
|||
|
243a6886d0
|
|||
|
dae16f4a26
|
|||
|
9b8f1d9dc6
|
|||
|
25720aaa0a
|
|||
|
f7661a813d
|
11
.clangd
11
.clangd
@@ -1,11 +0,0 @@
|
||||
---
|
||||
If:
|
||||
PathMatch: tests/.*
|
||||
CompileFlags:
|
||||
CompilationDatabase: builddir-debug/
|
||||
|
||||
---
|
||||
If:
|
||||
PathMatch: src/.*
|
||||
CompileFlags:
|
||||
CompilationDatabase: builddir/
|
||||
@@ -14,7 +14,7 @@ jobs:
|
||||
- name: install meson
|
||||
run: apt-get -yq install meson
|
||||
- name: Install build dependencies
|
||||
run: apt-get -yq install libusb-1.0-0-dev libpango1.0-dev libcairo2-dev gcovr
|
||||
run: apt-get -yq install libusb-1.0-0-dev libspdlog-dev libfmt-dev libpango1.0-dev libcairo2-dev gcovr
|
||||
- name: get build environment versions
|
||||
run: |
|
||||
echo "=== Start meson version ==="
|
||||
@@ -32,25 +32,23 @@ jobs:
|
||||
echo "=== Start dependency package version ==="
|
||||
apt-cache policy libpango1.0-dev
|
||||
apt-cache policy libcairo2-dev
|
||||
apt-cache policy libfmt-dev
|
||||
apt-cache policy libspdlog-dev
|
||||
apt-cache policy libusb-1.0-0-dev
|
||||
echo "=== End dependency package version ==="
|
||||
- name: Build ptprnt debug
|
||||
run: scripts/build.sh debug --coverage --test
|
||||
- name: Generate coverage
|
||||
run: scripts/generate_coverage.sh --text
|
||||
- name: setup builddir
|
||||
run: meson setup builddir -Db_coverage=true
|
||||
- name: build and test dist package
|
||||
run: ninja -C builddir dist
|
||||
- name: run unit tests
|
||||
run: ninja -C builddir test
|
||||
- name: calculate coverage
|
||||
run: ninja -C builddir coverage-text
|
||||
- name: Coverage report
|
||||
run: cat ./coverageReport/coverage.txt
|
||||
- name: build release
|
||||
run: scripts/build.sh release
|
||||
- name: upload release binary
|
||||
run: cat ./builddir/meson-logs/coverage.txt
|
||||
- name: upload dist package
|
||||
uses: actions/upload-artifact@v3
|
||||
with:
|
||||
name: ptprnt
|
||||
path: ./builddir/ptprnt
|
||||
if-no-files-found: error
|
||||
- name: upload coverage report
|
||||
uses: actions/upload-artifact@v3
|
||||
with:
|
||||
name: coverage.txt
|
||||
path: ./coverageReport/coverage.txt
|
||||
name: ptprnt-dist
|
||||
path: ./builddir/meson-dist/*
|
||||
if-no-files-found: error
|
||||
|
||||
16
.gitignore
vendored
16
.gitignore
vendored
@@ -1,5 +1,6 @@
|
||||
# Folder
|
||||
builddir*/
|
||||
builddir/
|
||||
ptouch-print/
|
||||
subprojects/*
|
||||
.cache/
|
||||
coverageReport/
|
||||
@@ -11,16 +12,3 @@ ptprnt.log
|
||||
!.vscode/c_cpp_properties.json
|
||||
!.vscode/settings.json
|
||||
!.vscode/launch.json
|
||||
|
||||
# ignore generated testlabels
|
||||
fakelabel_*.png
|
||||
|
||||
# ignore package capture files
|
||||
*.pcapng
|
||||
*.pcapng.gz
|
||||
*.pcap
|
||||
*.pcap.gz
|
||||
|
||||
# ignore coverage temporaries
|
||||
*.gcov.json
|
||||
*.gcov.json.gz
|
||||
7
.vscode/c_cpp_properties.json
vendored
7
.vscode/c_cpp_properties.json
vendored
@@ -5,12 +5,11 @@
|
||||
"compilerPath": "/usr/bin/clang",
|
||||
"cStandard": "c11",
|
||||
"cppStandard": "c++20",
|
||||
"compileCommands": "${workspaceFolder}/builddir/compile_commands.json",
|
||||
"browse": {
|
||||
"path": [
|
||||
"${workspaceFolder}"
|
||||
]
|
||||
"path": ["${workspaceFolder}"]
|
||||
}
|
||||
}
|
||||
],
|
||||
"version": 4
|
||||
}
|
||||
}
|
||||
|
||||
1
.vscode/settings.json
vendored
1
.vscode/settings.json
vendored
@@ -1,6 +1,7 @@
|
||||
{
|
||||
"clangd.arguments": [
|
||||
"-background-index",
|
||||
"-compile-commands-dir=builddir/"
|
||||
],
|
||||
"editor.formatOnType": false,
|
||||
"editor.formatOnSave": true,
|
||||
|
||||
70
CHANGELOG.md
70
CHANGELOG.md
@@ -1,70 +0,0 @@
|
||||
# Changelog
|
||||
|
||||
All notable changes to ptprnt will be documented in this file.
|
||||
|
||||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
||||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||
|
||||
## [0.2.0] - v0.2.0
|
||||
|
||||
### Added
|
||||
- Multi-label support - print multiple labels in sequence
|
||||
- LabelBuilder API with fluent interface for constructing labels
|
||||
- FakePrinter driver for testing without hardware (outputs PNG files)
|
||||
- PrinterService core service for printer operations
|
||||
- CliParser component with ICliParser interface
|
||||
- ICairoWrapper interface for testable graphics rendering
|
||||
- MockCairoWrapper for unit testing
|
||||
- Pre-commit hook for automatic copyright updates
|
||||
- USB trace mode for debugging (`-Dusb_trace_only=true`)
|
||||
|
||||
### Changed
|
||||
- **Major refactoring**: Reorganized codebase into layered architecture
|
||||
- Application layer: PtouchPrint, CliParser, PrinterService
|
||||
- Printer drivers: Moved to `src/printers/` with factory pattern
|
||||
- Graphics system: Added builder pattern and Cairo abstraction
|
||||
- Core services: Separated into `src/core/` directory
|
||||
- Label class now uses dependency injection for Cairo/Pango
|
||||
- CLI parsing separated from main application logic
|
||||
- Updated dependencies to latest versions
|
||||
- Improved project documentation (README.md, CLAUDE.md)
|
||||
|
||||
### Fixed
|
||||
- Label corruption issues resolved
|
||||
- Printer info retrieval bugs
|
||||
- USB attachment logic
|
||||
- Multiple lines handling in labels
|
||||
- Printer selection logic
|
||||
|
||||
## [0.1.0] - 2024
|
||||
|
||||
### Added
|
||||
- Initial release of ptprnt
|
||||
- Basic label printing functionality for Brother P-touch P700 series
|
||||
- Pango/Cairo-based text rendering
|
||||
- USB device communication via libusb-1.0
|
||||
- Template-based Bitmap class supporting multiple pixel formats (ALPHA8, RGBX8, RGBA8, ARGB8)
|
||||
- Monochrome bitmap conversion for printer output
|
||||
- PrinterDriverFactory for creating printer instances
|
||||
- USB device abstraction layer (IUsbDevice, UsbDevice, UsbDeviceFactory)
|
||||
- Command-line interface using CLI11
|
||||
- Logging with spdlog and file output
|
||||
- Unit tests using GoogleTest
|
||||
- Code coverage reporting with gcovr
|
||||
- Meson build system with C++20 support
|
||||
- CI/CD pipeline with automated testing
|
||||
|
||||
### Core Components (v0.1.0)
|
||||
- PtouchPrint: Main application orchestrator
|
||||
- P700Printer: Brother P-touch P700 driver implementation
|
||||
- Bitmap: Template-based image storage
|
||||
- Label: Text rendering with Pango/Cairo
|
||||
- Monochrome: Bitmap to monochrome conversion
|
||||
- UsbDevice: libusb wrapper for device communication
|
||||
|
||||
---
|
||||
|
||||
## Project Origins
|
||||
|
||||
ptprnt is a modern C++20 rewrite of [ptouch-print](https://git.familie-radermacher.ch/linux/ptouch-print.git).
|
||||
All credits for reverse engineering the Brother P-touch USB protocol go to Dominic Rademacher.
|
||||
265
README.md
265
README.md
@@ -1,252 +1,57 @@
|
||||
# ptprnt
|
||||
|
||||
A command-line label printer driver for Brother P-touch printers on Linux. Prints text labels directly from your terminal.
|
||||
This is a rewrite of [ptouch-print](https://git.familie-radermacher.ch/linux/ptouch-print.git) as a toy project for my personal amusement. The currently available solutions are good enough for generating labels, but i wanted to explore libusb and maybe improve the functionality of my label printer. All credits for reverse engineering the USB commands to Dominic Rademacher.
|
||||
|
||||
## Example
|
||||
## Dependencies
|
||||
This project requires:
|
||||
- spdlog
|
||||
- libusb
|
||||
- pango
|
||||
- cairo
|
||||
- meson
|
||||
- gtest (optional, for testing, will be installed by meson)
|
||||
- gcov (optional, for coverage reports)
|
||||
|
||||
Too print a label, provide your text and optionally a font and a font size. This command will print the label below on a Brother P-Touch P700:
|
||||
|
||||
```bash
|
||||
ptprnt --font "NotoMono Nerd Font" --fontsize 32 --text "🖶 ptprnt v0.2.0 🥰"
|
||||
Install dependencies on Arch Linux
|
||||
``` bash
|
||||
pacman -S libusb spdlog pango cairo meson gcovr
|
||||
```
|
||||
|
||||

|
||||
|
||||
## Quick Start
|
||||
|
||||
## Quick Start
|
||||
|
||||
**Binary dependencies**
|
||||
|
||||
Arch Linux:
|
||||
```bash
|
||||
pacman -S pango cairo libusb
|
||||
Install dependencies on Debian/Ubuntu
|
||||
``` bash
|
||||
apt-get install libusb-1.0-0-dev libspdlog-dev libfmt-dev libpango1.0-dev libcairo2-dev meson gcovr
|
||||
```
|
||||
|
||||
Debian/Ubuntu:
|
||||
## Build
|
||||
|
||||
Clone the repository and simply let meson do the heavy lifting.
|
||||
|
||||
```bash
|
||||
apt install libpangocairo-1.0-0 libusb-1.0-0
|
||||
meson setup builddir
|
||||
```
|
||||
If you want to generate coverage reports, enable them via the command line switch
|
||||
```bash
|
||||
meson setup builddir -Db_coverage=true
|
||||
```
|
||||
|
||||
**Build dependencies:**
|
||||
|
||||
Arch Linux:
|
||||
Rebuild by simply invoking ninja
|
||||
```bash
|
||||
pacman -S libusb spdlog pango cairo meson
|
||||
```
|
||||
|
||||
Debian/Ubuntu:
|
||||
```bash
|
||||
apt install libusb-1.0-0-dev libspdlog-dev libfmt-dev libpango1.0-dev libcairo2-dev meson
|
||||
```
|
||||
|
||||
Note: spdlog is built as a subproject and statically linked, so it's not required as a system dependency.
|
||||
|
||||
**Build and run:**
|
||||
Clone this repository first and enter the directory. Then build:
|
||||
```bash
|
||||
# Using the build script (recommended)
|
||||
./scripts/build.sh release
|
||||
builddir/ptprnt --help
|
||||
|
||||
# Or manually with meson
|
||||
meson setup builddir
|
||||
ninja -C builddir
|
||||
builddir/ptprnt --help
|
||||
```
|
||||
|
||||
## Usage
|
||||
## Run
|
||||
Run the binary from your builddir
|
||||
```bash
|
||||
builddir/ptprnt
|
||||
```
|
||||
|
||||
### Basic Text Printing
|
||||
|
||||
Print a simple label with text:
|
||||
## Test
|
||||
Testing is done via gtest. To run your test simply invoke ninja with the "test" target.
|
||||
```bash
|
||||
ptprnt --text "Hello World"
|
||||
ninja -C builddir test
|
||||
```
|
||||
|
||||
### Formatting Options
|
||||
|
||||
Control the appearance of your labels with these options:
|
||||
|
||||
- `--font FONT_NAME` - Set the font (e.g., "NotoMono Nerd Font", "DejaVu Sans")
|
||||
- `--fontsize SIZE` - Set font size in points (default: 24)
|
||||
- `--halign ALIGNMENT` - Horizontal alignment: `left`, `center`, `right` (default: center)
|
||||
- `--valign ALIGNMENT` - Vertical alignment: `top`, `center`, `bottom` (default: center)
|
||||
|
||||
**Example with formatting:**
|
||||
```bash
|
||||
ptprnt --font "DejaVu Sans Mono" --fontsize 28 --halign left --text "Left aligned text"
|
||||
```
|
||||
|
||||
### Multiple Text Elements
|
||||
|
||||
You can add multiple text elements to a single label. Formatting options apply to all subsequent `--text` arguments until changed:
|
||||
|
||||
```bash
|
||||
ptprnt \
|
||||
--font "DejaVu Sans" --fontsize 32 --text "Large Title" \
|
||||
--fontsize 18 --text "Smaller subtitle"
|
||||
```
|
||||
|
||||
### Multiple Labels (Stitching)
|
||||
|
||||
Create multiple labels that will be stitched together horizontally using the `--new` flag:
|
||||
|
||||
```bash
|
||||
ptprnt \
|
||||
--text "Label 1" \
|
||||
--new \
|
||||
--text "Label 2" \
|
||||
--new \
|
||||
--text "Label 3"
|
||||
```
|
||||
|
||||
Each `--new` starts a fresh label. The labels are automatically stitched together with 60 pixels of spacing.
|
||||
|
||||
**Example - Creating a series of address labels:**
|
||||
```bash
|
||||
ptprnt \
|
||||
--font "DejaVu Sans" --fontsize 20 \
|
||||
--text "Peter Lustig" --text "Am Bauwagen 1" \
|
||||
--new \
|
||||
--text "Donald Duck" --text "Blumenstraße 13" \
|
||||
--new \
|
||||
--text "Homer Simpson" --text "742 Evergreen Terrace"
|
||||
```
|
||||
|
||||
**Example - Mixed formatting across labels:**
|
||||
```bash
|
||||
ptprnt \
|
||||
--fontsize 32 --text "BIG" \
|
||||
--new \
|
||||
--fontsize 16 --text "small" \
|
||||
--new \
|
||||
--fontsize 24 --text "medium"
|
||||
```
|
||||
|
||||
### Printer Selection
|
||||
|
||||
By default, ptprnt auto-detects your printer. You can explicitly select a printer:
|
||||
|
||||
```bash
|
||||
ptprnt --printer P700 --text "Hello"
|
||||
```
|
||||
|
||||
List all available printer drivers:
|
||||
```bash
|
||||
ptprnt --list-all-drivers
|
||||
```
|
||||
|
||||
### Testing with Fake Printer
|
||||
|
||||
Before printing to your real printer, you can test your label output using the built-in fake printer. This generates a PNG image file instead of printing:
|
||||
|
||||
```bash
|
||||
ptprnt --printer FakePrinter --text "Test Label"
|
||||
```
|
||||
|
||||
This will create a file named `fakelabel_YYYYMMDD_HHMMSS.png` in your current directory with a preview of your label. Use this to:
|
||||
- Verify text formatting and layout
|
||||
- Test multi-label stitching
|
||||
- Preview before wasting label tape
|
||||
|
||||
**Example workflow:**
|
||||
```bash
|
||||
# First, test your label design
|
||||
ptprnt --printer FakePrinter \
|
||||
--font "DejaVu Sans" --fontsize 28 \
|
||||
--text "Test Design" --text "Check Layout"
|
||||
|
||||
# View the generated PNG file to verify
|
||||
# If satisfied, print to real printer
|
||||
ptprnt --printer P700 \
|
||||
--font "DejaVu Sans" --fontsize 28 \
|
||||
--text "Test Design" --text "Check Layout"
|
||||
```
|
||||
|
||||
### Verbose Output
|
||||
|
||||
Enable detailed logging for debugging:
|
||||
|
||||
```bash
|
||||
ptprnt --verbose --text "Debug mode"
|
||||
```
|
||||
|
||||
Enable USB trace to see raw USB communication:
|
||||
```bash
|
||||
ptprnt --trace --text "USB trace mode"
|
||||
```
|
||||
|
||||
### Complete Example
|
||||
|
||||
An example using a mixed bag of features:
|
||||
|
||||
```bash
|
||||
ptprnt \
|
||||
--verbose \
|
||||
--printer P700 \
|
||||
--font "NotoMono Nerd Font" \
|
||||
--fontsize 28 --halign center --text "Product Label" \
|
||||
--fontsize 20 --text "SKU: 12345" \
|
||||
--new \
|
||||
--fontsize 24 --text "Backup Label" \
|
||||
--fontsize 18 --text "Date: 2025-10-16"
|
||||
```
|
||||
|
||||
## Supported Printers
|
||||
(I need more printers for verification 😉)
|
||||
|
||||
- Brother P-touch P700 series
|
||||
|
||||
## Developer info
|
||||
|
||||
This is a modern C++20 rewrite of [ptouch-print](https://git.familie-radermacher.ch/linux/ptouch-print.git). Credits to Dominic Rademacher for reverse engineering the USB protocol.
|
||||
|
||||
**Build script:**
|
||||
```bash
|
||||
# Release build (tests disabled for faster builds)
|
||||
./scripts/build.sh release
|
||||
|
||||
# Debug build (tests enabled)
|
||||
./scripts/build.sh debug
|
||||
|
||||
# Debug with tests
|
||||
./scripts/build.sh debug --test
|
||||
|
||||
# Debug with coverage
|
||||
./scripts/build.sh debug --coverage
|
||||
|
||||
# Clean all build directories
|
||||
./scripts/build.sh clean
|
||||
|
||||
# Show all options
|
||||
./scripts/build.sh --help
|
||||
```
|
||||
|
||||
**Note:** Tests are only built in debug mode to keep release builds fast and small. Release builds do not include test binaries or link against gtest/gmock.
|
||||
|
||||
**Running tests:**
|
||||
```bash
|
||||
# Using build script
|
||||
./scripts/build.sh --test
|
||||
|
||||
# Or manually
|
||||
ninja -C builddir test
|
||||
```
|
||||
|
||||
**Coverage reports:**
|
||||
```bash
|
||||
# 1. Build with coverage enabled and run tests
|
||||
./scripts/build.sh debug --coverage --test
|
||||
|
||||
# 2. Generate coverage reports
|
||||
./scripts/generate_coverage.sh # All formats (html, xml, text)
|
||||
./scripts/generate_coverage.sh --html # HTML only
|
||||
./scripts/generate_coverage.sh --text # Text only
|
||||
./scripts/generate_coverage.sh --xml # XML only (for CI/CD)
|
||||
./scripts/generate_coverage.sh --html --xml # HTML and XML
|
||||
|
||||
```
|
||||
Coverage reports can be generated via gcov if you enabled them (see Build section) by building the `coverage-text` target.
|
||||
|
||||
## License
|
||||
|
||||
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 642 KiB |
27
generate_coverage.sh
Executable file
27
generate_coverage.sh
Executable file
@@ -0,0 +1,27 @@
|
||||
#!/bin/bash
|
||||
|
||||
SCRIPT_PATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
|
||||
HTML_COV_PATH="coverageReport/html"
|
||||
XML_COV_PATH="coverageReport/xml"
|
||||
HTML_START_FILE="index.html"
|
||||
|
||||
echo "Generating Coverage report for ptouch-prnt"
|
||||
|
||||
ninja -C builddir
|
||||
ninja -C builddir test
|
||||
|
||||
mkdir -p ${HTML_COV_PATH}
|
||||
gcovr --html --html-details --html-syntax-highlighting --filter src --output ${HTML_COV_PATH}/${HTML_START_FILE}
|
||||
|
||||
mkdir -p ${XML_COV_PATH}
|
||||
gcovr --xml-pretty --filter src --output ${XML_COV_PATH}/cov.xml
|
||||
|
||||
if [ $? ]
|
||||
then
|
||||
echo "Coverage report successful generated!"
|
||||
echo "Open: file://${SCRIPT_PATH}/${HTML_COV_PATH}/${HTML_START_FILE}"
|
||||
else
|
||||
echo "Error generating coverage report!"
|
||||
fi
|
||||
|
||||
rm *.gcov
|
||||
@@ -1,6 +1,6 @@
|
||||
# Git Hooks
|
||||
|
||||
This directory contains git hooks for the ptprnt repository.
|
||||
This directory contains git hooks for the ptouch-prnt repository.
|
||||
|
||||
## Installation
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#!/bin/bash
|
||||
# Install git hooks for ptprnt repository
|
||||
# Install git hooks for ptouch-prnt repository
|
||||
|
||||
set -e
|
||||
|
||||
|
||||
48
meson.build
48
meson.build
@@ -1,11 +1,11 @@
|
||||
project(
|
||||
'ptprnt',
|
||||
'cpp',
|
||||
version: 'v0.2.0-'
|
||||
+ run_command(
|
||||
version: 'v0.1.0-' + run_command(
|
||||
'git',
|
||||
'rev-parse',
|
||||
'--short', 'HEAD',
|
||||
'--short',
|
||||
'HEAD',
|
||||
check: true,
|
||||
).stdout().strip(),
|
||||
license: 'GPLv3',
|
||||
@@ -20,17 +20,11 @@ project(
|
||||
)
|
||||
|
||||
usb_dep = dependency('libusb-1.0')
|
||||
log_dep = dependency('spdlog')
|
||||
fmt_dep = dependency('fmt')
|
||||
pangocairo_dep = dependency('pangocairo')
|
||||
|
||||
# spdlog with std::format (C++20) - static library
|
||||
spdlog_proj = subproject('spdlog', default_options: ['std_format=enabled', 'default_library=static', 'compile_library=true'])
|
||||
log_dep = spdlog_proj.get_variable('spdlog_dep')
|
||||
|
||||
if not log_dep.found()
|
||||
error('spdlog not found, can not proceed')
|
||||
endif
|
||||
|
||||
# CLI11
|
||||
# CLI11
|
||||
cli11_proj = subproject('cli11')
|
||||
cli11_dep = cli11_proj.get_variable('CLI11_dep')
|
||||
if not cli11_dep.found()
|
||||
@@ -47,36 +41,28 @@ cpp_args = ['-DPROJ_VERSION="' + meson.project_version() + '"']
|
||||
# USB trace mode option (for debugging without sending to hardware)
|
||||
if get_option('usb_trace_only')
|
||||
cpp_args += ['-DUSB_TRACE_ONLY']
|
||||
message(
|
||||
'USB_TRACE_ONLY enabled: USB data will be logged but not sent to device',
|
||||
)
|
||||
message('USB_TRACE_ONLY enabled: USB data will be logged but not sent to device')
|
||||
endif
|
||||
|
||||
ptprnt_exe = executable(
|
||||
'ptprnt',
|
||||
'src/main.cpp',
|
||||
install: true,
|
||||
dependencies: [usb_dep, log_dep, pangocairo_dep, cli11_dep],
|
||||
dependencies: [usb_dep, log_dep, fmt_dep, pangocairo_dep, cli11_dep],
|
||||
include_directories: incdir,
|
||||
sources: [ptprnt_srcs],
|
||||
cpp_args: cpp_args,
|
||||
)
|
||||
|
||||
|
||||
### Unit tests
|
||||
# Only build tests for debug builds or when explicitly enabled
|
||||
build_tests = get_option('buildtype') == 'debug' or get_option('build_tests')
|
||||
|
||||
if build_tests
|
||||
# GTest and GMock
|
||||
gtest_proj = subproject('gtest')
|
||||
gtest_dep = gtest_proj.get_variable('gtest_main_dep')
|
||||
gmock_dep = gtest_proj.get_variable('gmock_main_dep')
|
||||
if not gtest_dep.found()
|
||||
error('MESON_SKIP_TEST: gtest not installed.')
|
||||
endif
|
||||
# GTest and GMock
|
||||
gtest_proj = subproject('gtest')
|
||||
gtest_dep = gtest_proj.get_variable('gtest_main_dep')
|
||||
gmock_dep = gtest_proj.get_variable('gmock_main_dep')
|
||||
if not gtest_dep.found()
|
||||
error('MESON_SKIP_TEST: gtest not installed.')
|
||||
endif
|
||||
|
||||
subdir('tests')
|
||||
message('Tests enabled (buildtype=' + get_option('buildtype') + ')')
|
||||
else
|
||||
message('Tests disabled (use debug build or -Dbuild_tests=true to enable)')
|
||||
endif
|
||||
subdir('tests')
|
||||
|
||||
@@ -2,8 +2,3 @@ option('usb_trace_only',
|
||||
type: 'boolean',
|
||||
value: false,
|
||||
description: 'Enable USB trace mode: log USB data without sending to device (saves label tape during debugging)')
|
||||
|
||||
option('build_tests',
|
||||
type: 'boolean',
|
||||
value: false,
|
||||
description: 'Build unit tests (automatically enabled for debug builds)')
|
||||
|
||||
162
scripts/build.sh
162
scripts/build.sh
@@ -1,162 +0,0 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Build script for ptprnt - simplifies common build configurations
|
||||
# Usage: ./scripts/build.sh [release|debug|clean] [options]
|
||||
|
||||
set -e
|
||||
|
||||
SCRIPT_PATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
|
||||
PROJECT_ROOT="${SCRIPT_PATH}/.."
|
||||
|
||||
# Color output
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[1;33m'
|
||||
BLUE='\033[0;34m'
|
||||
NC='\033[0m' # No Color
|
||||
|
||||
print_usage() {
|
||||
echo "Usage: $0 [build-type] [options]"
|
||||
echo ""
|
||||
echo "Build Types:"
|
||||
echo " release Build optimized release version (default)"
|
||||
echo " debug Build debug version with symbols"
|
||||
echo " clean Clean build directories"
|
||||
echo ""
|
||||
echo "Options:"
|
||||
echo " --coverage Enable coverage reporting (debug builds only)"
|
||||
echo " --reconfigure Force reconfiguration"
|
||||
echo " --test Run tests after building"
|
||||
echo " -j N Use N parallel jobs (default: auto)"
|
||||
echo ""
|
||||
echo "Examples:"
|
||||
echo " $0 # Build release"
|
||||
echo " $0 debug --test # Build debug and run tests"
|
||||
echo " $0 debug --coverage # Build debug with coverage"
|
||||
echo " $0 clean # Clean all build directories"
|
||||
}
|
||||
|
||||
# Default values
|
||||
BUILD_TYPE="release"
|
||||
BUILDDIR="builddir"
|
||||
COVERAGE=false
|
||||
RECONFIGURE=false
|
||||
RUN_TESTS=false
|
||||
JOBS=""
|
||||
|
||||
# Parse arguments
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case $1 in
|
||||
release|debug|clean)
|
||||
BUILD_TYPE="$1"
|
||||
shift
|
||||
;;
|
||||
--coverage)
|
||||
COVERAGE=true
|
||||
shift
|
||||
;;
|
||||
--reconfigure)
|
||||
RECONFIGURE=true
|
||||
shift
|
||||
;;
|
||||
--test)
|
||||
RUN_TESTS=true
|
||||
shift
|
||||
;;
|
||||
-j)
|
||||
JOBS="-j $2"
|
||||
shift 2
|
||||
;;
|
||||
-h|--help)
|
||||
print_usage
|
||||
exit 0
|
||||
;;
|
||||
*)
|
||||
echo -e "${RED}Error: Unknown option: $1${NC}"
|
||||
print_usage
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
cd "${PROJECT_ROOT}"
|
||||
|
||||
# Handle clean
|
||||
if [[ "${BUILD_TYPE}" == "clean" ]]; then
|
||||
echo -e "${YELLOW}Cleaning build directories...${NC}"
|
||||
rm -rf builddir builddir-debug
|
||||
echo -e "${GREEN}Clean complete!${NC}"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Set build directory and options based on build type
|
||||
if [[ "${BUILD_TYPE}" == "debug" ]]; then
|
||||
BUILDDIR="builddir-debug"
|
||||
MESON_OPTS="--buildtype=debug"
|
||||
|
||||
if [[ "${COVERAGE}" == true ]]; then
|
||||
MESON_OPTS="${MESON_OPTS} -Db_coverage=true"
|
||||
echo -e "${BLUE}Building debug with coverage enabled${NC}"
|
||||
else
|
||||
echo -e "${BLUE}Building debug version${NC}"
|
||||
fi
|
||||
else
|
||||
BUILDDIR="builddir"
|
||||
MESON_OPTS="--buildtype=release"
|
||||
|
||||
if [[ "${COVERAGE}" == true ]]; then
|
||||
echo -e "${YELLOW}Warning: Coverage is only supported for debug builds, ignoring --coverage${NC}"
|
||||
fi
|
||||
|
||||
if [[ "${RUN_TESTS}" == true ]]; then
|
||||
echo -e "${YELLOW}Warning: Tests are not built for release builds (use debug build for testing)${NC}"
|
||||
RUN_TESTS=false
|
||||
fi
|
||||
|
||||
echo -e "${BLUE}Building release version (tests disabled)${NC}"
|
||||
fi
|
||||
|
||||
# Setup or reconfigure build directory
|
||||
if [[ ! -d "${BUILDDIR}" ]] || [[ "${RECONFIGURE}" == true ]]; then
|
||||
if [[ "${RECONFIGURE}" == true ]]; then
|
||||
echo -e "${YELLOW}Reconfiguring build...${NC}"
|
||||
meson setup "${BUILDDIR}" ${MESON_OPTS} --wipe --reconfigure
|
||||
else
|
||||
echo -e "${YELLOW}Setting up build directory...${NC}"
|
||||
meson setup "${BUILDDIR}" ${MESON_OPTS}
|
||||
fi
|
||||
fi
|
||||
|
||||
# Build
|
||||
echo -e "${YELLOW}Building...${NC}"
|
||||
ninja -C "${BUILDDIR}" ${JOBS}
|
||||
|
||||
if [[ $? -eq 0 ]]; then
|
||||
echo -e "${GREEN}Build successful!${NC}"
|
||||
echo -e "Binary: ${BUILDDIR}/ptprnt"
|
||||
else
|
||||
echo -e "${RED}Build failed!${NC}"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Run tests if requested
|
||||
if [[ "${RUN_TESTS}" == true ]]; then
|
||||
echo -e "${YELLOW}Running tests...${NC}"
|
||||
ninja -C "${BUILDDIR}" test
|
||||
|
||||
if [[ $? -eq 0 ]]; then
|
||||
echo -e "${GREEN}All tests passed!${NC}"
|
||||
else
|
||||
echo -e "${RED}Tests failed!${NC}"
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
# Show binary info
|
||||
echo ""
|
||||
echo -e "${BLUE}Build Information:${NC}"
|
||||
echo " Build type: ${BUILD_TYPE}"
|
||||
echo " Build dir: ${BUILDDIR}"
|
||||
echo " Binary: $(ls -lh ${BUILDDIR}/ptprnt | awk '{print $5, $9}')"
|
||||
echo ""
|
||||
echo -e "${GREEN}Done!${NC}"
|
||||
@@ -1,195 +0,0 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Coverage report generator for ptprnt
|
||||
# Usage: ./scripts/generate_coverage.sh [options]
|
||||
|
||||
set -e
|
||||
|
||||
SCRIPT_PATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
|
||||
PROJECT_ROOT="${SCRIPT_PATH}/.."
|
||||
|
||||
# Output paths
|
||||
HTML_COV_PATH="coverageReport/html"
|
||||
XML_COV_PATH="coverageReport/xml"
|
||||
TEXT_COV_PATH="coverageReport"
|
||||
HTML_START_FILE="index.html"
|
||||
|
||||
# Color output
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[1;33m'
|
||||
BLUE='\033[0;34m'
|
||||
NC='\033[0m' # No Color
|
||||
|
||||
# Default values - all formats enabled by default
|
||||
GENERATE_HTML=false
|
||||
GENERATE_XML=false
|
||||
GENERATE_TEXT=false
|
||||
BUILDDIR="builddir-debug"
|
||||
|
||||
# Common gcovr options
|
||||
GCOVR_OPTS="--filter src --root ."
|
||||
|
||||
print_usage() {
|
||||
echo "Usage: $0 [options]"
|
||||
echo ""
|
||||
echo "Coverage Report Generator - generates coverage reports from existing coverage data"
|
||||
echo ""
|
||||
echo "Prerequisites:"
|
||||
echo " Build with coverage enabled first:"
|
||||
echo " ./scripts/build.sh debug --coverage --test"
|
||||
echo ""
|
||||
echo "Format Options (if none specified, all formats are generated):"
|
||||
echo " --html Generate HTML coverage report"
|
||||
echo " --xml Generate XML coverage report (for CI/CD)"
|
||||
echo " --text Generate text coverage report (terminal output)"
|
||||
echo ""
|
||||
echo "Build Options:"
|
||||
echo " --builddir DIR Use custom build directory (default: builddir-debug)"
|
||||
echo ""
|
||||
echo "Other Options:"
|
||||
echo " -h, --help Show this help message"
|
||||
echo ""
|
||||
echo "Examples:"
|
||||
echo " $0 # Generate all formats (html, xml, text)"
|
||||
echo " $0 --html # Generate only HTML report"
|
||||
echo " $0 --html --text # Generate HTML and text reports"
|
||||
echo " $0 --xml # Generate XML for CI/CD"
|
||||
echo " $0 --builddir builddir # Use release build directory"
|
||||
}
|
||||
|
||||
# Parse arguments
|
||||
# If no format arguments provided, enable all
|
||||
if [[ $# -eq 0 ]]; then
|
||||
GENERATE_HTML=true
|
||||
GENERATE_XML=true
|
||||
GENERATE_TEXT=true
|
||||
fi
|
||||
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case $1 in
|
||||
--html)
|
||||
GENERATE_HTML=true
|
||||
shift
|
||||
;;
|
||||
--xml)
|
||||
GENERATE_XML=true
|
||||
shift
|
||||
;;
|
||||
--text)
|
||||
GENERATE_TEXT=true
|
||||
shift
|
||||
;;
|
||||
--builddir)
|
||||
BUILDDIR="$2"
|
||||
shift 2
|
||||
;;
|
||||
-h|--help)
|
||||
print_usage
|
||||
exit 0
|
||||
;;
|
||||
*)
|
||||
echo -e "${RED}Error: Unknown option: $1${NC}"
|
||||
print_usage
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
# Check if any format was selected when arguments were provided
|
||||
if [[ $GENERATE_HTML == false && $GENERATE_XML == false && $GENERATE_TEXT == false ]]; then
|
||||
echo -e "${RED}Error: No output format specified. Use --html, --xml, and/or --text${NC}"
|
||||
print_usage
|
||||
exit 1
|
||||
fi
|
||||
|
||||
cd "${PROJECT_ROOT}"
|
||||
|
||||
# Check if build directory exists
|
||||
if [[ ! -d "${BUILDDIR}" ]]; then
|
||||
echo -e "${RED}Error: Build directory '${BUILDDIR}' does not exist${NC}"
|
||||
echo ""
|
||||
echo "Build with coverage enabled first:"
|
||||
echo " ./scripts/build.sh debug --coverage --test"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Check if coverage data exists by looking for .gcda files
|
||||
if ! find "${BUILDDIR}" -name "*.gcda" -print -quit | grep -q .; then
|
||||
echo -e "${RED}Error: No coverage data found in '${BUILDDIR}'${NC}"
|
||||
echo ""
|
||||
echo "Make sure you built with coverage enabled and ran the tests:"
|
||||
echo " ./scripts/build.sh debug --coverage --test"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo -e "${BLUE}Generating Coverage report for ptprnt${NC}"
|
||||
echo "Build directory: ${BUILDDIR}"
|
||||
echo "Formats: $(${GENERATE_HTML} && echo -n "html ")$(${GENERATE_XML} && echo -n "xml ")$(${GENERATE_TEXT} && echo -n "text")"
|
||||
echo ""
|
||||
|
||||
# Check if gcovr is available
|
||||
if ! command -v gcovr &> /dev/null; then
|
||||
echo -e "${RED}Error: gcovr is not installed${NC}"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [[ "${GENERATE_HTML}" == true ]]; then
|
||||
echo -e "${YELLOW}Generating HTML coverage report...${NC}"
|
||||
mkdir -p "${HTML_COV_PATH}"
|
||||
gcovr ${GCOVR_OPTS} \
|
||||
--html --html-details --html-syntax-highlighting \
|
||||
--output "${HTML_COV_PATH}/${HTML_START_FILE}"
|
||||
|
||||
if [[ $? -eq 0 ]]; then
|
||||
echo -e "${GREEN}✓ HTML report generated${NC}"
|
||||
echo ""
|
||||
echo -e "${BLUE}To view HTML report, open:${NC}"
|
||||
echo " file://${SCRIPT_PATH}/../${HTML_COV_PATH}/${HTML_START_FILE}"
|
||||
|
||||
else
|
||||
echo -e "${RED}✗ HTML report failed${NC}"
|
||||
fi
|
||||
fi
|
||||
|
||||
if [[ "${GENERATE_XML}" == true ]]; then
|
||||
echo -e "${YELLOW}Generating XML coverage report...${NC}"
|
||||
mkdir -p "${XML_COV_PATH}"
|
||||
gcovr ${GCOVR_OPTS} \
|
||||
--xml-pretty \
|
||||
--output "${XML_COV_PATH}/cov.xml"
|
||||
|
||||
if [[ $? -eq 0 ]]; then
|
||||
echo -e "${GREEN}✓ XML report generated${NC}"
|
||||
echo ""
|
||||
echo -e "${BLUE}To view XML report, open:${NC}"
|
||||
echo " file://${SCRIPT_PATH}/../${XML_COV_PATH}/cov.xml"
|
||||
else
|
||||
echo -e "${RED}✗ XML report failed${NC}"
|
||||
fi
|
||||
fi
|
||||
|
||||
if [[ "${GENERATE_TEXT}" == true ]]; then
|
||||
echo -e "${YELLOW}Generating text coverage report...${NC}"
|
||||
mkdir -p "${TEXT_COV_PATH}"
|
||||
|
||||
# Save to file
|
||||
gcovr ${GCOVR_OPTS} --output "${TEXT_COV_PATH}/coverage.txt"
|
||||
|
||||
if [[ $? -eq 0 ]]; then
|
||||
echo -e "${GREEN}✓ Text report generated${NC}"
|
||||
echo ""
|
||||
echo -e "${BLUE}To view TXT report, open:${NC}"
|
||||
echo "file://${SCRIPT_PATH}/../${TEXT_COV_PATH}/coverage.txt"
|
||||
else
|
||||
echo -e "${RED}✗ Text report failed${NC}"
|
||||
fi
|
||||
fi
|
||||
|
||||
# Clean up gcov files
|
||||
rm -f *.gcov 2>/dev/null
|
||||
|
||||
# Summary
|
||||
echo ""
|
||||
echo -e "${GREEN}Coverage reports generated successfully!${NC}"
|
||||
echo ""
|
||||
@@ -18,8 +18,7 @@
|
||||
*/
|
||||
#include "PtouchPrint.hpp"
|
||||
|
||||
#include <format>
|
||||
#include <iostream>
|
||||
#include <fmt/core.h>
|
||||
#include <spdlog/sinks/basic_file_sink.h>
|
||||
#include <spdlog/sinks/stdout_color_sinks.h>
|
||||
#include <spdlog/spdlog.h>
|
||||
@@ -105,11 +104,11 @@ bool PtouchPrint::handleListDrivers() {
|
||||
auto driverFactory = std::make_unique<PrinterDriverFactory>();
|
||||
auto drivers = driverFactory->listAllDrivers();
|
||||
|
||||
std::cout << "Available printer drivers:\n";
|
||||
fmt::print("Available printer drivers:\n");
|
||||
for (const auto& driver : drivers) {
|
||||
std::cout << std::format(" - {}\n", driver);
|
||||
fmt::print(" - {}\n", driver);
|
||||
}
|
||||
std::cout << "\nUse with: -p <driver_name> or --printer <driver_name>\n";
|
||||
fmt::print("\nUse with: -p <driver_name> or --printer <driver_name>\n");
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -19,8 +19,7 @@
|
||||
|
||||
#include "CliParser.hpp"
|
||||
|
||||
#include <format>
|
||||
#include <iostream>
|
||||
#include <fmt/core.h>
|
||||
|
||||
namespace ptprnt::cli {
|
||||
|
||||
@@ -93,7 +92,7 @@ void CliParser::reorderCommandsByArgv(int argc, char** argv) {
|
||||
void CliParser::setupParser() {
|
||||
// Version callback
|
||||
auto printVersion = [this](std::size_t) {
|
||||
std::cout << std::format("ptprnt version: {}\n", mVersionString);
|
||||
fmt::print("ptprnt version: {}\n", mVersionString);
|
||||
throw CLI::CallForVersion();
|
||||
};
|
||||
|
||||
|
||||
@@ -25,7 +25,6 @@
|
||||
#include <cassert>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
@@ -275,7 +274,7 @@ bool Label::append(const ILabel& other, uint32_t spacingPx) {
|
||||
int newStride = mCairoWrapper->cairo_image_surface_get_stride(newSurface.get());
|
||||
|
||||
// Clear the new surface (set to transparent/white)
|
||||
memset(newData, 0x00, newStride * height);
|
||||
std::memset(newData, 0x00, newStride * height);
|
||||
|
||||
// Copy current label data
|
||||
for (int y = 0; y < height; ++y) {
|
||||
|
||||
@@ -90,11 +90,6 @@ libusbwrap::usbId P700Printer::getUsbId() {
|
||||
}
|
||||
|
||||
bool P700Printer::attachUsbDevice(std::shared_ptr<libusbwrap::IUsbDevice> usbHndl) {
|
||||
if (!usbHndl) {
|
||||
spdlog::error("Cannot attach null USB device");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!usbHndl->open()) {
|
||||
spdlog::error("Unable to open USB device: {}", usbHndl->getLastErrorString());
|
||||
return false;
|
||||
@@ -187,11 +182,6 @@ bool P700Printer::printMonochromeData(const graphics::MonochromeData& data) {
|
||||
}
|
||||
|
||||
bool P700Printer::printLabel(std::unique_ptr<graphics::ILabel> label) {
|
||||
if (!label) {
|
||||
spdlog::error("Cannot print null label");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Convert label directly to MonochromeData
|
||||
// getRaw() returns data in Cairo surface coordinates matching getWidth() × getHeight()
|
||||
auto pixels = label->getRaw();
|
||||
@@ -210,15 +200,9 @@ bool P700Printer::printLabel(std::unique_ptr<graphics::ILabel> label) {
|
||||
}
|
||||
|
||||
bool P700Printer::print() {
|
||||
if (!send(p700::commands::LF)) {
|
||||
return false;
|
||||
}
|
||||
if (!send(p700::commands::FF)) {
|
||||
return false;
|
||||
}
|
||||
if (!send(p700::commands::EJECT)) {
|
||||
return false;
|
||||
}
|
||||
send(p700::commands::LF);
|
||||
send(p700::commands::FF);
|
||||
send(p700::commands::EJECT);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
[wrap-file]
|
||||
directory = spdlog-1.15.3
|
||||
source_url = https://github.com/gabime/spdlog/archive/refs/tags/v1.15.3.tar.gz
|
||||
source_filename = spdlog-1.15.3.tar.gz
|
||||
source_hash = 15a04e69c222eb6c01094b5c7ff8a249b36bb22788d72519646fb85feb267e67
|
||||
source_fallback_url = https://github.com/mesonbuild/wrapdb/releases/download/spdlog_1.15.3-5/spdlog-1.15.3.tar.gz
|
||||
patch_filename = spdlog_1.15.3-5_patch.zip
|
||||
patch_url = https://wrapdb.mesonbuild.com/v2/spdlog_1.15.3-5/get_patch
|
||||
patch_hash = 5e0eaf0002ff589cd8dac58e1b38c297422e7a0404d7d47ff0d2e285ed18169c
|
||||
wrapdb_version = 1.15.3-5
|
||||
|
||||
[provide]
|
||||
dependency_names = spdlog
|
||||
1
test_copyright.cpp
Normal file
1
test_copyright.cpp
Normal file
@@ -0,0 +1 @@
|
||||
# Test file
|
||||
@@ -25,8 +25,8 @@
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "../../tests/mocks/MockCairoWrapper.hpp"
|
||||
#include "graphics/interface/ILabel.hpp"
|
||||
#include "mocks/MockCairoWrapper.hpp"
|
||||
|
||||
using ::testing::_;
|
||||
using ::testing::DoAll;
|
||||
|
||||
@@ -1,43 +1,51 @@
|
||||
# Consolidated test binary - all tests in one executable for faster linking
|
||||
|
||||
test_sources = [
|
||||
# Test files
|
||||
'bitmap_test/bitmap_test.cpp',
|
||||
'monochrome_test/monochrome_test.cpp',
|
||||
'label_test/label_test.cpp',
|
||||
'printer_service_test/printer_service_test.cpp',
|
||||
'p700_printer_test/p700_printer_test.cpp',
|
||||
|
||||
# Source files under test - graphics
|
||||
'../src/graphics/Bitmap.cpp',
|
||||
'../src/graphics/Monochrome.cpp',
|
||||
'../src/graphics/Label.cpp',
|
||||
|
||||
# Source files under test - core
|
||||
'../src/core/PrinterService.cpp',
|
||||
'../src/core/PrinterDriverFactory.cpp',
|
||||
|
||||
# Source files under test - printers
|
||||
'../src/printers/P700Printer.cpp',
|
||||
'../src/printers/FakePrinter.cpp',
|
||||
|
||||
# Source files under test - USB
|
||||
'../src/libusbwrap/UsbDevice.cpp',
|
||||
'../src/libusbwrap/UsbDeviceFactory.cpp',
|
||||
tests = [
|
||||
[
|
||||
'bitmap_test',
|
||||
'bitmap_test_exe',
|
||||
['../src/graphics/Bitmap.cpp', 'bitmap_test/bitmap_test.cpp'],
|
||||
],
|
||||
[
|
||||
'monochrome_test',
|
||||
'monochrome_test_exe',
|
||||
[
|
||||
'../src/graphics/Monochrome.cpp',
|
||||
'monochrome_test/monochrome_test.cpp',
|
||||
],
|
||||
],
|
||||
]
|
||||
|
||||
test_exe = executable(
|
||||
'ptprnt_tests',
|
||||
sources: test_sources,
|
||||
include_directories: incdir,
|
||||
dependencies: [
|
||||
gmock_dep, # GMock includes GTest
|
||||
usb_dep,
|
||||
log_dep,
|
||||
pangocairo_dep,
|
||||
cli11_dep,
|
||||
],
|
||||
)
|
||||
foreach test : tests
|
||||
test(
|
||||
test.get(0),
|
||||
executable(
|
||||
test.get(1),
|
||||
sources: test.get(2),
|
||||
include_directories: incdir,
|
||||
dependencies: [
|
||||
gtest_dep,
|
||||
usb_dep,
|
||||
log_dep,
|
||||
pangocairo_dep,
|
||||
cli11_dep,
|
||||
],
|
||||
),
|
||||
)
|
||||
endforeach
|
||||
|
||||
# Single test that runs all test suites
|
||||
test('all_tests', test_exe)
|
||||
# Label test requires GMock for mocking Cairo/Pango
|
||||
test(
|
||||
'label_test',
|
||||
executable(
|
||||
'label_test_exe',
|
||||
sources: ['../src/graphics/Label.cpp', 'label_test/label_test.cpp'],
|
||||
include_directories: incdir,
|
||||
dependencies: [
|
||||
gmock_dep,
|
||||
gtest_dep,
|
||||
usb_dep,
|
||||
log_dep,
|
||||
pangocairo_dep,
|
||||
cli11_dep,
|
||||
],
|
||||
),
|
||||
)
|
||||
@@ -1,50 +0,0 @@
|
||||
/*
|
||||
ptrnt - print labels on linux
|
||||
Copyright (C) 2025 Moritz Martinius
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <gmock/gmock.h>
|
||||
|
||||
#include "printers/interface/IPrinterDriver.hpp"
|
||||
|
||||
namespace ptprnt {
|
||||
|
||||
/**
|
||||
* @brief GMock implementation of IPrinterDriver for unit testing
|
||||
*
|
||||
* This mock allows tests to verify printer interactions without
|
||||
* requiring actual printer hardware.
|
||||
*/
|
||||
class MockPrinterDriver : public IPrinterDriver {
|
||||
public:
|
||||
MOCK_METHOD(std::string_view, getDriverName, (), (override));
|
||||
MOCK_METHOD(std::string_view, getName, (), (override));
|
||||
MOCK_METHOD(std::string_view, getVersion, (), (override));
|
||||
MOCK_METHOD(libusbwrap::usbId, getUsbId, (), (override));
|
||||
MOCK_METHOD(PrinterInfo, getPrinterInfo, (), (override));
|
||||
MOCK_METHOD(PrinterStatus, getPrinterStatus, (), (override));
|
||||
MOCK_METHOD(bool, attachUsbDevice, (std::shared_ptr<libusbwrap::IUsbDevice> usbHndl), (override));
|
||||
MOCK_METHOD(bool, detachUsbDevice, (), (override));
|
||||
MOCK_METHOD(bool, printBitmap, (const graphics::Bitmap<graphics::ALPHA8>& bitmap), (override));
|
||||
MOCK_METHOD(bool, printMonochromeData, (const graphics::MonochromeData& data), (override));
|
||||
MOCK_METHOD(bool, printLabel, (const std::unique_ptr<graphics::ILabel> label), (override));
|
||||
MOCK_METHOD(bool, print, (), (override));
|
||||
};
|
||||
|
||||
} // namespace ptprnt
|
||||
@@ -1,57 +0,0 @@
|
||||
/*
|
||||
ptrnt - print labels on linux
|
||||
Copyright (C) 2025 Moritz Martinius
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <gmock/gmock.h>
|
||||
|
||||
#include "libusbwrap/interface/IUsbDevice.hpp"
|
||||
|
||||
namespace libusbwrap {
|
||||
|
||||
/**
|
||||
* @brief GMock implementation of IUsbDevice for unit testing
|
||||
*
|
||||
* This mock allows tests to verify USB device interactions without
|
||||
* requiring actual hardware or libusb context.
|
||||
*/
|
||||
class MockUsbDevice : public IUsbDevice {
|
||||
public:
|
||||
MOCK_METHOD(bool, open, (), (override));
|
||||
MOCK_METHOD(void, close, (), (override));
|
||||
|
||||
// libusb wrappers
|
||||
MOCK_METHOD(bool, detachKernelDriver, (int interfaceNo), (override));
|
||||
MOCK_METHOD(bool, claimInterface, (int interfaceNo), (override));
|
||||
MOCK_METHOD(bool, releaseInterface, (int interfaceNo), (override));
|
||||
MOCK_METHOD(bool, bulkTransfer,
|
||||
(uint8_t endpoint, const std::vector<uint8_t>& data, int* tx, unsigned int timeout), (override));
|
||||
|
||||
// getters
|
||||
MOCK_METHOD(const usbId, getUsbId, (), (override));
|
||||
MOCK_METHOD(const device::Speed, getSpeed, (), (override));
|
||||
MOCK_METHOD(const uint8_t, getBusNumber, (), (override));
|
||||
MOCK_METHOD(const uint8_t, getPortNumber, (), (override));
|
||||
|
||||
// errors
|
||||
MOCK_METHOD(const Error, getLastError, (), (override));
|
||||
MOCK_METHOD(const std::string, getLastErrorString, (), (override));
|
||||
};
|
||||
|
||||
} // namespace libusbwrap
|
||||
@@ -1,40 +0,0 @@
|
||||
/*
|
||||
ptrnt - print labels on linux
|
||||
Copyright (C) 2025 Moritz Martinius
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <gmock/gmock.h>
|
||||
|
||||
#include "libusbwrap/interface/IUsbDeviceFactory.hpp"
|
||||
|
||||
namespace libusbwrap {
|
||||
|
||||
/**
|
||||
* @brief GMock implementation of IUsbDeviceFactory for unit testing
|
||||
*
|
||||
* This mock allows tests to control USB device discovery without
|
||||
* requiring actual libusb context or hardware.
|
||||
*/
|
||||
class MockUsbDeviceFactory : public IUsbDeviceFactory {
|
||||
public:
|
||||
MOCK_METHOD(std::vector<std::unique_ptr<IUsbDevice>>, findAllDevices, (), (override));
|
||||
MOCK_METHOD(std::vector<std::unique_ptr<IUsbDevice>>, findDevices, (uint16_t vid, uint16_t pid), (override));
|
||||
};
|
||||
|
||||
} // namespace libusbwrap
|
||||
@@ -57,6 +57,9 @@ TEST(basic_test, Monochrome_convertWithCustomThreshhold_yieldsMonochromeRespecti
|
||||
}
|
||||
|
||||
TEST(basic_test, Monochrome_convertNonAlignedPixels_spillsOverIntoNewByte) {
|
||||
// TODO: We need to find to access the vector without the possiblity of out-of-bounds access
|
||||
// Ideas: constexpr? compile time check?
|
||||
GTEST_SKIP() << "Skipping this test, as ASAN will halt as this is an out-of-bounds access";
|
||||
const std::vector<uint8_t> pixels(
|
||||
{0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF});
|
||||
|
||||
|
||||
@@ -1,169 +0,0 @@
|
||||
/*
|
||||
ptrnt - print labels on linux
|
||||
Copyright (C) 2025 Moritz Martinius
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
#include <gmock/gmock.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "graphics/Bitmap.hpp"
|
||||
#include "graphics/Monochrome.hpp"
|
||||
#include "mocks/MockUsbDevice.hpp"
|
||||
#include "printers/P700Printer.hpp"
|
||||
|
||||
using ::testing::_;
|
||||
using ::testing::DoAll;
|
||||
using ::testing::NiceMock;
|
||||
using ::testing::Return;
|
||||
using ::testing::SetArgPointee;
|
||||
|
||||
namespace ptprnt::printer {
|
||||
|
||||
// Test fixture for P700Printer tests
|
||||
class P700PrinterTest : public ::testing::Test {
|
||||
protected:
|
||||
void SetUp() override {
|
||||
printer = std::make_unique<P700Printer>();
|
||||
mockUsbDev = std::make_shared<NiceMock<libusbwrap::MockUsbDevice>>();
|
||||
|
||||
// Default mock behaviors
|
||||
ON_CALL(*mockUsbDev, open()).WillByDefault(Return(true));
|
||||
ON_CALL(*mockUsbDev, close()).WillByDefault(Return());
|
||||
ON_CALL(*mockUsbDev, detachKernelDriver(_)).WillByDefault(Return(true));
|
||||
ON_CALL(*mockUsbDev, claimInterface(_)).WillByDefault(Return(true));
|
||||
ON_CALL(*mockUsbDev, releaseInterface(_)).WillByDefault(Return(true));
|
||||
ON_CALL(*mockUsbDev, bulkTransfer(_, _, _, _)).WillByDefault(Return(true));
|
||||
ON_CALL(*mockUsbDev, getUsbId()).WillByDefault(Return(libusbwrap::usbId{0x04f9, 0x2061}));
|
||||
}
|
||||
|
||||
void TearDown() override { printer.reset(); }
|
||||
|
||||
std::unique_ptr<P700Printer> printer;
|
||||
std::shared_ptr<libusbwrap::MockUsbDevice> mockUsbDev;
|
||||
};
|
||||
|
||||
// Test: Get printer driver name
|
||||
TEST_F(P700PrinterTest, GetDriverName) {
|
||||
EXPECT_EQ(printer->getDriverName(), "P700");
|
||||
}
|
||||
|
||||
// Test: Get printer name
|
||||
TEST_F(P700PrinterTest, GetName) {
|
||||
auto name = printer->getName();
|
||||
EXPECT_FALSE(name.empty());
|
||||
}
|
||||
|
||||
// Test: Get USB ID
|
||||
TEST_F(P700PrinterTest, GetUsbId) {
|
||||
auto usbId = printer->getUsbId();
|
||||
EXPECT_EQ(usbId.first, 0x04f9); // Brother VID
|
||||
EXPECT_EQ(usbId.second, 0x2061); // P700 PID
|
||||
}
|
||||
|
||||
// Test: Get printer version
|
||||
TEST_F(P700PrinterTest, GetVersion) {
|
||||
auto version = printer->getVersion();
|
||||
EXPECT_FALSE(version.empty());
|
||||
}
|
||||
|
||||
// Test: Get printer info
|
||||
TEST_F(P700PrinterTest, GetPrinterInfo) {
|
||||
auto info = printer->getPrinterInfo();
|
||||
EXPECT_EQ(info.pixelLines, 128);
|
||||
EXPECT_FALSE(info.name.empty());
|
||||
}
|
||||
|
||||
// Test: Attach USB device
|
||||
TEST_F(P700PrinterTest, AttachUsbDevice) {
|
||||
bool result = printer->attachUsbDevice(mockUsbDev);
|
||||
|
||||
EXPECT_TRUE(result);
|
||||
}
|
||||
|
||||
// Test: Attach USB device with null pointer
|
||||
TEST_F(P700PrinterTest, AttachNullUsbDevice) {
|
||||
bool result = printer->attachUsbDevice(nullptr);
|
||||
|
||||
EXPECT_FALSE(result);
|
||||
}
|
||||
|
||||
// Test: Detach USB device
|
||||
TEST_F(P700PrinterTest, DetachUsbDevice) {
|
||||
printer->attachUsbDevice(mockUsbDev);
|
||||
|
||||
bool result = printer->detachUsbDevice();
|
||||
|
||||
EXPECT_TRUE(result);
|
||||
}
|
||||
|
||||
// Test: Detach when no device attached
|
||||
TEST_F(P700PrinterTest, DetachNoDevice) {
|
||||
bool result = printer->detachUsbDevice();
|
||||
|
||||
// Production code returns true when no device is attached (just logs warning)
|
||||
EXPECT_TRUE(result);
|
||||
}
|
||||
|
||||
// Test: Get printer status without device
|
||||
TEST_F(P700PrinterTest, GetStatusNoDevice) {
|
||||
auto status = printer->getPrinterStatus();
|
||||
|
||||
EXPECT_EQ(status.tapeWidthPixel, 0); // Should be 0 when no device
|
||||
}
|
||||
|
||||
// Test: Print without attached device
|
||||
TEST_F(P700PrinterTest, PrintWithoutDevice) {
|
||||
bool result = printer->print();
|
||||
|
||||
// Should fail when no device is attached
|
||||
EXPECT_FALSE(result);
|
||||
}
|
||||
|
||||
// Test: Print bitmap without device
|
||||
TEST_F(P700PrinterTest, PrintBitmapWithoutDevice) {
|
||||
graphics::Bitmap<graphics::ALPHA8> bitmap(10, 10);
|
||||
|
||||
bool result = printer->printBitmap(bitmap);
|
||||
|
||||
EXPECT_FALSE(result);
|
||||
}
|
||||
|
||||
// Test: Print monochrome data without device
|
||||
TEST_F(P700PrinterTest, PrintMonochromeDataWithoutDevice) {
|
||||
graphics::MonochromeData data;
|
||||
data.width = 10;
|
||||
data.height = 10;
|
||||
data.bytes = std::vector<uint8_t>(20, 0xFF);
|
||||
data.stride = 2;
|
||||
|
||||
bool result = printer->printMonochromeData(data);
|
||||
|
||||
EXPECT_FALSE(result);
|
||||
}
|
||||
|
||||
// Test: Print label with null pointer
|
||||
TEST_F(P700PrinterTest, PrintNullLabel) {
|
||||
std::unique_ptr<graphics::ILabel> label = nullptr;
|
||||
|
||||
bool result = printer->printLabel(std::move(label));
|
||||
|
||||
EXPECT_FALSE(result);
|
||||
}
|
||||
|
||||
} // namespace ptprnt::printer
|
||||
@@ -1,114 +0,0 @@
|
||||
/*
|
||||
ptrnt - print labels on linux
|
||||
Copyright (C) 2025 Moritz Martinius
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
#include <gmock/gmock.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "core/PrinterDriverFactory.hpp"
|
||||
#include "core/PrinterService.hpp"
|
||||
|
||||
using ::testing::_;
|
||||
using ::testing::NiceMock;
|
||||
using ::testing::Return;
|
||||
|
||||
namespace ptprnt::core {
|
||||
|
||||
// Test fixture for PrinterService tests
|
||||
class PrinterServiceTest : public ::testing::Test {
|
||||
protected:
|
||||
void SetUp() override {
|
||||
// Service under test
|
||||
service = std::make_unique<PrinterService>();
|
||||
}
|
||||
|
||||
void TearDown() override { service.reset(); }
|
||||
|
||||
std::unique_ptr<PrinterService> service;
|
||||
};
|
||||
|
||||
// Test: PrinterService initialization
|
||||
TEST_F(PrinterServiceTest, InitializeSuccess) {
|
||||
// PrinterService::initialize() calls UsbDeviceFactory::init()
|
||||
// This will attempt to initialize libusb - we can't easily mock this
|
||||
// without dependency injection, but we can test the call succeeds
|
||||
// when libusb is available
|
||||
EXPECT_TRUE(service->initialize());
|
||||
}
|
||||
|
||||
// Test: Detect printers when none are connected
|
||||
TEST_F(PrinterServiceTest, DetectPrintersNoneFound) {
|
||||
service->initialize();
|
||||
|
||||
auto printers = service->detectPrinters();
|
||||
|
||||
// With no compatible USB devices, we should get an empty list
|
||||
// (This depends on actual USB devices present, so it might find real hardware)
|
||||
// In a real test environment without hardware, this should be empty
|
||||
EXPECT_GE(printers.size(), 0); // Non-negative count
|
||||
}
|
||||
|
||||
// Test: Select printer with auto-detect
|
||||
TEST_F(PrinterServiceTest, SelectPrinterAuto) {
|
||||
service->initialize();
|
||||
service->detectPrinters();
|
||||
|
||||
auto printer = service->selectPrinter("auto");
|
||||
|
||||
// This will be nullptr if no printers detected
|
||||
// In test environment without hardware, expect nullptr
|
||||
// (Test passes either way - just exercises the code path)
|
||||
if (printer != nullptr) {
|
||||
EXPECT_NE(printer, nullptr);
|
||||
EXPECT_EQ(service->getCurrentPrinter(), printer);
|
||||
} else {
|
||||
EXPECT_EQ(printer, nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
// Test: Select non-existent printer
|
||||
TEST_F(PrinterServiceTest, SelectPrinterNotFound) {
|
||||
service->initialize();
|
||||
service->detectPrinters();
|
||||
|
||||
auto printer = service->selectPrinter("NonExistentPrinter");
|
||||
|
||||
EXPECT_EQ(printer, nullptr);
|
||||
EXPECT_EQ(service->getCurrentPrinter(), nullptr);
|
||||
}
|
||||
|
||||
// Test: Get current printer when none selected
|
||||
TEST_F(PrinterServiceTest, GetCurrentPrinterNoneSelected) {
|
||||
EXPECT_EQ(service->getCurrentPrinter(), nullptr);
|
||||
}
|
||||
|
||||
// Test: Print label without selecting printer
|
||||
TEST_F(PrinterServiceTest, PrintLabelNoPrinterSelected) {
|
||||
// Create a simple label (we need a valid ILabel pointer)
|
||||
std::unique_ptr<graphics::ILabel> label = nullptr; // nullptr label for this test
|
||||
|
||||
bool result = service->printLabel(std::move(label));
|
||||
|
||||
// Should fail because no printer is selected
|
||||
EXPECT_FALSE(result);
|
||||
}
|
||||
|
||||
} // namespace ptprnt::core
|
||||
Reference in New Issue
Block a user