Skip to content

Commit 76c642a

Browse files
committed
refactor: text type option test
1 parent e45037c commit 76c642a

File tree

5 files changed

+86
-99
lines changed

5 files changed

+86
-99
lines changed

frontend/rust-lib/flowy-folder/src/services/view/controller.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ impl ViewController {
210210
let index = views
211211
.iter()
212212
.position(|view| view.id == view_id)
213-
.and_then(|index| Some(index as i32));
213+
.map(|index| index as i32);
214214
Ok(DeletedViewPB {
215215
view_id: view_id.clone(),
216216
index,

frontend/rust-lib/flowy-grid/src/services/cell/cell_operation.rs

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,16 @@ pub trait CellDisplayable<CD> {
2929
// CD: Short for CellData. This type is the type return by apply_changeset function.
3030
// CS: Short for Changeset. Parse the string into specific Changeset type.
3131
pub trait CellDataOperation<CD, CS> {
32-
/// The cell_data is able to parse into the specific data if CD impl the FromCellData trait.
33-
/// For example:
34-
/// URLCellData, DateCellData. etc.
32+
/// Decode the cell data into `CD` that is certain type of data.
33+
///
34+
/// Each `CD` type represents as a specific field type data. For example:
35+
/// FieldType::URL => URLCellData
36+
/// FieldType::Date=> DateCellData
37+
///
38+
/// `decoded_field_type`: the field type of the cell data
39+
///
40+
/// Returns the error if the cell data can't be parsed into `CD`.
41+
///
3542
fn decode_cell_data(
3643
&self,
3744
cell_data: CellData<CD>,
@@ -82,7 +89,7 @@ pub fn decode_any_cell_data<T: TryInto<AnyCellData, Error = FlowyError> + Debug>
8289
Ok(any_cell_data) => {
8390
let AnyCellData { data, field_type } = any_cell_data;
8491
let to_field_type = field_rev.ty.into();
85-
match try_decode_cell_data(data.into(), field_rev, &field_type, &to_field_type) {
92+
match try_decode_cell_data(data.into(), &field_type, &to_field_type, field_rev) {
8693
Ok(cell_bytes) => cell_bytes,
8794
Err(e) => {
8895
tracing::error!("Decode cell data failed, {:?}", e);
@@ -99,37 +106,42 @@ pub fn decode_any_cell_data<T: TryInto<AnyCellData, Error = FlowyError> + Debug>
99106
}
100107
}
101108

109+
/// Use the `to_field_type`'s TypeOption to parse the cell data into `from_field_type`'s data.
110+
///
111+
/// Each `FieldType` has its corresponding `TypeOption` that implements the `CellDisplayable`
112+
/// and `CellDataOperation` traits.
113+
///
102114
pub fn try_decode_cell_data(
103115
cell_data: CellData<String>,
116+
from_field_type: &FieldType,
117+
to_field_type: &FieldType,
104118
field_rev: &FieldRevision,
105-
s_field_type: &FieldType,
106-
t_field_type: &FieldType,
107119
) -> FlowyResult<CellBytes> {
108120
let cell_data = cell_data.try_into_inner()?;
109121
let get_cell_data = || {
110-
let field_type: FieldTypeRevision = t_field_type.into();
111-
let data = match t_field_type {
122+
let field_type: FieldTypeRevision = to_field_type.into();
123+
let data = match to_field_type {
112124
FieldType::RichText => field_rev
113125
.get_type_option::<RichTextTypeOptionPB>(field_type)?
114-
.decode_cell_data(cell_data.into(), s_field_type, field_rev),
126+
.decode_cell_data(cell_data.into(), from_field_type, field_rev),
115127
FieldType::Number => field_rev
116128
.get_type_option::<NumberTypeOptionPB>(field_type)?
117-
.decode_cell_data(cell_data.into(), s_field_type, field_rev),
129+
.decode_cell_data(cell_data.into(), from_field_type, field_rev),
118130
FieldType::DateTime => field_rev
119131
.get_type_option::<DateTypeOptionPB>(field_type)?
120-
.decode_cell_data(cell_data.into(), s_field_type, field_rev),
132+
.decode_cell_data(cell_data.into(), from_field_type, field_rev),
121133
FieldType::SingleSelect => field_rev
122134
.get_type_option::<SingleSelectTypeOptionPB>(field_type)?
123-
.decode_cell_data(cell_data.into(), s_field_type, field_rev),
135+
.decode_cell_data(cell_data.into(), from_field_type, field_rev),
124136
FieldType::MultiSelect => field_rev
125137
.get_type_option::<MultiSelectTypeOptionPB>(field_type)?
126-
.decode_cell_data(cell_data.into(), s_field_type, field_rev),
138+
.decode_cell_data(cell_data.into(), from_field_type, field_rev),
127139
FieldType::Checkbox => field_rev
128140
.get_type_option::<CheckboxTypeOptionPB>(field_type)?
129-
.decode_cell_data(cell_data.into(), s_field_type, field_rev),
141+
.decode_cell_data(cell_data.into(), from_field_type, field_rev),
130142
FieldType::URL => field_rev
131143
.get_type_option::<URLTypeOptionPB>(field_type)?
132-
.decode_cell_data(cell_data.into(), s_field_type, field_rev),
144+
.decode_cell_data(cell_data.into(), from_field_type, field_rev),
133145
};
134146
Some(data)
135147
};
@@ -224,6 +236,12 @@ where
224236
}
225237
}
226238

239+
impl std::convert::From<usize> for CellData<String> {
240+
fn from(n: usize) -> Self {
241+
CellData(Some(n.to_string()))
242+
}
243+
}
244+
227245
impl<T> std::convert::From<T> for CellData<T> {
228246
fn from(val: T) -> Self {
229247
CellData(Some(val))
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
#![allow(clippy::module_inception)]
2+
mod text_tests;
23
mod text_type_option;
4+
35
pub use text_type_option::*;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#[cfg(test)]
2+
mod tests {
3+
use crate::entities::FieldType;
4+
use crate::services::cell::CellDataOperation;
5+
use crate::services::field::FieldBuilder;
6+
use crate::services::field::*;
7+
8+
// Test parser the cell data which field's type is FieldType::Date to cell data
9+
// which field's type is FieldType::Text
10+
#[test]
11+
fn date_type_to_text_type() {
12+
let type_option = RichTextTypeOptionPB::default();
13+
let field_type = FieldType::DateTime;
14+
let field_rev = FieldBuilder::from_field_type(&field_type).build();
15+
16+
assert_eq!(
17+
type_option
18+
.decode_cell_data(1647251762.into(), &field_type, &field_rev)
19+
.unwrap()
20+
.parser::<DateCellDataParser>()
21+
.unwrap()
22+
.date,
23+
"Mar 14,2022".to_owned()
24+
);
25+
}
26+
27+
// Test parser the cell data which field's type is FieldType::SingleSelect to cell data
28+
// which field's type is FieldType::Text
29+
#[test]
30+
fn single_select_to_text_type() {
31+
let type_option = RichTextTypeOptionPB::default();
32+
33+
let field_type = FieldType::SingleSelect;
34+
let done_option = SelectOptionPB::new("Done");
35+
let option_id = done_option.id.clone();
36+
let single_select = SingleSelectTypeOptionBuilder::default().add_option(done_option.clone());
37+
let field_rev = FieldBuilder::new(single_select).build();
38+
39+
assert_eq!(
40+
type_option
41+
.decode_cell_data(option_id.into(), &field_type, &field_rev)
42+
.unwrap()
43+
.parser::<SelectOptionCellDataParser>()
44+
.unwrap()
45+
.select_options,
46+
vec![done_option],
47+
);
48+
}
49+
}

frontend/rust-lib/flowy-grid/src/services/field/type_options/text_type_option/text_type_option.rs

Lines changed: 1 addition & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ impl CellDataOperation<String, String> for RichTextTypeOptionPB {
5858
|| decoded_field_type.is_multi_select()
5959
|| decoded_field_type.is_number()
6060
{
61-
try_decode_cell_data(cell_data, field_rev, decoded_field_type, decoded_field_type)
61+
try_decode_cell_data(cell_data, decoded_field_type, decoded_field_type, field_rev)
6262
} else {
6363
self.display_data(cell_data, decoded_field_type, field_rev)
6464
}
@@ -104,85 +104,3 @@ impl CellBytesParser for TextCellDataParser {
104104
}
105105
}
106106
}
107-
108-
#[cfg(test)]
109-
mod tests {
110-
use crate::entities::FieldType;
111-
use crate::services::cell::CellDataOperation;
112-
113-
use crate::services::field::FieldBuilder;
114-
use crate::services::field::*;
115-
116-
#[test]
117-
fn text_description_test() {
118-
let type_option = RichTextTypeOptionPB::default();
119-
120-
// date
121-
let field_type = FieldType::DateTime;
122-
let date_time_field_rev = FieldBuilder::from_field_type(&field_type).build();
123-
124-
assert_eq!(
125-
type_option
126-
.decode_cell_data(1647251762.to_string().into(), &field_type, &date_time_field_rev)
127-
.unwrap()
128-
.parser::<DateCellDataParser>()
129-
.unwrap()
130-
.date,
131-
"Mar 14,2022".to_owned()
132-
);
133-
134-
// Single select
135-
let done_option = SelectOptionPB::new("Done");
136-
let done_option_id = done_option.id.clone();
137-
let single_select = SingleSelectTypeOptionBuilder::default().add_option(done_option.clone());
138-
let single_select_field_rev = FieldBuilder::new(single_select).build();
139-
140-
assert_eq!(
141-
type_option
142-
.decode_cell_data(
143-
done_option_id.into(),
144-
&FieldType::SingleSelect,
145-
&single_select_field_rev
146-
)
147-
.unwrap()
148-
.parser::<SelectOptionCellDataParser>()
149-
.unwrap()
150-
.select_options,
151-
vec![done_option],
152-
);
153-
154-
// Multiple select
155-
let google_option = SelectOptionPB::new("Google");
156-
let facebook_option = SelectOptionPB::new("Facebook");
157-
let ids = vec![google_option.id.clone(), facebook_option.id.clone()].join(SELECTION_IDS_SEPARATOR);
158-
let cell_data_changeset = SelectOptionCellChangeset::from_insert(&ids).to_str();
159-
let multi_select = MultiSelectTypeOptionBuilder::default()
160-
.add_option(google_option.clone())
161-
.add_option(facebook_option.clone());
162-
let multi_select_field_rev = FieldBuilder::new(multi_select).build();
163-
let multi_type_option = MultiSelectTypeOptionPB::from(&multi_select_field_rev);
164-
let cell_data = multi_type_option
165-
.apply_changeset(cell_data_changeset.into(), None)
166-
.unwrap();
167-
assert_eq!(
168-
type_option
169-
.decode_cell_data(cell_data.into(), &FieldType::MultiSelect, &multi_select_field_rev)
170-
.unwrap()
171-
.parser::<SelectOptionCellDataParser>()
172-
.unwrap()
173-
.select_options,
174-
vec![google_option, facebook_option]
175-
);
176-
177-
//Number
178-
let number = NumberTypeOptionBuilder::default().set_format(NumberFormat::USD);
179-
let number_field_rev = FieldBuilder::new(number).build();
180-
assert_eq!(
181-
type_option
182-
.decode_cell_data("18443".to_owned().into(), &FieldType::Number, &number_field_rev)
183-
.unwrap()
184-
.to_string(),
185-
"$18,443".to_owned()
186-
);
187-
}
188-
}

0 commit comments

Comments
 (0)