@@ -49,7 +49,7 @@ impl TypeOptionCellWriter for CheckboxTypeOption {
4949 Value :: Number ( n) => Some ( n. to_string ( ) ) ,
5050 _ => None ,
5151 } {
52- cell. insert ( CELL_DATA . into ( ) , bool_from_str ( & data) . into ( ) ) ;
52+ cell. insert ( CELL_DATA . into ( ) , bool_from_str ( & data) . to_string ( ) . into ( ) ) ;
5353 }
5454 cell
5555 }
@@ -75,3 +75,85 @@ fn bool_from_str(s: &str) -> bool {
7575 _ => false ,
7676 }
7777}
78+
79+ #[ cfg( test) ]
80+ mod tests {
81+ use super :: * ;
82+
83+ #[ test]
84+ fn test_checkbox_type_option_json_cell ( ) {
85+ let option = CheckboxTypeOption :: new ( ) ;
86+ let mut cell = new_cell_builder ( FieldType :: Checkbox ) ;
87+ cell. insert ( CELL_DATA . into ( ) , "true" . into ( ) ) ;
88+
89+ // Convert cell to JSON
90+ let value = option. json_cell ( & cell) ;
91+ assert_eq ! ( value, Value :: String ( "true" . to_string( ) ) ) ;
92+
93+ // Test with empty data
94+ let empty_cell = new_cell_builder ( FieldType :: Checkbox ) ;
95+ let empty_value = option. json_cell ( & empty_cell) ;
96+ assert_eq ! ( empty_value, Value :: String ( "" . to_string( ) ) ) ;
97+ }
98+
99+ #[ test]
100+ fn test_checkbox_type_option_numeric_cell ( ) {
101+ let option = CheckboxTypeOption :: new ( ) ;
102+
103+ let mut true_cell = new_cell_builder ( FieldType :: Checkbox ) ;
104+ true_cell. insert ( CELL_DATA . into ( ) , "true" . into ( ) ) ;
105+ assert_eq ! ( option. numeric_cell( & true_cell) , Some ( 1.0 ) ) ;
106+
107+ let mut false_cell = new_cell_builder ( FieldType :: Checkbox ) ;
108+ false_cell. insert ( CELL_DATA . into ( ) , "false" . into ( ) ) ;
109+ assert_eq ! ( option. numeric_cell( & false_cell) , Some ( 0.0 ) ) ;
110+
111+ let mut invalid_cell = new_cell_builder ( FieldType :: Checkbox ) ;
112+ invalid_cell. insert ( CELL_DATA . into ( ) , "invalid" . into ( ) ) ;
113+ assert_eq ! ( option. numeric_cell( & invalid_cell) , Some ( 0.0 ) ) ;
114+ }
115+
116+ #[ test]
117+ fn test_checkbox_type_option_write_json ( ) {
118+ let option = CheckboxTypeOption :: new ( ) ;
119+
120+ // Write a string
121+ let value = Value :: String ( "true" . to_string ( ) ) ;
122+ let cell = option. write_json ( value) ;
123+ assert_eq ! ( cell. get_as:: <String >( CELL_DATA ) . unwrap( ) , "true" ) ;
124+
125+ // Write a boolean
126+ let value = Value :: Bool ( true ) ;
127+ let cell = option. write_json ( value) ;
128+ assert_eq ! ( cell. get_as:: <String >( CELL_DATA ) . unwrap( ) , "true" ) ;
129+
130+ // Write a number
131+ let value = Value :: Number ( 1 . into ( ) ) ;
132+ let cell = option. write_json ( value) ;
133+ assert_eq ! ( cell. get_as:: <String >( CELL_DATA ) . unwrap( ) , "true" ) ;
134+ }
135+
136+ #[ test]
137+ fn test_checkbox_type_option_raw_conversion ( ) {
138+ let option = CheckboxTypeOption :: new ( ) ;
139+ assert_eq ! (
140+ option. convert_raw_cell_data( "raw data" ) ,
141+ "raw data" . to_string( )
142+ ) ;
143+ }
144+
145+ #[ test]
146+ fn test_bool_from_str ( ) {
147+ assert ! ( bool_from_str( "true" ) ) ;
148+ assert ! ( bool_from_str( "1" ) ) ;
149+ assert ! ( bool_from_str( "yes" ) ) ;
150+
151+ assert ! ( !bool_from_str( "false" ) ) ;
152+ assert ! ( !bool_from_str( "0" ) ) ;
153+ assert ! ( !bool_from_str( "no" ) ) ;
154+
155+ // Invalid inputs default to false
156+ assert ! ( !bool_from_str( "invalid" ) ) ;
157+ assert ! ( !bool_from_str( "" ) ) ;
158+ }
159+ }
0 commit comments