use klever_sc::types::{BoxedBytes, KdaTokenPayment, ManagedBuffer, TokenIdentifier}; use klever_sc_scenario::{ api::StaticApi, klever_sc, managed_klv_token_id, managed_test_util::check_managed_top_encode_decode, managed_token_id, managed_token_id_wrapped, }; #[test] fn test_klv() { assert!(TokenIdentifier::::klv().is_klv()); } #[test] fn test_codec() { check_managed_top_encode_decode( TokenIdentifier::::klv(), TokenIdentifier::::KLV_REPRESENTATION, ); let expected = BoxedBytes::from_concat(&[ &[0, 0, 0, 3], &TokenIdentifier::::KLV_REPRESENTATION[..], ]); check_managed_top_encode_decode( vec![TokenIdentifier::::klv()], expected.as_slice(), ); } #[test] #[rustfmt::skip] fn test_is_valid_kda_identifier() { // valid identifier assert!(TokenIdentifier::::from("ALC-6258").is_valid_kda_identifier()); // valid identifier with numbers in ticker assert!(TokenIdentifier::::from("ALC123-6258").is_valid_kda_identifier()); // valid ticker only numbers assert!(TokenIdentifier::::from("12345-6258").is_valid_kda_identifier()); // missing dash assert!(!TokenIdentifier::::from("ALC6258").is_valid_kda_identifier()); // wrong dash position assert!(!TokenIdentifier::::from("AL-C6258").is_valid_kda_identifier()); // lowercase ticker assert!(!TokenIdentifier::::from("alc-6258").is_valid_kda_identifier()); // lowercase random chars assert!(!TokenIdentifier::::from("ALC-62d2").is_valid_kda_identifier()); // too many random chars assert!(!TokenIdentifier::::from("ALC-6258D2").is_valid_kda_identifier()); // ticker too short assert!(!TokenIdentifier::::from("AL-6258").is_valid_kda_identifier()); // ticker too long assert!(!TokenIdentifier::::from("ALCCCCCCCCC-6258").is_valid_kda_identifier()); } #[test] #[rustfmt::skip] fn test_ticker() { // valid identifier assert_eq!( TokenIdentifier::::from("ALC-6258").ticker(), ManagedBuffer::::from("ALC"), ); // valid identifier with numbers in ticker assert_eq!( TokenIdentifier::::from("ALC123-6258").ticker(), ManagedBuffer::::from("ALC123"), ); // valid ticker only numbers assert_eq!( TokenIdentifier::::from("12345-6258").ticker(), ManagedBuffer::::from("12345"), ); // missing dash assert_eq!( TokenIdentifier::::from("ALC6258").ticker(), ManagedBuffer::::from("AL"), ); // wrong dash position assert_eq!( TokenIdentifier::::from("AL-C6258").ticker(), ManagedBuffer::::from("AL-"), ); // lowercase ticker assert_eq!( TokenIdentifier::::from("alc-6258").ticker(), ManagedBuffer::::from("alc"), ); // uppercase random chars assert_eq!( TokenIdentifier::::from("ALC-6258").ticker(), ManagedBuffer::::from("ALC"), ); // ticker too short assert_eq!( TokenIdentifier::::from("AL-6258").ticker(), ManagedBuffer::::from("AL"), ); // ticker too long assert_eq!( TokenIdentifier::::from("ALCCCCCCCCC-6258").ticker(), ManagedBuffer::::from("ALCCCCCCCCC"), ); } #[test] fn test_is_valid_klv_or_kda() { // klv is always valid assert!(TokenIdentifier::::klv().is_valid()); // valid kda assert!(TokenIdentifier::::from("ALC-6258").is_valid()); // invalid kda, see above assert!(!TokenIdentifier::::from("ALCCCCCCCCC-6258").is_valid()); } #[test] fn test_token_identifier_eq() { assert_eq!( TokenIdentifier::::from("KDA-0000"), TokenIdentifier::::from("KDA-0000") ); assert_ne!( TokenIdentifier::::from("KDA-0001"), TokenIdentifier::::from("KDA-0002") ); assert_eq!( TokenIdentifier::::from(TokenIdentifier::from("KDA-0003")), TokenIdentifier::::from("KDA-0003") ); assert_ne!( TokenIdentifier::::klv(), TokenIdentifier::::from("ANYTHING-1234") ); assert_eq!( TokenIdentifier::::klv(), TokenIdentifier::::from("KLV") ); } #[test] fn test_payment_eq() { assert_eq!( KdaTokenPayment::::new("PAY-0000".into(), 0, 1000u32.into()), KdaTokenPayment::::new("PAY-0000".into(), 0, 1000u32.into()), ); assert_ne!( KdaTokenPayment::::new("PAY-0001".into(), 0, 1000u32.into()), KdaTokenPayment::::new("PAY-0002".into(), 0, 1000u32.into()), ); assert_eq!( KdaTokenPayment::::no_payment(), KdaTokenPayment::::no_payment(), ); assert_eq!( KdaTokenPayment::::new(TokenIdentifier::from("KDAPAY-0000"), 0, 1000u32.into(),), KdaTokenPayment::::new(TokenIdentifier::from("KDAPAY-0000"), 0, 1000u32.into(),), ); assert_ne!( KdaTokenPayment::::new(TokenIdentifier::from("KDAPAY-0001"), 0, 1000u32.into(),), KdaTokenPayment::::new(TokenIdentifier::from("KDAPAY-0002"), 0, 1000u32.into(),), ); assert_ne!( KdaTokenPayment::::new(TokenIdentifier::from("KDAPAY-0001"), 0, 1000u32.into(),), KdaTokenPayment::::no_payment(), ); } #[test] fn test_managed_token_id_macro() { assert_eq!(managed_klv_token_id!(), TokenIdentifier::::klv()); assert_eq!( managed_token_id!(b"ALC-6258"), TokenIdentifier::::from("ALC-6258") ); assert_eq!( managed_token_id_wrapped!(b"ALC-6258"), TokenIdentifier::::from("ALC-6258") ) }