1(** Basic tests for GPX library *) 2 3open Gpx 4 5let test_coordinate_validation () = 6 (* Test valid coordinates *) 7 assert (Result.is_ok (latitude 45.0)); 8 assert (Result.is_ok (longitude (-122.0))); 9 assert (Result.is_ok (degrees 180.0)); 10 11 (* Test invalid coordinates *) 12 assert (Result.is_error (latitude 91.0)); 13 assert (Result.is_error (longitude 180.0)); 14 assert (Result.is_error (degrees 360.0)); 15 16 Printf.printf "✓ Coordinate validation tests passed\n" 17 18let test_fix_type_conversion () = 19 (* Test fix type string conversion *) 20 assert (fix_type_to_string Fix_2d = "2d"); 21 assert (fix_type_of_string "3d" = Some Fix_3d); 22 assert (fix_type_of_string "invalid" = None); 23 24 Printf.printf "✓ Fix type conversion tests passed\n" 25 26let test_gpx_creation () = 27 let creator = "test" in 28 let gpx = make_gpx ~creator in 29 assert (gpx.creator = creator); 30 assert (gpx.version = "1.1"); 31 assert (gpx.waypoints = []); 32 33 Printf.printf "✓ GPX creation tests passed\n" 34 35let test_simple_parsing () = 36 let gpx_xml = {|<?xml version="1.0" encoding="UTF-8"?> 37<gpx version="1.1" creator="test" xmlns="http://www.topografix.com/GPX/1/1"> 38 <wpt lat="37.7749" lon="-122.4194"> 39 <name>San Francisco</name> 40 <desc>The Golden Gate Bridge area</desc> 41 </wpt> 42</gpx>|} in 43 44 match parse_string gpx_xml with 45 | Ok gpx -> 46 assert (gpx.creator = "test"); 47 assert (List.length gpx.waypoints = 1); 48 let wpt = List.hd gpx.waypoints in 49 assert (wpt.name = Some "San Francisco"); 50 Printf.printf "✓ Simple parsing tests passed\n" 51 | Error e -> 52 Printf.printf "✗ Parsing failed: %s\n" 53 (match e with 54 | Invalid_xml s | Invalid_coordinate s | Validation_error s -> s 55 | _ -> "unknown error"); 56 assert false 57 58let test_simple_writing () = 59 let lat = Result.get_ok (latitude 37.7749) in 60 let lon = Result.get_ok (longitude (-122.4194)) in 61 let wpt = { (make_waypoint_data lat lon) with 62 name = Some "Test Point"; 63 desc = Some "A test waypoint" } in 64 let gpx = { (make_gpx ~creator:"test") with 65 waypoints = [wpt] } in 66 67 match write_string gpx with 68 | Ok xml_string -> 69 assert (try ignore (String.index xml_string 'T'); true with Not_found -> false); 70 assert (try ignore (String.index xml_string '3'); true with Not_found -> false); 71 Printf.printf "✓ Simple writing tests passed\n" 72 | Error e -> 73 Printf.printf "✗ Writing failed: %s\n" 74 (match e with 75 | Invalid_xml s | Xml_error s -> s 76 | _ -> "unknown error"); 77 assert false 78 79let test_validation () = 80 let gpx = make_gpx ~creator:"" in 81 let validation = validate_gpx gpx in 82 assert (not validation.is_valid); 83 let errors = List.filter (fun issue -> issue.level = `Error) validation.issues in 84 assert (List.length errors > 0); 85 86 Printf.printf "✓ Validation tests passed\n" 87 88let run_tests () = 89 Printf.printf "Running GPX library tests...\n"; 90 test_coordinate_validation (); 91 test_fix_type_conversion (); 92 test_gpx_creation (); 93 test_simple_parsing (); 94 test_simple_writing (); 95 test_validation (); 96 Printf.printf "All tests passed! ✓\n" 97 98let () = run_tests ()