Is there a way to created nested computation expressions?

In F#, I want to build a hierarchical data structure in a way with a minimum
amount of language noise. The actual problem is trying to build an RSpec
inspired framework using F#. RSpec allows the construction of tests in a nested way. For example.

describe Order do
  context "with no items" do
    it "behaves one way" do
      # ...
    end
  end

  context "with one item" do
    it "behaves another way" do
      # ...
    end
  end
end

I have something working here, but there are some
potential problems with the current solution. In particular general
setup/teardown code can become somewhat ugly.

My primary concern is to write an API that allows the user to write
tests/specifications in a way, where the language gets as little in the way as
possible.

I think that using computation expressions could allow my to create a better
API, but I am struggling with implementing nested contexts.

Currently I do have a builder that allows me to write:

let specs =
    describe "Some feature" {
        it "has some behavior" (fun () -> 
            // Test code goes here
        )
    }

But in order to have nested contexts I need to be able to write something like
this:

let specs =
    describe "Some feature" {
        describe "in some context" {
            it "has some behavior" (fun () -> 
                // Test code goes here
            )
        }
        describe "in some other context" {
            it "has some behavior" (fun () -> 
                // Test code goes here
            )
        }
    }

I have no idea how to implement the nested context. Or even if it is possible to
bend F# in a way to allow me to create such a builder.

I did make an experiment, that allowed me to write this:

let specs =
    desribe "Some feature" {
        child (describe "in some other" {
            it "has some behavior" (fun () -> 
                // Test code goes here
            )
        })
        child (describe "in some context" {
            it "has some behavior" (fun () -> 
                // Test code goes here
            )
        })
    }

But the added parenthesis and explicit builder construction is exactly what I
want to avoid in the first place.

type DescribeBuilder(name : string) =
    [<CustomOperation("it")>]
    member __.It (x, name, test : unit -> unit) =
        let t = TestContext.createTest name test
        x |> TestContext.addTest t
    [<CustomOperation("child")>]
    member __.Child(x, child :TestContext.T) =
        x |> TestContext.addChildContext child
    member __.Yield y =
        TestContext.create name
    member __.Delay (x) =
        x()
let describe name = new DescribeBuilder(name)

6

Have you tried any of the bang (!) keywords? You might be able to do it like so:

let specs =
    describe "Some feature" {
        do! describe "in some context" {
            do! it "has some behavior" (fun () -> 
                // Test code goes here
            )
        }
        do! describe "in some other context" {
            do! it "has some behavior" (fun () -> 
                // Test code goes here
            )
        }
    }

assuming your it returns something in the describe workflow.

(The above might be improper F# as I’ve been away from it for a while, but you should be able to do something like it.)

For this, I don’t see why would you want to use computation expressions, a simple discriminated union with single case would suffice:

type Hierarchy = Hierarchy of Hierarchy list

let h = Hierarchy [ Hierarchy []; Hierarchy [ Hierarchy [] ] ]

It’s possible you need something more, but it’s not clear from your example.

1

I saw your fspec project and by proxy got to this question. I like your attempt to use computation expressions (CE). You seem to be trying to use them like macro definitions.

The Ruby example uses context instead of a second describe. Maybe you can do the same.

let specs = 
    describe "Order" {
        context "with no items"
        it "behaves one way" (fun () -> 0)

        context "with one item"
        it "behaves another way" (fun () -> 1)
    }

Just having the members might give the same expressiveness without needing nesting. You can put the yield and child code together for the context. Zero can have an empty TestContext created and it could work with the forwarded child context as x instead on an explicit one.

x.createTest name test
|> x.addTest
f x // forward through continuation

http://fsharpforfunandprofit.com/posts/computation-expressions-intro/

https://docs.microsoft.com/en-us/dotnet/articles/fsharp/language-reference/computation-expressions

So you can do nesting given you that have Delay and Return(Yield) defined, but it may be to noisy with your custom operations. Thank you. Good day.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật