Skip to content

Commit 0c82e6a

Browse files
authored
refactor: move tests to separate files (#52)
tests became a bit hard to manage, so moving them into separate files. The integration tests were easy to move into tests/ dir, but the unit tests should be in a separate file because we don't want to grow public API surface
1 parent 260f514 commit 0c82e6a

File tree

4 files changed

+901
-906
lines changed

4 files changed

+901
-906
lines changed

src/lib.rs

Lines changed: 5 additions & 224 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ use serde::{Deserialize, Serialize};
88

99
pub mod parser;
1010

11+
#[cfg(test)]
12+
mod parser_tests;
13+
1114
/// Re-export of `encoding_rs` as encodings to simplify usage
1215
#[cfg(feature = "encodings")]
1316
pub use encoding_rs as encodings;
@@ -188,8 +191,8 @@ pub enum AttributeValueType {
188191

189192
#[derive(Clone, Debug, PartialEq, Getters, Serialize, Deserialize)]
190193
pub struct ValDescription {
191-
id: f64,
192-
description: String,
194+
pub id: f64,
195+
pub description: String,
193196
}
194197

195198
// FIXME: not used!
@@ -544,225 +547,3 @@ impl<'a> TryFrom<&'a str> for Dbc {
544547
Ok(dbc)
545548
}
546549
}
547-
548-
#[cfg(test)]
549-
mod tests {
550-
551-
use super::*;
552-
553-
const SAMPLE_DBC: &str = r#"
554-
VERSION "0.1"
555-
NS_ :
556-
NS_DESC_
557-
CM_
558-
BA_DEF_
559-
BA_
560-
VAL_
561-
CAT_DEF_
562-
CAT_
563-
FILTER
564-
BA_DEF_DEF_
565-
EV_DATA_
566-
ENVVAR_DATA_
567-
SGTYPE_
568-
SGTYPE_VAL_
569-
BA_DEF_SGTYPE_
570-
BA_SGTYPE_
571-
SIG_TYPE_REF_
572-
VAL_TABLE_
573-
SIG_GROUP_
574-
SIG_VALTYPE_
575-
SIGTYPE_VALTYPE_
576-
BO_TX_BU_
577-
BA_DEF_REL_
578-
BA_REL_
579-
BA_DEF_DEF_REL_
580-
BU_SG_REL_
581-
BU_EV_REL_
582-
BU_BO_REL_
583-
SG_MUL_VAL_
584-
BS_:
585-
BU_: PC
586-
BO_ 2000 WebData_2000: 4 Vector__XXX
587-
SG_ Signal_8 : 24|8@1+ (1,0) [0|255] "" Vector__XXX
588-
SG_ Signal_7 : 16|8@1+ (1,0) [0|255] "" Vector__XXX
589-
SG_ Signal_6 : 8|8@1+ (1,0) [0|255] "" Vector__XXX
590-
SG_ Signal_5 : 0|8@1+ (1,0) [0|255] "" Vector__XXX
591-
BO_ 1840 WebData_1840: 4 PC
592-
SG_ Signal_4 : 24|8@1+ (1,0) [0|255] "" Vector__XXX
593-
SG_ Signal_3 : 16|8@1+ (1,0) [0|255] "" Vector__XXX
594-
SG_ Signal_2 : 8|8@1+ (1,0) [0|255] "" Vector__XXX
595-
SG_ Signal_1 : 0|8@1+ (1,0) [0|0] "" Vector__XXX
596-
597-
BO_ 3040 WebData_3040: 8 Vector__XXX
598-
SG_ Signal_6 m2 : 0|4@1+ (1,0) [0|15] "" Vector__XXX
599-
SG_ Signal_5 m3 : 16|8@1+ (1,0) [0|255] "kmh" Vector__XXX
600-
SG_ Signal_4 m3 : 8|8@1+ (1,0) [0|255] "" Vector__XXX
601-
SG_ Signal_3 m3 : 0|4@1+ (1,0) [0|3] "" Vector__XXX
602-
SG_ Signal_2 m1 : 3|12@0+ (1,0) [0|4095] "Byte" Vector__XXX
603-
SG_ Signal_1 m0 : 0|4@1+ (1,0) [0|7] "Byte" Vector__XXX
604-
SG_ Switch M : 4|4@1+ (1,0) [0|3] "" Vector__XXX
605-
606-
EV_ Environment1: 0 [0|220] "" 0 6 DUMMY_NODE_VECTOR0 DUMMY_NODE_VECTOR2;
607-
EV_ Environment2: 0 [0|177] "" 0 7 DUMMY_NODE_VECTOR1 DUMMY_NODE_VECTOR2;
608-
ENVVAR_DATA_ SomeEnvVarData: 399;
609-
610-
CM_ BO_ 1840 "Some Message comment";
611-
CM_ SG_ 1840 Signal_4 "asaklfjlsdfjlsdfgls
612-
HH?=(%)/&KKDKFSDKFKDFKSDFKSDFNKCnvsdcvsvxkcv";
613-
CM_ SG_ 5 TestSigLittleUnsigned1 "asaklfjlsdfjlsdfgls
614-
=0943503450KFSDKFKDFKSDFKSDFNKCnvsdcvsvxkcv";
615-
616-
BA_DEF_DEF_ "BusType" "AS";
617-
618-
BA_ "Attr" BO_ 4358435 283;
619-
BA_ "Attr" BO_ 56949545 344;
620-
621-
VAL_ 2000 Signal_3 255 "NOP";
622-
623-
SIG_VALTYPE_ 2000 Signal_8 : 1;
624-
"#;
625-
626-
#[test]
627-
fn dbc_definition_test() {
628-
match Dbc::try_from(SAMPLE_DBC) {
629-
Ok(dbc_content) => println!("DBC Content{dbc_content:#?}"),
630-
Err(e) => {
631-
match e {
632-
Error::Nom(nom::Err::Incomplete(needed)) => {
633-
eprintln!("Error incomplete input, needed: {needed:?}");
634-
}
635-
Error::Nom(nom::Err::Error(error)) => {
636-
eprintln!("Nom Error: {error:?}");
637-
}
638-
Error::Nom(nom::Err::Failure(ctx)) => eprintln!("Failure {ctx:?}"),
639-
Error::Incomplete(dbc, remaining) => eprintln!(
640-
"Not all data in buffer was read {dbc:#?}, remaining unparsed: {remaining}",
641-
),
642-
Error::MultipleMultiplexors => eprintln!("Multiple multiplexors defined"),
643-
}
644-
panic!("Failed to read DBC");
645-
}
646-
}
647-
}
648-
649-
#[test]
650-
fn lookup_signal_comment() {
651-
let dbc_content = Dbc::try_from(SAMPLE_DBC).expect("Failed to parse DBC");
652-
let comment = dbc_content
653-
.signal_comment(MessageId::Standard(1840), "Signal_4")
654-
.expect("Signal comment missing");
655-
assert_eq!(
656-
"asaklfjlsdfjlsdfgls\nHH?=(%)/&KKDKFSDKFKDFKSDFKSDFNKCnvsdcvsvxkcv",
657-
comment
658-
);
659-
}
660-
661-
#[test]
662-
fn lookup_signal_comment_none_when_missing() {
663-
let dbc_content = Dbc::try_from(SAMPLE_DBC).expect("Failed to parse DBC");
664-
let comment = dbc_content.signal_comment(MessageId::Standard(1840), "Signal_2");
665-
assert_eq!(None, comment);
666-
}
667-
668-
#[test]
669-
fn lookup_message_comment() {
670-
let dbc_content = Dbc::try_from(SAMPLE_DBC).expect("Failed to parse DBC");
671-
let comment = dbc_content
672-
.message_comment(MessageId::Standard(1840))
673-
.expect("Message comment missing");
674-
assert_eq!("Some Message comment", comment);
675-
}
676-
677-
#[test]
678-
fn lookup_message_comment_none_when_missing() {
679-
let dbc_content = Dbc::try_from(SAMPLE_DBC).expect("Failed to parse DBC");
680-
let comment = dbc_content.message_comment(MessageId::Standard(2000));
681-
assert_eq!(None, comment);
682-
}
683-
684-
#[test]
685-
fn lookup_value_descriptions_for_signal() {
686-
let dbc_content = Dbc::try_from(SAMPLE_DBC).expect("Failed to parse DBC");
687-
let val_descriptions = dbc_content
688-
.value_descriptions_for_signal(MessageId::Standard(2000), "Signal_3")
689-
.expect("Message comment missing");
690-
691-
let exp = vec![ValDescription {
692-
id: 255.0,
693-
description: "NOP".to_string(),
694-
}];
695-
assert_eq!(exp, val_descriptions);
696-
}
697-
698-
#[test]
699-
fn lookup_value_descriptions_for_signal_none_when_missing() {
700-
let dbc_content = Dbc::try_from(SAMPLE_DBC).expect("Failed to parse DBC");
701-
let val_descriptions =
702-
dbc_content.value_descriptions_for_signal(MessageId::Standard(2000), "Signal_2");
703-
assert_eq!(None, val_descriptions);
704-
}
705-
706-
#[test]
707-
fn lookup_extended_value_type_for_signal() {
708-
let dbc_content = Dbc::try_from(SAMPLE_DBC).expect("Failed to parse DBC");
709-
let extended_value_type =
710-
dbc_content.extended_value_type_for_signal(MessageId::Standard(2000), "Signal_8");
711-
assert_eq!(
712-
extended_value_type,
713-
Some(&SignalExtendedValueType::IEEEfloat32Bit)
714-
);
715-
}
716-
717-
#[test]
718-
fn lookup_extended_value_type_for_signal_none_when_missing() {
719-
let dbc_content = Dbc::try_from(SAMPLE_DBC).expect("Failed to parse DBC");
720-
let extended_value_type =
721-
dbc_content.extended_value_type_for_signal(MessageId::Standard(2000), "Signal_1");
722-
assert_eq!(extended_value_type, None);
723-
}
724-
725-
#[test]
726-
fn lookup_signal_by_name() {
727-
let dbc_content = Dbc::try_from(SAMPLE_DBC).expect("Failed to parse DBC");
728-
let signal = dbc_content.signal_by_name(MessageId::Standard(2000), "Signal_8");
729-
assert!(signal.is_some());
730-
}
731-
732-
#[test]
733-
fn lookup_signal_by_name_none_when_missing() {
734-
let dbc_content = Dbc::try_from(SAMPLE_DBC).expect("Failed to parse DBC");
735-
let signal = dbc_content.signal_by_name(MessageId::Standard(2000), "Signal_25");
736-
assert_eq!(signal, None);
737-
}
738-
739-
#[test]
740-
fn lookup_multiplex_indicator_switch() {
741-
let dbc_content = Dbc::try_from(SAMPLE_DBC).expect("Failed to parse DBC");
742-
let multiplexor_switch = dbc_content.message_multiplexor_switch(MessageId::Standard(3040));
743-
assert!(multiplexor_switch.is_ok());
744-
assert!(multiplexor_switch.as_ref().unwrap().is_some());
745-
assert_eq!(multiplexor_switch.unwrap().unwrap().name(), "Switch");
746-
}
747-
748-
#[test]
749-
fn lookup_multiplex_indicator_switch_none_when_missing() {
750-
let dbc_content = Dbc::try_from(SAMPLE_DBC).expect("Failed to parse DBC");
751-
let multiplexor_switch = dbc_content.message_multiplexor_switch(MessageId::Standard(1840));
752-
assert!(multiplexor_switch.unwrap().is_none());
753-
}
754-
755-
#[test]
756-
fn extended_message_id_raw() {
757-
let message_id = MessageId::Extended(2);
758-
assert_eq!(message_id.raw(), 2 | 1 << 31);
759-
let message_id = MessageId::Extended(2 ^ 29);
760-
assert_eq!(message_id.raw(), 2 ^ 29 | 1 << 31);
761-
}
762-
763-
#[test]
764-
fn standard_message_id_raw() {
765-
let message_id = MessageId::Standard(2);
766-
assert_eq!(message_id.raw(), 2);
767-
}
768-
}

0 commit comments

Comments
 (0)