From 51f4dc6ada38ffc9ed3d3d7fd91fa895a6893787 Mon Sep 17 00:00:00 2001 From: meskill <8974488+meskill@users.noreply.github.com> Date: Mon, 15 Apr 2024 15:02:01 +0000 Subject: [PATCH] add unit tests for merge right --- src/config/config_module.rs | 90 +++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/src/config/config_module.rs b/src/config/config_module.rs index a7d10fdb75..fb2143a3c1 100644 --- a/src/config/config_module.rs +++ b/src/config/config_module.rs @@ -191,3 +191,93 @@ impl From for ConfigModule { ConfigModule { config, input_types, output_types, ..Default::default() } } } + +#[cfg(test)] +mod tests { + mod extensions { + mod merge_right { + use std::path::Path; + + use prost_reflect::prost_types::FileDescriptorSet; + + use crate::config::Extensions; + use crate::merge_right::MergeRight; + + #[test] + fn grpc_file_descriptor_set_none() { + let extensions1 = Extensions::default(); + let extensions2 = Extensions::default(); + + assert_eq!( + extensions1 + .merge_right(extensions2) + .grpc_file_descriptor_set, + None + ); + } + + #[test] + fn grpc_file_descriptor_set_single() { + let greetings_path = Path::new("src/grpc/tests/proto/greetings.proto"); + + let file_descriptor_set = protox::compile([greetings_path], ["."]).unwrap(); + let extensions1 = Extensions { + grpc_file_descriptor_set: Some(file_descriptor_set.clone()), + ..Default::default() + }; + let extensions2 = Extensions::default(); + + assert_eq!( + extensions1 + .merge_right(extensions2) + .grpc_file_descriptor_set, + Some(file_descriptor_set.clone()) + ); + + let extensions1 = Extensions::default(); + let extensions2 = Extensions { + grpc_file_descriptor_set: Some(file_descriptor_set.clone()), + ..Default::default() + }; + + assert_eq!( + extensions1 + .merge_right(extensions2) + .grpc_file_descriptor_set, + Some(file_descriptor_set) + ); + } + + #[test] + fn grpc_file_descriptor_set_both() { + let greetings_path = Path::new("src/grpc/tests/proto/greetings.proto"); + let news_path = Path::new("src/grpc/tests/proto/news.proto"); + + let file_descriptor_set_greetings = + protox::compile([greetings_path], ["."]).unwrap(); + let file_descriptor_set_news = protox::compile([news_path], ["."]).unwrap(); + let extensions1 = Extensions { + grpc_file_descriptor_set: Some(file_descriptor_set_greetings.clone()), + ..Default::default() + }; + let extensions2 = Extensions { + grpc_file_descriptor_set: Some(file_descriptor_set_news.clone()), + ..Default::default() + }; + + assert_eq!( + extensions1 + .merge_right(extensions2) + .grpc_file_descriptor_set, + Some(FileDescriptorSet { + file: file_descriptor_set_greetings + .file + .into_iter() + .chain(file_descriptor_set_news.file) + .collect() + }) + ); + } + } + } +}