Skip to content

Commit a09eb40

Browse files
committed
Re-add & refactor tests
Signed-off-by: Sebastian Ziebell <[email protected]>
1 parent 049d69e commit a09eb40

File tree

13 files changed

+166
-250
lines changed

13 files changed

+166
-250
lines changed

cargo-cyclonedx/src/generator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ use cyclonedx_bom::models::metadata::Metadata;
4949
use cyclonedx_bom::models::metadata::MetadataError;
5050
use cyclonedx_bom::models::organization::OrganizationalContact;
5151
use cyclonedx_bom::models::tool::{Tool, Tools};
52-
use cyclonedx_bom::validation::{Validate, ValidationResult};
52+
use cyclonedx_bom::validation::Validate;
5353
use once_cell::sync::Lazy;
5454
use regex::Regex;
5555

cyclonedx-bom/src/external_models/date_time.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* SPDX-License-Identifier: Apache-2.0
1717
*/
1818

19-
use std::{convert::TryFrom, path::StripPrefixError};
19+
use std::convert::TryFrom;
2020

2121
use thiserror::Error;
2222
use time::{format_description::well_known::Iso8601, OffsetDateTime};

cyclonedx-bom/src/models/component.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ impl Validate for Component {
122122
validate_normalized_string,
123123
);
124124
ctx.add_field_option("group", self.group.as_ref(), validate_normalized_string);
125-
ctx.add_field("name", self.name.as_ref(), validate_normalized_string);
125+
ctx.add_field("name", &self.name, validate_normalized_string);
126126
ctx.add_field_option("version", self.version.as_ref(), validate_normalized_string);
127127
ctx.add_field_option(
128128
"description",

cyclonedx-bom/src/models/hash.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub struct Hash {
3333
}
3434

3535
impl Validate for Hash {
36-
fn validate_version(&self, version: SpecVersion) -> ValidationResult {
36+
fn validate_version(&self, _version: SpecVersion) -> ValidationResult {
3737
ValidationContext::new()
3838
.add_field("alg", &self.alg, validate_hash_algorithm)
3939
.add_field("content", &self.content, validate_hash_value)

cyclonedx-bom/src/models/license.rs

Lines changed: 77 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,7 @@ impl License {
110110
impl Validate for License {
111111
fn validate_version(&self, version: SpecVersion) -> ValidationResult {
112112
ValidationContext::new()
113-
.add_field(
114-
"license_identifier",
115-
&self.license_identifier,
116-
validate_license_identifier,
117-
)
113+
.add_struct("license_identifier", &self.license_identifier, version)
118114
.add_struct_option("text", self.text.as_ref(), version)
119115
.add_field_option("url", self.url.as_ref(), validate_uri)
120116
.into()
@@ -154,14 +150,16 @@ impl Validate for LicenseIdentifier {
154150
.add_enum("Name", name, validate_normalized_string)
155151
.into(),
156152
LicenseIdentifier::SpdxId(id) => ValidationContext::new()
157-
.add_field("SpdxId", id, validate_spdx_identifier)
153+
.add_enum("SpdxId", id, validate_spdx_identifier)
158154
.into(),
159155
}
160156
}
161157
}
162158

163159
#[cfg(test)]
164160
mod test {
161+
use crate::validation;
162+
165163
use super::*;
166164
use pretty_assertions::assert_eq;
167165

@@ -186,30 +184,25 @@ mod test {
186184
})])
187185
.validate();
188186

189-
/*
190187
assert_eq!(
191188
validation_result,
192-
ValidationResult::Failed {
193-
reasons: vec![FailureReason {
194-
message: "NormalizedString contains invalid characters \\r \\n \\t or \\r\\n"
195-
.to_string(),
196-
context: ValidationContext(vec![
197-
ValidationPathComponent::Array { index: 0 },
198-
ValidationPathComponent::EnumVariant {
199-
variant_name: "License".to_string()
200-
},
201-
ValidationPathComponent::Struct {
202-
struct_name: "License".to_string(),
203-
field_name: "license_identifier".to_string(),
204-
},
205-
ValidationPathComponent::EnumVariant {
206-
variant_name: "Name".to_string()
207-
},
208-
])
209-
}]
210-
}
189+
validation::list(
190+
"inner",
191+
[(
192+
0,
193+
validation::r#struct(
194+
"license",
195+
validation::r#struct(
196+
"license_identifier",
197+
validation::r#enum(
198+
"Name",
199+
"NormalizedString contains invalid characters \\r \\n \\t or \\r\\n"
200+
)
201+
)
202+
)
203+
)]
204+
)
211205
);
212-
*/
213206
}
214207

215208
#[test]
@@ -221,29 +214,22 @@ mod test {
221214
})])
222215
.validate();
223216

