Skip to content

Instantly share code, notes, and snippets.

@cv
Created February 4, 2009 22:38
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 cv/58391 to your computer and use it in GitHub Desktop.
Save cv/58391 to your computer and use it in GitHub Desktop.
Refactoring = Origin mimic do(
ifToUnless = method("Returns a message with all the if statements converted to unless statements", message,
unless(message kind == "Message", return)
message each(i,
Refactoring ifToUnless(i next)
i arguments each(j,
Refactoring ifToUnless(j)
)
)
if(message name == :if,
message name = :unless
tmp = message arguments[1]
message arguments[1] = message arguments[2]
message arguments[2] = tmp
)
message
)
unlessToIf = method("Returns a message with all the unless statements converted to if statements", message,
unless(message kind == "Message", return)
message each(i,
Refactoring unlessToIf(i next)
i arguments each(j,
Refactoring unlessToIf(j)
)
)
if(message name == :unless,
message name = :if
tmp = message arguments[1]
message arguments[1] = message arguments[2]
message arguments[2] = tmp
)
message
)
rename = method("Returns a message with all references to oldName converted to newName", message, oldName, newName,
unless(message kind == "Message", return)
message each(i,
Refactoring rename(i next, oldName, newName)
i arguments each(j,
Refactoring rename(j, oldName, newName)
)
)
if(message name == oldName, message name = newName)
message
)
)
use("ispec")
describe("Refactoring",
describe("ifToUnless",
it("should convert simple messages containing only an if statement",
Refactoring ifToUnless('if(true, "a", "b")) code should == 'unless(true, "b", "a") code
)
it("should convert messages containing multiple statements in 'condition' part",
Refactoring ifToUnless('if(1. true, "a", "b")) code should == 'unless(1. true, "b", "a") code
)
it("should convert messages containing multiple statements in 'then' part",
Refactoring ifToUnless('if(true, 1. "a", "b")) code should == 'unless(true, "b", 1. "a") code
)
it("should convert messages containing multiple statements in 'else' part",
Refactoring ifToUnless('if(true, "a", 1. "b")) code should == 'unless(true, 1. "b", "a") code
)
it("should convert message chain containing multiple if statements",
Refactoring ifToUnless(Message fromText(#[if(true, "a", "b"). if(false, "a", "b")])) code should == Message fromText(#[unless(true, "b", "a"). unless(false, "b", "a")]) code
)
it("should not interfere with other messages",
Refactoring ifToUnless(Message fromText(#[1 + 1. if(true, "a", "b"). while(false, nil)])) code should == Message fromText(#[1 + 1. unless(true, "b", "a"). while(false, nil)]) code
)
it("should convert messages passed as arguments to other messages",
Refactoring ifToUnless('method(if(true, "a", "b"))) code should == 'method(unless(true, "b", "a")) code
)
)
describe("unlessToIf",
it("should convert simple messages containing only an unless statement",
Refactoring unlessToIf('unless(true, "a", "b")) code should == 'if(true, "b", "a") code
)
it("should convert messages containing multiple statements in 'condition' part",
Refactoring unlessToIf('unless(1. true, "a", "b")) code should == 'if(1. true, "b", "a") code
)
it("should convert messages containing multiple statements in 'then' part",
Refactoring unlessToIf('unless(true, 1. "a", "b")) code should == 'if(true, "b", 1. "a") code
)
it("should convert messages containing multiple statements in 'else' part",
Refactoring unlessToIf('unless(true, "a", 1. "b")) code should == 'if(true, 1. "b", "a") code
)
it("should convert message chain containing multiple unless statements",
Refactoring unlessToIf(Message fromText(#[unless(true, "a", "b"). unless(false, "a", "b")])) code should == Message fromText(#[if(true, "b", "a"). if(false, "b", "a")]) code
)
it("should not interfere with other messages",
Refactoring unlessToIf(Message fromText(#[1 + 1. unless(true, "a", "b"). while(false, nil)])) code should == Message fromText(#[1 + 1. if(true, "b", "a"). while(false, nil)]) code
)
it("should convert messages passed as arguments to other messages",
Refactoring unlessToIf('method(unless(true, "a", "b"))) code should == 'method(if(true, "b", "a")) code
)
)
describe("rename",
it("should rename a local variable assigned inside a method",
Refactoring rename('method(old = 1), :old, :new) code should == 'method(new = 1) code
)
it("should rename a method call inside a method",
Refactoring rename('method(old(1)), :old, :new) code should == 'method(new(1)) code
)
it("should rename a local variable passed as a parameter",
Refactoring rename('method(foo(old)), :old, :new) code should == 'method(foo(new)) code
Refactoring rename('method(old = 1. foo(old)), :old, :new) code should == 'method(new = 1. foo(new)) code
)
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment