// Copyright (c) 2019-present Dmitry Stepanov and Fyrox Engine contributors. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. #![allow(unused_imports)] use std::{env, fs::File, io::Write, path::PathBuf}; use futures::executor::block_on; use fyrox_core::visitor::prelude::*; #[derive(Debug, Clone, Default, PartialEq, Visit)] struct NamedFields { a: f32, snake_case: u32, } #[derive(Debug, Clone, Default, PartialEq, Visit)] struct UnitStruct; #[derive(Debug, Clone, Default, PartialEq, Visit)] struct TupleStruct(f32, u32); #[derive(Debug, Clone, Default, PartialEq, Visit)] struct SkipAttr { visited: f32, #[visit(skip)] skipped: u32, } #[derive(Debug, Clone, PartialEq, Visit)] struct Generics { items: Vec, } // NOTE: This enum doesn't implement copy, but `#[derive(Visit)]` still works #[derive(Debug, Clone, PartialEq, Eq, Hash, Visit, Default)] enum PlainEnum { #[default] A, B, C, } #[derive(Debug, Clone, PartialEq, Visit)] enum OneOfTheTypes { NamedFields(NamedFields), PlainEnum(PlainEnum), U32(u32), } #[derive(Debug, Clone, PartialEq, Visit)] enum ComplexEnum { UnitVariant, Tuple(i32, i32), NamedFields { a: f32, b: u32 }, } #[derive(Debug, Clone, PartialEq, Visit)] enum GenericEnum where T: std::fmt::Debug + Default + Visit + 'static, { UnitVariant, Tuple(i32, Vec), } #[test] fn named_fields() { let mut data = NamedFields { a: 100.0, snake_case: 200, }; let mut data_default = NamedFields::default(); super::save_load("named_fields", &mut data, &mut data_default); // The default data was overwritten with saved data. // Now it should be same as the original data: assert_eq!(data, data_default); } #[test] fn unit_struct() { let mut data = UnitStruct; let mut data_default = UnitStruct; // non seuse.. but anyways, // `Visit` is implemented `UnitStruct;` as empty code block `{}` super::save_load("unit_struct", &mut data, &mut data_default); assert_eq!(data, data_default); } #[test] fn tuple_struct() { let mut data = TupleStruct(10.0, 20); let mut data_default = TupleStruct(0.0, 0); super::save_load("tuple_struct", &mut data, &mut data_default); assert_eq!(data, data_default); } #[test] fn skip_attr() { let mut data = SkipAttr { visited: 10.0, skipped: 20, }; let mut data_default = SkipAttr::default(); super::save_load("skip_attr", &mut data, &mut data_default); // The default data was overwritten with saved data, // except the `skipped` field: assert_eq!( data_default, SkipAttr { visited: data.visited, skipped: Default::default(), } ); } #[test] fn generics() { // But `Generics