Skip to content

Commit 1e6b45c

Browse files
authored
featureflags: fix a bug which caused all flags to report as 'false' (#268)
- Adds the featureflag unit test, which was missing from my previous PR (forgot to add). - Adds a set of special internal values to help verify the behavior of the three possible valid macro states. - Also clarifies the use of "feature flags" as the term for this new table, both in function name and comments.
1 parent 426a4a2 commit 1e6b45c

File tree

3 files changed

+56
-10
lines changed

3 files changed

+56
-10
lines changed

lutro.c

+19-9
Original file line numberDiff line numberDiff line change
@@ -204,28 +204,38 @@ static void init_lutro_global_table(lua_State *L)
204204
lua_pop(L, 1);
205205
}
206206

207-
// reveals which configuration options were used to compile lutro.
208-
static void init_config(lua_State *L)
207+
// exposes build configuration options used to compile lutro to lua
208+
static void init_feature_flags(lua_State *L)
209209
{
210210
player_checked_stack_begin(L);
211211

212212
luax_reqglobal(L, "lutro");
213213
lua_newtable(L);
214214

215-
char buf[128];
215+
char buf[64];
216216

217-
#define STR(a) #a
218-
#define CAPABILITY(cap) \
219-
snprintf(buf, sizeof(buf), "%s", #cap); \
220-
lua_pushboolean(L, (buf[0] && strtold(buf, NULL) != 0)); \
221-
lua_setfield(L, -2, #cap)
217+
#define _CAPABILITY(capname, capval) \
218+
snprintf(buf, sizeof(buf), "%s", # capval); \
219+
lua_pushboolean(L, (buf[0] && strtod(buf, NULL) != 0)); \
220+
lua_setfield(L, -2, capname)
221+
222+
#define CAPABILITY(cap) _CAPABILITY(#cap, cap)
222223

223224
CAPABILITY(HAVE_COMPOSITION);
224225
CAPABILITY(HAVE_TRANSFORM);
225226
CAPABILITY(HAVE_INOTIFY);
226227
CAPABILITY(HAVE_JIT);
227228
CAPABILITY(HAVE_LUASOCKET);
228229

230+
// for unit testing purposes only.
231+
#define _VERIFY_AS_TRUE 1
232+
#define _VERIFY_AS_FALSE 0
233+
CAPABILITY(_VERIFY_AS_TRUE);
234+
CAPABILITY(_VERIFY_AS_FALSE);
235+
CAPABILITY(_VERIFY_AS_UNDEFINED);
236+
#undef _VERIFY_AS_TRUE
237+
#undef _VERIFY_AS_FALSE
238+
229239
#undef STR
230240
#undef CAPABILITY
231241

@@ -237,7 +247,7 @@ static void init_config(lua_State *L)
237247

238248
static int lutro_core_preload(lua_State *L)
239249
{
240-
init_config(L);
250+
init_feature_flags(L);
241251

242252
return 1;
243253
}

test/main.lua

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
-- Lutro Tester
22
local availableStates = {
3-
"config/print",
43
"graphics/print",
54
"unit/tests",
65
"joystick/isDown",

test/unit/modules/featureflags.lua

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
local function printBuildFlags()
3+
-- Step 1: duplicate the table
4+
-- Step 2: Sort the array alphabetically
5+
-- Step 3: Iterate over the sorted array
6+
7+
local copy = {}
8+
for k in pairs(lutro.featureflags) do
9+
table.insert(copy, k)
10+
end
11+
12+
table.sort(copy, function(a, b)
13+
return a:lower() < b:lower()
14+
end)
15+
16+
-- named prefixed with _ are for internal lutro use.
17+
for _, key in ipairs(copy) do
18+
if not key:find("^_") then
19+
print( ("lutro.featureflags.%s = %s"):format(key, tostring(lutro.featureflags[key])) )
20+
end
21+
end
22+
end
23+
24+
local function verify_as_truefalse()
25+
unit.assertEquals(lutro.featureflags._VERIFY_AS_TRUE, true)
26+
unit.assertEquals(lutro.featureflags._VERIFY_AS_FALSE, false)
27+
end
28+
29+
local function verify_as_undefined()
30+
unit.assertEquals(lutro.featureflags._VERIFY_AS_UNDEFINED, false)
31+
end
32+
33+
return {
34+
printBuildFlags,
35+
verify_as_truefalse,
36+
verify_as_undefined,
37+
}

0 commit comments

Comments
 (0)