Refactor concat, add __concat metatag
This commit is contained in:
@@ -1116,23 +1116,23 @@ public class TypeTest extends TestCase {
|
||||
}
|
||||
|
||||
public void testCheckJavaString() {
|
||||
throwsErrorReq( somenil, "checkString" );
|
||||
throwsErrorReq( sometrue, "checkString" );
|
||||
throwsErrorReq( somefalse, "checkString" );
|
||||
throwsErrorReq( somenil, "checkjstring" );
|
||||
throwsErrorReq( sometrue, "checkjstring" );
|
||||
throwsErrorReq( somefalse, "checkjstring" );
|
||||
assertEquals( String.valueOf(zero), zero.checkjstring() );
|
||||
assertEquals( String.valueOf(intint), intint.checkjstring() );
|
||||
assertEquals( String.valueOf(longdouble), longdouble.checkjstring() );
|
||||
assertEquals( String.valueOf(doubledouble), doubledouble.checkjstring() );
|
||||
throwsErrorReq( somefunc, "checkString" );
|
||||
throwsErrorReq( someclosure, "checkString" );
|
||||
throwsErrorReq( somefunc, "checkjstring" );
|
||||
throwsErrorReq( someclosure, "checkjstring" );
|
||||
assertEquals( samplestringstring, stringstring.checkjstring() );
|
||||
assertEquals( samplestringint, stringint.checkjstring() );
|
||||
assertEquals( samplestringlong, stringlong.checkjstring() );
|
||||
assertEquals( samplestringdouble, stringdouble.checkjstring() );
|
||||
throwsErrorReq( thread, "checkString" );
|
||||
throwsErrorReq( table, "checkString" );
|
||||
throwsErrorReq( userdataobj, "checkString" );
|
||||
throwsErrorReq( userdatacls, "checkString" );
|
||||
throwsErrorReq( thread, "checkjstring" );
|
||||
throwsErrorReq( table, "checkjstring" );
|
||||
throwsErrorReq( userdataobj, "checkjstring" );
|
||||
throwsErrorReq( userdatacls, "checkjstring" );
|
||||
}
|
||||
|
||||
public void testCheckLuaString() {
|
||||
|
||||
@@ -572,4 +572,78 @@ public class UnaryBinaryOperatorsTest extends TestCase {
|
||||
assertEquals(t, aaa.lteq(aaa));
|
||||
assertEquals(t, aaa.gteq(aaa));
|
||||
}
|
||||
|
||||
public void testBuffer() {
|
||||
LuaValue abc = LuaValue.valueOf("abcdefghi").substring(0,3);
|
||||
LuaValue def = LuaValue.valueOf("abcdefghi").substring(3,6);
|
||||
LuaValue ghi = LuaValue.valueOf("abcdefghi").substring(6,9);
|
||||
LuaValue n123 = LuaValue.valueOf(123);
|
||||
|
||||
// basic append
|
||||
Buffer b = new Buffer(); assertEquals( "", b.value().tojstring() );
|
||||
b.append(def); assertEquals( "def", b.value().tojstring() );
|
||||
b.append(abc); assertEquals( "defabc", b.value().tojstring() );
|
||||
b.append(ghi); assertEquals( "defabcghi", b.value().tojstring() );
|
||||
b.append(n123); assertEquals( "defabcghi123", b.value().tojstring() );
|
||||
|
||||
// basic prepend
|
||||
b = new Buffer(); assertEquals( "", b.value().tojstring() );
|
||||
b.prepend(def.strvalue()); assertEquals( "def", b.value().tojstring() );
|
||||
b.prepend(ghi.strvalue()); assertEquals( "ghidef", b.value().tojstring() );
|
||||
b.prepend(abc.strvalue()); assertEquals( "abcghidef", b.value().tojstring() );
|
||||
b.prepend(n123.strvalue()); assertEquals( "123abcghidef", b.value().tojstring() );
|
||||
|
||||
// mixed append, prepend
|
||||
b = new Buffer(); assertEquals( "", b.value().tojstring() );
|
||||
b.append(def); assertEquals( "def", b.value().tojstring() );
|
||||
b.append(abc); assertEquals( "defabc", b.value().tojstring() );
|
||||
b.prepend(ghi.strvalue()); assertEquals( "ghidefabc", b.value().tojstring() );
|
||||
b.prepend(n123.strvalue()); assertEquals( "123ghidefabc", b.value().tojstring() );
|
||||
b.append(def); assertEquals( "123ghidefabcdef", b.value().tojstring() );
|
||||
b.append(abc); assertEquals( "123ghidefabcdefabc", b.value().tojstring() );
|
||||
b.prepend(ghi.strvalue()); assertEquals( "ghi123ghidefabcdefabc", b.value().tojstring() );
|
||||
b.prepend(n123.strvalue()); assertEquals( "123ghi123ghidefabcdefabc", b.value().tojstring() );
|
||||
|
||||
// value
|
||||
b = new Buffer(def); assertEquals( "def", b.value().tojstring() );
|
||||
b.append(abc); assertEquals( "defabc", b.value().tojstring() );
|
||||
b.prepend(ghi.strvalue()); assertEquals( "ghidefabc", b.value().tojstring() );
|
||||
b.setvalue(def); assertEquals( "def", b.value().tojstring() );
|
||||
b.prepend(ghi.strvalue()); assertEquals( "ghidef", b.value().tojstring() );
|
||||
b.append(abc); assertEquals( "ghidefabc", b.value().tojstring() );
|
||||
}
|
||||
|
||||
public void testConcat() {
|
||||
LuaValue abc = LuaValue.valueOf("abcdefghi").substring(0,3);
|
||||
LuaValue def = LuaValue.valueOf("abcdefghi").substring(3,6);
|
||||
LuaValue ghi = LuaValue.valueOf("abcdefghi").substring(6,9);
|
||||
LuaValue n123 = LuaValue.valueOf(123);
|
||||
|
||||
assertEquals( "abc", abc.tojstring() );
|
||||
assertEquals( "def", def.tojstring() );
|
||||
assertEquals( "ghi", ghi.tojstring() );
|
||||
assertEquals( "123", n123.tojstring() );
|
||||
assertEquals( "abcabc", abc.concat(abc).tojstring() );
|
||||
assertEquals( "defghi", def.concat(ghi).tojstring() );
|
||||
assertEquals( "ghidef", ghi.concat(def).tojstring() );
|
||||
assertEquals( "ghidefabcghi", ghi.concat(def).concat(abc).concat(ghi).tojstring() );
|
||||
assertEquals( "123def", n123.concat(def).tojstring() );
|
||||
assertEquals( "def123", def.concat(n123).tojstring() );
|
||||
}
|
||||
|
||||
public void testConcatBuffer() {
|
||||
LuaValue abc = LuaValue.valueOf("abcdefghi").substring(0,3);
|
||||
LuaValue def = LuaValue.valueOf("abcdefghi").substring(3,6);
|
||||
LuaValue ghi = LuaValue.valueOf("abcdefghi").substring(6,9);
|
||||
LuaValue n123 = LuaValue.valueOf(123);
|
||||
Buffer b;
|
||||
|
||||
b = new Buffer(def); assertEquals( "def", b.value().tojstring() );
|
||||
b = ghi.concat(b); assertEquals( "ghidef", b.value().tojstring() );
|
||||
b = abc.concat(b); assertEquals( "abcghidef", b.value().tojstring() );
|
||||
b = n123.concat(b); assertEquals( "123abcghidef", b.value().tojstring() );
|
||||
b.setvalue(n123);
|
||||
b = def.concat(b); assertEquals( "def123", b.value().tojstring() );
|
||||
b = abc.concat(b); assertEquals( "abcdef123", b.value().tojstring() );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,6 +46,7 @@ local mt = {
|
||||
__metatable={},
|
||||
__index=buildop('index'),
|
||||
__newindex=buildop('newindex'),
|
||||
__concat=buildop('concat'),
|
||||
}
|
||||
|
||||
-- pcall a function and check for a pattern in the error string
|
||||
@@ -118,8 +119,9 @@ end
|
||||
print( '---- __eq, __lt, __le, same types' )
|
||||
local bfunction = function() end
|
||||
local bthread = coroutine.create( bfunction )
|
||||
local btable = {}
|
||||
local groups
|
||||
groups = { {afunction, bfunction}, {true, true}, {true, false}, {afunction, bfunction}, {athread, bthread}, {atable, atable}, {atable, {}} }
|
||||
groups = { {true, true}, {true, false}, {afunction, bfunction}, {athread, bthread}, {atable, atable}, {atable, btable} }
|
||||
for i=1,#groups do
|
||||
local a,b = groups[i][1], groups[i][2]
|
||||
print( type(a), type(b), 'before', pcall( function() return a==b end ) )
|
||||
@@ -199,3 +201,32 @@ for i=1,#values do
|
||||
print( debug.setmetatable( a, nil ) )
|
||||
end
|
||||
|
||||
print( '---- __concat' )
|
||||
groups = { {atable, afunction}, {afunction, atable}, {123, nil}, {nil, 123} }
|
||||
local s,t,u = 'sss',777
|
||||
local concatresult = setmetatable( { '__concat-result' }, {
|
||||
__tostring=function()
|
||||
return 'concat-string-result'
|
||||
end } )
|
||||
local concatmt = {
|
||||
__concat=function(a,b)
|
||||
print( 'mt.__concat('..type(a)..','..type(b)..')', a, b )
|
||||
return concatresult
|
||||
end
|
||||
}
|
||||
for i=1,#groups do
|
||||
local a,b = groups[i][1], groups[i][2]
|
||||
print( type(a), type(b), 'before', ecall( 'attempt to concatenate ', function() return a..b end ) )
|
||||
print( type(a), type(b), 'before', ecall( 'attempt to concatenate ', function() return b..a end ) )
|
||||
print( type(a), type(s), type(t), 'before', ecall( 'attempt to concatenate ', function() return a..s..t end ) )
|
||||
print( type(s), type(a), type(t), 'before', ecall( 'attempt to concatenate ', function() return s..a..t end ) )
|
||||
print( type(s), type(t), type(a), 'before', ecall( 'attempt to concatenate ', function() return s..t..a end ) )
|
||||
print( debug.setmetatable( a, concatmt ) )
|
||||
print( type(a), type(b), 'after', pcall( function() return a..b end ) )
|
||||
print( type(a), type(b), 'after', pcall( function() return b..a end ) )
|
||||
print( type(a), type(s), type(t), 'before', pcall( function() return a..s..t end ) )
|
||||
print( type(s), type(a), type(t), 'before', ecall( 'attempt to concatenate ', function() return s..a..t end ) )
|
||||
print( type(s), type(t), type(a), 'before', ecall( 'attempt to concatenate ', function() return s..t..a end ) )
|
||||
print( debug.setmetatable( a, nil ) )
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user