M8Test Help

字符串操作

import com.m8test.script.GlobalVariables.* /** * Strings 接口测试脚本 * 全局变量: _strings */ // 辅助函数:用于对比结果并打印日志 fun assertLog(name: String, actual: Any?, expected: Any?) { // 简单的相等性检查 if (actual.toString() == expected.toString()) { _console.log("✅ [$name] Passed. Result: $actual") } else { _console.error("❌ [$name] Failed.\n Expected: $expected\n Actual : $actual") } } _console.log("🚀 开始测试 Strings 接口...") // ========================================== // 1. 基础属性与检查 (Basic Check) // ========================================== val textBasic = " Hello M8 " val emptyText = "" val blankText = " " // getLength val len = _strings.getLength(textBasic) assertLog("getLength", len, 10) // isEmpty assertLog("isEmpty - false", _strings.isEmpty(textBasic), false) assertLog("isEmpty - true", _strings.isEmpty(emptyText), true) // isBlank assertLog("isBlank - false", _strings.isBlank(textBasic), false) assertLog("isBlank - true (空格)", _strings.isBlank(blankText), true) assertLog("isBlank - true (空串)", _strings.isBlank(emptyText), true) // ========================================== // 2. 字符串操作 (Manipulation) // ========================================== // concat val concatRes = _strings.concat("M8", "Test") assertLog("concat", concatRes, "M8Test") // format // Kotlin 中传数组需要用 arrayOf val fmtRes = _strings.format("Hello %s, Score: %d", arrayOf<Any>("World", 100)) assertLog("format", fmtRes, "Hello World, Score: 100") // toUpperCase / toLowerCase assertLog("toUpperCase", _strings.toUpperCase("abc"), "ABC") assertLog("toLowerCase", _strings.toLowerCase("XYZ"), "xyz") // trim assertLog("trim", _strings.trim(" abc "), "abc") // ========================================== // 3. 搜索与判断 (Search & Check) // ========================================== val textSearch = "banana" // contains assertLog("contains - true", _strings.contains(textSearch, "nan"), true) assertLog("contains - false", _strings.contains(textSearch, "xyz"), false) // startsWith / endsWith assertLog("startsWith", _strings.startsWith(textSearch, "ba"), true) assertLog("endsWith", _strings.endsWith(textSearch, "na"), true) // indexOf / lastIndexOf assertLog("indexOf", _strings.indexOf(textSearch, "a"), 1) // 第一个 a 在索引 1 assertLog("lastIndexOf", _strings.lastIndexOf(textSearch, "a"), 5) // 最后一个 a 在索引 5 // ========================================== // 4. 截取与分割 (Substring & Split) // ========================================== val textSub = "0123456789" // substring (常规) assertLog("substring (1, 4)", _strings.substring(textSub, 1, 4), "123") // substring (测试 -1 截取到末尾) assertLog("substring (5, -1)", _strings.substring(textSub, 5, -1), "56789") // split val splitList = _strings.split("A,B,C", ",") _console.log("👉 split 结果: " + splitList) // 应该是 [A, B, C] // splitList 在 Kotlin 中通常是 List<String> if (splitList.size == 3 && splitList[1] == "B") { _console.log("✅ [split] Passed.") } else { _console.error("❌ [split] Failed.") } // ========================================== // 5. 替换 (Replacement) // ========================================== // replace (普通替换) assertLog("replace", _strings.replace("aba", "a", "c"), "cbc") // replaceRegex (正则替换:将数字替换为 *) assertLog("replaceRegex", _strings.replaceRegex("a1b2c3", "\\d", "*"), "a*b*c*") // ========================================== // 6. 加密与编码 (Crypto & Encoding) // ========================================== val raw = "123456" // MD5 // 123456 的 MD5 通常是 e10adc3949ba59abbe56e057f20f883e val md5Val = _strings.md5(raw) assertLog("md5", md5Val, "e10adc3949ba59abbe56e057f20f883e") // Base64 Encode val b64Encoded = _strings.base64Encode("A") assertLog("base64Encode", b64Encoded, "QQ==") // Base64 Decode val b64Decoded = _strings.base64Decode("QQ==") assertLog("base64Decode", b64Decoded, "A") // Hex Encode ( 'A' 的 ASCII 是 65 -> 0x41, 'B' 是 66 -> 0x42 ) val hexStr = _strings.toHexString("AB") assertLog("toHexString", hexStr, "4142") // Hex Decode val fromHex = _strings.fromHexString("4142") assertLog("fromHexString", fromHex, "AB") // ========================================== // 7. 字节转换 (Bytes) // ========================================== // toBytes val bytes = _strings.toBytes("Test", "UTF-8") _console.log("👉 toBytes 长度: " + bytes.size) // Kotlin 数组长度用 size // fromBytes val strFromBytes = _strings.fromBytes(bytes, "UTF-8") assertLog("fromBytes", strFromBytes, "Test") // ========================================== // 8. 随机生成 (Random) // ========================================== val randStr = _strings.random(16) _console.log("👉 random(16) 结果: " + randStr) if (_strings.getLength(randStr) == 16) { _console.log("✅ [random] Passed (Length check).") } else { _console.error("❌ [random] Failed.") } _console.log("🎉 Strings 接口测试完毕。") _script.getThreads().getMain().setBackground(true)
/** * Strings 接口测试脚本 * 全局变量: $strings */ $console.log("🚀 开始测试 Strings 接口...") // ========================================== // 1. 基础属性与检查 (Basic Check) // ========================================== def textBasic = " Hello M8 " def emptyText = "" def blankText = " " // getLength def len = $strings.getLength(textBasic) assertLog("getLength", len, 10) // isEmpty assertLog("isEmpty - false", $strings.isEmpty(textBasic), false) assertLog("isEmpty - true", $strings.isEmpty(emptyText), true) // isBlank assertLog("isBlank - false", $strings.isBlank(textBasic), false) assertLog("isBlank - true (空格)", $strings.isBlank(blankText), true) assertLog("isBlank - true (空串)", $strings.isBlank(emptyText), true) // ========================================== // 2. 字符串操作 (Manipulation) // ========================================== // concat def concatRes = $strings.concat("M8", "Test") assertLog("concat", concatRes, "M8Test") // format (注意:Groovy List 传给 Java Array 需要转换,或者引擎支持直接传) // 假设这里直接传 List 即可,若报错请尝试 ["World"] as Object[] def fmtRes = $strings.format("Hello %s, Score: %d", ["World", 100] as Object[]) assertLog("format", fmtRes, "Hello World, Score: 100") // toUpperCase / toLowerCase assertLog("toUpperCase", $strings.toUpperCase("abc"), "ABC") assertLog("toLowerCase", $strings.toLowerCase("XYZ"), "xyz") // trim assertLog("trim", $strings.trim(" abc "), "abc") // ========================================== // 3. 搜索与判断 (Search & Check) // ========================================== def textSearch = "banana" // contains assertLog("contains - true", $strings.contains(textSearch, "nan"), true) assertLog("contains - false", $strings.contains(textSearch, "xyz"), false) // startsWith / endsWith assertLog("startsWith", $strings.startsWith(textSearch, "ba"), true) assertLog("endsWith", $strings.endsWith(textSearch, "na"), true) // indexOf / lastIndexOf assertLog("indexOf", $strings.indexOf(textSearch, "a"), 1) // 第一个 a 在索引 1 assertLog("lastIndexOf", $strings.lastIndexOf(textSearch, "a"), 5) // 最后一个 a 在索引 5 // ========================================== // 4. 截取与分割 (Substring & Split) // ========================================== def textSub = "0123456789" // substring (常规) assertLog("substring (1, 4)", $strings.substring(textSub, 1, 4), "123") // substring (测试 -1 截取到末尾) assertLog("substring (5, -1)", $strings.substring(textSub, 5, -1), "56789") // split def splitList = $strings.split("A,B,C", ",") $console.log("👉 split 结果: " + splitList) // 应该是 [A, B, C] if (splitList.size() == 3 && splitList[1] == "B") { $console.log("✅ [split] Passed.") } else { $console.error("❌ [split] Failed.") } // ========================================== // 5. 替换 (Replacement) // ========================================== // replace (普通替换) assertLog("replace", $strings.replace("aba", "a", "c"), "cbc") // replaceRegex (正则替换:将数字替换为 *) assertLog("replaceRegex", $strings.replaceRegex("a1b2c3", "\\d", "*"), "a*b*c*") // ========================================== // 6. 加密与编码 (Crypto & Encoding) // ========================================== def raw = "123456" // MD5 // 123456 的 MD5 通常是 e10adc3949ba59abbe56e057f20f883e def md5Val = $strings.md5(raw) assertLog("md5", md5Val, "e10adc3949ba59abbe56e057f20f883e") // Base64 Encode def b64Encoded = $strings.base64Encode("A") assertLog("base64Encode", b64Encoded, "QQ==") // Base64 Decode def b64Decoded = $strings.base64Decode("QQ==") assertLog("base64Decode", b64Decoded, "A") // Hex Encode ( 'A' 的 ASCII 是 65 -> 0x41, 'B' 是 66 -> 0x42 ) def hexStr = $strings.toHexString("AB") assertLog("toHexString", hexStr, "4142") // 视实现细节,可能是 4142 或 41004200 (如果是UTF-16),通常是UTF-8 hex // Hex Decode def fromHex = $strings.fromHexString("4142") assertLog("fromHexString", fromHex, "AB") // ========================================== // 7. 字节转换 (Bytes) // ========================================== // toBytes def bytes = $strings.toBytes("Test", "UTF-8") $console.log("👉 toBytes 长度: " + bytes.length) // fromBytes def strFromBytes = $strings.fromBytes(bytes, "UTF-8") assertLog("fromBytes", strFromBytes, "Test") // ========================================== // 8. 随机生成 (Random) // ========================================== def randStr = $strings.random(16) $console.log("👉 random(16) 结果: " + randStr) if ($strings.getLength(randStr) == 16) { $console.log("✅ [random] Passed (Length check).") } else { $console.error("❌ [random] Failed.") } $console.log("🎉 Strings 接口测试完毕。") // ========================================== // 辅助函数:用于对比结果并打印日志 // ========================================== def assertLog(name, actual, expected) { // 简单的相等性检查,忽略大小写敏感的Hex情况可以自行调整 if (actual.toString() == expected.toString()) { $console.log("✅ [${name}] Passed. Result: ${actual}") } else { $console.error("❌ [${name}] Failed.\n Expected: ${expected}\n Actual : ${actual}") } } $script.getThreads().getMain().setBackground(true)
/** * Strings 接口测试脚本 * 全局变量: $strings, $reflectors, $arrays */ $console.log("🚀 开始测试 Strings 接口..."); // ========================================== // 1. 基础属性与检查 (Basic Check) // ========================================== const textBasic = " Hello M8 "; const emptyText = ""; const blankText = " "; // getLength const len = $strings.getLength(textBasic); assertLog("getLength", len, 10); // isEmpty assertLog("isEmpty - false", $strings.isEmpty(textBasic), false); assertLog("isEmpty - true", $strings.isEmpty(emptyText), true); // isBlank assertLog("isBlank - false", $strings.isBlank(textBasic), false); assertLog("isBlank - true (空格)", $strings.isBlank(blankText), true); assertLog("isBlank - true (空串)", $strings.isBlank(emptyText), true); // ========================================== // 2. 字符串操作 (Manipulation) // ========================================== // concat const concatRes = $strings.concat("M8", "Test"); assertLog("concat", concatRes, "M8Test"); // format // 修正:使用 $reflectors 获取 Object Class const ObjectClass = $reflectors.reflectClassName("java.lang.Object").getTargetClass(); const fmtRes = $strings.format("Hello %s, Score: %s", $arrays.arrayOf(ObjectClass, "World", "100")); assertLog("format", fmtRes, "Hello World, Score: 100"); // toUpperCase / toLowerCase assertLog("toUpperCase", $strings.toUpperCase("abc"), "ABC"); assertLog("toLowerCase", $strings.toLowerCase("XYZ"), "xyz"); // trim assertLog("trim", $strings.trim(" abc "), "abc"); // ========================================== // 3. 搜索与判断 (Search & Check) // ========================================== const textSearch = "banana"; // contains assertLog("contains - true", $strings.contains(textSearch, "nan"), true); assertLog("contains - false", $strings.contains(textSearch, "xyz"), false); // startsWith / endsWith assertLog("startsWith", $strings.startsWith(textSearch, "ba"), true); assertLog("endsWith", $strings.endsWith(textSearch, "na"), true); // indexOf / lastIndexOf assertLog("indexOf", $strings.indexOf(textSearch, "a"), 1); // 第一个 a 在索引 1 assertLog("lastIndexOf", $strings.lastIndexOf(textSearch, "a"), 5); // 最后一个 a 在索引 5 // ========================================== // 4. 截取与分割 (Substring & Split) // ========================================== const textSub = "0123456789"; // substring (常规) assertLog("substring (1, 4)", $strings.substring(textSub, 1, 4), "123"); // substring (测试 -1 截取到末尾) assertLog("substring (5, -1)", $strings.substring(textSub, 5, -1), "56789"); // split const splitList = $strings.split("A,B,C", ","); $console.log("👉 split 结果: " + splitList); // 应该是 [A, B, C] if (splitList.size() == 3 && splitList.get(1) == "B") { $console.log("✅ [split] Passed."); } else { $console.error("❌ [split] Failed."); } // ========================================== // 5. 替换 (Replacement) // ========================================== // replace (普通替换) assertLog("replace", $strings.replace("aba", "a", "c"), "cbc"); // replaceRegex (正则替换:将数字替换为 *) assertLog("replaceRegex", $strings.replaceRegex("a1b2c3", "\\d", "*"), "a*b*c*"); // ========================================== // 6. 加密与编码 (Crypto & Encoding) // ========================================== const raw = "123456"; // MD5 // 123456 的 MD5 通常是 e10adc3949ba59abbe56e057f20f883e const md5Val = $strings.md5(raw); assertLog("md5", md5Val, "e10adc3949ba59abbe56e057f20f883e"); // Base64 Encode const b64Encoded = $strings.base64Encode("A"); assertLog("base64Encode", b64Encoded, "QQ=="); // Base64 Decode const b64Decoded = $strings.base64Decode("QQ=="); assertLog("base64Decode", b64Decoded, "A"); // Hex Encode ( 'A' 的 ASCII 是 65 -> 0x41, 'B' 是 66 -> 0x42 ) const hexStr = $strings.toHexString("AB"); assertLog("toHexString", hexStr, "4142"); // Hex Decode const fromHex = $strings.fromHexString("4142"); assertLog("fromHexString", fromHex, "AB"); // ========================================== // 7. 字节转换 (Bytes) // ========================================== // toBytes const bytes = $strings.toBytes("Test", "UTF-8"); $console.log("👉 toBytes 长度: " + bytes.length); // fromBytes const strFromBytes = $strings.fromBytes(bytes, "UTF-8"); assertLog("fromBytes", strFromBytes, "Test"); // ========================================== // 8. 随机生成 (Random) // ========================================== const randStr = $strings.random(16); $console.log("👉 random(16) 结果: " + randStr); if ($strings.getLength(randStr) == 16) { $console.log("✅ [random] Passed (Length check)."); } else { $console.error("❌ [random] Failed."); } $console.log("🎉 Strings 接口测试完毕。"); // ========================================== // 辅助函数:用于对比结果并打印日志 // ========================================== /** * @param {string} name * @param {any} actual * @param {any} expected */ function assertLog(name, actual, expected) { if (actual.toString() == expected.toString()) { $console.log("✅ [" + name + "] Passed. Result: " + actual); } else { $console.error("❌ [" + name + "] Failed.\n Expected: " + expected + "\n Actual : " + actual); } } $script.getThreads().getMain().setBackground(true);
-- 文件名: 4.lua -- 引入转换所需的 Java 类 local String = require("m8test_java.java.lang.String") local Object = require("m8test_java.java.lang.Object") --- -- Strings 接口测试脚本 -- 全局变量: _strings -- _console:log("🚀 开始测试 Strings 接口...") -- ========================================== -- 辅助函数:用于对比结果并打印日志 -- ========================================== local function assertLog(name, actual, expected) -- 简单的相等性检查 if tostring(actual) == tostring(expected) then _console:log("✅ [" .. name .. "] Passed. Result: " .. tostring(actual)) else _console:error("❌ [" .. name .. "] Failed.\n Expected: " .. tostring(expected) .. "\n Actual : " .. tostring(actual)) end end -- ========================================== -- 1. 基础属性与检查 (Basic Check) -- ========================================== local textBasic = " Hello M8 " local emptyText = "" local blankText = " " -- getLength local len = _strings:getLength(textBasic) assertLog("getLength", len, 10) -- isEmpty assertLog("isEmpty - false", _strings:isEmpty(textBasic), false) assertLog("isEmpty - true", _strings:isEmpty(emptyText), true) -- isBlank assertLog("isBlank - false", _strings:isBlank(textBasic), false) assertLog("isBlank - true (空格)", _strings:isBlank(blankText), true) assertLog("isBlank - true (空串)", _strings:isBlank(emptyText), true) -- ========================================== -- 2. 字符串操作 (Manipulation) -- ========================================== -- concat local concatRes = _strings:concat("M8", "Test") assertLog("concat", concatRes, "M8Test") -- format local formatArgs = _arrays:arrayOf(Object, "World", 100) local fmtRes = _strings:format("Hello %s, Score: %d", formatArgs) assertLog("format", fmtRes, "Hello World, Score: 100") -- toUpperCase / toLowerCase assertLog("toUpperCase", _strings:toUpperCase("abc"), "ABC") assertLog("toLowerCase", _strings:toLowerCase("XYZ"), "xyz") -- trim assertLog("trim", _strings:trim(" abc "), "abc") -- ========================================== -- 3. 搜索与判断 (Search & Check) -- ========================================== local textSearch = "banana" -- contains assertLog("contains - true", _strings:contains(textSearch, "nan"), true) assertLog("contains - false", _strings:contains(textSearch, "xyz"), false) -- startsWith / endsWith assertLog("startsWith", _strings:startsWith(textSearch, "ba"), true) assertLog("endsWith", _strings:endsWith(textSearch, "na"), true) -- indexOf / lastIndexOf assertLog("indexOf", _strings:indexOf(textSearch, "a"), 1) -- 第一个 a 在索引 1 assertLog("lastIndexOf", _strings:lastIndexOf(textSearch, "a"), 5) -- 最后一个 a 在索引 5 -- ========================================== -- 4. 截取与分割 (Substring & Split) -- ========================================== local textSub = "0123456789" -- substring (常规) assertLog("substring (1, 4)", _strings:substring(textSub, 1, 4), "123") -- substring (测试 -1 截取到末尾) assertLog("substring (5, -1)", _strings:substring(textSub, 5, -1), "56789") -- split (返回一个 Java List, 必须包装后才能操作) local rawSplitList = _strings:split("A,B,C", ",") local splitList = _objectWrappers:wrapList(rawSplitList) _console:log("👉 split 结果: " .. tostring(splitList:getOrigin())) -- 应该是 [A, B, C] -- ListWrapper 有 count() 方法, get(index) 需要通过 getOrigin() 调用原始 list 的方法 if splitList:count() == 3 and splitList:getOrigin():get(1) == "B" then _console:log("✅ [split] Passed.") else _console:error("❌ [split] Failed.") end -- ========================================== -- 5. 替换 (Replacement) -- ========================================== -- replace (普通替换) assertLog("replace", _strings:replace("aba", "a", "c"), "cbc") -- replaceRegex (正则替换:将数字替换为 *) assertLog("replaceRegex", _strings:replaceRegex("a1b2c3", "\\d", "*"), "a*b*c*") -- ========================================== -- 6. 加密与编码 (Crypto & Encoding) -- ========================================== local raw = "123456" -- MD5 -- 123456 的 MD5 通常是 e10adc3949ba59abbe56e057f20f883e local md5Val = _strings:md5(raw) assertLog("md5", md5Val, "e10adc3949ba59abbe56e057f20f883e") -- Base64 Encode local b64Encoded = _strings:base64Encode("A") assertLog("base64Encode", b64Encoded, "QQ==") -- Base64 Decode local b64Decoded = _strings:base64Decode("QQ==") assertLog("base64Decode", b64Decoded, "A") -- Hex Encode ( 'A' 的 ASCII 是 65 -> 0x41, 'B' 是 66 -> 0x42 ) local hexStr = _strings:toHexString("AB") assertLog("toHexString", hexStr, "4142") -- Hex Decode local fromHex = _strings:fromHexString("4142") assertLog("fromHexString", fromHex, "AB") -- ========================================== -- 7. 字节转换 (Bytes) -- ========================================== -- toBytes (返回 byte[], 必须包装) local rawBytes = _strings:toBytes("Test", "UTF-8") local wrappedBytes = _objectWrappers:wrapByteArray(rawBytes) -- <<<<< 修正点:包装 byte[] _console:log("👉 toBytes 长度: " .. tostring(wrappedBytes:count())) -- <<<<< 修正点:使用包装器的 count() 方法 -- fromBytes (需要传入原始的 byte[] 对象) local strFromBytes = _strings:fromBytes(rawBytes, "UTF-8") -- <<<<< 修正点:传入原始对象 assertLog("fromBytes", strFromBytes, "Test") -- ========================================== -- 8. 随机生成 (Random) -- ========================================== local randStr = _strings:random(16) _console:log("👉 random(16) 结果: " .. randStr) if _strings:getLength(randStr) == 16 then _console:log("✅ [random] Passed (Length check).") else _console:error("❌ [random] Failed.") end _console:log("🎉 Strings 接口测试完毕。") _script:getThreads():getMain():setBackground(true)
<?php /** @var m8test_java\com\m8test\script\core\api\text\Strings $strings */ global $strings; /** @var m8test_java\com\m8test\script\core\api\console\Console $console */ global $console; /** @var m8test_java\com\m8test\script\core\api\engine\Script $script */ global $script; /** @var m8test_java\com\m8test\script\core\api\wrapper\ObjectWrappers $objectWrappers */ global $objectWrappers; /** * Strings 接口测试脚本 * 全局变量: $strings */ $console->log(javaString("🚀 开始测试 Strings 接口...")); // ========================================== // 1. 基础属性与检查 (Basic Check) // ========================================== $textBasic = javaString(" Hello M8 "); $emptyText = javaString(""); $blankText = javaString(" "); // getLength $len = $strings->getLength($textBasic); assertLog(javaString("getLength"), $len, 10); // isEmpty assertLog(javaString("isEmpty - false"), $strings->isEmpty($textBasic), false); assertLog(javaString("isEmpty - true"), $strings->isEmpty($emptyText), true); // isBlank assertLog(javaString("isBlank - false"), $strings->isBlank($textBasic), false); assertLog(javaString("isBlank - true (空格)"), $strings->isBlank($blankText), true); assertLog(javaString("isBlank - true (空串)"), $strings->isBlank($emptyText), true); // ========================================== // 2. 字符串操作 (Manipulation) // ========================================== // concat $concatRes = $strings->concat(javaString("M8"), javaString("Test")); assertLog(javaString("concat"), $concatRes, javaString("M8Test")); // format (注意:Groovy List 传给 Java Array 需要转换,或者引擎支持直接传) // 假设这里直接传 List 即可,若报错请尝试 ["World"] as Object[] //$fmtRes = $strings->format(javaString("Hello %s, Score: %s"), [javaString("World"), "100"]); //assertLog(javaString("format"), $fmtRes, javaString("Hello World, Score: 100")); // toUpperCase / toLowerCase assertLog(javaString("toUpperCase"), $strings->toUpperCase(javaString("abc")), javaString("ABC")); assertLog(javaString("toLowerCase"), $strings->toLowerCase(javaString("XYZ")), javaString("xyz")); // trim assertLog(javaString("trim"), $strings->trim(javaString(" abc ")), javaString("abc")); // ========================================== // 3. 搜索与判断 (Search & Check) // ========================================== $textSearch = javaString("banana"); // contains assertLog(javaString("contains - true"), $strings->contains($textSearch, javaString("nan")), true); assertLog(javaString("contains - false"), $strings->contains($textSearch, javaString("xyz")), false); // startsWith / endsWith assertLog(javaString("startsWith"), $strings->startsWith($textSearch, javaString("ba")), true); assertLog(javaString("endsWith"), $strings->endsWith($textSearch, javaString("na")), true); // indexOf / lastIndexOf assertLog(javaString("indexOf"), $strings->indexOf($textSearch, javaString("a")), 1); // 第一个 a 在索引 1 assertLog(javaString("lastIndexOf"), $strings->lastIndexOf($textSearch, javaString("a")), 5); // 最后一个 a 在索引 5 // ========================================== // 4. 截取与分割 (Substring & Split) // ========================================== $textSub = javaString("0123456789"); // substring (常规) assertLog(javaString("substring (1, 4)"), $strings->substring($textSub, 1, 4), javaString("123")); // substring (测试 -1 截取到末尾) assertLog(javaString("substring (5, -1)"), $strings->substring($textSub, 5, -1), javaString("56789")); // split $rawSplitList = $strings->split(javaString("A,B,C"), javaString(",")); $splitListWrapper = $objectWrappers->wrapList($rawSplitList); // 使用 wrapList 包装 $console->log(javaString("👉 split 结果: "), $splitListWrapper); // 应该是 [A, B, C] if ($splitListWrapper->getSize() == 3 && $splitListWrapper->getOrNull(1) == "B") { $console->log(javaString("✅ [split] Passed.")); } else { $console->error(javaString("❌ [split] Failed.")); } // ========================================== // 5. 替换 (Replacement) // ========================================== // replace (普通替换) assertLog(javaString("replace"), $strings->replace(javaString("aba"), javaString("a"), javaString("c")), javaString("cbc")); // replaceRegex (正则替换:将数字替换为 *) assertLog(javaString("replaceRegex"), $strings->replaceRegex(javaString("a1b2c3"), javaString("\\d"), javaString("*")), javaString("a*b*c*")); // ========================================== // 6. 加密与编码 (Crypto & Encoding) // ========================================== $raw = javaString("123456"); // MD5 // 123456 的 MD5 通常是 e10adc3949ba59abbe56e057f20f883e $md5Val = $strings->md5($raw); assertLog(javaString("md5"), $md5Val, javaString("e10adc3949ba59abbe56e057f20f883e")); // Base64 Encode $b64Encoded = $strings->base64Encode(javaString("A")); assertLog(javaString("base64Encode"), $b64Encoded, javaString("QQ==")); // Base64 Decode $b64Decoded = $strings->base64Decode(javaString("QQ==")); assertLog(javaString("base64Decode"), $b64Decoded, javaString("A")); // Hex Encode ( 'A' 的 ASCII 是 65 -> 0x41, 'B' 是 66 -> 0x42 ) $hexStr = $strings->toHexString(javaString("AB")); assertLog(javaString("toHexString"), $hexStr, javaString("4142")); // 视实现细节,可能是 4142 或 41004200 (如果是UTF-16),通常是UTF-8 hex // Hex Decode $fromHex = $strings->fromHexString(javaString("4142")); assertLog(javaString("fromHexString"), $fromHex, javaString("AB")); // ========================================== // 7. 字节转换 (Bytes) // ========================================== // toBytes $rawBytes = $strings->toBytes(javaString("Test"), javaString("UTF-8")); $bytesWrapper = $objectWrappers->wrapByteArray($rawBytes); // 使用 wrapByteArray 包装 $console->log(javaString("👉 toBytes 长度: "), $bytesWrapper->count()); // fromBytes (需要传递原始的byte[], 而不是包装器) $strFromBytes = $strings->fromBytes($rawBytes, javaString("UTF-8")); assertLog(javaString("fromBytes"), $strFromBytes, javaString("Test")); // ========================================== // 8. 随机生成 (Random) // ========================================== $randStr = $strings->random(16); $console->log(javaString("👉 random(16) 结果: "), $randStr); if ($strings->getLength($randStr) == 16) { $console->log(javaString("✅ [random] Passed (Length check).")); } else { $console->error(javaString("❌ [random] Failed.")); } $console->log(javaString("🎉 Strings 接口测试完毕。")); // ========================================== // 辅助函数:用于对比结果并打印日志 // ========================================== function assertLog($name, $actual, $expected) { global $console; if ($actual == $expected) { $console->log("✅ [" . $name . "] Passed. Result: ", $actual); } else { $console->error("❌ [" . $name . "] Failed.\n Expected: ", $expected, "\n Actual : ", $actual); } } $script->getThreads()->getMain()->setBackground(true);
# 导入所需的全局变量 from m8test_java.com.m8test.script.GlobalVariables import _console from m8test_java.com.m8test.script.GlobalVariables import _strings from m8test_java.com.m8test.script.GlobalVariables import _script from m8test_java.com.m8test.script.GlobalVariables import _arrays from m8test_java.com.m8test.script.GlobalVariables import _reflectors # ========================================== # 辅助函数:用于对比结果并打印日志 # ========================================== def assertLog(name, actual, expected): # 使用 str() 转换以安全比较 if str(actual) == str(expected): _console.log(f"✅ [{name}] Passed. Result: {actual}") else: _console.error(f"❌ [{name}] Failed.\n Expected: {expected}\n Actual : {actual}") """ Strings 接口测试脚本 全局变量: $strings """ _console.log("🚀 开始测试 Strings 接口...") # ========================================== # 1. 基础属性与检查 (Basic Check) # ========================================== textBasic = " Hello M8 " emptyText = "" blankText = " " # getLength len_val = _strings.getLength(textBasic) assertLog("getLength", len_val, 10) # isEmpty assertLog("isEmpty - false", _strings.isEmpty(textBasic), False) assertLog("isEmpty - true", _strings.isEmpty(emptyText), True) # isBlank assertLog("isBlank - false", _strings.isBlank(textBasic), False) assertLog("isBlank - true (空格)", _strings.isBlank(blankText), True) assertLog("isBlank - true (空串)", _strings.isBlank(emptyText), True) # ========================================== # 2. 字符串操作 (Manipulation) # ========================================== # concat concatRes = _strings.concat("M8", "Test") assertLog("concat", concatRes, "M8Test") # format # Python list is passed to Java method, Jep handles conversion. # Using _arrays.arrayOf is a more robust way if direct list passing fails. # 获取 java.lang.Object 的 Class 对象 object_class = _reflectors.reflect("java.lang.Object").getTargetClass() fmtRes = _strings.format("Hello %s, Score: %d", _arrays.arrayOf(object_class, "World", 100)) assertLog("format", fmtRes, "Hello World, Score: 100") # toUpperCase / toLowerCase assertLog("toUpperCase", _strings.toUpperCase("abc"), "ABC") assertLog("toLowerCase", _strings.toLowerCase("XYZ"), "xyz") # trim assertLog("trim", _strings.trim(" abc "), "abc") # ========================================== # 3. 搜索与判断 (Search & Check) # ========================================== textSearch = "banana" # contains assertLog("contains - true", _strings.contains(textSearch, "nan"), True) assertLog("contains - false", _strings.contains(textSearch, "xyz"), False) # startsWith / endsWith assertLog("startsWith", _strings.startsWith(textSearch, "ba"), True) assertLog("endsWith", _strings.endsWith(textSearch, "na"), True) # indexOf / lastIndexOf assertLog("indexOf", _strings.indexOf(textSearch, "a"), 1) # 第一个 a 在索引 1 assertLog("lastIndexOf", _strings.lastIndexOf(textSearch, "a"), 5) # 最后一个 a 在索引 5 # ========================================== # 4. 截取与分割 (Substring & Split) # ========================================== textSub = "0123456789" # substring (常规) assertLog("substring (1, 4)", _strings.substring(textSub, 1, 4), "123") # substring (测试 -1 截取到末尾) assertLog("substring (5, -1)", _strings.substring(textSub, 5, -1), "56789") # split splitList = _strings.split("A,B,C", ",") _console.log("👉 split 结果: " + str(splitList)) # 应该是 [A, B, C] if len(splitList) == 3 and splitList[1] == "B": _console.log("✅ [split] Passed.") else: _console.error("❌ [split] Failed.") # ========================================== # 5. 替换 (Replacement) # ========================================== # replace (普通替换) assertLog("replace", _strings.replace("aba", "a", "c"), "cbc") # replaceRegex (正则替换:将数字替换为 *) assertLog("replaceRegex", _strings.replaceRegex("a1b2c3", "\\d", "*"), "a*b*c*") # ========================================== # 6. 加密与编码 (Crypto & Encoding) # ========================================== raw = "123456" # MD5 md5Val = _strings.md5(raw) assertLog("md5", md5Val, "e10adc3949ba59abbe56e057f20f883e") # Base64 Encode b64Encoded = _strings.base64Encode("A") assertLog("base64Encode", b64Encoded, "QQ==") # Base64 Decode b64Decoded = _strings.base64Decode("QQ==") assertLog("base64Decode", b64Decoded, "A") # Hex Encode hexStr = _strings.toHexString("AB") assertLog("toHexString", hexStr, "4142") # Hex Decode fromHex = _strings.fromHexString("4142") assertLog("fromHexString", fromHex, "AB") # ========================================== # 7. 字节转换 (Bytes) # ========================================== # toBytes bytes_val = _strings.toBytes("Test", "UTF-8") _console.log("👉 toBytes 长度: " + str(len(bytes_val))) # fromBytes strFromBytes = _strings.fromBytes(bytes_val, "UTF-8") assertLog("fromBytes", strFromBytes, "Test") # ========================================== # 8. 随机生成 (Random) # ========================================== randStr = _strings.random(16) _console.log("👉 random(16) 结果: " + randStr) if _strings.getLength(randStr) == 16: _console.log("✅ [random] Passed (Length check).") else: _console.error("❌ [random] Failed.") _console.log("🎉 Strings 接口测试完毕。") _script.getThreads().getMain().setBackground(True)
# encoding: utf-8 # Strings 接口测试脚本 # 全局变量: $strings $console.log("🚀 开始测试 Strings 接口...") # ========================================== # 辅助函数:用于对比结果并打印日志 # ========================================== def assertLog(name, actual, expected) # 简单的相等性检查,忽略大小写敏感的Hex情况可以自行调整 if actual.to_s == expected.to_s $console.log("✅ [#{name}] Passed. Result: #{actual}") else $console.error("❌ [#{name}] Failed.\n Expected: #{expected}\n Actual : #{actual}") end end # ========================================== # 1. 基础属性与检查 (Basic Check) # ========================================== textBasic = " Hello M8 " emptyText = "" blankText = " " # getLength len = $strings.getLength(textBasic) assertLog("getLength", len, 10) # isEmpty assertLog("isEmpty - false", $strings.isEmpty(textBasic), false) assertLog("isEmpty - true", $strings.isEmpty(emptyText), true) # isBlank assertLog("isBlank - false", $strings.isBlank(textBasic), false) assertLog("isBlank - true (空格)", $strings.isBlank(blankText), true) assertLog("isBlank - true (空串)", $strings.isBlank(emptyText), true) # ========================================== # 2. 字符串操作 (Manipulation) # ========================================== # concat concatRes = $strings.concat("M8", "Test") assertLog("concat", concatRes, "M8Test") # format # Ruby 中使用 Array [...] 传递参数 fmtRes = $strings.format("Hello %s, Score: %d", ["World", 100]) assertLog("format", fmtRes, "Hello World, Score: 100") # toUpperCase / toLowerCase assertLog("toUpperCase", $strings.toUpperCase("abc"), "ABC") assertLog("toLowerCase", $strings.toLowerCase("XYZ"), "xyz") # trim assertLog("trim", $strings.trim(" abc "), "abc") # ========================================== # 3. 搜索与判断 (Search & Check) # ========================================== textSearch = "banana" # contains assertLog("contains - true", $strings.contains(textSearch, "nan"), true) assertLog("contains - false", $strings.contains(textSearch, "xyz"), false) # startsWith / endsWith assertLog("startsWith", $strings.startsWith(textSearch, "ba"), true) assertLog("endsWith", $strings.endsWith(textSearch, "na"), true) # indexOf / lastIndexOf assertLog("indexOf", $strings.indexOf(textSearch, "a"), 1) # 第一个 a 在索引 1 assertLog("lastIndexOf", $strings.lastIndexOf(textSearch, "a"), 5) # 最后一个 a 在索引 5 # ========================================== # 4. 截取与分割 (Substring & Split) # ========================================== textSub = "0123456789" # substring (常规) assertLog("substring (1, 4)", $strings.substring(textSub, 1, 4), "123") # substring (测试 -1 截取到末尾) assertLog("substring (5, -1)", $strings.substring(textSub, 5, -1), "56789") # split splitList = $strings.split("A,B,C", ",") $console.log("👉 split 结果: " + splitList.to_s) # 应该是 [A, B, C] # Ruby 中数组大小使用 .size 或 .length, 访问元素使用 [] if splitList.size == 3 && splitList[1] == "B" $console.log("✅ [split] Passed.") else $console.error("❌ [split] Failed.") end # ========================================== # 5. 替换 (Replacement) # ========================================== # replace (普通替换) assertLog("replace", $strings.replace("aba", "a", "c"), "cbc") # replaceRegex (正则替换:将数字替换为 *) assertLog("replaceRegex", $strings.replaceRegex("a1b2c3", "\\d", "*"), "a*b*c*") # ========================================== # 6. 加密与编码 (Crypto & Encoding) # ========================================== raw = "123456" # MD5 # 123456 的 MD5 通常是 e10adc3949ba59abbe56e057f20f883e md5Val = $strings.md5(raw) assertLog("md5", md5Val, "e10adc3949ba59abbe56e057f20f883e") # Base64 Encode b64Encoded = $strings.base64Encode("A") assertLog("base64Encode", b64Encoded, "QQ==") # Base64 Decode b64Decoded = $strings.base64Decode("QQ==") assertLog("base64Decode", b64Decoded, "A") # Hex Encode ( 'A' 的 ASCII 是 65 -> 0x41, 'B' 是 66 -> 0x42 ) hexStr = $strings.toHexString("AB") assertLog("toHexString", hexStr, "4142") # 视实现细节,可能是 4142 或 41004200 (如果是UTF-16),通常是UTF-8 hex # Hex Decode fromHex = $strings.fromHexString("4142") assertLog("fromHexString", fromHex, "AB") # ========================================== # 7. 字节转换 (Bytes) # ========================================== # toBytes bytes = $strings.toBytes("Test", "UTF-8") # 这里假设返回的是 Java 数组或 wrapper,Ruby 中可能需要使用 .size 或 .length $console.log("👉 toBytes 长度: " + bytes.length.to_s) # fromBytes strFromBytes = $strings.fromBytes(bytes, "UTF-8") assertLog("fromBytes", strFromBytes, "Test") # ========================================== # 8. 随机生成 (Random) # ========================================== randStr = $strings.random(16) $console.log("👉 random(16) 结果: " + randStr.to_s) if $strings.getLength(randStr) == 16 $console.log("✅ [random] Passed (Length check).") else $console.error("❌ [random] Failed.") end $console.log("🎉 Strings 接口测试完毕。") $script.getThreads().getMain().setBackground(true)
09 December 2025