Skip to content

Commit

Permalink
Add spec file (Thanks @daurnimator!), more examples, new rockspec.
Browse files Browse the repository at this point in the history
  • Loading branch information
hishamhm committed Jan 11, 2016
1 parent f2e8c87 commit f801afa
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 0 deletions.
9 changes: 9 additions & 0 deletions examples/env.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
local F = require("F")

local assert = assert
local _ENV = {x = "foo"}
function gee()
assert("foo" == x)
assert("foo" == F'{x}')
end
gee()
6 changes: 6 additions & 0 deletions examples/fahrenheit.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
local F = require("F")

local f = 99
local c = (f-32)/9*5

print(F"{f} degrees Fahrenheit is {c:%.2f} degrees Celsius")
15 changes: 15 additions & 0 deletions examples/upvalue.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

local F = require("F")

local u = "Hello"

local function f()
local w = "World"
return function()
local gobo = "GOBO"
print(u .. " " .. w)
print(F"{u} {w} of {gobo}")
end
end

f()()
25 changes: 25 additions & 0 deletions rockspecs/f-strings-0.2-1.rockspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package = "f-strings"
version = "0.2-1"
source = {
url = "git://github.com/hishamhm/f-strings",
tag = "v0.2",
}
description = {
summary = [[String interpolation for Lua.]],
detailed = [[
String interpolation for Lua, inspired by f-strings,
a form of string interpolation in Python 3.6.
It allows you to interpolate variables in strings
(including local variables) using the syntax:
str = F"The value of x is {x} and the value of y is {y:%.2f}."
]],
homepage = "http://github.com/hishamhm/f-strings",
license = "MIT"
}
dependencies = {}
build = {
type = "builtin",
modules = {
F = "F.lua",
}
}
81 changes: 81 additions & 0 deletions spec/F_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
describe("string interpolation", function()
local F = require "F"
local assert = assert
it("works with literals", function()
assert.same("foo", F'{"foo"}')
end)
it("works with locals", function()
do
local x = "foo"
assert.same("foo", F'{x}')
end
end)
it("works with referenced upvalues", function()
do
local x = "foo"
(function()
assert.same("foo", x) -- reference the upvalue
assert.same("foo", F'{x}')
end)()
end
end)
it("fails with unreferenced upvalues", function()
do
(function()
local x = "foo"
return function()
assert.not_same("foo", F'{x}')
end
end)()()
end
end);
(_VERSION == "Lua 5.1" and it or pending)("works with setfenv'd functions", function()
setfenv(1, {x = "foo"})
assert.same("foo", x)
assert.same("foo", F'{x}')
end);
(_VERSION == "Lua 5.1" and pending or it)("works with _ENV scopes", function()
do
local _ENV = {x = "foo"}
assert.same("foo", F'{x}')
end
end);
(_VERSION == "Lua 5.1" and pending or it)("fails with _ENV as an unreferenced upvalue", function()
do
local _ENV = {x = "foo"}
(function()
assert.not_same("foo", F'{x}')
end)()
end
end);
(_VERSION == "Lua 5.1" and pending or it)("works with _ENV as a referenced upvalue", function()
do
local _ENV = {x = "foo"}
(function()
assert.same("foo", x) -- reference the upvalue
assert.same("foo", F'{x}')
end)()
end
end);
(_VERSION == "Lua 5.1" and pending or it)("fails with unreferenced upvalue in custom _ENV", function()
do
(function()
local _ENV = {x = "foo"}
return function()
assert.not_same("foo", F'{x}')
end
end)()()
end
end);
(_VERSION == "Lua 5.1" and pending or it)("works with referenced upvalue in custom _ENV", function()
do
(function()
local _ENV = {x = "foo"}
return function()
assert.same("foo", x) -- reference the upvalue
assert.same("foo", F'{x}')
end
end)()()
end
end)
end)

0 comments on commit f801afa

Please sign in to comment.