Skip to content

Commit 83ce76e

Browse files
committed
Add an example to demonstrate usage, and fix artifact name
1 parent fb9b321 commit 83ce76e

File tree

7 files changed

+100
-7
lines changed

7 files changed

+100
-7
lines changed

README.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,25 @@ Provides a package to be used by the zig package manager for C++ programs.
44

55
## Status
66

7-
| Refname | Catch2 version | Zig `0.12.x` | Zig `0.13.x` | Zig `0.14.0-dev` |
8-
|:--------|:---------------|:------------:|:------------:|:----------------:|
9-
| `3.7.1` | `v3.7.1` ||||
7+
| Refname | Catch2 version | Zig `0.12.x` | Zig `0.13.x` | Zig `0.14.0-dev` |
8+
|:----------|:---------------|:------------:|:------------:|:----------------:|
9+
| `3.7.1+1` | `v3.7.1` ||||
1010

1111
## Use
1212

1313
Add the dependency in your `build.zig.zon` by running the following command:
1414
```bash
15-
zig fetch --save git+https://github.com/allyourcodebase/catch2#3.7.1
15+
zig fetch --save git+https://github.com/allyourcodebase/catch2#3.7.1+1
1616
```
1717

1818
Then, in your `build.zig`:
1919
```zig
2020
const catch2_dep = b.dependency("catch2", { .target = target, .optimize = optimize });
21-
const catch2 = catch2_dep.artifact("Catch2"); // or Catch2WithMain
21+
const catch2_lib = catch2_dep.artifact("Catch2");
22+
const catch2_main = catch2_dep.artifact("Catch2WithMain");
2223
// wherever needed:
23-
exe.linkLibrary(catch2);
24+
exe.linkLibrary(catch2_lib);
25+
exe.linkLibrary(catch2_main);
2426
```
27+
28+
A complete usage demonstration is provided in the [example](example) directory

build.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub fn build(b: *std.Build) !void {
2929
},
3030
);
3131

32-
const catch2 = b.addStaticLibrary(.{ .name = "catch2", .target = target, .optimize = optimize });
32+
const catch2 = b.addStaticLibrary(.{ .name = "Catch2", .target = target, .optimize = optimize });
3333
catch2.addCSourceFiles(.{
3434
.root = upstream.path("src/catch2"),
3535
.files = &source_files,

example/build.zig

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
const std = @import("std");
2+
3+
pub fn build(b: *std.Build) !void {
4+
const target = b.standardTargetOptions(.{});
5+
const optimize = b.standardOptimizeOption(.{});
6+
7+
const include = b.path("include");
8+
9+
const lib = b.addStaticLibrary(.{
10+
.name = "demo",
11+
.target = target,
12+
.optimize = optimize,
13+
});
14+
lib.addIncludePath(include);
15+
lib.addCSourceFiles(.{
16+
.root = b.path("src"),
17+
.files = &.{"source.cpp"},
18+
.flags = &CXXFLAGS,
19+
});
20+
lib.linkLibCpp();
21+
lib.installHeadersDirectory(include, "demo", .{ .include_extensions = &.{".hpp"} });
22+
b.installArtifact(lib);
23+
24+
{ // Test
25+
const test_step = b.step("test", "Run tests");
26+
const test_exe = b.addExecutable(.{ .name = "test_demo", .target = target, .optimize = optimize });
27+
const run_test = b.addRunArtifact(test_exe);
28+
29+
const catch2_dep = b.dependency("catch2", .{ .target = target, .optimize = optimize });
30+
const catch2_lib = catch2_dep.artifact("Catch2");
31+
const catch2_main = catch2_dep.artifact("Catch2WithMain");
32+
33+
test_exe.addCSourceFiles(.{ .root = b.path("test"), .files = &.{"test.cpp"}, .flags = &CXXFLAGS });
34+
test_exe.linkLibrary(lib);
35+
test_exe.linkLibrary(catch2_lib);
36+
test_exe.linkLibrary(catch2_main);
37+
test_step.dependOn(&run_test.step);
38+
}
39+
}
40+
41+
const CXXFLAGS = .{
42+
"--std=c++23",
43+
"-Wall",
44+
"-Wextra",
45+
"-Werror",
46+
};

example/build.zig.zon

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
.{
2+
.name = "cath2UsageDemo",
3+
.version = "1.0.0",
4+
.dependencies = .{
5+
.catch2 = .{
6+
.url = "..",
7+
.hash = "1220aef10cc88ccd919517ece637e54cfd7381c91ab9ee79a2d0d8791165ccd6bfff",
8+
},
9+
},
10+
.minimum_zig_version = "0.12.0",
11+
.paths = .{
12+
"build.zig",
13+
"build.zig.zon",
14+
"src",
15+
"include",
16+
"test",
17+
},
18+
}

example/include/header.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include <cstdint> // uintmax_t uint8_t
2+
3+
uintmax_t fibonacci(uint8_t n, uintmax_t f0 = 0, uintmax_t f1 = 1) __attribute__((pure));

example/src/source.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include "header.hpp"
2+
3+
auto fibonacci(uint8_t n, uintmax_t f0, uintmax_t f1) -> uintmax_t
4+
{
5+
switch (n) {
6+
case 0: return f0;
7+
case 1: return f1;
8+
}
9+
return fibonacci(n-1, f1, f0 + f1);
10+
}

example/test/test.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <demo/header.hpp>
2+
3+
#include <catch2/catch_test_macros.hpp>
4+
5+
TEST_CASE("Fibonacci")
6+
{
7+
CHECK(fibonacci(7) == 13);
8+
CHECK(fibonacci(6, 1, 1) == 13);
9+
CHECK(fibonacci(5, 1, 2) == 13);
10+
11+
CHECK(fibonacci(4, 5, 10) == 40);
12+
}

0 commit comments

Comments
 (0)