224-
/*
225217
assert_eq!(
226218
validation_result,
227-
ValidationResult::Failed {
228-
reasons: vec![FailureReason {
229-
message: "SPDX identifier is not valid".to_string(),
230-
context: ValidationContext(vec![
231-
ValidationPathComponent::Array { index: 0 },
232-
ValidationPathComponent::EnumVariant {
233-
variant_name: "License".to_string()
234-
},
235-
ValidationPathComponent::Struct {
236-
struct_name: "License".to_string(),
237-
field_name: "license_identifier".to_string(),
238-
},
239-
ValidationPathComponent::EnumVariant {
240-
variant_name: "SpdxId".to_string()
241-
},
242-
])
243-
}]
244-
}
219+
validation::list(
220+
"inner",
221+
[(
222+
0,
223+
validation::r#struct(
224+
"license",
225+
validation::r#struct(
226+
"license_identifier",
227+
validation::r#enum("SpdxId", "SPDX identifier is not valid")
228+
)
229+
)
230+
)]
231+
)
245232
);
246-
*/
247233
}
248234

249235
#[test]
@@ -253,22 +239,16 @@ mod test {
253239
))])
254240
.validate();
255241

256-
/*
257242
assert_eq!(
258243
validation_result,
259-
ValidationResult::Failed {
260-
reasons: vec![FailureReason {
261-
message: "SPDX expression is not valid".to_string(),
262-
context: ValidationContext(vec![
263-
ValidationPathComponent::Array { index: 0 },
264-
ValidationPathComponent::EnumVariant {
265-
variant_name: "Expression".to_string()
266-
}
267-
])
268-
}]
269-
}
244+
validation::list(
245+
"inner",
246+
[(
247+
0,
248+
validation::r#enum("expression", "SPDX expression is not valid")
249+
)],
250+
)
270251
);
271-
*/
272252
}
273253

274254
#[test]
@@ -296,49 +276,34 @@ mod test {
296276
])
297277
.validate();
298278

299-
/*
300279
assert_eq!(
301280
validation_result,
302-
ValidationResult::Failed {
303-
reasons: vec![
304-
FailureReason {
305-
message:
306-
"NormalizedString contains invalid characters \\r \\n \\t or \\r\\n"
307-
.to_string(),
308-
context: ValidationContext(vec![
309-
ValidationPathComponent::Array { index: 1 },
310-
ValidationPathComponent::EnumVariant {
311-
variant_name: "License".to_string()
312-
},
313-
ValidationPathComponent::Struct {
314-
struct_name: "License".to_string(),
315-
field_name: "license_identifier".to_string(),
316-
},
317-
ValidationPathComponent::EnumVariant {
318-
variant_name: "Name".to_string()
319-
},
320-
])
321-
},
322-
FailureReason {
323-
message: "SPDX identifier is not valid".to_string(),
324-
context: ValidationContext(vec![
325-
ValidationPathComponent::Array { index: 2 },
326-
ValidationPathComponent::EnumVariant {
327-
variant_name: "License".to_string()
328-
},
329-
ValidationPathComponent::Struct {
330-
struct_name: "License".to_string(),
331-
field_name: "license_identifier".to_string(),
332-
},
333-
ValidationPathComponent::EnumVariant {
334-
variant_name: "SpdxId".to_string()
335-
},
336-
])
337-
}
338-
]
339-
}
281+
validation::list(
282+
"inner",
283+
[(
284+
1,
285+
validation::r#struct(
286+
"license",
287+
validation::r#struct(
288+
"license_identifier",
289+
validation::r#enum(
290+
"Name",
291+
"NormalizedString contains invalid characters \\r \\n \\t or \\r\\n"
292+
)
293+
)
294+
)
295+
), (
296+
2,
297+
validation::r#struct(
298+
"license",
299+
validation::r#struct(
300+
"license_identifier",
301+
validation::r#enum("SpdxId", "SPDX identifier is not valid")
302+
)
303+
)
304+
)]
305+
)
340306
);
341-
*/
342307
}
343308

344309
#[test]
@@ -350,32 +315,21 @@ mod test {
350315
])
351316
.validate();
352317

353-
/*
354318
assert_eq!(
355319
validation_result,
356-
ValidationResult::Failed {
357-
reasons: vec![
358-
FailureReason {
359-
message: "SPDX expression is not valid".to_string(),
360-
context: ValidationContext(vec![
361-
ValidationPathComponent::Array { index: 1 },
362-
ValidationPathComponent::EnumVariant {
363-
variant_name: "Expression".to_string()
364-
}
365-
])
366-
},
367-
FailureReason {
368-
message: "SPDX expression is not valid".to_string(),
369-
context: ValidationContext(vec![
370-
ValidationPathComponent::Array { index: 2 },
371-
ValidationPathComponent::EnumVariant {
372-
variant_name: "Expression".to_string()
373-
}
374-
])
375-
}
320+
validation::list(
321+
"inner",
322+
[
323+
(
324+
1,
325+
validation::r#enum("expression", "SPDX expression is not valid"),
326+
),
327+
(
328+
2,
329+
validation::r#enum("expression", "SPDX expression is not valid"),
330+
)
376331
]
377-
}
332+
)
378333
);
379-
*/
380334
}
381335
}

0 commit comments

Comments
 (0)