Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

handle slim exceptions

  • Loading branch information...
commit 7d9c43672760ffbadc1bc5d96a3965139c210a98 1 parent 65f9e69
authored February 23, 2009
2  .gitignore
@@ -7,4 +7,4 @@ bin/
7 7
 obj/
8 8
 TestResult.xml
9 9
 *.cache
10  
-*.bat
  10
+fitnesse*.cmd
1  source/fitnesse/fitnesse.csproj
@@ -43,6 +43,7 @@
43 43
     <Compile Include="slim\Messenger.cs" />
44 44
     <Compile Include="slim\operators\ComposeBoolean.cs" />
45 45
     <Compile Include="slim\operators\ComposeDefault.cs" />
  46
+    <Compile Include="slim\operators\ComposeException.cs" />
46 47
     <Compile Include="slim\operators\ComposeList.cs" />
47 48
     <Compile Include="slim\operators\ExecuteBase.cs" />
48 49
     <Compile Include="slim\operators\ExecuteCall.cs" />
1  source/fitnesse/slim/Service.cs
@@ -21,6 +21,7 @@ public class Service: Processor<string> {
21 21
             AddOperator(new ParseList());
22 22
             AddOperator(new ParseSymbol(), 1);
23 23
             AddOperator(new ComposeDefault());
  24
+            AddOperator(new ComposeException());
24 25
             AddOperator(new ComposeBoolean());
25 26
             AddOperator(new ComposeList());
26 27
         }
41  source/fitnesse/slim/operators/ComposeException.cs
... ...
@@ -0,0 +1,41 @@
  1
+// Copyright © Syterra Software Inc. All rights reserved.
  2
+// The use and distribution terms for this software are covered by the Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
  3
+// which can be found in the file license.txt at the root of this distribution. By using this software in any fashion, you are agreeing
  4
+// to be bound by the terms of this license. You must not remove this notice, or any other, from this software.
  5
+
  6
+using System;
  7
+using fitnesse.mtee.engine;
  8
+using fitnesse.mtee.exception;
  9
+using fitnesse.mtee.model;
  10
+
  11
