SPB Git

Repository Public BSD-3-Clause

InstructionTests.swift 14.2 KB 229 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : ORIX Maps9// File             : Packages/ORIXRouting/Tests/ORIXRoutingTests/InstructionTests.swift10// Module           : ORIXRoutingTests11// Repository       : https://git.spboucher.ai/orix-maps12// Created          : 2026-11-2513// Last Modified    : 2026-11-2514//15// =============================================================================16// AUTHOR & PROFESSIONAL AFFILIATION17// =============================================================================18//19// Author           : Simon-Pierre Boucher20// Position         : Professor of Finance21// Institution      : Université du Québec en Outaouais (UQO)22// Department       : Département des sciences administratives23// Organization     : SP Boucher24//25// Website          : https://spboucher.ai26// Git              : https://git.spboucher.ai27//28// =============================================================================29// OFFICIAL CONTACT DIRECTORY30// =============================================================================31//32// General          : info@spboucher.ai33// Professional     : contact@spboucher.ai34// Direct           : simon@spboucher.ai35// Research         : research@spboucher.ai36// Development      : dev@spboucher.ai37// Support          : support@spboucher.ai38// Security/Admin   : admin@spboucher.ai39// Notifications    : notifications@spboucher.ai40//41// Preferred Contact: support@spboucher.ai42//43// =============================================================================44// COPYRIGHT & INTELLECTUAL PROPERTY45// =============================================================================46//47// Copyright (c) 202648// Simon-Pierre Boucher49//50// All rights reserved where applicable.51//52// This file is governed by the applicable project license,53// copyright notices, institutional agreements and relevant law.54//55// Unless expressly authorized by the applicable license or56// rights holder, copying, redistribution, sublicensing,57// publication and commercial exploitation are not permitted.58//59// Existing third-party rights and notices remain unaffected.60//61// =============================================================================62// CONFIDENTIALITY & SECURITY63// =============================================================================64//65// Classification   : Public66// Distribution     : Public repository; use is governed by the project license67// Security Contact : admin@spboucher.ai68//69// Access to this source does not independently grant permission70// to redistribute or disclose confidential materials.71//72// Do not embed credentials, private keys, access tokens,73// certificates or confidential personal information.74//75// =============================================================================76// LICENSE & MAINTENANCE77// =============================================================================78//79// License          : Proprietary, all rights reserved (see license file)80// License File     : LICENSE81// Maintainer       : Simon-Pierre Boucher82// Technical Contact: dev@spboucher.ai83//84// =============================================================================85// END OF SP BOUCHER SOURCE HEADER86// =============================================================================8788import Foundation89import ORIXGeoCore90import Testing91@testable import ORIXRouting9293@Suite struct InstructionTests {94    static func maneuver(_ type: ManeuverType, _ modifier: ManeuverModifier? = nil, name: String? = nil, ref: String? = nil,95                         roadClass: RoadClass = .residential, exit: Int? = nil, waypoint: Int? = nil, bearing: Double = 0) -> Maneuver {96        Maneuver(type: type, modifier: modifier, roundaboutExit: exit, waypointIndex: waypoint, location: Coordinate(latitude: 45.5, longitude: -73.6),97                 bearingBefore: bearing, bearingAfter: bearing, name: name, ref: ref, roadClass: roadClass, distanceMeters: 100,98                 durationSeconds: 10, geometryRange: 0...1, legIndex: 0)99    }100101    let en = InstructionFormatter(language: .english, units: .metric)102    let fr = InstructionFormatter(language: .french, units: .metric)103104    @Test func turns() {105        let m = Self.maneuver(.turn, .left, name: "Rue Sherbrooke Ouest")106        #expect(en.instruction(for: m) == "Turn left onto Rue Sherbrooke Ouest")107        #expect(fr.instruction(for: m) == "Tournez à gauche sur la rue Sherbrooke Ouest")108        #expect(en.instruction(for: Self.maneuver(.turn, .slightRight, name: "Avenue du Parc")) == "Turn slightly right onto Avenue du Parc")109        #expect(fr.instruction(for: Self.maneuver(.turn, .slightRight, name: "Avenue du Parc")) == "Tournez légèrement à droite sur l'avenue du Parc")110        #expect(fr.instruction(for: Self.maneuver(.turn, .sharpLeft, name: "Boulevard René-Lévesque Est")) == "Tournez franchement à gauche sur le boulevard René-Lévesque Est")111        #expect(en.instruction(for: Self.maneuver(.turn, .sharpRight)) == "Turn sharply right")112        #expect(fr.instruction(for: Self.maneuver(.turn, .right)) == "Tournez à droite")113        #expect(en.instruction(for: Self.maneuver(.continue, .straight, name: "Chemin Sainte-Foy")) == "Continue onto Chemin Sainte-Foy")114        #expect(fr.instruction(for: Self.maneuver(.continue, .straight, name: "Chemin Sainte-Foy")) == "Continuez sur le chemin Sainte-Foy")115        #expect(fr.instruction(for: Self.maneuver(.continue, .straight)) == "Continuez tout droit")116        #expect(en.instruction(for: Self.maneuver(.uTurn, .uTurn)) == "Make a U-turn")117        #expect(fr.instruction(for: Self.maneuver(.uTurn, .uTurn, name: "Rue Ontario Est")) == "Faites demi-tour et continuez sur la rue Ontario Est")118    }119120    @Test func departArriveAndStops() {121        #expect(en.instruction(for: Self.maneuver(.depart, name: "Rue Saint-Denis", bearing: 5)) == "Head north on Rue Saint-Denis")122        #expect(fr.instruction(for: Self.maneuver(.depart, name: "Rue Saint-Denis", bearing: 5)) == "Dirigez-vous vers le nord sur la rue Saint-Denis")123        #expect(fr.instruction(for: Self.maneuver(.depart, bearing: 92)) == "Dirigez-vous vers l'est")124        #expect(en.instruction(for: Self.maneuver(.depart, bearing: 225)) == "Head southwest")125        #expect(fr.instruction(for: Self.maneuver(.depart, bearing: 315)) == "Dirigez-vous vers le nord-ouest")126        #expect(en.instruction(for: Self.maneuver(.arrive)) == "You have arrived at your destination")127        #expect(en.instruction(for: Self.maneuver(.arrive, .right)) == "You have arrived at your destination, on the right")128        #expect(fr.instruction(for: Self.maneuver(.arrive, .left)) == "Vous êtes arrivé à destination, à votre gauche")129        #expect(en.instruction(for: Self.maneuver(.waypoint, waypoint: 2)) == "You have reached stop 2")130        #expect(fr.instruction(for: Self.maneuver(.waypoint, .right, waypoint: 1)) == "Vous êtes arrivé à l'étape 1, à votre droite")131    }132133    @Test func roundaboutsRampsForksAndFerries() {134        let second = Self.maneuver(.roundabout, name: "Rue Principale", exit: 2)135        #expect(en.instruction(for: second) == "At the roundabout, take the 2nd exit onto Rue Principale")136        #expect(fr.instruction(for: second) == "Au carrefour giratoire, prenez la 2e sortie sur la rue Principale")137        #expect(fr.instruction(for: Self.maneuver(.roundabout, exit: 1)) == "Au carrefour giratoire, prenez la 1re sortie")138        #expect(en.instruction(for: Self.maneuver(.roundabout, exit: 3)) == "At the roundabout, take the 3rd exit")139        #expect(en.instruction(for: Self.maneuver(.roundabout, exit: 11)) == "At the roundabout, take the 11th exit")140        #expect(en.instruction(for: Self.maneuver(.roundabout)) == "Enter the roundabout")141        #expect(fr.instruction(for: Self.maneuver(.exitRoundabout, name: "Rue du Nord")) == "Quittez le carrefour giratoire sur la rue du Nord")142143        let merge = Self.maneuver(.merge, .slightLeft, name: "Autoroute Jean-Lesage", ref: "20", roadClass: .motorway)144        #expect(en.instruction(for: merge) == "Merge left onto Autoroute Jean-Lesage (20)")145        #expect(fr.instruction(for: merge) == "Rejoignez l'autoroute Jean-Lesage (20)")146        let ramp = Self.maneuver(.onRamp, .slightRight, ref: "40", roadClass: .motorway)147        #expect(en.instruction(for: ramp) == "Take the ramp on the right onto Highway 40")148        #expect(fr.instruction(for: ramp) == "Prenez la bretelle à droite vers l'autoroute 40")149        let exit = Self.maneuver(.offRamp, .slightRight, name: "Boulevard Taschereau", ref: "134", roadClass: .primary)150        #expect(en.instruction(for: exit) == "Take the exit on the right toward Boulevard Taschereau (134)")151        #expect(fr.instruction(for: exit) == "Prenez la sortie à droite vers le boulevard Taschereau (134)")152        #expect(fr.instruction(for: Self.maneuver(.offRamp, .slightLeft)) == "Prenez la sortie à gauche")153        let fork = Self.maneuver(.fork, .slightLeft, ref: "132", roadClass: .primary)154        #expect(en.instruction(for: fork) == "Keep left at the fork onto Route 132")155        #expect(fr.instruction(for: fork) == "Gardez la gauche à l'embranchement pour prendre la route 132")156        #expect(fr.instruction(for: Self.maneuver(.fork, .slightRight)) == "Gardez la droite à l'embranchement")157        let ferry = Self.maneuver(.ferry, name: "Traversier Québec–Lévis", roadClass: .ferry)158        #expect(en.instruction(for: ferry) == "Take the ferry Traversier Québec–Lévis")159        #expect(fr.instruction(for: Self.maneuver(.ferry, roadClass: .ferry)) == "Prenez le traversier")160    }161162    @Test func frenchRoadNames() {163        #expect(InstructionFormatter.frenchRoad("Rue Sherbrooke Ouest") == "la rue Sherbrooke Ouest")164        #expect(InstructionFormatter.frenchRoad("Avenue du Mont-Royal Est") == "l'avenue du Mont-Royal Est")165        #expect(InstructionFormatter.frenchRoad("Chemin de la Côte-des-Neiges") == "le chemin de la Côte-des-Neiges")166        #expect(InstructionFormatter.frenchRoad("Autoroute Décarie") == "l'autoroute Décarie")167        #expect(InstructionFormatter.frenchRoad("Pont Jacques-Cartier") == "le pont Jacques-Cartier")168        #expect(InstructionFormatter.frenchRoad("3e Avenue") == "la 3e Avenue")169        #expect(InstructionFormatter.frenchRoad("1re Rue") == "la 1re Rue")170        #expect(InstructionFormatter.frenchRoad("5e Rang") == "le 5e Rang")171        #expect(InstructionFormatter.frenchRoad("Grande Allée Est") == "Grande Allée Est")172        #expect(InstructionFormatter.frenchRoad("Main Street") == "Main Street")173        #expect(en.roadName(for: Self.maneuver(.turn, .left, name: "Rue Sherbrooke Ouest", ref: "138", roadClass: .primary)) == "Rue Sherbrooke Ouest (138)")174        #expect(en.roadName(for: Self.maneuver(.turn, .left, name: "Rue Ontario", ref: "9", roadClass: .residential)) == "Rue Ontario")175        #expect(en.roadName(for: Self.maneuver(.turn, .left, ref: "20;132", roadClass: .motorway)) == "Highway 20 / 132")176        #expect(fr.roadName(for: Self.maneuver(.turn, .left, ref: "A-20", roadClass: .motorway)) == "A-20")177        #expect(en.roadName(for: Self.maneuver(.turn, .left)) == nil)178    }179180    @Test func distances() {181        let imperialEN = InstructionFormatter(language: .english, units: .imperial), imperialFR = InstructionFormatter(language: .french, units: .imperial)182        #expect(en.distance(4) == "10 m")183        #expect(en.distance(47) == "50 m")184        #expect(en.distance(312) == "300 m")185        #expect(en.distance(985) == "1 km")186        #expect(en.distance(1234) == "1.2 km")187        #expect(fr.distance(1234) == "1,2 km")188        #expect(en.distance(25_400) == "25 km")189        #expect(en.distance(312, style: .spoken) == "300 metres")190        #expect(fr.distance(312, style: .spoken) == "300 mètres")191        #expect(en.distance(1000, style: .spoken) == "1 kilometre")192        #expect(fr.distance(1500, style: .spoken) == "1,5 kilomètre")193        #expect(fr.distance(2400, style: .spoken) == "2,4 kilomètres")194        #expect(imperialEN.distance(100) == "350 ft")195        #expect(imperialFR.distance(100) == "350 pi")196        #expect(imperialEN.distance(100, style: .spoken) == "350 feet")197        #expect(imperialFR.distance(100, style: .spoken) == "350 pieds")198        #expect(imperialEN.distance(800) == "0.5 mi")199        #expect(imperialFR.distance(800) == "0,5 mi")200        #expect(imperialEN.distance(1609.344, style: .spoken) == "1 mile")201        #expect(imperialEN.distance(16_093.44, style: .spoken) == "10 miles")202        #expect(imperialFR.distance(4828, style: .spoken) == "3 milles")203        #expect(en.distance(.nan) == "10 m")204        #expect(en.duration(40) == "1 min" && en.duration(10) == "less than 1 min" && fr.duration(10) == "moins de 1 min")205        #expect(en.duration(9900) == "2 h 45 min" && en.duration(7200) == "2 h")206    }207208    @Test func announcements() {209        let m = Self.maneuver(.turn, .left, name: "Rue Sherbrooke Ouest")210        #expect(en.announcement(for: m, distanceAheadMeters: 300) == "In 300 metres, turn left onto Rue Sherbrooke Ouest")211        #expect(fr.announcement(for: m, distanceAheadMeters: 300) == "Dans 300 mètres, tournez à gauche sur la rue Sherbrooke Ouest")212        #expect(fr.announcement(for: m, distanceAheadMeters: 1500, style: .written) == "Dans 1,5 km, tournez à gauche sur la rue Sherbrooke Ouest")213        let imperial = InstructionFormatter(language: .english, units: .imperial)214        #expect(imperial.announcement(for: Self.maneuver(.roundabout, exit: 2), distanceAheadMeters: 400) == "In 0.2 miles, at the roundabout, take the 2nd exit")215    }216217    @Test func everyManeuverTypeHasTextInBothLanguages() {218        for type in ManeuverType.allCases {219            for modifier in [nil] + ManeuverModifier.allCases.map(Optional.some) {220                for name in [nil, "Rue Test"] {221                    let m = Self.maneuver(type, modifier, name: name, exit: type == .roundabout ? 2 : nil, waypoint: 1)222                    #expect(!en.instruction(for: m).isEmpty && !fr.instruction(for: m).isEmpty)223                    #expect(en.instruction(for: m) != fr.instruction(for: m))224                }225            }226        }227    }228}229
Files main
Menu

Theme