|
| 1 | +import { expect } from "./_chai.spec" |
| 2 | +import { |
| 3 | + parseCoordinate, |
| 4 | + parseCoordinates, |
| 5 | + parseDecimalCoordinate, |
| 6 | +} from "./CoordinateParser" |
| 7 | + |
| 8 | +describe("Coordinate Parser", () => { |
| 9 | + describe("parsePosition", () => { |
| 10 | + it("should parse valid DMS coordinates", () => { |
| 11 | + const input = "40° 26' 46\" N 79° 58' 56\" W" |
| 12 | + const result = parseCoordinates(input) |
| 13 | + expect(result).to.eql({ |
| 14 | + latitude: 40.446111, |
| 15 | + longitude: -79.982222, |
| 16 | + }) |
| 17 | + }) |
| 18 | + it("should parse valid DMS coordinates from ExifTool", () => { |
| 19 | + const input = "37 deg 46' 29.64\" N, 122 deg 25' 9.85\" W" |
| 20 | + const result = parseCoordinates(input) |
| 21 | + expect(result).to.eql({ |
| 22 | + latitude: 37.7749, |
| 23 | + longitude: -122.419403, |
| 24 | + }) |
| 25 | + }) |
| 26 | + |
| 27 | + it("should parse valid DM coordinates", () => { |
| 28 | + const input = "40° 26.767' N 79° 58.933' W" |
| 29 | + const result = parseCoordinates(input) |
| 30 | + expect(result).to.eql({ |
| 31 | + latitude: 40.446117, |
| 32 | + longitude: -79.982217, |
| 33 | + }) |
| 34 | + }) |
| 35 | + |
| 36 | + it("should parse valid decimal coordinates", () => { |
| 37 | + const input = "40.44611° N 79.98222° W" |
| 38 | + const result = parseCoordinates(input) |
| 39 | + expect(result).to.eql({ |
| 40 | + latitude: 40.44611, |
| 41 | + longitude: -79.98222, |
| 42 | + }) |
| 43 | + }) |
| 44 | + |
| 45 | + it("should throw on empty input", () => { |
| 46 | + expect(() => parseCoordinates("")).to.throw( |
| 47 | + "Input string cannot be empty" |
| 48 | + ) |
| 49 | + }) |
| 50 | + |
| 51 | + it("should throw on multiple latitude values", () => { |
| 52 | + const input = "40° N 50° N" |
| 53 | + expect(() => parseCoordinates(input)).to.throw( |
| 54 | + "Multiple latitude values found" |
| 55 | + ) |
| 56 | + }) |
| 57 | + }) |
| 58 | + |
| 59 | + describe("parseDecimalCoordinate", () => { |
| 60 | + it("should parse valid decimal coordinate", () => { |
| 61 | + const result = parseDecimalCoordinate("40.44611° N") |
| 62 | + expect(result).to.eql({ |
| 63 | + degrees: 40.44611, |
| 64 | + direction: "N", |
| 65 | + }) |
| 66 | + }) |
| 67 | + |
| 68 | + it("should throw on non-decimal format", () => { |
| 69 | + expect(() => parseDecimalCoordinate("40° 26' N")).to.throw( |
| 70 | + "Expected decimal degrees format" |
| 71 | + ) |
| 72 | + }) |
| 73 | + }) |
| 74 | + |
| 75 | + describe("parseCoordinates", () => { |
| 76 | + it("should parse multiple coordinates", () => { |
| 77 | + const input = "40° N 79° W" |
| 78 | + const result = parseCoordinates(input) |
| 79 | + expect(result).to.eql({ |
| 80 | + latitude: 40, |
| 81 | + longitude: -79, |
| 82 | + }) |
| 83 | + }) |
| 84 | + |
| 85 | + it("should handle mixed formats", () => { |
| 86 | + const input = "40° 26' 46\" N 79.98222° W" |
| 87 | + const result = parseCoordinates(input) |
| 88 | + expect(result).to.eql({ |
| 89 | + latitude: 40.446111, |
| 90 | + longitude: -79.98222, |
| 91 | + }) |
| 92 | + }) |
| 93 | + }) |
| 94 | + |
| 95 | + describe("parseCoordinate", () => { |
| 96 | + it("should parse DMS format", () => { |
| 97 | + const result = parseCoordinate("40° 26' 46\" N") |
| 98 | + expect(result).to.eql({ |
| 99 | + degrees: 40, |
| 100 | + minutes: 26, |
| 101 | + seconds: 46, |
| 102 | + direction: "N", |
| 103 | + format: "DMS", |
| 104 | + remainder: "", |
| 105 | + }) |
| 106 | + }) |
| 107 | + |
| 108 | + it("should parse DM format", () => { |
| 109 | + const result = parseCoordinate("40° 26.767' N") |
| 110 | + expect(result).to.eql({ |
| 111 | + degrees: 40, |
| 112 | + minutes: 26.767, |
| 113 | + seconds: undefined, |
| 114 | + direction: "N", |
| 115 | + format: "DM", |
| 116 | + remainder: "", |
| 117 | + }) |
| 118 | + }) |
| 119 | + |
| 120 | + it("should parse decimal format", () => { |
| 121 | + const result = parseCoordinate("40.44611° N") |
| 122 | + expect(result).to.eql({ |
| 123 | + degrees: 40.44611, |
| 124 | + minutes: undefined, |
| 125 | + seconds: undefined, |
| 126 | + direction: "N", |
| 127 | + format: "D", |
| 128 | + remainder: "", |
| 129 | + }) |
| 130 | + }) |
| 131 | + |
| 132 | + it("should handle negative degrees", () => { |
| 133 | + const result = parseCoordinate("-40.44611° S") |
| 134 | + expect(result.degrees).to.eql(-40.44611) |
| 135 | + }) |
| 136 | + |
| 137 | + it("should handle remainder text", () => { |
| 138 | + const result = parseCoordinate("40° N Additional Text") |
| 139 | + expect(result.remainder).to.eql("Additional Text") |
| 140 | + }) |
| 141 | + |
| 142 | + it("should throw on invalid minutes", () => { |
| 143 | + expect(() => parseCoordinate("40° 60' N")).to.throw( |
| 144 | + "Minutes must be between 0 and 59" |
| 145 | + ) |
| 146 | + }) |
| 147 | + |
| 148 | + it("should throw on invalid seconds", () => { |
| 149 | + expect(() => parseCoordinate("40° 30' 60\" N")).to.throw( |
| 150 | + "Seconds must be between 0 and 59" |
| 151 | + ) |
| 152 | + }) |
| 153 | + |
| 154 | + it("should throw on invalid latitude degrees", () => { |
| 155 | + expect(() => parseCoordinate("91° N")).to.throw( |
| 156 | + "Degrees must be between -90 and 90" |
| 157 | + ) |
| 158 | + }) |
| 159 | + |
| 160 | + it("should throw on invalid longitude degrees", () => { |
| 161 | + expect(() => parseCoordinate("181° E")).to.throw( |
| 162 | + "Degrees must be between -180 and 180" |
| 163 | + ) |
| 164 | + }) |
| 165 | + }) |
| 166 | +}) |
0 commit comments