Skip to content

Commit

Permalink
Add comprehensive string truncation test
Browse files Browse the repository at this point in the history
Expanded test coverage in `output_java_test.go` to include a variety of constant types and truncation scenarios. Updated string truncation logic in `java_output.go` to handle different quote lengths correctly for more accurate truncation.
  • Loading branch information
spachava753 committed Oct 10, 2024
1 parent 5d149eb commit c41bb82
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 4 deletions.
10 changes: 6 additions & 4 deletions codemap/java_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func generateJavaFileOutput(src []byte, maxLiteralLen int) (string, error) {
for _, capture := range match.Captures {
start := capture.Node.StartByte()
end := capture.Node.EndByte()
content := src[start:end]
content := string(src[start:end])

// Check if the string literal is within a class, method, or constructor body
inBody := false
Expand All @@ -115,10 +115,12 @@ func generateJavaFileOutput(src []byte, maxLiteralLen int) (string, error) {
}
}

if !inBody && len(content)-2 > maxLiteralLen { // -2 for the quotes
str := strings.Trim(content, "\"")
quoteLen := (len(content) - len(str)) / 2
if !inBody && len(str) > maxLiteralLen {
cutRanges = append(cutRanges, cutRange{
start: start + uint(maxLiteralLen) + 1, // +1 to keep the starting quote
end: end - 1, // -1 to keep the closing quote
start: start + uint(maxLiteralLen) + uint(quoteLen), // +1 to keep the starting quote
end: end - uint(quoteLen), // -quoteLen to keep the closing quote
addEllipsis: true,
})
}
Expand Down
81 changes: 81 additions & 0 deletions codemap/output_java_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,87 @@ func TestGenerateJavaOutput(t *testing.T) {
maxLen int
expected string
}{
{
name: "Comprehensive string literal truncation test",
maxLen: 10,
files: map[string]string{
"Constants.java": `
package com.example;
public class Constants {
// Single constant declaration
public static final String SINGLE_CONST = "This is a long single constant";
// Grouped constant declaration
public static final String GROUPED_CONST1 = "First grouped constant";
public static final String GROUPED_CONST2 = "Second grouped constant";
public static final String GROUPED_CONST3 = """
Third grouped constant
with multiple lines
""";
// Constants with different types
public static final int INT_CONST = 42;
public static final double FLOAT_CONST = 3.14;
public static final boolean BOOL_CONST = true;
public static final char CHAR_CONST = 'A';
public static final String STRING_CONST = "Regular string constant";
// Byte array constants
public static final byte[] BYTE_ARRAY_CONST1 = "Byte array constant".getBytes();
public static final byte[] BYTE_ARRAY_CONST2 = """
Raw byte array constant
""".getBytes();
// Constant expressions
public static final int CONST_EXPR1 = "Hello, World!".length();
public static final int CONST_EXPR2 = 60 * 60 * 24; // Seconds in a day
private Constants() {
// Private constructor to prevent instantiation
}
}
`,
},
expected: `<code_map>
<file>
<path>Constants.java</path>
<file_map>
package com.example;
public class Constants {
// Single constant declaration
public static final String SINGLE_CONST = "This is a ...";
// Grouped constant declaration
public static final String GROUPED_CONST1 = "First grou...";
public static final String GROUPED_CONST2 = "Second gro...";
public static final String GROUPED_CONST3 = """
T...""";
// Constants with different types
public static final int INT_CONST = 42;
public static final double FLOAT_CONST = 3.14;
public static final boolean BOOL_CONST = true;
public static final char CHAR_CONST = 'A';
public static final String STRING_CONST = "Regular st...";
// Byte array constants
public static final byte[] BYTE_ARRAY_CONST1 = "Byte array...".getBytes();
public static final byte[] BYTE_ARRAY_CONST2 = """
R...""".getBytes();
// Constant expressions
public static final int CONST_EXPR1 = "Hello, Wor...".length();
public static final int CONST_EXPR2 = 60 * 60 * 24; // Seconds in a day
private Constants()
}
</file_map>
</file>
</code_map>
`,
},
{
name: "Basic Java class",
maxLen: 50,
Expand Down

0 comments on commit c41bb82

Please sign in to comment.