@@ -1579,7 +1579,75 @@ TEST_CASE("Lua StateBase can raise errors.", "[lua][state]")
15791579 }
15801580}
15811581
1582- TEST_CASE (" Lua StateBase can compile Lua code." , " [lua][state]" ) {}
1582+ TEST_CASE (" Lua StateBase can compile and dump Lua code." , " [lua][state]" )
1583+ {
1584+ auto lua = dlua::State ();
1585+
1586+ auto text_chunk_content = " return 42" ;
1587+ auto binary_chunk_content = [&] {
1588+ auto stack = dlua::ScopedStack (lua);
1589+ return lua.load (dlua::TextChunk{text_chunk_content}).value ().dump ();
1590+ }();
1591+
1592+ SECTION (" Text chunks can be compiled." )
1593+ {
1594+ auto function = lua.load (dlua::TextChunk{text_chunk_content}).value ();
1595+ CHECK (function.call <1 >() == 42 );
1596+ }
1597+ SECTION (" Chunks can be named." )
1598+ {
1599+ auto function = lua.load (dlua::TextChunk{" " , " foo" }).value ();
1600+ CHECK (function.getFunctionInfo <dlua::DebugInfoSource>().source == " foo" );
1601+ }
1602+ SECTION (" Binary chunks can be loaded." )
1603+ {
1604+ auto function = lua.load (dlua::BinaryChunk{binary_chunk_content}).value ();
1605+ CHECK (function.call <1 >() == 42 );
1606+ }
1607+ SECTION (" Text chunks cannot load binary content." )
1608+ {
1609+ lua.load (dlua::TextChunk{binary_chunk_content})
1610+ .map ([](auto ) { FAIL (" Lua error expected." ); })
1611+ .map_error ([](const dlua::Error& error) {
1612+ CHECK (error.status == dlua::Status::SyntaxError);
1613+ CHECK (error.message == " attempt to load a binary chunk (mode is 't')" );
1614+ });
1615+ }
1616+ SECTION (" Binary chunks cannot load text content." )
1617+ {
1618+ lua.load (dlua::BinaryChunk{text_chunk_content})
1619+ .map ([](auto ) { FAIL (" Lua error expected." ); })
1620+ .map_error ([](const dlua::Error& error) {
1621+ CHECK (error.status == dlua::Status::SyntaxError);
1622+ CHECK (error.message == " attempt to load a text chunk (mode is 'b')" );
1623+ });
1624+ }
1625+ SECTION (" When loading a chunk, Lua can automatically detect whether it is text or binary." )
1626+ {
1627+ SECTION (" Loading a text chunk." )
1628+ {
1629+ auto function = lua.load (dlua::AnyChunk{text_chunk_content}).value ();
1630+ CHECK (function.call <1 >() == 42 );
1631+ }
1632+ SECTION (" Loading a binary chunk." )
1633+ {
1634+ auto function = lua.load (dlua::AnyChunk{binary_chunk_content}).value ();
1635+ CHECK (function.call <1 >() == 42 );
1636+ }
1637+ }
1638+ SECTION (" Functions can be dumped even when they aren't on the top of the stack." )
1639+ {
1640+ // Regular dump is already checked via binary_chunk_content.
1641+ // However, lua_dump can only dump the function on the top of the stack.
1642+ // This implementation supports dump on any index as shown:
1643+
1644+ lua.load (dlua::TextChunk{text_chunk_content});
1645+ lua.push (1 );
1646+
1647+ CHECK (lua.dump (1 ) == binary_chunk_content);
1648+ CHECK (lua.size () == 2 );
1649+ }
1650+ }
15831651
15841652TEST_CASE (" Lua StateBase can call functions." , " [lua][state]" ) {}
15851653
0 commit comments