2 people like it.

"use"able temporary file

A simple shortcut for creating temporary files for unit tests.

Implementation

1: 
2: 
3: 
4: 
5: 
type TempFile() =
    let path = System.IO.Path.GetTempFileName()
    member x.Path = path
    interface System.IDisposable with
        member x.Dispose() = System.IO.File.Delete(path)

Sample usage

1: 
2: 
3: 
4: 
5: 
6: 
    [<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
Multiple items
type FactAttribute =
  inherit Attribute
  new : unit -> FactAttribute

Full name: Script.FactAttribute

--------------------
new : unit -> FactAttribute
val ReadWriteRoundTrip : unit -> bool

Full name: Script.Tests.ReadWriteRoundTrip
val tmp : TempFile
val data : string
val filewrite : path:'a -> data:'b -> unit

Full name: Script.filewrite
property TempFile.Path: string
type Assert =
  static member Equal : a:obj * b:obj -> bool

Full name: Script.Assert
static member Assert.Equal : a:obj * b:obj -> bool
val fileread : path:'a -> string

Full name: Script.fileread

More information

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