test_parseStreetAddress.js (2878B)
1 "use strict"; 2 3 const { AddressParser } = ChromeUtils.importESModule( 4 "resource://gre/modules/shared/AddressParser.sys.mjs" 5 ); 6 7 // To add a new test entry to a "TESTCASES" variable, 8 // you would need to create a new array containing two elements. 9 // - The first element is a string representing a street address to be parsed. 10 // - The second element is an array containing the expected output after parsing the address, 11 // which should follow the format of 12 // [street number, street name, apartment number (if applicable), floor number (if applicable)]. 13 // 14 // Note. If we expect the passed street address cannot be parsed, set the second element to null. 15 const TESTCASES = [ 16 ["123 Main St. Apt 4, Floor 2", [123, "Main St.", "4", 2]], 17 ["32 Vassar Street MIT Room 4", [32, "Vassar Street MIT", "4", null]], 18 ["32 Vassar Street MIT", [32, "Vassar Street MIT", null, null]], 19 [ 20 "32 Vassar Street MIT Room 32-G524", 21 [32, "Vassar Street MIT", "32-G524", null], 22 ], 23 ["163 W Hastings\nSuite 209", [163, "W Hastings", "209", null]], 24 ["1234 Elm St. Apt 4, Floor 2", [1234, "Elm St.", "4", 2]], 25 ["456 Oak Drive, Unit 2A", [456, "Oak Drive", "2A", null]], 26 ["789 Maple Ave, Suite 300", [789, "Maple Ave", "300", null]], 27 ["321 Willow Lane, #5", [321, "Willow Lane", "5", null]], 28 ["654 Pine Circle, Apt B", [654, "Pine Circle", "B", null]], 29 ["987 Birch Court, 3rd Floor", [987, "Birch Court", null, 3]], 30 ["234 Cedar Way, Unit 6-2", [234, "Cedar Way", "6-2", null]], 31 ["345 Cherry St, Ste 12", [345, "Cherry St", "12", null]], 32 ["234 Palm St, Bldg 1, Apt 12", null], 33 ]; 34 35 add_task(async function test_parseStreetAddress() { 36 for (const TEST of TESTCASES) { 37 let [address, expected] = TEST; 38 const result = AddressParser.parseStreetAddress(address); 39 if (!expected) { 40 Assert.equal(result, null, "Expect failure to parse this street address"); 41 continue; 42 } 43 44 const options = { 45 trim: true, 46 }; 47 48 const expectedSN = AddressParser.normalizeString(expected[0], options); 49 Assert.equal( 50 result.street_number, 51 expectedSN, 52 `expect street number to be ${expectedSN}, but got ${result.street_number}` 53 ); 54 const expectedSNA = AddressParser.normalizeString(expected[1], options); 55 Assert.equal( 56 result.street_name, 57 expectedSNA, 58 `expect street name to be ${expectedSNA}, but got ${result.street_name}` 59 ); 60 const expectedAN = AddressParser.normalizeString(expected[2], options); 61 Assert.equal( 62 result.apartment_number, 63 expectedAN, 64 `expect apartment number to be ${expectedAN}, but got ${result.apartment_number}` 65 ); 66 const expectedFN = AddressParser.normalizeString(expected[3], options); 67 Assert.equal( 68 result.floor_number, 69 expectedFN, 70 `expect floor number to be ${expectedFN}, but got ${result.floor_number}` 71 ); 72 } 73 });