pasta_lua 0.1.23

Pasta Lua - Lua integration for Pasta DSL
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
//! Unit tests for the @pasta_log module.
//!
//! Tests cover:
//! - All 5 log level functions (trace/debug/info/warn/error)
//! - Value conversion (string, number, boolean, table, nil)
//! - Caller information capture (source, line, function name)
//! - Table JSON conversion with size/depth limits
//! - Edge cases (empty args, circular refs, large tables)

use mlua::{Lua, StdLib, Table};
use tracing_test::traced_test;

/// Create a minimal Lua VM for testing with the @pasta_log module registered.
fn create_test_lua_with_log() -> Lua {
    let lua =
        unsafe { Lua::unsafe_new_with(StdLib::ALL_SAFE, mlua::LuaOptions::default()) };

    // Register the log module
    let log_module = pasta_lua::runtime::log::register(&lua).expect("Failed to register log module");

    // Put it in package.loaded
    let package: Table = lua.globals().get("package").unwrap();
    let loaded: Table = package.get("loaded").unwrap();
    loaded.set("@pasta_log", log_module).unwrap();

    lua
}

// ============================================================================
// Requirement 1.1: 5-level log functions
// ============================================================================

#[traced_test]
#[test]
fn test_log_trace_outputs_message() {
    let lua = create_test_lua_with_log();
    lua.load(r#"
        local log = require "@pasta_log"
        log.trace("trace message test")
    "#)
    .exec()
    .unwrap();

    assert!(logs_contain("trace message test"));
}

#[traced_test]
#[test]
fn test_log_debug_outputs_message() {
    let lua = create_test_lua_with_log();
    lua.load(r#"
        local log = require "@pasta_log"
        log.debug("debug message test")
    "#)
    .exec()
    .unwrap();

    assert!(logs_contain("debug message test"));
}

#[traced_test]
#[test]
fn test_log_info_outputs_message() {
    let lua = create_test_lua_with_log();
    lua.load(r#"
        local log = require "@pasta_log"
        log.info("info message test")
    "#)
    .exec()
    .unwrap();

    assert!(logs_contain("info message test"));
}

#[traced_test]
#[test]
fn test_log_warn_outputs_message() {
    let lua = create_test_lua_with_log();
    lua.load(r#"
        local log = require "@pasta_log"
        log.warn("warn message test")
    "#)
    .exec()
    .unwrap();

    assert!(logs_contain("warn message test"));
}

#[traced_test]
#[test]
fn test_log_error_outputs_message() {
    let lua = create_test_lua_with_log();
    lua.load(r#"
        local log = require "@pasta_log"
        log.error("error message test")
    "#)
    .exec()
    .unwrap();

    assert!(logs_contain("error message test"));
}

// ============================================================================
// Requirement 1.3: String argument
// ============================================================================

#[traced_test]
#[test]
fn test_log_string_value() {
    let lua = create_test_lua_with_log();
    lua.load(r#"
        local log = require "@pasta_log"
        log.info("hello world")
    "#)
    .exec()
    .unwrap();

    assert!(logs_contain("hello world"));
}

// ============================================================================
// Requirement 1.4: Non-string value conversion
// ============================================================================

#[traced_test]
#[test]
fn test_log_integer_value() {
    let lua = create_test_lua_with_log();
    lua.load(r#"
        local log = require "@pasta_log"
        log.info(123)
    "#)
    .exec()
    .unwrap();

    assert!(logs_contain("123"));
}

#[traced_test]
#[test]
fn test_log_number_value() {
    let lua = create_test_lua_with_log();
    lua.load(r#"
        local log = require "@pasta_log"
        log.info(3.14)
    "#)
    .exec()
    .unwrap();

    assert!(logs_contain("3.14"));
}

#[traced_test]
#[test]
fn test_log_boolean_true() {
    let lua = create_test_lua_with_log();
    lua.load(r#"
        local log = require "@pasta_log"
        log.info(true)
    "#)
    .exec()
    .unwrap();

    assert!(logs_contain("true"));
}

#[traced_test]
#[test]
fn test_log_boolean_false() {
    let lua = create_test_lua_with_log();
    lua.load(r#"
        local log = require "@pasta_log"
        log.info(false)
    "#)
    .exec()
    .unwrap();

    assert!(logs_contain("false"));
}

#[traced_test]
#[test]
fn test_log_table_as_json() {
    let lua = create_test_lua_with_log();
    lua.load(r#"
        local log = require "@pasta_log"
        log.info({key = "value"})
    "#)
    .exec()
    .unwrap();

    // Table should be converted to JSON
    assert!(logs_contain("key"));
    assert!(logs_contain("value"));
}

#[traced_test]
#[test]
fn test_log_array_table_as_json() {
    let lua = create_test_lua_with_log();
    lua.load(r#"
        local log = require "@pasta_log"
        log.info({1, 2, 3})
    "#)
    .exec()
    .unwrap();

    assert!(logs_contain("[1,2,3]"));
}

// ============================================================================
// Requirement 1.5: nil / no argument
// ============================================================================

#[traced_test]
#[test]
fn test_log_nil_value() {
    let lua = create_test_lua_with_log();
    // Should not error
    lua.load(r#"
        local log = require "@pasta_log"
        log.info(nil)
    "#)
    .exec()
    .unwrap();
}

#[traced_test]
#[test]
fn test_log_no_argument() {
    let lua = create_test_lua_with_log();
    // Calling with no argument should not error
    lua.load(r#"
        local log = require "@pasta_log"
        log.info()
    "#)
    .exec()
    .unwrap();
}

// ============================================================================
// Requirement 1.4: Table size limit (>1000 elements)
// ============================================================================

#[traced_test]
#[test]
fn test_log_large_table_abbreviated() {
    let lua = create_test_lua_with_log();
    lua.load(r#"
        local log = require "@pasta_log"
        local t = {}
        for i = 1, 1001 do
            t[i] = i
        end
        log.info(t)
    "#)
    .exec()
    .unwrap();

    assert!(logs_contain("<table:"));
    assert!(logs_contain("elements>"));
}

// ============================================================================
// Requirement 2.1-2.2: Caller information
// ============================================================================

#[traced_test]
#[test]
fn test_log_contains_caller_source() {
    let lua = create_test_lua_with_log();
    lua.load(r#"
        local log = require "@pasta_log"
        log.info("caller test")
    "#)
    .set_name("test_source.lua")
    .exec()
    .unwrap();

    assert!(logs_contain("lua_source="));
}

#[traced_test]
#[test]
fn test_log_contains_caller_line() {
    let lua = create_test_lua_with_log();
    lua.load(r#"
        local log = require "@pasta_log"
        log.info("line test")
    "#)
    .exec()
    .unwrap();

    assert!(logs_contain("lua_line="));
}

#[traced_test]
#[test]
fn test_log_contains_caller_fn() {
    let lua = create_test_lua_with_log();
    lua.load(r#"
        local log = require "@pasta_log"
        local function test_func()
            log.info("from function")
        end
        test_func()
    "#)
    .exec()
    .unwrap();

    assert!(logs_contain("lua_fn="));
}

// ============================================================================
// Requirement 3.3: Module metadata
// ============================================================================

#[test]
fn test_module_has_version() {
    let lua = create_test_lua_with_log();
    let result: String = lua
        .load(r#"
            local log = require "@pasta_log"
            return log._VERSION
        "#)
        .eval()
        .unwrap();

    assert_eq!(result, "0.1.0");
}

#[test]
fn test_module_has_description() {
    let lua = create_test_lua_with_log();
    let result: String = lua
        .load(r#"
            local log = require "@pasta_log"
            return log._DESCRIPTION
        "#)
        .eval()
        .unwrap();

    assert!(!result.is_empty());
}

// ============================================================================
// Requirement 3.1: Module accessible via require
// ============================================================================

#[test]
fn test_module_accessible_via_require() {
    let lua = create_test_lua_with_log();
    let result: bool = lua
        .load(r#"
            local log = require "@pasta_log"
            return log ~= nil
        "#)
        .eval()
        .unwrap();

    assert!(result);
}

#[test]
fn test_module_has_all_functions() {
    let lua = create_test_lua_with_log();
    let result: bool = lua
        .load(r#"
            local log = require "@pasta_log"
            return type(log.trace) == "function"
                and type(log.debug) == "function"
                and type(log.info) == "function"
                and type(log.warn) == "function"
                and type(log.error) == "function"
        "#)
        .eval()
        .unwrap();

    assert!(result);
}

// ============================================================================
// Edge cases
// ============================================================================

#[traced_test]
#[test]
fn test_log_nested_table_json() {
    let lua = create_test_lua_with_log();
    lua.load(r#"
        local log = require "@pasta_log"
        log.info({nested = {a = 1}})
    "#)
    .exec()
    .unwrap();

    assert!(logs_contain("nested"));
}

#[traced_test]
#[test]
fn test_log_circular_reference_no_error() {
    let lua = create_test_lua_with_log();
    // Circular reference should fall back to tostring(), not error
    lua.load(r#"
        local log = require "@pasta_log"
        local t = {}
        t.self = t
        log.info(t)
    "#)
    .exec()
    .unwrap();

    // Should not crash — tostring fallback
}