Skip to content

Commit

Permalink
add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
alingse committed Dec 2, 2024
1 parent 7c6328e commit 2a5b098
Show file tree
Hide file tree
Showing 11 changed files with 783 additions and 5 deletions.
2 changes: 2 additions & 0 deletions lib/src/main/java/thriftlabs/thriftfmt/FormatterUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ public void process(PureThriftFormatter formatter, ParseTree node) {

public static final NodeProcessFunc defaultInline = genInlineContext(" ", null);

public static final NodeProcessFunc tightInline = genInlineContext("", null);

public static final NodeProcessFunc listSeparatorInline = genInlineContext(
" ",
(index, node) -> node instanceof ThriftParser.List_separatorContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,15 +282,15 @@ protected void IntegerContext(ThriftParser.IntegerContext node) {
}

protected void Container_typeContext(ThriftParser.Container_typeContext node) {
FormatterUtil.defaultInline.process(this, node);
FormatterUtil.tightInline.process(this, node);
}

protected void Set_typeContext(ThriftParser.Set_typeContext node) {
FormatterUtil.defaultInline.process(this, node);
FormatterUtil.tightInline.process(this, node);
}

protected void List_typeContext(ThriftParser.List_typeContext node) {
FormatterUtil.defaultInline.process(this, node);
FormatterUtil.tightInline.process(this, node);
}

protected void Cpp_typeContext(ThriftParser.Cpp_typeContext node) {
Expand All @@ -310,7 +310,7 @@ protected void List_separatorContext(ThriftParser.List_separatorContext node) {
}

protected void Field_idContext(ThriftParser.Field_idContext node) {
FormatterUtil.defaultInline.process(this, node);
FormatterUtil.tightInline.process(this, node);
}

protected void Field_reqContext(ThriftParser.Field_reqContext node) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package thriftlabs.thriftfmt;

public class ThriftFormatter {
public class ThriftFormatter extends PureThriftFormatter {
public ThriftFormatter() {
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
package thriftlabs.thriftfmt;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

import org.junit.Test;

Expand All @@ -15,4 +27,47 @@ public void TestSimpleFormatter() {
var content = formatter.formatNode(result.document);
assertEquals("include \"shared.thrift\"", content);
}

@SuppressWarnings("resource")
private String readResourceFile(String fileName) {
ClassLoader classLoader = getClass().getClassLoader();
try (InputStream inputStream = classLoader.getResourceAsStream(fileName)) {
if (inputStream == null) {
throw new IllegalArgumentException("File not found: " + fileName);
}
return new Scanner(inputStream, StandardCharsets.UTF_8.name()).useDelimiter("\\A").next();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}

@Test
public void testFindThriftFiles() {
String directory = "src/test/resources/thrifts"; // 资源目录
try {
List<String> thriftFiles = findThriftFiles(directory);
thriftFiles.forEach(path -> testFormatterFile(path));
} catch (IOException e) {
e.printStackTrace();
}
}

private List<String> findThriftFiles(String directory) throws IOException {
Path dirPath = Paths.get(directory);
return Files.walk(dirPath)
.filter(path -> path.toString().endsWith(".thrift"))
.map(Path::getFileName)
.map(Path::toString)
.collect(Collectors.toList());
}

public void testFormatterFile(String fileName) {
String content = readResourceFile("thrifts/" + fileName);
assertNotNull("Fixture file should be found", content);

Thrift.ParserResult result = Thrift.parse(content);
var formatter = new PureThriftFormatter();
formatter.formatNode(result.document);
}
}
70 changes: 70 additions & 0 deletions lib/src/test/resources/thrifts/AnnotationTest.thrift
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

typedef list<i32>( cpp.template = "std::list" ) int_linked_list

struct foo {
1: required i32 bar ( presence = "required" ),
2: required i32 baz ( presence = "manual", cpp.use_pointer = "" ),
3: required i32 qux,
4: required i32 bop,
} ( cpp.type = "DenseFoo", python.type = "DenseFoo", java.final = "", annotation.without.value )

const string default_user = "\'default_user\'" ;
const string default_name = '"abc\'s"' ;
exception foo_error {
1: required i32 error_code ( foo = "bar\'" ),
2: required string error_msg,
} ( foo = "bar" )
typedef string ( unicode.encoding = "UTF-16" ) non_latin_string ( foo = "bar" )
typedef list<double ( cpp.fixed_point = "16" )> tiny_float_list
enum weekdays {
SUNDAY ( weekend = "yes" ),
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY ( weekend = "yes" ),
} ( foo.bar = "baz" )
/* Note that annotations on senum values are not supported. */
struct ostr_default {
1: required i32 bar,
}
struct ostr_custom {
1: required i32 bar,
} ( cpp.customostream )
service foo_service {
void foo() ( foo = "bar" ),
} ( a.b = "c" )
service deprecate_everything {
# TODO: fix LITERAL
void Foo() ( deprecated = "This method has neither 'x' nor \"y\"" ),
void Bar() ( deprecated = "Fails to deliver 中文 колбаса" ),
void Baz() ( deprecated = "Need this to work with tabs (\t) or Umlauts (äöüÄÖÜß) too" ),
void Deprecated() ( deprecated ), // no comment
}
21 changes: 21 additions & 0 deletions lib/src/test/resources/thrifts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Thrift Tutorial

License
=======

Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
Loading

0 comments on commit 2a5b098

Please sign in to comment.