Skip to content

Commit

Permalink
READY(format_tools): added tests for new collections (#1449)
Browse files Browse the repository at this point in the history
added tests for new collections
  • Loading branch information
Sakapoi authored Aug 30, 2024
1 parent bb4b764 commit d8b26ed
Show file tree
Hide file tree
Showing 2 changed files with 287 additions and 0 deletions.
216 changes: 216 additions & 0 deletions module/core/format_tools/tests/inc/collection_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,220 @@ fn bmap_basic()

}

#[ test ]
fn bset_basic()
{

let data : collection_tools::Bset< TestObject > = bset!
{
TestObject
{
id : "1".to_string(),
created_at : 1627845583,
file_ids : vec![ "file1".to_string(), "file2".to_string() ],
tools : None
},
TestObject
{
id : "2".to_string(),
created_at : 13,
file_ids : vec![ "file3".to_string(), "file4\nmore details".to_string() ],
tools : Some
(
vec!
[
{
let mut map = HashMap::new();
map.insert( "tool1".to_string(), "value1".to_string() );
map
},
{
let mut map = HashMap::new();
map.insert( "tool2".to_string(), "value2".to_string() );
map
}
]
),
},
};

use the_module::TableFormatter;
let _as_table : AsTable< '_, BTreeSet< TestObject >, &str, TestObject, str, WithRef > = AsTable::new( &data );
let as_table = AsTable::new( &data );

let rows = TableRows::rows( &as_table );
assert_eq!( rows.len(), 2 );

let mut output = String::new();
let mut context = print::Context::new( &mut output, Default::default() );
let _got = the_module::TableFormatter::fmt( &as_table, &mut context );
let got = as_table.table_to_string();
assert!( got.contains( "│ id │ created_at │ file_ids │ tools │" ) );
assert!( got.contains( "│ 13 │ [ │ [ │" ) );
assert!( got.contains( "│ 1627845583 │ [ │ │" ) );

}

#[ test ]
fn deque_basic()
{

let data : collection_tools::Deque< TestObject > = deque!
{
TestObject
{
id : "1".to_string(),
created_at : 1627845583,
file_ids : vec![ "file1".to_string(), "file2".to_string() ],
tools : None
},
TestObject
{
id : "2".to_string(),
created_at : 13,
file_ids : vec![ "file3".to_string(), "file4\nmore details".to_string() ],
tools : Some
(
vec!
[
{
let mut map = HashMap::new();
map.insert( "tool1".to_string(), "value1".to_string() );
map
},
{
let mut map = HashMap::new();
map.insert( "tool2".to_string(), "value2".to_string() );
map
}
]
),
},
};

use the_module::TableFormatter;
let _as_table : AsTable< '_, VecDeque< TestObject >, &str, TestObject, str, WithRef > = AsTable::new( &data );
let as_table = AsTable::new( &data );

let rows = TableRows::rows( &as_table );
assert_eq!( rows.len(), 2 );

let mut output = String::new();
let mut context = print::Context::new( &mut output, Default::default() );
let _got = the_module::TableFormatter::fmt( &as_table, &mut context );
let got = as_table.table_to_string();
assert!( got.contains( "│ id │ created_at │ file_ids │ tools │" ) );
assert!( got.contains( "│ 13 │ [ │ [ │" ) );
assert!( got.contains( "│ 1627845583 │ [ │ │" ) );

}

