1 #![allow(clippy::unreadable_literal)]
2 #![cfg_attr(feature = "unstable", feature(never_type))]
3
4 use std::cell::RefCell;
5 use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
6 use std::ffi::CString;
7 use std::net;
8 use std::num::Wrapping;
9 use std::ops::Bound;
10 use std::path::{Path, PathBuf};
11 use std::rc::{Rc, Weak as RcWeak};
12 use std::sync::atomic::{
13 AtomicBool, AtomicI16, AtomicI32, AtomicI8, AtomicIsize, AtomicU16, AtomicU32, AtomicU8,
14 AtomicUsize,
15 };
16 use std::sync::{Arc, Weak as ArcWeak};
17 use std::time::{Duration, UNIX_EPOCH};
18
19 #[cfg(unix)]
20 use std::str;
21 #[cfg(target_arch = "x86_64")]
22 use std::sync::atomic::{AtomicI64, AtomicU64};
23
24 use fnv::FnvHasher;
25 use serde::Serialize;
26 use serde_test::{assert_ser_tokens, assert_ser_tokens_error, Configure, Token};
27
28 #[macro_use]
29 mod macros;
30
31 //////////////////////////////////////////////////////////////////////////
32
33 #[derive(Serialize)]
34 struct UnitStruct;
35
36 #[derive(Serialize)]
37 struct TupleStruct(i32, i32, i32);
38
39 #[derive(Serialize)]
40 struct Struct {
41 a: i32,
42 b: i32,
43 c: i32,
44 }
45
46 #[derive(PartialEq, Debug)]
47 struct NotSerializable;
48
49 #[derive(Serialize, PartialEq, Debug)]
50 enum Enum {
51 Unit,
52 One(i32),
53 Seq(i32, i32),
54 Map {
55 a: i32,
56 b: i32,
57 },
58 #[serde(skip_serializing)]
59 SkippedUnit,
60 #[serde(skip_serializing)]
61 SkippedOne(i32),
62 #[serde(skip_serializing)]
63 SkippedSeq(i32, i32),
64 #[serde(skip_serializing)]
65 SkippedMap {
66 _a: i32,
67 _b: i32,
68 },
69 OneWithSkipped(#[serde(skip_serializing)] NotSerializable),
70 }
71
72 //////////////////////////////////////////////////////////////////////////
73
74 #[test]
test_unit()75 fn test_unit() {
76 assert_ser_tokens(&(), &[Token::Unit]);
77 }
78
79 #[test]
test_bool()80 fn test_bool() {
81 assert_ser_tokens(&true, &[Token::Bool(true)]);
82 assert_ser_tokens(&false, &[Token::Bool(false)]);
83 }
84
85 #[test]
test_isizes()86 fn test_isizes() {
87 assert_ser_tokens(&0i8, &[Token::I8(0)]);
88 assert_ser_tokens(&0i16, &[Token::I16(0)]);
89 assert_ser_tokens(&0i32, &[Token::I32(0)]);
90 assert_ser_tokens(&0i64, &[Token::I64(0)]);
91 }
92
93 #[test]
test_usizes()94 fn test_usizes() {
95 assert_ser_tokens(&0u8, &[Token::U8(0)]);
96 assert_ser_tokens(&0u16, &[Token::U16(0)]);
97 assert_ser_tokens(&0u32, &[Token::U32(0)]);
98 assert_ser_tokens(&0u64, &[Token::U64(0)]);
99 }
100
101 #[test]
test_floats()102 fn test_floats() {
103 assert_ser_tokens(&0f32, &[Token::F32(0.)]);
104 assert_ser_tokens(&0f64, &[Token::F64(0.)]);
105 }
106
107 #[test]
test_char()108 fn test_char() {
109 assert_ser_tokens(&'a', &[Token::Char('a')]);
110 }
111
112 #[test]
test_str()113 fn test_str() {
114 assert_ser_tokens(&"abc", &[Token::Str("abc")]);
115 assert_ser_tokens(&"abc".to_owned(), &[Token::Str("abc")]);
116 }
117
118 #[test]
test_option()119 fn test_option() {
120 assert_ser_tokens(&None::<i32>, &[Token::None]);
121 assert_ser_tokens(&Some(1), &[Token::Some, Token::I32(1)]);
122 }
123
124 #[test]
test_result()125 fn test_result() {
126 assert_ser_tokens(
127 &Ok::<i32, i32>(0),
128 &[
129 Token::NewtypeVariant {
130 name: "Result",
131 variant: "Ok",
132 },
133 Token::I32(0),
134 ],
135 );
136 assert_ser_tokens(
137 &Err::<i32, i32>(1),
138 &[
139 Token::NewtypeVariant {
140 name: "Result",
141 variant: "Err",
142 },
143 Token::I32(1),
144 ],
145 );
146 }
147
148 #[test]
test_slice()149 fn test_slice() {
150 assert_ser_tokens(&[0][..0], &[Token::Seq { len: Some(0) }, Token::SeqEnd]);
151 assert_ser_tokens(
152 &[1, 2, 3][..],
153 &[
154 Token::Seq { len: Some(3) },
155 Token::I32(1),
156 Token::I32(2),
157 Token::I32(3),
158 Token::SeqEnd,
159 ],
160 );
161 }
162
163 #[test]
test_array()164 fn test_array() {
165 assert_ser_tokens(&[0; 0], &[Token::Tuple { len: 0 }, Token::TupleEnd]);
166 assert_ser_tokens(
167 &[1, 2, 3],
168 &[
169 Token::Tuple { len: 3 },
170 Token::I32(1),
171 Token::I32(2),
172 Token::I32(3),
173 Token::TupleEnd,
174 ],
175 );
176 }
177
178 #[test]
test_vec()179 fn test_vec() {
180 assert_ser_tokens(
181 &Vec::<isize>::new(),
182 &[Token::Seq { len: Some(0) }, Token::SeqEnd],
183 );
184 assert_ser_tokens(
185 &vec![vec![], vec![1], vec![2, 3]],
186 &[
187 Token::Seq { len: Some(3) },
188 Token::Seq { len: Some(0) },
189 Token::SeqEnd,
190 Token::Seq { len: Some(1) },
191 Token::I32(1),
192 Token::SeqEnd,
193 Token::Seq { len: Some(2) },
194 Token::I32(2),
195 Token::I32(3),
196 Token::SeqEnd,
197 Token::SeqEnd,
198 ],
199 );
200 }
201
202 #[test]
test_btreeset()203 fn test_btreeset() {
204 assert_ser_tokens(
205 &BTreeSet::<isize>::new(),
206 &[Token::Seq { len: Some(0) }, Token::SeqEnd],
207 );
208 assert_ser_tokens(
209 &btreeset![1],
210 &[Token::Seq { len: Some(1) }, Token::I32(1), Token::SeqEnd],
211 );
212 }
213
214 #[test]
test_hashset()215 fn test_hashset() {
216 assert_ser_tokens(
217 &HashSet::<isize>::new(),
218 &[Token::Seq { len: Some(0) }, Token::SeqEnd],
219 );
220 assert_ser_tokens(
221 &hashset![1],
222 &[Token::Seq { len: Some(1) }, Token::I32(1), Token::SeqEnd],
223 );
224 assert_ser_tokens(
225 &hashset![FnvHasher @ 1],
226 &[Token::Seq { len: Some(1) }, Token::I32(1), Token::SeqEnd],
227 );
228 }
229
230 #[test]
test_tuple()231 fn test_tuple() {
232 assert_ser_tokens(
233 &(1,),
234 &[Token::Tuple { len: 1 }, Token::I32(1), Token::TupleEnd],
235 );
236 assert_ser_tokens(
237 &(1, 2, 3),
238 &[
239 Token::Tuple { len: 3 },
240 Token::I32(1),
241 Token::I32(2),
242 Token::I32(3),
243 Token::TupleEnd,
244 ],
245 );
246 }
247
248 #[test]
test_btreemap()249 fn test_btreemap() {
250 assert_ser_tokens(
251 &btreemap![1 => 2],
252 &[
253 Token::Map { len: Some(1) },
254 Token::I32(1),
255 Token::I32(2),
256 Token::MapEnd,
257 ],
258 );
259 assert_ser_tokens(
260 &btreemap![1 => 2, 3 => 4],
261 &[
262 Token::Map { len: Some(2) },
263 Token::I32(1),
264 Token::I32(2),
265 Token::I32(3),
266 Token::I32(4),
267 Token::MapEnd,
268 ],
269 );
270 assert_ser_tokens(
271 &btreemap![1 => btreemap![], 2 => btreemap![3 => 4, 5 => 6]],
272 &[
273 Token::Map { len: Some(2) },
274 Token::I32(1),
275 Token::Map { len: Some(0) },
276 Token::MapEnd,
277 Token::I32(2),
278 Token::Map { len: Some(2) },
279 Token::I32(3),
280 Token::I32(4),
281 Token::I32(5),
282 Token::I32(6),
283 Token::MapEnd,
284 Token::MapEnd,
285 ],
286 );
287 }
288
289 #[test]
test_hashmap()290 fn test_hashmap() {
291 assert_ser_tokens(
292 &HashMap::<isize, isize>::new(),
293 &[Token::Map { len: Some(0) }, Token::MapEnd],
294 );
295 assert_ser_tokens(
296 &hashmap![1 => 2],
297 &[
298 Token::Map { len: Some(1) },
299 Token::I32(1),
300 Token::I32(2),
301 Token::MapEnd,
302 ],
303 );
304 assert_ser_tokens(
305 &hashmap![FnvHasher @ 1 => 2],
306 &[
307 Token::Map { len: Some(1) },
308 Token::I32(1),
309 Token::I32(2),
310 Token::MapEnd,
311 ],
312 );
313 }
314
315 #[test]
test_unit_struct()316 fn test_unit_struct() {
317 assert_ser_tokens(&UnitStruct, &[Token::UnitStruct { name: "UnitStruct" }]);
318 }
319
320 #[test]
test_tuple_struct()321 fn test_tuple_struct() {
322 assert_ser_tokens(
323 &TupleStruct(1, 2, 3),
324 &[
325 Token::TupleStruct {
326 name: "TupleStruct",
327 len: 3,
328 },
329 Token::I32(1),
330 Token::I32(2),
331 Token::I32(3),
332 Token::TupleStructEnd,
333 ],
334 );
335 }
336
337 #[test]
test_struct()338 fn test_struct() {
339 assert_ser_tokens(
340 &Struct { a: 1, b: 2, c: 3 },
341 &[
342 Token::Struct {
343 name: "Struct",
344 len: 3,
345 },
346 Token::Str("a"),
347 Token::I32(1),
348 Token::Str("b"),
349 Token::I32(2),
350 Token::Str("c"),
351 Token::I32(3),
352 Token::StructEnd,
353 ],
354 );
355 }
356
357 #[test]
test_enum()358 fn test_enum() {
359 assert_ser_tokens(
360 &Enum::Unit,
361 &[Token::UnitVariant {
362 name: "Enum",
363 variant: "Unit",
364 }],
365 );
366 assert_ser_tokens(
367 &Enum::One(42),
368 &[
369 Token::NewtypeVariant {
370 name: "Enum",
371 variant: "One",
372 },
373 Token::I32(42),
374 ],
375 );
376 assert_ser_tokens(
377 &Enum::Seq(1, 2),
378 &[
379 Token::TupleVariant {
380 name: "Enum",
381 variant: "Seq",
382 len: 2,
383 },
384 Token::I32(1),
385 Token::I32(2),
386 Token::TupleVariantEnd,
387 ],
388 );
389 assert_ser_tokens(
390 &Enum::Map { a: 1, b: 2 },
391 &[
392 Token::StructVariant {
393 name: "Enum",
394 variant: "Map",
395 len: 2,
396 },
397 Token::Str("a"),
398 Token::I32(1),
399 Token::Str("b"),
400 Token::I32(2),
401 Token::StructVariantEnd,
402 ],
403 );
404 assert_ser_tokens(
405 &Enum::OneWithSkipped(NotSerializable),
406 &[Token::UnitVariant {
407 name: "Enum",
408 variant: "OneWithSkipped",
409 }],
410 );
411 }
412
413 #[test]
test_box()414 fn test_box() {
415 assert_ser_tokens(&Box::new(0i32), &[Token::I32(0)]);
416 }
417
418 #[test]
test_boxed_slice()419 fn test_boxed_slice() {
420 assert_ser_tokens(
421 &Box::new([0, 1, 2]),
422 &[
423 Token::Tuple { len: 3 },
424 Token::I32(0),
425 Token::I32(1),
426 Token::I32(2),
427 Token::TupleEnd,
428 ],
429 );
430 }
431
432 #[test]
test_duration()433 fn test_duration() {
434 assert_ser_tokens(
435 &Duration::new(1, 2),
436 &[
437 Token::Struct {
438 name: "Duration",
439 len: 2,
440 },
441 Token::Str("secs"),
442 Token::U64(1),
443 Token::Str("nanos"),
444 Token::U32(2),
445 Token::StructEnd,
446 ],
447 );
448 }
449
450 #[test]
test_system_time()451 fn test_system_time() {
452 let system_time = UNIX_EPOCH + Duration::new(1, 200);
453 assert_ser_tokens(
454 &system_time,
455 &[
456 Token::Struct {
457 name: "SystemTime",
458 len: 2,
459 },
460 Token::Str("secs_since_epoch"),
461 Token::U64(1),
462 Token::Str("nanos_since_epoch"),
463 Token::U32(200),
464 Token::StructEnd,
465 ],
466 );
467 }
468
469 #[test]
test_range()470 fn test_range() {
471 assert_ser_tokens(
472 &(1u32..2u32),
473 &[
474 Token::Struct {
475 name: "Range",
476 len: 2,
477 },
478 Token::Str("start"),
479 Token::U32(1),
480 Token::Str("end"),
481 Token::U32(2),
482 Token::StructEnd,
483 ],
484 );
485 }
486
487 #[test]
test_range_inclusive()488 fn test_range_inclusive() {
489 assert_ser_tokens(
490 &(1u32..=2u32),
491 &[
492 Token::Struct {
493 name: "RangeInclusive",
494 len: 2,
495 },
496 Token::Str("start"),
497 Token::U32(1),
498 Token::Str("end"),
499 Token::U32(2),
500 Token::StructEnd,
501 ],
502 );
503 }
504
505 #[test]
test_bound()506 fn test_bound() {
507 assert_ser_tokens(
508 &Bound::Unbounded::<()>,
509 &[
510 Token::Enum { name: "Bound" },
511 Token::Str("Unbounded"),
512 Token::Unit,
513 ],
514 );
515 assert_ser_tokens(
516 &Bound::Included(0u8),
517 &[
518 Token::Enum { name: "Bound" },
519 Token::Str("Included"),
520 Token::U8(0),
521 ],
522 );
523 assert_ser_tokens(
524 &Bound::Excluded(0u8),
525 &[
526 Token::Enum { name: "Bound" },
527 Token::Str("Excluded"),
528 Token::U8(0),
529 ],
530 );
531 }
532
533 #[test]
test_path()534 fn test_path() {
535 assert_ser_tokens(
536 &Path::new("/usr/local/lib"),
537 &[Token::Str("/usr/local/lib")],
538 );
539 }
540
541 #[test]
test_path_buf()542 fn test_path_buf() {
543 assert_ser_tokens(
544 &PathBuf::from("/usr/local/lib"),
545 &[Token::Str("/usr/local/lib")],
546 );
547 }
548
549 #[test]
test_cstring()550 fn test_cstring() {
551 assert_ser_tokens(&CString::new("abc").unwrap(), &[Token::Bytes(b"abc")]);
552 }
553
554 #[test]
test_cstr()555 fn test_cstr() {
556 let cstring = CString::new("abc").unwrap();
557 assert_ser_tokens(cstring.as_c_str(), &[Token::Bytes(b"abc")]);
558 }
559
560 #[test]
test_rc()561 fn test_rc() {
562 assert_ser_tokens(&Rc::new(true), &[Token::Bool(true)]);
563 }
564
565 #[test]
test_rc_weak_some()566 fn test_rc_weak_some() {
567 let rc = Rc::new(true);
568 assert_ser_tokens(&Rc::downgrade(&rc), &[Token::Some, Token::Bool(true)]);
569 }
570
571 #[test]
test_rc_weak_none()572 fn test_rc_weak_none() {
573 assert_ser_tokens(&RcWeak::<bool>::new(), &[Token::None]);
574 }
575
576 #[test]
test_arc()577 fn test_arc() {
578 assert_ser_tokens(&Arc::new(true), &[Token::Bool(true)]);
579 }
580
581 #[test]
test_arc_weak_some()582 fn test_arc_weak_some() {
583 let arc = Arc::new(true);
584 assert_ser_tokens(&Arc::downgrade(&arc), &[Token::Some, Token::Bool(true)]);
585 }
586
587 #[test]
test_arc_weak_none()588 fn test_arc_weak_none() {
589 assert_ser_tokens(&ArcWeak::<bool>::new(), &[Token::None]);
590 }
591
592 #[test]
test_wrapping()593 fn test_wrapping() {
594 assert_ser_tokens(&Wrapping(1usize), &[Token::U64(1)]);
595 }
596
597 #[test]
test_rc_dst()598 fn test_rc_dst() {
599 assert_ser_tokens(&Rc::<str>::from("s"), &[Token::Str("s")]);
600 assert_ser_tokens(
601 &Rc::<[bool]>::from(&[true][..]),
602 &[
603 Token::Seq { len: Some(1) },
604 Token::Bool(true),
605 Token::SeqEnd,
606 ],
607 );
608 }
609
610 #[test]
test_arc_dst()611 fn test_arc_dst() {
612 assert_ser_tokens(&Arc::<str>::from("s"), &[Token::Str("s")]);
613 assert_ser_tokens(
614 &Arc::<[bool]>::from(&[true][..]),
615 &[
616 Token::Seq { len: Some(1) },
617 Token::Bool(true),
618 Token::SeqEnd,
619 ],
620 );
621 }
622
623 #[test]
test_fmt_arguments()624 fn test_fmt_arguments() {
625 assert_ser_tokens(&format_args!("{}{}", 1, 'a'), &[Token::Str("1a")]);
626 }
627
628 #[test]
test_atomic()629 fn test_atomic() {
630 assert_ser_tokens(&AtomicBool::new(false), &[Token::Bool(false)]);
631 assert_ser_tokens(&AtomicBool::new(true), &[Token::Bool(true)]);
632 assert_ser_tokens(&AtomicI8::new(63i8), &[Token::I8(63i8)]);
633 assert_ser_tokens(&AtomicI16::new(-318i16), &[Token::I16(-318i16)]);
634 assert_ser_tokens(&AtomicI32::new(65792i32), &[Token::I32(65792i32)]);
635 assert_ser_tokens(&AtomicIsize::new(-65792isize), &[Token::I64(-65792i64)]);
636 assert_ser_tokens(&AtomicU8::new(192u8), &[Token::U8(192u8)]);
637 assert_ser_tokens(&AtomicU16::new(510u16), &[Token::U16(510u16)]);
638 assert_ser_tokens(&AtomicU32::new(131072u32), &[Token::U32(131072u32)]);
639 assert_ser_tokens(&AtomicUsize::new(655360usize), &[Token::U64(655360u64)]);
640 }
641
642 #[cfg(target_arch = "x86_64")]
643 #[test]
test_atomic64()644 fn test_atomic64() {
645 assert_ser_tokens(
646 &AtomicI64::new(-4295032832i64),
647 &[Token::I64(-4295032832i64)],
648 );
649 assert_ser_tokens(
650 &AtomicU64::new(12884901888u64),
651 &[Token::U64(12884901888u64)],
652 );
653 }
654
655 #[test]
test_net_ipv4addr_readable()656 fn test_net_ipv4addr_readable() {
657 assert_ser_tokens(
658 &"1.2.3.4".parse::<net::Ipv4Addr>().unwrap().readable(),
659 &[Token::Str("1.2.3.4")],
660 );
661 }
662
663 #[test]
test_net_ipv6addr_readable()664 fn test_net_ipv6addr_readable() {
665 assert_ser_tokens(
666 &"::1".parse::<net::Ipv6Addr>().unwrap().readable(),
667 &[Token::Str("::1")],
668 );
669 }
670
671 #[test]
test_net_ipaddr_readable()672 fn test_net_ipaddr_readable() {
673 assert_ser_tokens(
674 &"1.2.3.4".parse::<net::IpAddr>().unwrap().readable(),
675 &[Token::Str("1.2.3.4")],
676 );
677 }
678
679 #[test]
test_net_socketaddr_readable()680 fn test_net_socketaddr_readable() {
681 assert_ser_tokens(
682 &"1.2.3.4:1234"
683 .parse::<net::SocketAddr>()
684 .unwrap()
685 .readable(),
686 &[Token::Str("1.2.3.4:1234")],
687 );
688 assert_ser_tokens(
689 &"1.2.3.4:1234"
690 .parse::<net::SocketAddrV4>()
691 .unwrap()
692 .readable(),
693 &[Token::Str("1.2.3.4:1234")],
694 );
695 assert_ser_tokens(
696 &"[::1]:1234"
697 .parse::<net::SocketAddrV6>()
698 .unwrap()
699 .readable(),
700 &[Token::Str("[::1]:1234")],
701 );
702 }
703
704 #[test]
test_net_ipv4addr_compact()705 fn test_net_ipv4addr_compact() {
706 assert_ser_tokens(
707 &net::Ipv4Addr::from(*b"1234").compact(),
708 &seq![
709 Token::Tuple { len: 4 },
710 b"1234".iter().copied().map(Token::U8),
711 Token::TupleEnd,
712 ],
713 );
714 }
715
716 #[test]
test_net_ipv6addr_compact()717 fn test_net_ipv6addr_compact() {
718 assert_ser_tokens(
719 &net::Ipv6Addr::from(*b"1234567890123456").compact(),
720 &seq![
721 Token::Tuple { len: 16 },
722 b"1234567890123456".iter().copied().map(Token::U8),
723 Token::TupleEnd,
724 ],
725 );
726 }
727
728 #[test]
test_net_ipaddr_compact()729 fn test_net_ipaddr_compact() {
730 assert_ser_tokens(
731 &net::IpAddr::from(*b"1234").compact(),
732 &seq![
733 Token::NewtypeVariant {
734 name: "IpAddr",
735 variant: "V4"
736 },
737 Token::Tuple { len: 4 },
738 b"1234".iter().copied().map(Token::U8),
739 Token::TupleEnd,
740 ],
741 );
742 }
743
744 #[test]
test_net_socketaddr_compact()745 fn test_net_socketaddr_compact() {
746 assert_ser_tokens(
747 &net::SocketAddr::from((*b"1234567890123456", 1234)).compact(),
748 &seq![
749 Token::NewtypeVariant {
750 name: "SocketAddr",
751 variant: "V6"
752 },
753 Token::Tuple { len: 2 },
754 Token::Tuple { len: 16 },
755 b"1234567890123456".iter().copied().map(Token::U8),
756 Token::TupleEnd,
757 Token::U16(1234),
758 Token::TupleEnd,
759 ],
760 );
761 assert_ser_tokens(
762 &net::SocketAddrV4::new(net::Ipv4Addr::from(*b"1234"), 1234).compact(),
763 &seq![
764 Token::Tuple { len: 2 },
765 Token::Tuple { len: 4 },
766 b"1234".iter().copied().map(Token::U8),
767 Token::TupleEnd,
768 Token::U16(1234),
769 Token::TupleEnd,
770 ],
771 );
772 assert_ser_tokens(
773 &net::SocketAddrV6::new(net::Ipv6Addr::from(*b"1234567890123456"), 1234, 0, 0).compact(),
774 &seq![
775 Token::Tuple { len: 2 },
776 Token::Tuple { len: 16 },
777 b"1234567890123456".iter().copied().map(Token::U8),
778 Token::TupleEnd,
779 Token::U16(1234),
780 Token::TupleEnd,
781 ],
782 );
783 }
784
785 #[cfg(feature = "unstable")]
786 #[test]
test_never_result()787 fn test_never_result() {
788 assert_ser_tokens(
789 &Ok::<u8, !>(0),
790 &[
791 Token::NewtypeVariant {
792 name: "Result",
793 variant: "Ok",
794 },
795 Token::U8(0),
796 ],
797 );
798 }
799
800 #[test]
801 #[cfg(unix)]
test_cannot_serialize_paths()802 fn test_cannot_serialize_paths() {
803 let path = unsafe { str::from_utf8_unchecked(b"Hello \xF0\x90\x80World") };
804 assert_ser_tokens_error(
805 &Path::new(path),
806 &[],
807 "path contains invalid UTF-8 characters",
808 );
809
810 let mut path_buf = PathBuf::new();
811 path_buf.push(path);
812
813 assert_ser_tokens_error(&path_buf, &[], "path contains invalid UTF-8 characters");
814 }
815
816 #[test]
test_cannot_serialize_mutably_borrowed_ref_cell()817 fn test_cannot_serialize_mutably_borrowed_ref_cell() {
818 let ref_cell = RefCell::new(42);
819 let _reference = ref_cell.borrow_mut();
820 assert_ser_tokens_error(&ref_cell, &[], "already mutably borrowed");
821 }
822
823 #[test]
test_enum_skipped()824 fn test_enum_skipped() {
825 assert_ser_tokens_error(
826 &Enum::SkippedUnit,
827 &[],
828 "the enum variant Enum::SkippedUnit cannot be serialized",
829 );
830 assert_ser_tokens_error(
831 &Enum::SkippedOne(42),
832 &[],
833 "the enum variant Enum::SkippedOne cannot be serialized",
834 );
835 assert_ser_tokens_error(
836 &Enum::SkippedSeq(1, 2),
837 &[],
838 "the enum variant Enum::SkippedSeq cannot be serialized",
839 );
840 assert_ser_tokens_error(
841 &Enum::SkippedMap { _a: 1, _b: 2 },
842 &[],
843 "the enum variant Enum::SkippedMap cannot be serialized",
844 );
845 }
846
847 #[test]
test_integer128()848 fn test_integer128() {
849 assert_ser_tokens_error(&1i128, &[], "i128 is not supported");
850
851 assert_ser_tokens_error(&1u128, &[], "u128 is not supported");
852 }
853