Skip to content

Instantly share code, notes, and snippets.

@kberridge
Created November 30, 2012 18:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kberridge/4177403 to your computer and use it in GitHub Desktop.
Save kberridge/4177403 to your computer and use it in GitHub Desktop.
canopy: dynamically find and initialize contexts in all files in a project
// ContextAttribute.fs
module ContextAttribute
type ContextAttribute() =
inherit System.Attribute()
// example_test_file.fs
module example_test_file
open canopy
open runner
open ContextAttribute
[<Context>]
let ``example context`` =
test(fun _ ->
describe "example test"
url "http://google.com"
"input[type='text']" << "hello world")
// Program.fs
module main
open canopy
open runner
open ContextAttribute
let findContexts (m : System.Type) =
let isContext (mem : MemberInfo) =
mem.CustomAttributes |> Seq.exists (fun a -> a.AttributeType = typedefof<ContextAttribute>)
m.GetMembers() |> Seq.filter isContext |> Seq.toArray
let types = System.Reflection.Assembly.GetExecutingAssembly().GetTypes()
let modules = types |> Array.filter (fun t -> FSharpType.IsModule t && t.Name <> "main")
let contextFuns = modules |> Array.collect findContexts
contextFuns |> Array.iter (fun memberInfo ->
context memberInfo.Name
(memberInfo :?> PropertyInfo).GetGetMethod().Invoke(null, null) |> ignore)
start "chrome"
run()
quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment