Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 182 lines (140 sloc) 4.797 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181

(*******************)
(***Log Example*****)
(*******************)

let logExampleMain() = begin
    Log.setRunLevel Log.Info;

    Log.info "This info message will get logged";
    Log.warn ("This warning will show up in the logs, too, it's higher " ^
        "precedence than Log.Info");
    Log.debug ("This debug statement won't show up. It would only show up " ^
        " if Log.setRunLevel was Log.Debug or Log.Spew")
    end

(********************)
(**Comparer Example**)
(********************)

(** the type we want to be able to compare *)
type tSkyObject =
    |Sun
    |Moon

(** compare function *)
let compareSkyObjects skyObj1 skyObj2 = begin
    match skyObj1, skyObj2 with
    |Sun, Sun -> 0
    |Sun, Moon -> 1
    |Moon, Sun -> (-1)
    |Moon, Moon -> 0
    end

(** toString function *)
let skyObjectToString skyObj = begin
    match skyObj with
    |Sun -> "Sun"
    |Moon -> "Moon"
    end

let comparerExampleMain () = begin

    let skyComp = Comparer.create
        ~toStringFun:skyObjectToString
        ~compareFun:compareSkyObjects
    in

    let s1 = Sun in
    let s2 = Moon in

    if (Comparer.greaterThan skyComp s1 s2) then begin
        Log.info ("s1 was greater than s2 for values: [s1 = " ^
            (Comparer.toString skyComp s1) ^
            ", s2 = " ^
            (Comparer.toString skyComp s2) ^
            "]");
        end;
    end


(********************)
(***Verify Example***)
(********************)

let verifyExampleMain () = begin

    let skyComp = Comparer.create
        ~toStringFun:skyObjectToString
        ~compareFun:compareSkyObjects
    in

    (* set the global type of response to create when a verification
succeeds or fails *)
    CheckHandler.setSuccessResponse Log.Info;
    CheckHandler.setFailResponse CheckHandler.Continue Log.Crash;

    Verify.notEqual
        ~label: "I have two sky objects, making sure they aren't the same"
        ~cmp: skyComp
        ~x1: Sun
        ~x2: Moon;

    (* Most importantly, we can compose higher order Comparers that
still work with the Verify.* methods. see Comparer.make*() methods
for more.
Here, we make a comparer for a pair of type (tSkyObject * int)
*)


    let pairComparer = Comparer.makePair skyComp Comparer.ints in

    (** this verification is going to fail at runtime *)
    Verify.areEqual
        ~label:"Are my pairs equal?"
        ~cmp:pairComparer
        ~x1:(Sun, 1)
        ~x2:(Moon, 2);

    end

(***********************)
(***Unit Test Example***)
(***********************)


let testCaseExampleMain () = begin

    (** application code that creates a list of the numbers (1..x) *)
    let countToX x = begin

        (** this could fail in either testing or while running
the application *)
        Verify.gt
            ~label: "can only count to positive numbers, is x > 0?"
            ~cmp:Comparer.ints
            ~x1: x
            ~x2: 0;

        let rec makeListLoop accList countdown = begin
            match countdown == 0 with
            |true -> accList
            |false -> makeListLoop (countdown::accList) (countdown - 1)
            end
        in

        let countList = makeListLoop [] x in
        countList
        end
    in

    (** a test case that will exercise our countToX method and make sure it
outputs some correct values. *)
    let testCase1 = begin
        let testFun () = begin
            Log.test "Starting TestCase1";

            let observedCountTo3 = countToX 3 in
            let expectedCountTo3 = [1; 2; 3] in

            let intListComparer = ListUtil.makeComparer Comparer.ints in
            
            Verify.areEqual
                ~label:"(exp, obs) countTo3"
                ~cmp: intListComparer
                ~x1: expectedCountTo3
                ~x2: observedCountTo3;

            (** Now to get fancy... This will verify that the block of
code generates a failure. Note that the failure will
occur inside the countToX method. *)

            Verify.doesFail
                ~label:"Passing in a negative number should fail"
                ~block: (fun () -> ignore(countToX (-22)));
                
            Log.test "Finished TestCase1";
            ()
            end
        in
        let atomicTestCase = TestCase.createAtomic
            ~label:"TestCase1"
            ~testFun:testFun
        in
        atomicTestCase
        end
    in

    CheckHandler.setFailResponse CheckHandler.Exit Log.Crash;
    (** now run the test case. Results will be printed to the log (stdout). *)
    ignore (TestCase.run testCase1);
    end


let _ = begin
    logExampleMain ();
    comparerExampleMain ();
    verifyExampleMain ();
    testCaseExampleMain ();
    end

Something went wrong with that request. Please try again.