former 2.45.0

A flexible implementation of the Builder pattern supporting nested builders and collection-specific subformers. Simplify the construction of complex objects.
Documentation
#![allow(clippy::used_underscore_binding, clippy::all, warnings, missing_docs)]
//! Collection Former Vec Tests
//! 

// #![ allow( dead_code ) ]

use super::*;
#[ allow( unused_imports ) ]
use test_tools::a_id;
#[ allow( unused_imports ) ]
use collection_tools::Vec;
use the_module::VecExt;

/// Tests Vec collection former add operations with various patterns.
#[ test ]
fn add() {
  // expliccit with CollectionFormer

  let got: Vec<String> = the_module::CollectionFormer::<
    String,
    former::VectorDefinition<String, (), Vec<String>, the_module::ReturnStorage>,
  >::new(former::ReturnStorage)
  .add("a")
  .add("b")
  .form();
  let exp = collection_tools::vec!["a".to_string(), "b".to_string(),];
  a_id!(got, exp);

  // expliccit with VectorFormer

  let got: Vec<String> =
    the_module::VectorFormer::<String, (), Vec<String>, the_module::ReturnStorage>::new(former::ReturnStorage)
      .add("a")
      .add("b")
      .form();
  let exp = collection_tools::vec!["a".to_string(), "b".to_string(),];
  a_id!(got, exp);

  // compact with VectorFormer

  let got: Vec<String> = the_module::VectorFormer::new(former::ReturnStorage).add("a").add("b").form();
  let exp = collection_tools::vec!["a".to_string(), "b".to_string(),];
  a_id!(got, exp);

  // with begin_coercing

  let got: Vec<String> =
    the_module::VectorFormer::begin(Some(collection_tools::vec!["a".to_string()]), Some(()), former::ReturnStorage)
      .add("b")
      .form();
  let exp = collection_tools::vec!["a".to_string(), "b".to_string(),];
  a_id!(got, exp);

  // with help of ext

  let got: Vec<String> = Vec::former().add("a").add("b").form();
  let exp = collection_tools::vec!["a".to_string(), "b".to_string(),];
  a_id!(got, exp);

  //
}

/// Tests Vec collection former replace operation.
#[ test ]
fn replace() {
  let got: Vec<String> = the_module::VectorFormer::new(former::ReturnStorage)
    .add("x")
    .replace(collection_tools::vec!["a".to_string(), "b".to_string()])
    .form();
  let exp = collection_tools::vec!["a".to_string(), "b".to_string(),];
  a_id!(got, exp);
}

/// Tests entity to former conversion and storage traits.
// qqq : make similar test for all collections -- done
#[ test ]
fn entity_to() {
  // qqq : uncomment and make it working -- done
  let got =
    <Vec<i32> as former::EntityToFormer<former::VectorDefinition<i32, (), Vec<i32>, former::ReturnPreformed>>>::Former::new(
      former::ReturnPreformed,
    )
    .add(13)
    .form();
  let exp = collection_tools::vec![13];
  a_id!(got, exp);

  // qqq : uncomment and make it working
  let got = <Vec<i32> as former::EntityToStorage>::Storage::default();
  let exp =
    <Vec<i32> as former::EntityToFormer<former::VectorDefinition<i32, (), Vec<i32>, former::ReturnPreformed>>>::Former::new(
      former::ReturnPreformed,
    )
    .form();
  a_id!(got, exp);

  let got = <Vec<i32> as former::EntityToStorage>::Storage::default();
  let exp = <Vec<i32> as former::EntityToFormer<
    <Vec<i32> as former::EntityToDefinition<(), Vec<i32>, former::ReturnPreformed>>::Definition,
  >>::Former::new(former::ReturnPreformed)
  .form();
  a_id!(got, exp);
}

/// Tests entry to value conversion trait.
#[ test ]
fn entry_to_val() {
  let got = former::EntryToVal::<Vec<i32>>::entry_to_val(13i32);
  let exp = 13i32;
  a_id!(got, exp);
}

/// Tests value to entry conversion trait.
#[ test ]
fn val_to_entry() {
  let got = former::ValToEntry::<Vec<i32>>::val_to_entry(13i32);
  let exp = 13i32;
  a_id!(got, exp);
}

/// Tests subformer collection integration with parent-child relationships.
#[ test ]
fn subformer() {
  /// Parameter description.
  #[ derive( Debug, Default, PartialEq, the_module::Former ) ]
  pub struct Child {
    name: String,
    data: bool,
  }

  /// Parent required for the template.
  #[ derive( Debug, Default, PartialEq, the_module::Former ) ]
  pub struct Parent {
    #[ subform_collection( definition = former::VectorDefinition ) ]
    children: Vec<Child>,
  }

  let got = Parent::former()
    .children()
    .add(Child::former().name("a").form())
    .add(Child::former().name("b").form())
    .end()
    .form();

  let children = collection_tools::vec![
    Child {
      name: "a".to_string(),
      data: false
    },
    Child {
      name: "b".to_string(),
      data: false
    },
  ];
  let exp = Parent { children };
  a_id!(got, exp);
}