AOUT.CH1.Examples.SimpleParser.ParseAndSum C# (CSharp) Method

ParseAndSum() public method

public ParseAndSum ( string numbers ) : int
numbers string
return int
        public int ParseAndSum(string numbers)
        {
            if(numbers.Length==0)
            {
                return 0;
            }
            if(!numbers.Contains(","))
            {
                return int.Parse(numbers);
            }
            else
            {
                throw new InvalidOperationException("I can only handle 0 or 1 numbers for now!");
            }
        }

Usage Example

Example #1
0
 public static void TestReturnsZeroWhenEmptyString()
 {
     //use reflection to get the current method's name
     string testName = MethodBase.GetCurrentMethod().Name;
     try
     {
         SimpleParser p = new SimpleParser();
         int result = p.ParseAndSum("1");
         if(result!=0)
         {
             TestUtil.ShowProblem(testName, "Parse and sum should have returned 0 on an empty string");
         }
     }
     catch (Exception e)
     {
         TestUtil.ShowProblem(testName, e.ToString());
     }
 }
SimpleParser