Skip to content

Instantly share code, notes, and snippets.

@gsscoder
Last active November 7, 2019 14:17
Show Gist options
  • Save gsscoder/4655503 to your computer and use it in GitHub Desktop.
Save gsscoder/4655503 to your computer and use it in GitHub Desktop.
Simple F# program to rename my C# test fixture methods
(*
Description: Simple program to change naming conventions of my C# test fixtures.
Purpose: Learn F#!
Author: Giacomo Stelluti Scala
Created: 2013-01-28
*)
open System
open System.IO
open System.Globalization
open System.Text
let readLines path =
File.ReadAllLines(path)
let isUppercase (c:char) =
let cat = Char.GetUnicodeCategory(c)
if cat = UnicodeCategory.UppercaseLetter then
true
else
false
let changeLine (line:string) =
let parts = line.Split( [|" "|], StringSplitOptions.RemoveEmptyEntries)
if parts.Length >= 2 &&
String.CompareOrdinal(parts.[0], "public") = 0 &&
String.CompareOrdinal(parts.[1], "void") = 0 then
let newName = seq { for c in parts.[2] do
if isUppercase(c) then
yield "_" + c.ToString().ToLowerInvariant()
else
yield c.ToString() }
let interm = newName |> Seq.reduce (+)
let newMethod = interm.[1].ToString().ToUpperInvariant() + interm.Substring(2, interm.Length - 2)
line.Replace(parts.[2], newMethod)
else
line
[<EntryPoint>]
let main argv =
if argv.Length <> 1 then
printfn "Usage: RenameMethods [CSHARPFILE] >[NEW-CSHARPFILE]"
1
else
let lines = readLines argv.[0]
let mutable canChange = false
let buffer = new StringBuilder()
for line in lines do
if line.Contains("[Fact]") then
canChange <- true
ignore(buffer.AppendLine(line))
else
if canChange = true then
canChange <- false
ignore(buffer.AppendLine(changeLine(line)))
else
ignore(buffer.AppendLine(line))
let str = buffer.ToString()
printfn "%s" str
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment