Skip to content

Commit

Permalink
Fix AddressFormat::Middle formatting on NonFungibleGlobalId (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
matiasbzurovski authored Apr 30, 2024
1 parent f7e66d2 commit 46f302f
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sargon"
version = "0.6.55"
version = "0.6.56"
edition = "2021"
build = "build.rs"

Expand Down
4 changes: 3 additions & 1 deletion apple/Tests/TestCases/Address/AccountAddressTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ final class AccountAddressTests: AddressTest<AccountAddress> {
XCTAssertEqual(SUT.sample.shortFormat, "acco...nvjdwr")
}

func test_middle() {
func test_formatted() {
XCTAssertEqual(SUT.sample.formatted(.full), "account_rdx128y6j78mt0aqv6372evz28hrxp8mn06ccddkr7xppc88hyvynvjdwr")
XCTAssertEqual(SUT.sample.formatted(.default), "acco...nvjdwr")
XCTAssertEqual(SUT.sample.formatted(.middle), "unt_rdx128y6j78mt0aqv6372evz28hrxp8mn06ccddkr7xppc88hyvy")
}

Expand Down
15 changes: 13 additions & 2 deletions apple/Tests/TestCases/Prelude/NonFungibleGlobalIDTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,25 @@ final class NonFungibleGlobalIDTests: IdentifiableByStringProtocolTest<NonFungib
XCTAssertEqual(SUT.sample.id, SUT.sample.description)
}

func test_formatted() throws {
func test_formatted_ruid() throws {
let sut = try SUT(
nonFungibleResourceAddress: .sample,
localID: .ruid(
value: .init(hex: "deadbeef12345678babecafe87654321fadedeaf01234567ecadabba76543210")
)
)
XCTAssertNoDifference(sut.formatted(.default), "reso...c9wlxa:dead...3210")
XCTAssertNoDifference(sut.formatted(.raw), "resource_rdx1nfyg2f68jw7hfdlg5hzvd8ylsa7e0kjl68t5t62v3ttamtejc9wlxa:{deadbeef12345678-babecafe87654321-fadedeaf01234567-ecadabba76543210}")
XCTAssertNoDifference(sut.formatted(.default), "reso...c9wlxa:dead...3210")
XCTAssertNoDifference(sut.formatted(.middle), "urce_rdx1nfyg2f68jw7hfdlg5hzvd8ylsa7e0kjl68t5t62v3ttamtej:beef12345678-babecafe87654321-fadedeaf01234567-ecadabba7654")
}

func test_formatted_string() throws {
let sut = try SUT(
nonFungibleResourceAddress: .sample,
localID: .stringID("foobar")
)
XCTAssertNoDifference(sut.formatted(.raw), "resource_rdx1nfyg2f68jw7hfdlg5hzvd8ylsa7e0kjl68t5t62v3ttamtejc9wlxa:<foobar>")
XCTAssertNoDifference(sut.formatted(.default), "reso...c9wlxa:foobar")
XCTAssertNoDifference(sut.formatted(.middle), "urce_rdx1nfyg2f68jw7hfdlg5hzvd8ylsa7e0kjl68t5t62v3ttamtej")
}
}
62 changes: 56 additions & 6 deletions src/profile/v100/address/non_fungible_global_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,22 @@ impl NonFungibleGlobalId {

pub fn formatted(&self, format: AddressFormat) -> String {
match format {
AddressFormat::Default
| AddressFormat::Full
| AddressFormat::Middle => format!(
AddressFormat::Default | AddressFormat::Full => format!(
"{}:{}",
self.resource_address.formatted(format),
self.non_fungible_local_id.formatted(format)
),
AddressFormat::Raw => self.to_canonical_string(),
AddressFormat::Middle => match self.non_fungible_local_id {
NonFungibleLocalId::Ruid { value: _ } => {
format!(
"{}:{}",
self.resource_address.formatted(format),
self.non_fungible_local_id.formatted(format)
)
}
_ => self.resource_address.formatted(format),
},
}
}
}
Expand Down Expand Up @@ -305,11 +313,53 @@ mod tests {
}

#[test]
fn formatted_middle() {
fn formatted_default_vs_middle() {
let resource_address = NonFungibleResourceAddress::sample();

// local_id: integer
let mut local_id = NonFungibleLocalId::integer(12345678);
let mut item = SUT::new(resource_address, local_id);
assert_eq!(
item.formatted(AddressFormat::Default),
"reso...c9wlxa:12345678"
);
assert_eq!(
SUT::sample_ruid().formatted(AddressFormat::Middle),
"urce_rdx1nfyg2f68jw7hfdlg5hzvd8ylsa7e0kjl68t5t62v3ttamtej:beef12345678-babecafe87654321-fadedeaf01234567-ecadabba7654"
item.formatted(AddressFormat::Middle),
"urce_rdx1nfyg2f68jw7hfdlg5hzvd8ylsa7e0kjl68t5t62v3ttamtej"
);

// local_id: string
local_id = NonFungibleLocalId::string("foobar").unwrap();
item = SUT::new(resource_address, local_id);
assert_eq!(
item.formatted(AddressFormat::Default),
"reso...c9wlxa:foobar"
);
assert_eq!(
item.formatted(AddressFormat::Middle),
"urce_rdx1nfyg2f68jw7hfdlg5hzvd8ylsa7e0kjl68t5t62v3ttamtej"
);

// local_id: bytes
local_id = NonFungibleLocalId::bytes([0xde, 0xad]).unwrap();
item = SUT::new(resource_address, local_id);
assert_eq!(
item.formatted(AddressFormat::Default),
"reso...c9wlxa:dead"
);
assert_eq!(
item.formatted(AddressFormat::Middle),
"urce_rdx1nfyg2f68jw7hfdlg5hzvd8ylsa7e0kjl68t5t62v3ttamtej"
);

// local_id: ruid
local_id = NonFungibleLocalId::ruid(hex_decode("deadbeef12345678babecafe87654321fadedeaf01234567ecadabba76543210").unwrap()).unwrap();
item = SUT::new(resource_address, local_id);
assert_eq!(
item.formatted(AddressFormat::Default),
"reso...c9wlxa:dead...3210"
);
assert_eq!(item.formatted(AddressFormat::Middle), "urce_rdx1nfyg2f68jw7hfdlg5hzvd8ylsa7e0kjl68t5t62v3ttamtej:beef12345678-babecafe87654321-fadedeaf01234567-ecadabba7654");
}

#[test]
Expand Down

0 comments on commit 46f302f

Please sign in to comment.