// ------------------------------------------------------------------

// Operation.cs: Represents OCL testing for operation expressions

// ------------------------------------------------------------------

// Project: C# and OCL Compiler

// Module:  Testing

// Author:  Dave Arnold

// Version: 1.0

// ------------------------------------------------------------------

 

// ------------------------------------------------------------------

// Imports

// ------------------------------------------------------------------

using System;

using System.Collections;

// ------------------------------------------------------------------

 

// ------------------------------------------------------------------

// DaveArnold.OCLTesting Namespace

// ------------------------------------------------------------------

namespace DaveArnold.OCLTesting

{

       class Person

       {

              public string name = "Hello";

       }

      

       // A Class to test the operation expressions

       class OperationTests

       {

              [Query]

              public Person GetPerson()

              {

                     Person p = new Person();

                     return p;

              }

             

              OCL

              [

                     "context OperationTests::PersonName() : String"

                     "post: result = GetPerson().name"

              ]

              public string PersonName()

              {

                     return GetPerson().name;

              }

             

              OCL

              [

                     "context OperationTests::PersonName2() : String"

                     "post: result = GetPerson@pre().name.concat(' World')"

              ]

              public string PersonName2()

              {

                     return GetPerson().name + " World";

              }

             

              OCL

              [

                     "-- Operations Defined via OCL are Query"

                     "context OperationTests::PersonName3() : String"

                     "body: GetPerson().name"

              ]

             

              // The following OCL expression will generate compile time errors

              // because they use operations which do not have the [Query] attribute

              // set.

/*            OCL

              [

                     "context OperationTests::Int2String(s : String) : Integer"

                     "post: result = Int32.Parse(s)"

              ]

              public int Int2String(string s)

              {

                     return Int32.Parse(s);

              }

             

              OCL

              [

                     "context OperationTests::Str() : String"

                     "post: result = ar.ToString()"

              ]

              public string Str()

              {

                     Hashtable ar = new Hashtable();

                     return ar.ToString();

              }

             

              OCL

              [

                     "context OperationTests::StrLength(b : Boolean) : Integer"

                     "post: if b then result = ar.ToString().Length else result = ar2.ToString().Length endif"

              ]

              public int StrLength(bool b)

              {

                     Hashtable ar2 = null, ar = null;

                     if(b)

                     {

                           ar = new Hashtable();

                           return ar.ToString().Length;

                     }

                     else

                     {

                           ar2 = new Hashtable();

                           return ar2.ToString().Length;

                     }

              }

             

              OCL

              [

                     "context OperationTests::StrLength2(b : Boolean) : Integer"

                     "post: if b then result = ar.ToString().Length else result = ar2.ToString().Length endif"

              ]

              public int StrLength2(bool b)

              {

                     if(b)

                     {

                           Hashtable ar = new Hashtable();

                           return ar.ToString().Length;

                     }

                     else

                     {

                           Hashtable ar2 = new Hashtable();

                           return ar2.ToString().Length;

                     }

              } */

      

             

              public void RunTests()

              {

                     Console.WriteLine("---------------- OPERATION TESTS -----------------");

                     // WILL GENERATE COMPILE TIME ERRORS (No [Query])

              //     Console.WriteLine("O1 (Int2String(10)): {0}", this.Int2String("10"));

              //     Console.WriteLine("O2 (Str()): {0}", this.Str());

              //     Console.WriteLine("O3 (StrLength()): {0}", this.StrLength(true));

              //     Console.WriteLine("O4 (StrLength2()): {0}", this.StrLength2(false));

             

                     // CORRECT (Has [Query])

                     Console.WriteLine("O5 (PersonName()): {0}", this.PersonName());

                     Console.WriteLine("O6 (PersonName2()): {0}", this.PersonName2());

                     Console.WriteLine("O7 (PersonName3()): {0}", this.PersonName3());

                     Console.WriteLine("------------ OPERATION TESTS COMPLETE ------------");

              }

       }

}

 

// ------------------------------------------------------------------

// EOF