Skip to content

Commit

Permalink
Merge pull request #42895 from Shadow-Devil/use-text-blocks
Browse files Browse the repository at this point in the history
Replace String concatenation with Text block
  • Loading branch information
warunalakshitha authored Jun 19, 2024
2 parents eedd40d + 09cce4a commit ad9811e
Show file tree
Hide file tree
Showing 65 changed files with 1,414 additions and 1,035 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ public void testValidator() {
Assert.assertFalse(inputValidator.isComplete("while (x < y)"));

// For-each statements
Assert.assertTrue(inputValidator.isComplete("foreach var emp in top3 {\n" +
" io:println(emp);\n" +
"}"));
Assert.assertTrue(inputValidator.isComplete("""
foreach var emp in top3 {
io:println(emp);
}"""));

Assert.assertFalse(inputValidator.isComplete("foreach var emp in top3"));
// TODO : fix #34989
Expand All @@ -68,51 +69,56 @@ public void testValidator() {
// Function definitions
Assert.assertTrue(inputValidator.isComplete("function foo() {\n" +
"}"));
Assert.assertTrue(inputValidator.isComplete("\n" +
"\n" +
"function sum2(float[] v) returns float {\n" +
" float r = 0.0;\n" +
"\n" +
" foreach int i in 0 ..< v.length() {\n" +
" r += v[i];\n" +
" }\n" +
"\n" +
" return r;\n" +
"}"));
Assert.assertTrue(inputValidator.isComplete("function name() {\n" +
"}\n" +
"\n" +
"function foo() {\n" +
"}"));
Assert.assertTrue(inputValidator.isComplete("""
function sum2(float[] v) returns float {
float r = 0.0;
foreach int i in 0 ..< v.length() {
r += v[i];
}
return r;
}"""));
Assert.assertTrue(inputValidator.isComplete("""
function name() {
}
function foo() {
}"""));
Assert.assertTrue(inputValidator.isComplete("var f = function () { x = 12; }"));

Assert.assertFalse(inputValidator.isComplete("function parse(string s) returns int|error {"));
Assert.assertFalse(inputValidator.isComplete("function parse(string s)"));

// Type definitions
Assert.assertTrue(inputValidator.isComplete("type Coord record {\n" +
" int x;\n" +
" int y;\n" +
"};\n" +
"\n" +
"type Coord2 record {\n" +
" int a;\n" +
" int b;\n" +
"};"));
Assert.assertTrue(inputValidator.isComplete("""
type Coord record {
int x;
int y;
};
type Coord2 record {
int a;
int b;
};"""));

Assert.assertFalse(inputValidator.isComplete("type Coord record"));
Assert.assertFalse(inputValidator.isComplete("type Coord record {\n" + "int x;"));

// Query Expressions
Assert.assertTrue(inputValidator.isComplete("Employee[] employees = [\n" +
" {firstName: \"Jones\", lastName: \"Welsh\", salary: 1000.00},\n" +
" ];"));
Assert.assertTrue(inputValidator.isComplete("Employee[] top3 = from var e in employees\n" +
" order by e.salary descending\n" +
"\n" +
" limit 3\n" +
"\n" +
" select e;"));
Assert.assertTrue(inputValidator.isComplete("""
Employee[] employees = [
{firstName: "Jones", lastName: "Welsh", salary: 1000.00},
];"""));
Assert.assertTrue(inputValidator.isComplete("""
Employee[] top3 = from var e in employees
order by e.salary descending
limit 3
select e;"""));

Assert.assertFalse(inputValidator.isComplete("from var e in employees\n" +
" order by e.salary descending"));
Expand All @@ -121,13 +127,13 @@ public void testValidator() {
" {firstName: \"Jones\", lastName: \"Welsh\", salary: 1000.00},"));
Assert.assertFalse(inputValidator.isComplete("Employee[] top3 = from var e in employees\n" +
" order by e.salary descending"));
Assert.assertFalse(inputValidator.isComplete(
"Employee[] top3 = from var e in employees\n" +
" order by e.salary descending\n" +
"\n" +
" limit 3\n" +
"\n" +
" select"));
Assert.assertFalse(inputValidator.isComplete("""
Employee[] top3 = from var e in employees
order by e.salary descending
limit 3
select"""));
Assert.assertFalse(inputValidator.isComplete("int[] evenNums = from var i in nums\n" +
" where i % 2 == 0"));

Expand All @@ -137,12 +143,13 @@ public void testValidator() {
Assert.assertFalse(inputValidator.isComplete("public class Counter {"));

// Expressions
Assert.assertTrue(inputValidator.isComplete("from var e in employees\n" +
" order by e.salary descending\n" +
"\n" +
" limit 3\n" +
"\n" +
" select e;"));
Assert.assertTrue(inputValidator.isComplete("""
from var e in employees
order by e.salary descending
limit 3
select e;"""));

Assert.assertFalse(inputValidator.isComplete("x + "));
Assert.assertFalse(inputValidator.isComplete("from var i in nums\n" +
Expand All @@ -157,88 +164,94 @@ public void testValidator() {
Assert.assertFalse(inputValidator.isComplete("worker A {"));

// Complete code examples
Assert.assertTrue(inputValidator.isComplete("import ballerina/io;\n" +
"\n" +
"public function foo() {\n" +
"\n" +
"}\n" +
"\n" +
"public function foo2() {\n" +
"\n" +
"}"));
Assert.assertTrue(inputValidator.isComplete("import ballerina/io;\n" +
"\n" +
"int x = 1;\n" +
"\n" +
"public function foo() {\n" +
"\n" +
"}\n" +
"\n" +
"public function name() {\n" +
"\n" +
"}"));
Assert.assertTrue(inputValidator.isComplete("function foo() {\n" +
"\n" +
" int x = 5;\n" +
"\n" +
" if (x < 10) {\n" +
" io:println(x);\n" +
" }\n" +
"\n" +
" while (x < 10) {\n" +
" x = x + 1;\n" +
" }\n" +
"\n" +
"}"));
Assert.assertTrue(inputValidator.isComplete("""
import ballerina/io;
// Test case related to annotation not attached to construct error message
Assert.assertTrue(inputValidator.isComplete("@http:ServiceConfig {\n" +
" cors: {\n" +
" allowOrigins: [\"http://www.m3.com\", \"http://www.hello.com\"],\n" +
" allowCredentials: false,\n" +
" allowHeaders: [\"CORELATION_ID\"],\n" +
" exposeHeaders: [\"X-CUSTOM-HEADER\"],\n" +
" maxAge: 84900\n" +
" }\n" +
"}"));
public function foo() {
// Multiple function testcases
Assert.assertFalse(inputValidator.isComplete("function name() {\n" +
" int x = 1;\n" +
"}\n" +
"\n" +
"function name1() {\n" +
" name();"));
Assert.assertFalse(inputValidator.isComplete("function name() {\n" +
" int x = 1\n" +
"}\n" +
"\n" +
"function name1() {\n" +
" name();"));

Assert.assertTrue(inputValidator.isComplete("function name() {\n" +
" int x = 1;\n" +
"}\n" +
"\n" +
"function name1() {\n" +
" name();" +
"}"));
}
Assert.assertTrue(inputValidator.isComplete("function name() {\n" +
" int x = 1\n" +
"}\n" +
"\n" +
"function name1() {\n" +
" name();" +
"}"));
public function foo2() {
Assert.assertTrue(inputValidator.isComplete("function name() {\n" +
" int x = 1\n" +
"}\n" +
"\n" +
"function name1() {\n" +
" name();" +
"}"));
}"""));
Assert.assertTrue(inputValidator.isComplete("""
import ballerina/io;
int x = 1;
public function foo() {
}
public function name() {
}"""));
Assert.assertTrue(inputValidator.isComplete("""
function foo() {
int x = 5;
if (x < 10) {
io:println(x);
}
while (x < 10) {
x = x + 1;
}
}"""));

// Test case related to annotation not attached to construct error message
Assert.assertTrue(inputValidator.isComplete("""
@http:ServiceConfig {
cors: {
allowOrigins: ["http://www.m3.com", "http://www.hello.com"],
allowCredentials: false,
allowHeaders: ["CORELATION_ID"],
exposeHeaders: ["X-CUSTOM-HEADER"],
maxAge: 84900
}
}"""));

// Multiple function testcases
Assert.assertFalse(inputValidator.isComplete("""
function name() {
int x = 1;
}
function name1() {
name();"""));
Assert.assertFalse(inputValidator.isComplete("""
function name() {
int x = 1
}
function name1() {
name();"""));

Assert.assertTrue(inputValidator.isComplete("""
function name() {
int x = 1;
}
function name1() {
name();}"""));

Assert.assertTrue(inputValidator.isComplete("""
function name() {
int x = 1
}
function name1() {
name();}"""));

Assert.assertTrue(inputValidator.isComplete("""
function name() {
int x = 1
}
function name1() {
name();}"""));

// Command related testcases
Assert.assertTrue(inputValidator.isComplete(" "));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,18 @@ public void testShortenedString() {

@Test
public void testHighlightDiagnostic() {
testHighlightDiagnostic("int i = 100", "" +
"error: missing semicolon token\n" +
"\tint i = 100\n" +
"\t ^");
testHighlightDiagnostic("int i\\-\\like\\-hyphens = 100", "" +
"error: invalid escape sequence '\\l'\n" +
"\tint i\\-\\like\\-hyphens = 100\n" +
"\t ^---------------^");
testHighlightDiagnostic("\\l", "" +
"error: invalid escape sequence '\\l'\n" +
"\t\\l\n" +
"\t^^");
testHighlightDiagnostic("int i = 100", """
error: missing semicolon token
\tint i = 100
\t ^""");
testHighlightDiagnostic("int i\\-\\like\\-hyphens = 100", """
error: invalid escape sequence '\\l'
\tint i\\-\\like\\-hyphens = 100
\t ^---------------^""");
testHighlightDiagnostic("\\l", """
error: invalid escape sequence '\\l'
\t\\l
\t^^""");
}

private void testHighlightDiagnostic(String code, String expectedError) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,15 @@ public class RuntimeConstants {
public static final BigDecimal BINT_MIN_VALUE_BIG_DECIMAL_RANGE_MIN = new BigDecimal("-9223372036854775808.5",
MathContext.DECIMAL128);
// runtime related error message constant values
public static final String INTERNAL_ERROR_MESSAGE =
"ballerina: Oh no, something really went wrong. Bad. Sad.\n" +
"\n" +
"We appreciate it if you can report the code that broke Ballerina in\n" +
"https://github.com/ballerina-platform/ballerina-lang/issues with the\n" +
"log you get below and your sample code.\n" +
"\n" +
"We thank you for helping make us better.\n";
public static final String INTERNAL_ERROR_MESSAGE = """
ballerina: Oh no, something really went wrong. Bad. Sad.
We appreciate it if you can report the code that broke Ballerina in
https://github.com/ballerina-platform/ballerina-lang/issues with the
log you get below and your sample code.
We thank you for helping make us better.
""";

public static final String DEFAULT_LOG_FILE_HANDLER_PATTERN =
"org.ballerinalang.logging.handlers.DefaultLogFileHandler.pattern";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -539,8 +539,12 @@ public void testTomlProviderWithString() {
VariableKey booleanArr = new VariableKey(ROOT_MODULE, "booleanArr", new BIntersectionType(ROOT_MODULE,
new BType[]{}, TypeCreator.createArrayType(PredefinedTypes.TYPE_BOOLEAN), 0, false), true);
configVarMap.put(ROOT_MODULE, new VariableKey[]{intVar, stringVar, stringArr, booleanArr});
String tomlContent = "[rootOrg.test_module]\nintVar = 33\nstringVar = \"xyz\"\n" +
"stringArr = [\"aa\", \"bb\", \"cc\"]\nbooleanArr = [false, true, true, false]";
String tomlContent = """
[rootOrg.test_module]
intVar = 33
stringVar = "xyz"
stringArr = ["aa", "bb", "cc"]
booleanArr = [false, true, true, false]""";
ConfigResolver configResolver = new ConfigResolver(configVarMap, new RuntimeDiagnosticLog(),
List.of(new TomlContentProvider(ROOT_MODULE, tomlContent, configVarMap.keySet())));
Map<VariableKey, ConfigValue> configValueMap = configResolver.resolveConfigs();
Expand Down
Loading

0 comments on commit ad9811e

Please sign in to comment.