Software /
code /
prosody
Comparison
util/format.lua @ 8383:d967d6f2ad00
util.format: Move tests to spec/
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Fri, 10 Nov 2017 05:46:39 +0100 |
parent | 8382:e5d00bf4a4d5 |
child | 8417:e88db5668cfb |
comparison
equal
deleted
inserted
replaced
8382:e5d00bf4a4d5 | 8383:d967d6f2ad00 |
---|---|
2 -- A string.format wrapper that gracefully handles invalid arguments | 2 -- A string.format wrapper that gracefully handles invalid arguments |
3 -- | 3 -- |
4 | 4 |
5 local tostring = tostring; | 5 local tostring = tostring; |
6 local select = select; | 6 local select = select; |
7 local assert = assert; | |
8 local unpack = unpack; | 7 local unpack = unpack; |
9 local type = type; | 8 local type = type; |
10 | 9 |
11 local function format(formatstring, ...) | 10 local function format(formatstring, ...) |
12 local args, args_length = { ... }, select('#', ...); | 11 local args, args_length = { ... }, select('#', ...); |
58 end | 57 end |
59 | 58 |
60 return formatstring:format(unpack(args)); | 59 return formatstring:format(unpack(args)); |
61 end | 60 end |
62 | 61 |
63 local function test() | |
64 assert(format("%s", "hello") == "hello"); | |
65 assert(format("%s") == "<nil>"); | |
66 assert(format("%s", true) == "true"); | |
67 assert(format("%d", true) == "[true]"); | |
68 assert(format("%%", true) == "% [true]"); | |
69 end | |
70 | |
71 return { | 62 return { |
72 format = format; | 63 format = format; |
73 test = test; | |
74 }; | 64 }; |