From 3c3681990a8c15852f96f6bf6f4d9f7451df9c92 Mon Sep 17 00:00:00 2001 From: alingse Date: Mon, 9 Dec 2024 23:29:03 +0800 Subject: [PATCH] fix bug && add test --- .../java/thriftlabs/thriftfmt/Option.java | 2 +- .../main/java/thriftlabs/thriftfmt/Util.java | 9 ++++---- .../integration/ThriftFormatterTest.java | 22 +++++++++++++++++++ 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/lib/src/main/java/thriftlabs/thriftfmt/Option.java b/lib/src/main/java/thriftlabs/thriftfmt/Option.java index 1aef508..34b2fb8 100644 --- a/lib/src/main/java/thriftlabs/thriftfmt/Option.java +++ b/lib/src/main/java/thriftlabs/thriftfmt/Option.java @@ -13,7 +13,7 @@ public class Option { private boolean alignByField; public Option() { - this(DEFAULT_INDENT, true, true, true, true, false); + this(DEFAULT_INDENT, true, true, true, false, false); } public Option(int indent, boolean patchRequired, boolean patchSeparator, boolean keepComment, boolean alignByAssign, diff --git a/lib/src/main/java/thriftlabs/thriftfmt/Util.java b/lib/src/main/java/thriftlabs/thriftfmt/Util.java index 1666da4..793a147 100644 --- a/lib/src/main/java/thriftlabs/thriftfmt/Util.java +++ b/lib/src/main/java/thriftlabs/thriftfmt/Util.java @@ -234,15 +234,16 @@ public static void walkNode(ParseTree root, Consumer callback) { stack.add(root); while (!stack.isEmpty()) { - ParseTree node = stack.removeFirst(); // 移除并获取第一个元素 + ParseTree node = stack.removeFirst(); if (node == null) { break; } - callback.accept(root); // 调用回调函数 - List children = getNodeChildren(node); // 获取子节点 + callback.accept(node); + + List children = getNodeChildren(node); for (ParseTree child : children) { - stack.add(child); // 添加子节点到栈中 + stack.add(child); } } } diff --git a/lib/src/test/java/thriftlabs/thriftfmt/integration/ThriftFormatterTest.java b/lib/src/test/java/thriftlabs/thriftfmt/integration/ThriftFormatterTest.java index a8c564d..e588c0b 100644 --- a/lib/src/test/java/thriftlabs/thriftfmt/integration/ThriftFormatterTest.java +++ b/lib/src/test/java/thriftlabs/thriftfmt/integration/ThriftFormatterTest.java @@ -2,6 +2,7 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertEquals; import org.junit.Test; import java.io.IOException; @@ -69,4 +70,25 @@ public void testFormatterFile(String fileName) { var formatter2 = new ThriftFormatter(result, option); formatter2.format(); } + + @Test + public void testWithComplexThrift() { + String origin = "struct A {\n" + + " 1: i64 n,\n" + + " 2: string text\n" + + " 3: boolean flag = true\n" + + "}"; + var result = Thrift.parse(origin); + assertTrue(result.isSuccess()); + var opt = new Option(); + var formatter = new ThriftFormatter(result, opt); + var newContent = formatter.format(); + var expect = "struct A {\n" + + " 1: required i64 n,\n" + + " 2: required string text,\n" + + " 3: required boolean flag = true,\n" + + "}"; + assertEquals(newContent, expect); + } + }