1- module ` `action block allows ``
1+ module ` `action block is capable of ``
22
33open NUnit.Framework
44open Xake
@@ -7,7 +7,7 @@ let makeStringList() = new System.Collections.Generic.List<string>()
77let DebugOptions = { ExecOptions.Default with FailOnError = true ; FileLog = " " }
88
99[<Test>]
10- let ``executes the body`` () =
10+ let ``execute the body`` () =
1111
1212 let wasExecuted = ref false
1313
@@ -20,7 +20,7 @@ let ``executes the body``() =
2020 Assert.IsTrue(! wasExecuted)
2121
2222[<Test>]
23- let ``execution is ordered`` () =
23+ let ``ordered execution `` () =
2424
2525 let wasExecuted = ref false
2626
@@ -41,7 +41,7 @@ let ``execution is ordered``() =
4141 Assert.That( errorlist, Is.EquivalentTo([ " 1" ; " 2" ; " 3" ]))
4242
4343[<Test>]
44- let ``allows async operations`` () =
44+ let ``starting async operations`` () =
4545
4646 let wasExecuted = ref false
4747
@@ -63,7 +63,7 @@ let ``allows async operations``() =
6363 Assert.That( errorlist, Is.EqualTo([ " 1" ; " 2" ; " 3" ; " 4" ]))
6464
6565[<Test>]
66- let ``do ! action`` () =
66+ let ``invoke other actions within action ( do !) ``() =
6767
6868 let wasExecuted = ref false
6969
@@ -88,7 +88,7 @@ let ``do! action``() =
8888
8989
9090[<Test>]
91- let ``do ! action with result ignored ``() =
91+ let ``ignoring result of do ! action ``() =
9292
9393 let wasExecuted = ref false
9494
@@ -117,7 +117,7 @@ let ``do! action with result ignored``() =
117117
118118
119119[<Test>]
120- let ``let ! returning value ``() =
120+ let ``obtaining action result `` () =
121121
122122 let errorlist = makeStringList()
123123 let note = errorlist.Add
@@ -140,7 +140,7 @@ let ``let! returning value``() =
140140 Assert.That( errorlist, Is.EqualTo([ " 1" ; " 2+1" ; " 3" ] |> List.toArray))
141141
142142[<Test>]
143- let ``if of various kinds `` () =
143+ let ``ifs within actions `` () =
144144
145145 let errorlist = makeStringList()
146146 let note = errorlist.Add
@@ -172,7 +172,7 @@ let ``if of various kinds``() =
172172 Assert.That( errorlist, Is.EqualTo([ " i1-t" ; " i2-f" ; " 2" ; " 3" ] |> List.toArray))
173173
174174[<Test>]
175- let ``if without else`` () =
175+ let ``branching using if without else`` () =
176176
177177 let errorlist = makeStringList()
178178 let note = errorlist.Add
@@ -202,7 +202,7 @@ let ``if without else``() =
202202 Assert.That( errorlist, Is.EqualTo([ " i1-t" ; " 3" ; " 4" ] |> List.toArray))
203203
204204[<Test>]
205- let ``for and while`` () =
205+ let ``for and while loops `` () =
206206
207207 let errorlist = makeStringList()
208208 let note = errorlist.Add
@@ -230,40 +230,128 @@ let ``for and while``() =
230230
231231 Assert.That( errorlist, Is.EqualTo([ " 1" ; " i=1" ; " i=2" ; " i=3" ; " j=3" ; " j=4" ; " 4" ] |> List.toArray))
232232
233- [<Test; Explicit >]
234- let ``try catch finally`` () =
233+ [<Test>]
234+ let ``exception handling with ' try finally' ``() =
235235
236236 let errorlist = makeStringList()
237237 let note = errorlist.Add
238+ let anote txt = action {
239+ do note txt
240+ }
238241
239242 do xake DebugOptions {
240243
241244 phony " main" ( action {
245+ note " before try"
246+ try
247+ printfn " Body executed"
248+ do ! anote " try"
249+ finally
250+ printfn " Finally executed"
251+ do note " finally"
252+ do note " 4"
253+ })
254+ }
242255
243- let! s1 = action { return " 122 " }
244- do note s1
256+ // printfn "%A" errorlist
257+ Assert.That ( errorlist , Is.EqualTo ([ " before try " ; " try " ; " finally " ; " 4 " ] |> List.toArray ))
245258
246- note " before try"
259+ [<Test>]
260+ let ``try finally fail`` () =
247261
248- // try
249- // printfn "Body executed"
250- // do note "try"
251- // finally
252- // printfn "Finally executed"
253- // do note "finally"
254-
255- // try
256- // failwith "ee"
257- // with e ->
258- // do note e.Message
262+ let errorlist = makeStringList()
263+ let note = errorlist.Add
264+ let anote txt = action {
265+ do errorlist.Add txt
266+ }
267+
268+ do xake DebugOptions {
269+
270+ phony " main" ( action {
271+ do ! anote " before try"
272+
273+ try
274+ printfn " Body executed"
275+ do ! anote " try"
276+ failwith " Ouch"
277+ finally
278+ printfn " Finally executed"
279+ do note " finally"
259280
260- do note " 4"
281+ do ! anote " 4"
282+ } |> WhenError ignore)
283+ }
284+
285+ // printfn "%A" errorlist
286+ Assert.That( errorlist, Is.EqualTo([ " before try" ; " try" ; " finally" ] |> List.toArray))
287+
288+ [<Test>]
289+ let ``exception handling with 'try with'`` () =
290+
291+ let errorlist = makeStringList()
292+ let anote txt = action {
293+ do errorlist.Add txt
294+ }
295+
296+ do xake DebugOptions {
297+
298+ phony " main" ( action {
299+ do ! anote " before try"
300+
301+ try
302+ printfn " Body executed"
303+ do ! anote " try"
304+ failwith " Ouch"
305+ with e ->
306+ do ! anote e.Message
307+
308+ do ! anote " 4"
261309 })
310+ }
311+
312+ // printfn "%A" errorlist
313+ Assert.That( errorlist, Is.EqualTo([ " before try" ; " try" ; " Ouch" ; " 4" ] |> List.toArray))
314+
315+ [<Test>]
316+ let ``WhenError function to handle exceptions within actions`` () =
317+
318+ let taskReturn n = action {
319+ return n
320+ }
321+
322+ let excCount = ref 0
323+ do xake DebugOptions {
324+ rules [
325+ " main" => (
326+ WhenError ( fun _ -> excCount := 1 ) <|
327+ action {
328+ printfn " Some useful job"
329+ do ! taskReturn 3 |> FailWhen ((=) 3 ) " err" |> Action.Ignore
330+ printfn " This wont run"
331+ })
332+ ]
333+ }
334+
335+ Assert.AreEqual( 1 , ! excCount)
336+
337+ [<Test>]
338+ let ``try / with for the whole script body``() =
339+ let excCount = ref 0
340+ do xake DebugOptions {
341+ rules [
342+ " main" =>
343+ action {
344+ try
345+ printfn " Some useful job"
346+ do 3 / 0 |> ignore
347+ printfn " This wont run"
348+ with _ ->
349+ excCount := 1
350+ }
351+ ]
262352 }
263353
264- // "2222"; "ee";
265- printfn " %A " errorlist
266- Assert.That( errorlist, Is.EqualTo([ " 122" ; " try" ; " finally" ; " 4" ] |> List.toArray))
354+ Assert.AreEqual( 1 , ! excCount)
267355
268356// TODO use!, try with exception within action
269357
0 commit comments