<commit_before>﻿namespace FSharpPlus.Tests

open System
open FSharpPlus
open FSharpPlus.Builders
open FSharpPlus.Data
open NUnit.Framework

[<TestFixture>]
type Workflows() = 

    [<Test>]
    member x.MonadFx() = 
        let effects = ResizeArray()
        let zero = monad {
            effects.Add(1)
            do! Async.Sleep 10
            effects.Add(2) }        
        Assert.AreEqual(effects |> toList, [])

        let combine = monad { 
            if true then do! Async.Sleep 10
            return! zero }
        Assert.AreEqual(effects |> toList, [])
        Async.RunSynchronously combine
        Assert.AreEqual(effects |> toList, [1;2])

    [<Test>]
    member x.MonadPlus() = 
        let effects = ResizeArray()
        let lst: _ list = monad.plus {
            effects.Add(3)
            return 5;
            return 6; }
        Assert.AreEqual(effects |> toList, [3])
        Assert.AreEqual(lst, [5;6])

        let effects = ResizeArray()
        let seq3: seq<_> = monad.plus { 
            effects.Add "Start"
            try
                try
                    10 / 0 |> ignore
                finally
                    effects.Add "execute this"
            with
            | e -> 
                effects.Add (sprintf "Exception! %s" e.Message)
                return 42 }
        Assert.AreEqual(effects |> toList, [])
        let seqValue = seq3 |> Seq.toList
        Assert.AreEqual(effects |> toList, ["Start"; "execute this"; "Exception! Attempted to divide by zero."])
        Assert.AreEqual(seqValue, [42])<commit_msg>+ Helper and comments to CE tests<commit_after>﻿namespace FSharpPlus.Tests

open System
open FSharpPlus
open FSharpPlus.Builders
open FSharpPlus.Data
open NUnit.Framework

module Helpers =
    let areEqual (x:'t) (y:'t) = Assert.AreEqual (x, y)

open Helpers

[<TestFixture>]
type Workflows() = 

    [<Test>]
    member __.MonadFx() =
        let effects = ResizeArray()

        // This workflow perform side-effects before and after an async operation in a monad.fx
        let zerowf = monad {
            effects.Add(1)
            do! Async.Sleep 10
            effects.Add(2) }

        // Check side effects are not yet executed
        areEqual (toList effects) []

        // This workflow will always run the previous one
        let combinewf = monad { 
            if true then do! Async.Sleep 10
            return! zerowf }

        // The list should be empty, no workflow was run
        areEqual (toList effects) []

        Async.RunSynchronously combinewf

        // Since it's an FX workflow, the last line should have been executed
        areEqual (toList effects) [1;2]


    [<Test>]
    member __.MonadPlus() =
        let effects = ResizeArray()

        // This is a plus workflow
        // Although we're not explicitely using a strict workflow list hasn't a proper delay mechanism
        let lst: _ list = monad.plus {
            effects.Add(3)
            return 5;
            return 6; }

        // Check if side effect was already performed
        areEqual (effects |> toList) [3]

        // Check 'plus' (<|>) operation was properly performed
        areEqual lst [5;6]

        let effects = ResizeArray()

        // Now let's a try with seq, which has a delay mechanism
        let seq3: seq<_> = monad.plus { 
            effects.Add "Start"
            try
                try
                    10 / 0 |> ignore
                finally
                    effects.Add "execute this"
            with
            | e -> 
                effects.Add (sprintf "Exception! %s" e.Message)
                return 42 }

        // Confirm the side effect wasn't performed
        areEqual (toList effects) []

        let seqValue = toList seq3

        // Now they should
        areEqual (toList effects) ["Start"; "execute this"; "Exception! Attempted to divide by zero."]

        // Check the result
        areEqual seqValue [42]
{'fraction_non_alphanumeric': 0.09298201165442108, 'fraction_numerical': 0.011654421079300734, 'mean_word_length': 1.6857142857142857, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1783238', 'n_tokens_mistral': 1189, 'n_tokens_neox': 1084, 'n_words': 358}