-
Notifications
You must be signed in to change notification settings - Fork 0
/
example-report.cpp
200 lines (170 loc) · 3.63 KB
/
example-report.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include <iostream>
#include <optional>
#include <sstream>
#include <string>
#include <catch2/catch_test_macros.hpp>
#include <cray.hpp>
#include "testing.hpp"
struct Step {
std::string name;
std::string run;
};
struct Job {
std::vector<std::string> runs_on;
std::vector<Step> steps;
};
TEST_CASE("example-yaml") {
using namespace cray;
constexpr auto* data = R"(
name: Test
on: [push, pull_request]
jobs:
test:
runs-on: [ubuntu-20.04]
steps:
- name: Test
run: build && test
)";
std::stringstream in(data);
Node doc(load::fromYaml(in));
auto const name = doc["name"].as<std::string>(Annotation{
.title = "Name",
.description = "The name of your workflow.",
});
auto const run_name = doc["run-name"].as<std::optional<std::string>>(Annotation{
.title = "Run Name",
.description = "The name for workflow runs generated from the workflow.",
});
auto events = prop<Type::Str>().oneOf({
"push",
"pull_request",
"workflow_dispatch",
});
auto const on = doc["on"].is<Type::List>().of(events).get();
auto step =
prop<Type::Map>().to<Step>()
| field("name", &Step::name)
| field("run", &Step::run);
auto job =
prop<Type::Map>().to<Job>()
| field("runs-on", &Job::runs_on)
| field("steps", &Job::steps, step);
auto const jobs = doc["jobs"].is<Type::Map>().of(job).get();
REQUIRE(doc.ok());
{
std::stringstream out;
out << std::endl;
report::asYaml(out, doc);
out << std::endl;
constexpr auto* expected = R"(
---
# Name
# | The name of your workflow.
name: Test
# Run Name
# | The name for workflow runs generated from the workflow.
run-name: # <String>
on:
# • push | pull_request | workflow_dispatch
[push, pull_request]
jobs:
test:
runs-on: [ubuntu-20.04]
steps:
-
name: Test
run: build && test
)";
testing::requireReportEq(expected, std::move(out).str());
}
{
std::stringstream out;
out << std::endl;
report::asJsonSchema(out, doc);
out << std::endl;
constexpr auto* expected = R"(
{
"type": "object",
"required": [
"name",
"on",
"jobs"
],
"properties": {
"name": {
"type": "string",
"title": "Name",
"description": "The name of your workflow."
},
"run-name": {
"type": "string",
"title": "Run Name",
"description": "The name for workflow runs generated from the workflow."
},
"on": {
"type": "array",
"items": {
"type": "string",
"enum": [
"push",
"pull_request",
"workflow_dispatch"
]
}
},
"jobs": {
"type": "object",
"additionalProperties": {
"type": "object",
"required": [
"runs-on",
"steps"
],
"properties": {
"runs-on": {
"type": "array",
"items": {
"type": "string"
}
},
"steps": {
"type": "array",
"items": {
"type": "object",
"required": [
"name",
"run"
],
"properties": {
"name": {
"type": "string"
},
"run": {
"type": "string"
}
}
}
}
}
}
}
}
}
)";
testing::requireReportEq(expected, std::move(out).str());
}
REQUIRE("Test" == name);
REQUIRE(!run_name.has_value());
REQUIRE(2 == on.size());
REQUIRE("push" == on[0]);
REQUIRE("pull_request" == on[1]);
REQUIRE(1 == jobs.size());
REQUIRE(jobs.contains("test"));
auto const job_test = jobs.at("test");
REQUIRE(job_test.runs_on.size());
REQUIRE("ubuntu-20.04" == job_test.runs_on[0]);
REQUIRE(1 == job_test.steps.size());
auto const step_test = job_test.steps[0];
REQUIRE("Test" == step_test.name);
REQUIRE("build && test" == step_test.run);
}