2025-09-23 04:51:17 +09:00
|
|
|
|
// 完全統合テスト - 美しいモジュラー設計の動作確認
|
|
|
|
|
|
|
2025-11-06 15:41:52 +09:00
|
|
|
|
using "apps/lib/json_native/core/node.hako" as JsonNode
|
2025-09-23 04:51:17 +09:00
|
|
|
|
|
|
|
|
|
|
print("🎨 Nyash JSON Native 統合テスト開始")
|
|
|
|
|
|
print("美しいモジュラー設計 vs yyjson巨大ファイル")
|
|
|
|
|
|
|
|
|
|
|
|
// ===== 基本JSON生成・パーステスト =====
|
|
|
|
|
|
|
|
|
|
|
|
print("\n📊 基本JSON操作テスト")
|
|
|
|
|
|
|
|
|
|
|
|
// 複雑なJSONオブジェクト生成
|
|
|
|
|
|
local user = JsonNode.create_object()
|
|
|
|
|
|
user.object_set("id", JsonNode.create_int(42))
|
|
|
|
|
|
user.object_set("name", JsonNode.create_string("Alice \"Wonder\" Smith"))
|
|
|
|
|
|
user.object_set("active", JsonNode.create_bool(true))
|
|
|
|
|
|
user.object_set("profile", JsonNode.create_null())
|
|
|
|
|
|
|
|
|
|
|
|
// 配列生成
|
|
|
|
|
|
local tags = JsonNode.create_array()
|
|
|
|
|
|
tags.array_push(JsonNode.create_string("developer"))
|
|
|
|
|
|
tags.array_push(JsonNode.create_string("nyash-lover"))
|
|
|
|
|
|
tags.array_push(JsonNode.create_string("json-native"))
|
|
|
|
|
|
|
|
|
|
|
|
user.object_set("tags", tags)
|
|
|
|
|
|
|
|
|
|
|
|
// ネストしたオブジェクト
|
|
|
|
|
|
local settings = JsonNode.create_object()
|
|
|
|
|
|
settings.object_set("theme", JsonNode.create_string("dark"))
|
|
|
|
|
|
settings.object_set("notifications", JsonNode.create_bool(false))
|
|
|
|
|
|
|
|
|
|
|
|
user.object_set("settings", settings)
|
|
|
|
|
|
|
|
|
|
|
|
// JSON文字列生成
|
|
|
|
|
|
local json_output = user.stringify()
|
|
|
|
|
|
print("Generated JSON:")
|
|
|
|
|
|
print(json_output)
|
|
|
|
|
|
|
|
|
|
|
|
// ===== パース・ラウンドトリップテスト =====
|
|
|
|
|
|
|
|
|
|
|
|
print("\n🔄 パース・ラウンドトリップテスト")
|
|
|
|
|
|
|
|
|
|
|
|
// 基本値のパースとラウンドトリップ
|
|
|
|
|
|
local test_cases = new ArrayBox()
|
|
|
|
|
|
test_cases.push("null")
|
|
|
|
|
|
test_cases.push("true")
|
|
|
|
|
|
test_cases.push("false")
|
|
|
|
|
|
test_cases.push("42")
|
|
|
|
|
|
test_cases.push("\"hello world\"")
|
|
|
|
|
|
test_cases.push("\"say \\\"hello\\\"\"")
|
|
|
|
|
|
test_cases.push("[]")
|
|
|
|
|
|
test_cases.push("{}")
|
|
|
|
|
|
|
|
|
|
|
|
local i = 0
|
|
|
|
|
|
loop(i < test_cases.length()) {
|
|
|
|
|
|
local input = test_cases.get(i)
|
|
|
|
|
|
local parsed = JsonNode.parse(input)
|
|
|
|
|
|
local output = parsed.stringify()
|
|
|
|
|
|
|
|
|
|
|
|
print("Input: " + input)
|
|
|
|
|
|
print("Output: " + output)
|
|
|
|
|
|
|
|
|
|
|
|
if input == output {
|
|
|
|
|
|
print("✅ Perfect roundtrip!")
|
|
|
|
|
|
} else {
|
|
|
|
|
|
print("⚠️ Roundtrip difference (expected for complex cases)")
|
|
|
|
|
|
}
|
|
|
|
|
|
print("")
|
|
|
|
|
|
|
|
|
|
|
|
i = i + 1
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ===== 型安全アクセステスト =====
|
|
|
|
|
|
|
|
|
|
|
|
print("\n🔒 型安全アクセステスト")
|
|
|
|
|
|
|
|
|
|
|
|
// 数値アクセス
|
|
|
|
|
|
local num_node = JsonNode.create_int(123)
|
|
|
|
|
|
print("Integer node as int: " + num_node.as_int()) // 123
|
|
|
|
|
|
print("Integer node as string: " + num_node.as_string()) // ""
|
|
|
|
|
|
print("Integer node as bool: " + num_node.as_bool()) // false
|
|
|
|
|
|
|
|
|
|
|
|
// 文字列アクセス
|
|
|
|
|
|
local str_node = JsonNode.create_string("test")
|
|
|
|
|
|
print("String node as string: " + str_node.as_string()) // "test"
|
|
|
|
|
|
print("String node as int: " + str_node.as_int()) // 0
|
|
|
|
|
|
|
|
|
|
|
|
// bool アクセス
|
|
|
|
|
|
local bool_node = JsonNode.create_bool(true)
|
|
|
|
|
|
print("Bool node as bool: " + bool_node.as_bool()) // true
|
|
|
|
|
|
print("Bool node as string: " + bool_node.as_string()) // ""
|
|
|
|
|
|
|
|
|
|
|
|
// ===== 配列・オブジェクト操作テスト =====
|
|
|
|
|
|
|
|
|
|
|
|
print("\n📦 コレクション操作テスト")
|
|
|
|
|
|
|
|
|
|
|
|
// 配列操作
|
|
|
|
|
|
local arr = JsonNode.create_array()
|
|
|
|
|
|
arr.array_push(JsonNode.create_string("first"))
|
|
|
|
|
|
arr.array_push(JsonNode.create_int(2))
|
|
|
|
|
|
arr.array_push(JsonNode.create_bool(true))
|
|
|
|
|
|
|
|
|
|
|
|
print("Array size: " + arr.array_size())
|
|
|
|
|
|
print("Array[0]: " + arr.array_get(0).stringify())
|
|
|
|
|
|
print("Array[1]: " + arr.array_get(1).stringify())
|
|
|
|
|
|
print("Array[2]: " + arr.array_get(2).stringify())
|
|
|
|
|
|
print("Array JSON: " + arr.stringify())
|
|
|
|
|
|
|
|
|
|
|
|
// オブジェクト操作
|
|
|
|
|
|
local obj = JsonNode.create_object()
|
|
|
|
|
|
obj.object_set("key1", JsonNode.create_string("value1"))
|
|
|
|
|
|
obj.object_set("key2", JsonNode.create_int(999))
|
|
|
|
|
|
|
|
|
|
|
|
print("Object['key1']: " + obj.object_get("key1").stringify())
|
|
|
|
|
|
print("Object['key2']: " + obj.object_get("key2").stringify())
|
|
|
|
|
|
print("Object['missing']: " + obj.object_get("missing")) // null
|
|
|
|
|
|
print("Object JSON: " + obj.stringify())
|
|
|
|
|
|
|
|
|
|
|
|
// オブジェクトキー一覧
|
|
|
|
|
|
local keys = obj.object_keys()
|
|
|
|
|
|
print("Object keys count: " + keys.length())
|
|
|
|
|
|
local j = 0
|
|
|
|
|
|
loop(j < keys.length()) {
|
|
|
|
|
|
print("Key[" + j + "]: " + keys.get(j))
|
|
|
|
|
|
j = j + 1
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ===== エッジケーステスト =====
|
|
|
|
|
|
|
|
|
|
|
|
print("\n⚠️ エッジケーステスト")
|
|
|
|
|
|
|
|
|
|
|
|
// 空の構造
|
|
|
|
|
|
local empty_arr = JsonNode.create_array()
|
|
|
|
|
|
local empty_obj = JsonNode.create_object()
|
|
|
|
|
|
print("Empty array: " + empty_arr.stringify())
|
|
|
|
|
|
print("Empty object: " + empty_obj.stringify())
|
|
|
|
|
|
|
|
|
|
|
|
// エスケープが必要な文字列
|
|
|
|
|
|
local special_str = JsonNode.create_string("Line1\nLine2\tTabbed")
|
|
|
|
|
|
print("Special string: " + special_str.stringify())
|
|
|
|
|
|
|
|
|
|
|
|
// null値の処理
|
|
|
|
|
|
local null_node = JsonNode.create_null()
|
|
|
|
|
|
print("Null node: " + null_node.stringify())
|
|
|
|
|
|
print("Null as string: " + null_node.as_string())
|
|
|
|
|
|
print("Null as int: " + null_node.as_int())
|
|
|
|
|
|
print("Null as bool: " + null_node.as_bool())
|
|
|
|
|
|
|
|
|
|
|
|
print("\n🎉 統合テスト完了!")
|
|
|
|
|
|
print("🏆 美しいモジュラー設計の勝利 - 理解しやすく保守しやすいJSON実装")
|
|
|
|
|
|
print("💡 yyjsonの10000行に対し、Nyashは美しい200行×モジュール設計")
|
|
|
|
|
|
|
|
|
|
|
|
// ===== 性能予備測定 =====
|
|
|
|
|
|
|
|
|
|
|
|
print("\n⏱️ 簡易性能テスト")
|
|
|
|
|
|
|
|
|
|
|
|
local start_time = 0 // TODO: 時間測定機能が必要
|
|
|
|
|
|
local iterations = 100
|
|
|
|
|
|
|
|
|
|
|
|
// 生成テスト
|
|
|
|
|
|
local k = 0
|
|
|
|
|
|
loop(k < iterations) {
|
|
|
|
|
|
local temp_obj = JsonNode.create_object()
|
|
|
|
|
|
temp_obj.object_set("id", JsonNode.create_int(k))
|
|
|
|
|
|
temp_obj.object_set("name", JsonNode.create_string("User" + k))
|
|
|
|
|
|
temp_obj.stringify() // JSON生成
|
|
|
|
|
|
k = k + 1
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
print("生成テスト完了: " + iterations + " iterations")
|
|
|
|
|
|
|
|
|
|
|
|
// パーステスト
|
|
|
|
|
|
k = 0
|
|
|
|
|
|
loop(k < iterations) {
|
|
|
|
|
|
JsonNode.parse("42")
|
|
|
|
|
|
JsonNode.parse("\"test\"")
|
|
|
|
|
|
JsonNode.parse("true")
|
|
|
|
|
|
k = k + 1
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
print("パーステスト完了: " + iterations + " iterations")
|
2025-09-25 00:41:56 +09:00
|
|
|
|
print("🚀 次のステップ: Lexer・Parserで複雑なJSON対応")
|