2 people like it.

"use"able temporary file

A simple shortcut for creating temporary files for unit tests.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
type TempFile() =
    let path = System.IO.Path.GetTempFileName()
    member x.Path = path
    interface System.IDisposable with
        member x.Dispose() = System.IO.File.Delete(path)

(*Example Usage
    [<Fact>] //check that a round trip yields the same result
    let ReadWriteRoundTrip() =
        use tmp = new TempFile()
        let data = "this is a string\nOn two lines\t."
        filewrite tmp.Path data
        Assert.Equal(data, fileread tmp.Path)
*)
Multiple items
type TempFile =
  interface IDisposable
  new : unit -> TempFile
  member Path : string

Full name: Script.TempFile

--------------------
new : unit -> TempFile
val path : string
namespace System
namespace System.IO
type Path =
  static val DirectorySeparatorChar : char
  static val AltDirectorySeparatorChar : char
  static val VolumeSeparatorChar : char
  static val InvalidPathChars : char[]
  static val PathSeparator : char
  static member ChangeExtension : path:string * extension:string -> string
  static member Combine : [<ParamArray>] paths:string[] -> string + 3 overloads
  static member GetDirectoryName : path:string -> string
  static member GetExtension : path:string -> string
  static member GetFileName : path:string -> string
  ...

Full name: System.IO.Path
System.IO.Path.GetTempFileName() : string
val x : TempFile
member TempFile.Path : string

Full name: Script.TempFile.Path
type IDisposable =
  member Dispose : unit -> unit

Full name: System.IDisposable
override TempFile.Dispose : unit -> unit

Full name: Script.TempFile.Dispose
type File =
  static member AppendAllLines : path:string * contents:IEnumerable<string> -> unit + 1 overload
  static member AppendAllText : path:string * contents:string -> unit + 1 overload
  static member AppendText : path:string -> StreamWriter
  static member Copy : sourceFileName:string * destFileName:string -> unit + 1 overload
  static member Create : path:string -> FileStream + 3 overloads
  static member CreateText : path:string -> StreamWriter
  static member Decrypt : path:string -> unit
  static member Delete : path:string -> unit
  static member Encrypt : path:string -> unit
  static member Exists : path:string -> bool
  ...

Full name: System.IO.File
System.IO.File.Delete(path: string) : unit
Next Version Raw view Test code New version

More information

Link:http://fssnip.net/4N
Posted:12 years ago
Author:Benjol
Tags: unittests