Created
November 30, 2012 18:01
-
-
Save kberridge/4177403 to your computer and use it in GitHub Desktop.
canopy: dynamically find and initialize contexts in all files in a project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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