Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Parsing Latitude and Longitude Information

, 21 Feb 2012
Parses user input and extracts latitude and longitude information, taking into account the user's language and regional settings
using System;
using System.Reflection;
using Geospatial;
using NUnit.Framework;

namespace UnitTests
{
    public static class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {
                Console.Clear();
                Console.WriteLine("Type 'exit' to finish or 'tests' to run the unit tests.\n");
                Console.Write("Coordinate: ");

                string input = Console.ReadLine();
                switch (input.ToUpperInvariant())
                {
                    case "EXIT":
                        return;
                    case "TESTS":
                        RunTests();
                        break;
                    default:
                        Location location;
                        if (!Location.TryParse(input, out location))
                        {
                            Console.WriteLine("Invalid input.");
                        }
                        else
                        {
                            Console.WriteLine("{0:DMS1}\n", location);
                        }
                        break;
                }

                Console.Write("\nPress any key to continue...");
                Console.ReadKey();
            }
        }

        private static void RunTests()
        {
            int totalPassed = 0;
            int totalFailed = 0;
            foreach (var type in Assembly.GetAssembly(typeof(Program)).GetTypes())
            {
                if (type.GetCustomAttributes(typeof(TestFixtureAttribute), false).Length != 0)
                {
                    Console.WriteLine(type.Name);

                    int passed = 0;
                    int failed = 0;
                    RunTests(type, ref passed, ref failed);

                    totalPassed += passed;
                    totalFailed += failed;
                }
            }

            Console.WriteLine("\nTotal number of tests ran:{0} (Failed:{1})\n", totalPassed + totalFailed, totalFailed);
        }

        private static void RunTests(Type type, ref int passed, ref int failed)
        {
            object instance = Activator.CreateInstance(type);
            foreach (var method in type.GetMethods())
            {
                if (method.GetCustomAttributes(typeof(TestAttribute), false).Length != 0)
                {
                    Console.Write("\t{0}...", method.Name);
                    try
                    {
                        method.Invoke(instance, null);
                        Console.WriteLine("passed.");
                        passed++;
                    }
                    catch (TargetInvocationException ex)
                    {
                        Console.WriteLine("FAILED ({0})", ex.InnerException.Message);
                        failed++;
                    }
                }
            }
            Console.WriteLine("Passed:{0} Failed:{1}\n", passed, failed);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Samuel Cragg

United Kingdom United Kingdom
No Biography provided

| Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 21 Feb 2012
Article Copyright 2011 by Samuel Cragg
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid