GPS Exchange Format library/CLI in OCaml
1(** Basic tests for GPX library *)
2
3open Gpx
4
5let test_coordinate_validation () =
6 (* Test valid coordinates *)
7 assert (Result.is_ok (Coordinate.latitude 45.0));
8 assert (Result.is_ok (Coordinate.longitude (-122.0)));
9 assert (Result.is_ok (Coordinate.degrees 180.0));
10
11 (* Test invalid coordinates *)
12 assert (Result.is_error (Coordinate.latitude 91.0));
13 assert (Result.is_error (Coordinate.longitude 180.0));
14 assert (Result.is_error (Coordinate.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 (Waypoint.fix_type_to_string Waypoint.Fix_2d = "2d");
21 assert (Waypoint.fix_type_of_string "3d" = Some Waypoint.Fix_3d);
22 assert (Waypoint.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 = Doc.empty ~creator in
29 assert (Doc.creator gpx = creator);
30 assert (Doc.version gpx = "1.1");
31 assert (Doc.waypoints gpx = []);
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 (Doc.creator gpx = "test");
47 let waypoints = Doc.waypoints gpx in
48 assert (List.length waypoints = 1);
49 let wpt = List.hd waypoints in
50 assert (Waypoint.name wpt = Some "San Francisco");
51 Printf.printf "✓ Simple parsing tests passed\n"
52 | Error e ->
53 Printf.printf "✗ Parsing failed: %s\n" (Error.to_string e);
54 assert false
55
56let test_simple_writing () =
57 let lat = Result.get_ok (Coordinate.latitude 37.7749) in
58 let lon = Result.get_ok (Coordinate.longitude (-122.4194)) in
59 let wpt = Waypoint.make lat lon in
60 let wpt = { wpt with name = Some "Test Point"; desc = Some "A test waypoint" } in
61 let gpx = Doc.empty ~creator:"test" in
62 let gpx = Doc.add_waypoint gpx wpt in
63
64 match write_string gpx with
65 | Ok xml_string ->
66 assert (try ignore (String.index xml_string 'T'); true with Not_found -> false);
67 assert (try ignore (String.index xml_string '3'); true with Not_found -> false);
68 Printf.printf "✓ Simple writing tests passed\n"
69 | Error e ->
70 Printf.printf "✗ Writing failed: %s\n" (Error.to_string e);
71 assert false
72
73let test_validation () =
74 let gpx = Doc.empty ~creator:"" in
75 let validation = validate_gpx gpx in
76 assert (not validation.is_valid);
77 let errors = List.filter (fun issue -> issue.level = `Error) validation.issues in
78 assert (List.length errors > 0);
79
80 Printf.printf "✓ Validation tests passed\n"
81
82let run_tests () =
83 Printf.printf "Running GPX library tests...\n";
84 test_coordinate_validation ();
85 test_fix_type_conversion ();
86 test_gpx_creation ();
87 test_simple_parsing ();
88 test_simple_writing ();
89 test_validation ();
90 Printf.printf "All tests passed! ✓\n"
91
92let () = run_tests ()