001: /*
002: * Copyright (C) 2003-2007 Kepler Project.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining
005: * a copy of this software and associated documentation files (the
006: * "Software"), to deal in the Software without restriction, including
007: * without limitation the rights to use, copy, modify, merge, publish,
008: * distribute, sublicense, and/or sell copies of the Software, and to
009: * permit persons to whom the Software is furnished to do so, subject to
010: * the following conditions:
011: *
012: * The above copyright notice and this permission notice shall be
013: * included in all copies or substantial portions of the Software.
014: *
015: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
016: * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
017: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
018: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
019: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
020: * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
021: * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
022: */
023:
024: package org.keplerproject.luajava.test;
025:
026: import java.io.FileInputStream;
027:
028: import org.keplerproject.luajava.LuaException;
029: import org.keplerproject.luajava.LuaState;
030: import org.keplerproject.luajava.LuaStateFactory;
031:
032: import junit.framework.TestCase;
033:
034: /**
035: * Tests the use of lauxlib functions.
036: * Requires junit.
037: *
038: * @author thiago
039: */
040: public class TestLauxlib extends TestCase {
041: /**
042: * Tests the load functions
043: */
044: public void testLoadRun() throws Exception {
045: // test loadfile
046: LuaState L = LuaStateFactory.newLuaState();
047: L.openLibs();
048:
049: int loadRes = L.LloadFile("wrongLuaFile.lua");
050: assertEquals(loadRes, LuaState.LUA_ERRSYNTAX.intValue());
051: System.out.println(L.toString(-1));
052: L.pop(1);
053:
054: loadRes = L.LloadFile("simpleLuaFile.lua");
055: assertEquals(loadRes, 0);
056: L.pcall(0, 0, 0);
057:
058: // test loadbuffer
059: FileInputStream input = new FileInputStream("wrongLuaFile.lua");
060: byte[] bytes = new byte[input.available()];
061: input.read(bytes);
062: input.close();
063:
064: loadRes = L.LloadBuffer(bytes, "test");
065: assertEquals(LuaState.LUA_ERRSYNTAX.intValue(), loadRes);
066: System.out.println(L.toString(-1));
067:
068: input = new FileInputStream("simpleLuaFile.lua");
069: bytes = new byte[input.available()];
070: input.read(bytes);
071: input.close();
072:
073: loadRes = L.LloadBuffer(bytes, "test2");
074: assertEquals(0, loadRes);
075: L.pcall(0, 0, 0);
076:
077: L.close();
078: }
079:
080: /**
081: * Test the check functions.
082: */
083: public void testChecks() {
084: LuaState L = LuaStateFactory.newLuaState();
085: L.openLibs();
086:
087: String testCheckStr = "testCheck";
088:
089: L.pushString(testCheckStr);
090: assertEquals(testCheckStr, L.LcheckString(-1));
091: assertEquals(testCheckStr, L.LoptString(-1, "test"));
092:
093: L.pushNumber(1.0);
094: L.LcheckNumber(-1);
095:
096: assertTrue(L.LoptNumber(2, 2.0) == 1.0);
097: assertTrue(L.LcheckNumber(2) == 1.0);
098:
099: L.LcheckAny(2);
100:
101: L.LcheckType(1, LuaState.LUA_TSTRING.intValue());
102:
103: L.close();
104: }
105:
106: /**
107: * Checks the metamethods and metatable functions.
108: */
109: public void testMeta() throws LuaException {
110: LuaState L = LuaStateFactory.newLuaState();
111: L.openLibs();
112:
113: L.newTable();
114: L.newTable();
115: L.pushString("__index");
116: L.LdoString("return function()"
117: + "io.write( 'metatest\\n') io.stdout:flush() "
118: + "return 'foo' " + "end");
119: L.setTable(-3);
120: L.setMetaTable(-2);
121:
122: L.LcallMeta(-1, "__index");
123: System.out.println(L.toString(-1));
124: L.pop(1);
125: L.LgetMetaField(-1, "__index");
126: L.call(0, 1);
127: System.out.println(L.toString(-1));
128: L.pop(1);
129: L.pushString("testTable");
130: L.pushValue(-2);
131: L.setTable(LuaState.LUA_GLOBALSINDEX.intValue());
132: L.pop(1);
133: L.LdoString("str = testTable.ff; print(str..'fromLua');"
134: + " io.stdout:flush()");
135:
136: L.close();
137: }
138: }
|