Skip to content

Commit

Permalink
Tidy intermediate_representation tests Part I
Browse files Browse the repository at this point in the history
  • Loading branch information
jhugman committed Nov 22, 2023
1 parent 6ec8fa4 commit 78810d7
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 80 deletions.
5 changes: 3 additions & 2 deletions components/support/nimbus-fml/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,9 @@ uniffi::include_scaffolding!("fml");
#[cfg(test)]
mod unit_tests {
use super::*;
use crate::intermediate_representation::{
unit_tests::get_feature_manifest, FeatureDef, ModuleId, PropDef, TypeRef,
use crate::{
fixtures::intermediate_representation::get_feature_manifest,
intermediate_representation::{FeatureDef, ModuleId, PropDef, TypeRef},
};
use serde_json::{json, Map, Number, Value};
use std::collections::HashMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use std::collections::BTreeMap;
use std::collections::{BTreeMap, HashMap};

use crate::intermediate_representation::{
EnumDef, FeatureDef, FeatureManifest, PropDef, TypeRef, VariantDef,
EnumDef, FeatureDef, FeatureManifest, ModuleId, ObjectDef, PropDef, TypeRef, VariantDef,
};
use serde_json::json;

Expand Down Expand Up @@ -51,3 +51,71 @@ pub(crate) fn get_simple_homescreen_feature() -> FeatureManifest {
..Default::default()
}
}

pub(crate) fn get_feature_manifest(
obj_defs: Vec<ObjectDef>,
enum_defs: Vec<EnumDef>,
feature_defs: Vec<FeatureDef>,
all_imports: HashMap<ModuleId, FeatureManifest>,
) -> FeatureManifest {
FeatureManifest {
enum_defs: map_from(enum_defs, |e| e.name()),
obj_defs: map_from(obj_defs, |o| o.name()),
feature_defs: map_from(feature_defs, |f| f.name()),
all_imports,
..Default::default()
}
}

pub(crate) fn get_one_prop_feature_manifest(
obj_defs: Vec<ObjectDef>,
enum_defs: Vec<EnumDef>,
prop: &PropDef,
) -> FeatureManifest {
FeatureManifest {
enum_defs: map_from(enum_defs, |e| e.name()),
obj_defs: map_from(obj_defs, |o| o.name()),
feature_defs: BTreeMap::from([(
"".to_string(),
FeatureDef {
props: vec![prop.clone()],
..Default::default()
},
)]),
..Default::default()
}
}

pub(crate) fn get_one_prop_feature_manifest_with_imports(
obj_defs: Vec<ObjectDef>,
enum_defs: Vec<EnumDef>,
prop: &PropDef,
all_imports: HashMap<ModuleId, FeatureManifest>,
) -> FeatureManifest {
let mut fm = FeatureManifest {
enum_defs: map_from(enum_defs, |e| e.name()),
obj_defs: map_from(obj_defs, |o| o.name()),
all_imports,
..Default::default()
};
fm.add_feature(FeatureDef {
props: vec![prop.clone()],
..Default::default()
});
fm
}

fn map_from<T, F, K>(list: Vec<T>, key: F) -> BTreeMap<K, T>
where
K: Ord,
F: Fn(&T) -> K,
{
let mut res: BTreeMap<K, T> = Default::default();

for t in list {
let k = key(&t);
res.insert(k, t);
}

res
}
113 changes: 37 additions & 76 deletions components/support/nimbus-fml/src/intermediate_representation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -894,12 +894,22 @@ impl<'a> ImportedModule<'a> {

#[cfg(test)]
pub mod unit_tests {
use serde_json::{json, Number};
use serde_json::json;

use super::*;
use crate::error::Result;
use crate::fixtures::intermediate_representation::get_simple_homescreen_feature;

impl ObjectDef {
pub(crate) fn new(name: &str, props: &[PropDef]) -> Self {
Self {
name: name.into(),
doc: format!("Documentation for {name}"),
props: props.into(),
}
}
}

impl PropDef {
pub(crate) fn new(nm: &str, typ: TypeRef, default: Value) -> Self {
PropDef {
Expand All @@ -924,16 +934,6 @@ pub mod unit_tests {
}
}

impl ObjectDef {
pub(crate) fn new(name: &str, props: &[PropDef]) -> Self {
Self {
name: name.into(),
doc: format!("Documentation for {name}"),
props: props.into(),
}
}
}

#[test]
fn can_ir_represent_smoke_test() -> Result<()> {
let reference_manifest = get_simple_homescreen_feature();
Expand Down Expand Up @@ -986,6 +986,15 @@ pub mod unit_tests {

Ok(())
}
}

#[cfg(test)]
mod manifest_structure {
use super::*;

use serde_json::json;

use crate::fixtures::intermediate_representation::get_simple_homescreen_feature;

#[test]
fn validate_duplicate_feature_defs_fails() -> Result<()> {
Expand Down Expand Up @@ -1203,74 +1212,18 @@ pub mod unit_tests {
.expect_err("Should fail since we can't have nested optionals");
Ok(())
}
}

pub fn get_feature_manifest(
obj_defs: Vec<ObjectDef>,
enum_defs: Vec<EnumDef>,
feature_defs: Vec<FeatureDef>,
all_imports: HashMap<ModuleId, FeatureManifest>,
) -> FeatureManifest {
FeatureManifest {
enum_defs: map_from(enum_defs, |e| e.name()),
obj_defs: map_from(obj_defs, |o| o.name()),
feature_defs: map_from(feature_defs, |f| f.name()),
all_imports,
..Default::default()
}
}

fn get_one_prop_feature_manifest(
obj_defs: Vec<ObjectDef>,
enum_defs: Vec<EnumDef>,
prop: &PropDef,
) -> FeatureManifest {
FeatureManifest {
enum_defs: map_from(enum_defs, |e| e.name()),
obj_defs: map_from(obj_defs, |o| o.name()),
feature_defs: BTreeMap::from([(
"".to_string(),
FeatureDef {
props: vec![prop.clone()],
..Default::default()
},
)]),
..Default::default()
}
}

fn get_one_prop_feature_manifest_with_imports(
obj_defs: Vec<ObjectDef>,
enum_defs: Vec<EnumDef>,
prop: &PropDef,
all_imports: HashMap<ModuleId, FeatureManifest>,
) -> FeatureManifest {
let mut fm = FeatureManifest {
enum_defs: map_from(enum_defs, |e| e.name()),
obj_defs: map_from(obj_defs, |o| o.name()),
all_imports,
..Default::default()
};
fm.add_feature(FeatureDef {
props: vec![prop.clone()],
..Default::default()
});
fm
}

fn map_from<T, F, K>(list: Vec<T>, key: F) -> BTreeMap<K, T>
where
K: Ord,
F: Fn(&T) -> K,
{
let mut res: BTreeMap<K, T> = Default::default();
#[cfg(test)]
mod imports_tests {
use super::*;

for t in list {
let k = key(&t);
res.insert(k, t);
}
use serde_json::json;

res
}
use crate::fixtures::intermediate_representation::{
get_feature_manifest, get_one_prop_feature_manifest,
get_one_prop_feature_manifest_with_imports,
};

#[test]
fn test_iter_object_defs_deep_iterates_on_all_imports() -> Result<()> {
Expand Down Expand Up @@ -1501,6 +1454,14 @@ pub mod unit_tests {

Ok(())
}
}

#[cfg(test)]
mod feature_config_tests {
use serde_json::{json, Number};

use super::*;
use crate::fixtures::intermediate_representation::get_feature_manifest;

#[test]
fn test_validate_feature_config_success() -> Result<()> {
Expand Down

0 comments on commit 78810d7

Please sign in to comment.