#[ test ]
fn hset_basic()
{

let data : collection_tools::Hset< TestObject > = hset!
{
TestObject
{
id : "1".to_string(),
created_at : 1627845583,
file_ids : vec![ "file1".to_string(), "file2".to_string() ],
tools : None
},
TestObject
{
id : "2".to_string(),
created_at : 13,
file_ids : vec![ "file3".to_string(), "file4\nmore details".to_string() ],
tools : Some
(
vec!
[
{
let mut map = HashMap::new();
map.insert( "tool1".to_string(), "value1".to_string() );
map
},
{
let mut map = HashMap::new();
map.insert( "tool2".to_string(), "value2".to_string() );
map
}
]
),
},
};

use the_module::TableFormatter;
let _as_table : AsTable< '_, HashSet< TestObject >, &str, TestObject, str, WithRef > = AsTable::new( &data );
let as_table = AsTable::new( &data );

let rows = TableRows::rows( &as_table );
assert_eq!( rows.len(), 2 );

let mut output = String::new();
let mut context = print::Context::new( &mut output, Default::default() );
let _got = the_module::TableFormatter::fmt( &as_table, &mut context );
let got = as_table.table_to_string();
assert!( got.contains( "│ id │ created_at │ file_ids │ tools │" ) );
assert!( got.contains( "│ 13 │ [ │ [ │" ) );
assert!( got.contains( "│ 1627845583 │ [ │ │" ) );

}

#[ test ]
fn llist_basic()
{

let data : collection_tools::Llist< TestObject > = llist!
{
TestObject
{
id : "1".to_string(),
created_at : 1627845583,
file_ids : vec![ "file1".to_string(), "file2".to_string() ],
tools : None
},
TestObject
{
id : "2".to_string(),
created_at : 13,
file_ids : vec![ "file3".to_string(), "file4\nmore details".to_string() ],
tools : Some
(
vec!
[
{
let mut map = HashMap::new();
map.insert( "tool1".to_string(), "value1".to_string() );
map
},
{
let mut map = HashMap::new();
map.insert( "tool2".to_string(), "value2".to_string() );
map
}
]
),
},
};

use the_module::TableFormatter;
let _as_table : AsTable< '_, LinkedList< TestObject >, &str, TestObject, str, WithRef > = AsTable::new( &data );
let as_table = AsTable::new( &data );

let rows = TableRows::rows( &as_table );
assert_eq!( rows.len(), 2 );

let mut output = String::new();
let mut context = print::Context::new( &mut output, Default::default() );
let _got = the_module::TableFormatter::fmt( &as_table, &mut context );
let got = as_table.table_to_string();
assert!( got.contains( "│ id │ created_at │ file_ids │ tools │" ) );
assert!( got.contains( "│ 13 │ [ │ [ │" ) );
assert!( got.contains( "│ 1627845583 │ [ │ │" ) );

}

// qqq : xxx : implement for other containers
71 changes: 71 additions & 0 deletions module/core/format_tools/tests/inc/test_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ use the_module::
use std::
{
collections::HashMap,
hash::Hasher,
hash::Hash,
cmp::Ordering
// borrow::Cow,
};

Expand Down Expand Up @@ -58,6 +61,74 @@ for TestObject

}

impl Hash for TestObject
{

fn hash< H: Hasher >( &self, state: &mut H )
{
self.id.hash( state );
self.created_at.hash( state );
self.file_ids.hash( state );

if let Some( tools ) = &self.tools
{
for tool in tools
{
for ( key, value ) in tool
{
key.hash( state );
value.hash( state );
}
}
}
else
{
state.write_u8( 0 );
}
}

}

impl PartialEq for TestObject
{

fn eq( &self, other: &Self ) -> bool
{
self.id == other.id &&
self.created_at == other.created_at &&
self.file_ids == other.file_ids &&
self.tools == other.tools
}

}

impl Eq for TestObject
{
}

impl PartialOrd for TestObject
{

fn partial_cmp( &self, other: &Self ) -> Option< Ordering >
{
Some( self.cmp( other ) )
}

}

impl Ord for TestObject
{

fn cmp( &self, other: &Self ) -> Ordering
{
self.id
.cmp( &other.id )
.then_with( | | self.created_at.cmp( &other.created_at ) )
.then_with( | | self.file_ids.cmp( &other.file_ids ) )
}

}

//

pub fn test_objects_gen() -> Vec< TestObject >
Expand Down

0 comments on commit d8b26ed

Please sign in to comment.