changeset 271:82a3ebcfbafa

add internal tests git-svn-id: https://luan-java.googlecode.com/svn/trunk@272 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Fri, 31 Oct 2014 04:27:50 +0000
parents b2c20fdcf42a
children eb27e765affb
files core/src/luan/impl/LuanParser.java core/src/luan/impl/Parser.java core/src/luan/modules/StringLuan.java dist/scripts/test.luan web/src/luan/modules/web/test.luan web/src/luan/modules/web/web_run.luan web/src/luan/modules/web/web_shell.luan
diffstat 7 files changed, 61 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/core/src/luan/impl/LuanParser.java	Thu Oct 30 20:29:33 2014 +0000
+++ b/core/src/luan/impl/LuanParser.java	Fri Oct 31 04:27:50 2014 +0000
@@ -1316,7 +1316,7 @@
 			if( c != null ) {
 				buf.append(c);
 			} else {
-				if( !parser.anyChar() )
+				if( parser.test('\r') || parser.test('\n') || !parser.anyChar() )
 					throw parser.exception("Unclosed string");
 				buf.append(parser.lastChar());
 			}
@@ -1345,6 +1345,8 @@
 			if( Digit() ) Digit();  // optional
 			return parser.success((char)Integer.parseInt(parser.textFrom(start)));
 		}
+		if( EndOfLine() )
+			return parser.success('\n');
 		return parser.failure(null);
 	}
 
--- a/core/src/luan/impl/Parser.java	Thu Oct 30 20:29:33 2014 +0000
+++ b/core/src/luan/impl/Parser.java	Fri Oct 31 04:27:50 2014 +0000
@@ -141,6 +141,10 @@
 		return true;
 	}
 
+	public boolean test(char c) {
+		return !endOfInput() && text.charAt(i()) == c;
+	}
+
 	public boolean test(String s) {
 		return text.regionMatches(i(),s,0,s.length());
 	}
--- a/core/src/luan/modules/StringLuan.java	Thu Oct 30 20:29:33 2014 +0000
+++ b/core/src/luan/modules/StringLuan.java	Fri Oct 31 04:27:50 2014 +0000
@@ -18,19 +18,20 @@
 			LuanTable module = Luan.newTable();
 			try {
 				add( module, "to_binary", String.class );
-				add( module, "to_integers", String.class );
+				module.put( "byte", new LuanJavaFunction(StringLuan.class.getMethod( "byte_", String.class ),null) );
 				module.put( "char", new LuanJavaFunction(StringLuan.class.getMethod( "char_", new int[0].getClass() ),null) );
 				add( module, "find", String.class, String.class, Integer.class, Boolean.class );
 				add( module, "format", String.class, new Object[0].getClass() );
-				add( module, "gmatch", String.class, String.class );
+				add( module, "gmatch", LuanState.class, String.class, String.class );
 				add( module, "gsub", LuanState.class, String.class, String.class, Object.class, Integer.class );
-				add( module, "len", String.class );
-				add( module, "lower", String.class );
+				add( module, "len", LuanState.class, String.class );
+				add( module, "lower", LuanState.class, String.class );
 				add( module, "match", String.class, String.class, Integer.class );
 				add( module, "rep", String.class, Integer.TYPE, String.class );
-				add( module, "reverse", String.class );
+				add( module, "reverse", LuanState.class, String.class );
 				add( module, "sub", LuanState.class, String.class, Integer.TYPE, Integer.class );
-				add( module, "upper", String.class );
+				add( module, "trim", LuanState.class, String.class );
+				add( module, "upper", LuanState.class, String.class );
 			} catch(NoSuchMethodException e) {
 				throw new RuntimeException(e);
 			}
@@ -83,7 +84,7 @@
 		return s.getBytes();
 	}
 
-	public static int[] to_integers(String s) {
+	public static int[] byte_(String s) {
 		char[] a = s.toCharArray();
 		int[] chars = new int[a.length];
 		for( int i=0; i<a.length; i++ ) {
@@ -100,19 +101,28 @@
 		return new String(a);
 	}
 
-	public static int len(String s) {
+	public static int len(LuanState luan,String s) throws LuanException {
+		Utils.checkNotNull(luan,s);
 		return s.length();
 	}
 
-	public static String lower(String s) {
+	public static String lower(LuanState luan,String s) throws LuanException {
+		Utils.checkNotNull(luan,s);
 		return s.toLowerCase();
 	}
 
-	public static String upper(String s) {
+	public static String upper(LuanState luan,String s) throws LuanException {
+		Utils.checkNotNull(luan,s);
 		return s.toUpperCase();
 	}
 
-	public static String reverse(String s) {
+	public static String trim(LuanState luan,String s) throws LuanException {
+		Utils.checkNotNull(luan,s);
+		return s.trim();
+	}
+
+	public static String reverse(LuanState luan,String s) throws LuanException {
+		Utils.checkNotNull(luan,s);
 		return new StringBuilder(s).reverse().toString();
 	}
 
@@ -160,7 +170,8 @@
 		return rtn;
 	}
 
-	public static LuanFunction gmatch(String s,String pattern) {
+	public static LuanFunction gmatch(LuanState luan,String s,String pattern) throws LuanException {
+		Utils.checkNotNull(luan,s);
 		final Matcher m = Pattern.compile(pattern).matcher(s);
 		return new LuanFunction() {
 			@Override public Object call(LuanState luan,Object[] args) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dist/scripts/test.luan	Fri Oct 31 04:27:50 2014 +0000
@@ -0,0 +1,1 @@
+do_file "luan:web/test.luan"
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/web/src/luan/modules/web/test.luan	Fri Oct 31 04:27:50 2014 +0000
@@ -0,0 +1,22 @@
+import "luan:Io"
+import "luan:web/Http"
+
+
+local function print(...)
+	Io.print_to(Io.stderr,...)
+end
+
+function Io.protocols.site(path,loading)
+	return Io.Uri( "luan:web"..path, loading )
+end
+
+
+Http.init_for_test()
+Http.request.parameters.code = "print 'hi'"
+page = Http.get_page "/web_run"
+assert( page.trim() == "hi" )
+
+Http.init_for_test()
+Http.request.parameters.cmd = "'ab'..'cd'"
+page = Http.get_page "/web_shell"
+assert( page.find "abcd" )
--- a/web/src/luan/modules/web/web_run.luan	Thu Oct 30 20:29:33 2014 +0000
+++ b/web/src/luan/modules/web/web_run.luan	Fri Oct 31 04:27:50 2014 +0000
@@ -1,9 +1,9 @@
-import "Io"
-import "web/Http"
-import "String"
+import "luan:Io"
+import "luan:web/Http"
+import "luan:String"
 
 local function lines(s)
-	local matcher = String.gmatch(s,"([^\n]*)\n|([^\n])+$")
+	local matcher = s.gmatch "([^\n]*)\n|([^\n])+$"
 	return function()
 		local m1, m2 = matcher()
 		return m1 or m2
@@ -33,7 +33,7 @@
 		local run = load(code,"<web_run>",env)
 		run()
 	catch e do
-		Http.response.set_content_type "text/plain"
+		Http.response.content_type = "text/plain"
 		print(e)
 		print()
 		print()
--- a/web/src/luan/modules/web/web_shell.luan	Thu Oct 30 20:29:33 2014 +0000
+++ b/web/src/luan/modules/web/web_shell.luan	Fri Oct 31 04:27:50 2014 +0000
@@ -1,6 +1,6 @@
-import "Basic"
-import "Io"
-import "web/Http"
+import "luan:Basic"
+import "luan:Io"
+import "luan:web/Http"
 
 per_session = true