+namespace fitnesse.slim.operators {
  12
+    public class ComposeException: ComposeOperator<string> {
  13
+        private const string ExceptionResult = "__EXCEPTION__:{0}";
  14
+
  15
+        public bool TryCompose(Processor<string> processor, TypedValue instance, ref Tree<string> result) {
  16
+            if (!typeof(Exception).IsAssignableFrom(instance.Type)) return false;
  17
+
  18
+            if (TryResult<MemberException>(instance,
  19
+                e => string.Format("NO_METHOD_IN_CLASS {0} {1}", e.MemberName, e.Type), ref result)) return true;
  20
+
  21
+            if (TryResult<ConstructorException>(instance,
  22
+                e => string.Format("NO_CONSTRUCTOR {0}", e.Type), ref result)) return true;
  23
+
  24
+            result = MakeResult(instance.Value.ToString());
  25
+            return true;
  26
+        }
  27
+
  28
+        private delegate string Format<T>(T exception);
  29
+
  30
+        private static bool TryResult<T>(TypedValue exception, Format<T> formatter, ref Tree<string> result) where T: class {
  31
+            var candidateException = exception.Value as T;
  32
+            if (candidateException == null) return false;
  33
+            result = MakeResult(string.Format("message<<{0}>> {1}", formatter(candidateException), candidateException));
  34
+            return true;
  35
+        }
  36
+
  37
+        private static Tree<string> MakeResult(string message) {
  38
+            return new TreeLeaf<string>(string.Format(ExceptionResult, message));
  39
+        }
  40
+    }
  41
+}
3  source/fitnesse/slim/operators/ExecuteBase.cs
@@ -10,7 +10,6 @@
10 10
 namespace fitnesse.slim.operators {
11 11
     public abstract class ExecuteBase: ExecuteOperator<string> {
12 12
         private const string defaultResult = "OK";
13  
-        private const string ExceptionResult = "__EXCEPTION__:{0}";
14 13
         private readonly IdentifierName identifier;
15 14
 
16 15
         public bool TryExecute(Processor<string> processor, TypedValue instance, Tree<string> parameters, ref TypedValue result) {
@@ -19,7 +18,7 @@ public abstract class ExecuteBase: ExecuteOperator<string> {
19 18
                 result = new TypedValue(ExecuteOperation(processor, parameters));
20 19
             }
21 20
             catch (Exception e) {
22  
-                result = new TypedValue(Result(parameters, string.Format(ExceptionResult, e)));
  21
+                result = new TypedValue(Result(parameters, processor.Compose(e)));
23 22
             }
24 23
             return true;
25 24
         }
6  source/mtee/Engine/RuntimeType.cs
@@ -23,15 +23,13 @@ public class RuntimeType {
23 23
 
24 24
         public static RuntimeMember GetInstance(TypedValue instance, string memberName, int parameterCount) {
25 25
             RuntimeMember runtimeMember = FindInstance(instance.Value, memberName, parameterCount);
26  
-            if (runtimeMember == null) throw new MemberException(
27  
-                    string.Format("Member '{0}' with {1} parameter(s) not found for type '{2}'.", memberName, parameterCount, instance.Type.FullName));
  26
+            if (runtimeMember == null) throw new MemberException(instance.Type, memberName, parameterCount);
28 27
             return runtimeMember;
29 28
         }
30 29
 
31 30
         public RuntimeMember GetConstructor(int parameterCount) {
32 31
             RuntimeMember runtimeMember = FindInstance(Type, ".ctor", parameterCount);
33  
-            if (runtimeMember == null) throw new MemberException(
34  
-                    string.Format("Constructor with {0} parameter(s) not found for type '{1}'.", parameterCount, Type.FullName));
  32
+            if (runtimeMember == null) throw new ConstructorException(Type, parameterCount);
35 33
             return runtimeMember;
36 34
         }
37 35
 
19  source/mtee/exception/ConstructorException.cs
... ...
@@ -0,0 +1,19 @@
  1
+// Copyright © Syterra Software Inc. All rights reserved.
  2
+// The use and distribution terms for this software are covered by the Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
  3
+// which can be found in the file license.txt at the root of this distribution. By using this software in any fashion, you are agreeing
  4
+// to be bound by the terms of this license. You must not remove this notice, or any other, from this software.
  5
+
  6
+using System;
  7
+
  8
+namespace fitnesse.mtee.exception {
  9
+    public class ConstructorException: ApplicationException {
  10
+        public Type Type { get; private set; }
  11
+        public int ParameterCount { get; private set; }
  12
+
  13
+        public ConstructorException(Type type, int parameterCount)
  14
+            : base(string.Format("Constructor with {1} parameter(s) not found for type '{0}'.", type.FullName, parameterCount)) {
  15
+            Type = type;
  16
+            ParameterCount = parameterCount;
  17
+        }
  18
+    }
  19
+}
11  source/mtee/exception/MemberException.cs
@@ -7,6 +7,15 @@
7 7
 
8 8
 namespace fitnesse.mtee.exception {
9 9
     public class MemberException: ApplicationException {
10  
-        public MemberException(string message): base(message) {}
  10
+        public Type Type { get; private set; }
  11
+        public string MemberName { get; private set; }
  12
+        public int ParameterCount { get; private set; }
  13
+
  14
+        public MemberException(Type type, string memberName, int parameterCount)
  15
+            : base(string.Format("Member '{1}' with {2} parameter(s) not found for type '{0}'.", type.FullName, memberName, parameterCount)) {
  16
+            Type = type;
  17
+            MemberName = memberName;
  18
+            ParameterCount = parameterCount;
  19
+        }
11 20
     }
12 21
 }
1  source/mtee/mtee.csproj
@@ -44,6 +44,7 @@
44 44
     <Compile Include="Engine\RuntimeType.cs" />
45 45
     <Compile Include="Engine\Processor.cs" />
46 46
     <Compile Include="Engine\Operator.cs" />
  47
+    <Compile Include="exception\ConstructorException.cs" />
47 48
     <Compile Include="exception\MemberException.cs" />
48 49
     <Compile Include="exception\ParseException.cs" />
49 50
     <Compile Include="Model\Copyable.cs" />
3  source/sample/SlimSamples.cs
@@ -23,9 +23,6 @@ public class ShouldIBuyMilk {
23 23
         public string goToStore() {
24 24
             return (pints == 0 && (dollars > 2 || creditCard)) ? "yes" : "no";
25 25
         }
26  
-
27  
-        public void execute() {}
28  
-        public void reset() {}
29 26
     }
30 27
 
31 28
     public class EmployeesHiredBefore {
17  source/unitTest/slim/ComposeOperatorsTest.cs
@@ -6,6 +6,7 @@
6 6
 using System;
7 7
 using System.Collections.Generic;
8 8
 using fitnesse.mtee.engine;
  9
+using fitnesse.mtee.exception;
9 10
 using fitnesse.mtee.model;
10 11
 using fitnesse.slim.operators;
11 12
 using NUnit.Framework;
@@ -59,12 +60,28 @@ namespace fitnesse.unitTest.slim {
59 60
             Assert.AreEqual("c", result.Branches[1].Branches[1].Value); 
60 61
         }
61 62
 
  63
+        [Test] public void ExceptionIsComposed() {
  64
+            CheckExceptionCompose(new ApplicationException("blah"), string.Empty);
  65
+        }
  66
+
  67
+        [Test] public void MemberExceptionIsComposed() {
  68
+            CheckExceptionCompose(new MemberException(typeof(string), "garbage", 0), "message<<NO_METHOD_IN_CLASS garbage System.String>> ");
  69
+        }
  70
+
  71
+        [Test] public void ConstructorExceptionIsComposed() {
  72
+            CheckExceptionCompose(new ConstructorException(typeof(string), 0), "message<<NO_CONSTRUCTOR System.String>> ");
  73
+        }
  74
+
62 75
         private Tree<string> Compose(ComposeOperator<string> composeOperator, object instance, Type type) {
63 76
             Tree<string> result = null;
64 77
             Assert.IsTrue(composeOperator.TryCompose(processor, new TypedValue(instance, type), ref result));
65 78
             return result;
66 79
         }
67 80
 
  81
+        private void CheckExceptionCompose(Exception exception, string expected) {
  82
+            CheckCompose(new ComposeException(), exception, exception.GetType(), string.Format("__EXCEPTION__:{0}{1}", expected, exception));
  83
+        }
  84
+
68 85
         private void CheckCompose(ComposeOperator<string> composeOperator, object instance, Type type, object expected) {
69 86
             Tree<string> result = null;
70 87
             Assert.IsTrue(composeOperator.TryCompose(processor, new TypedValue(instance, type), ref result));
1  source/unitTest/slim/ExecuteOperatorsTest.cs
@@ -17,6 +17,7 @@ namespace fitnesse.unitTest.slim {
17 17
         [SetUp] public void SetUp() {
18 18
             processor = new Processor<string>();
19 19
             processor.AddOperator(new ComposeDefault());
  20
+            processor.AddOperator(new ComposeException());
20 21
             processor.AddMemory<SavedInstance>();
21 22
             processor.AddMemory<Symbol>();
22 23
         }

0 notes on commit 7d9c436

Please sign in to comment.
Something went wrong with that request. Please try again.