Skip to content

Commit

Permalink
Add optional lineSeparator parameter to readFile native function (fin…
Browse files Browse the repository at this point in the history
…os#799)

* Add optional lineSeparator parameter to readFile native function

* Clean up some tests
  • Loading branch information
kevin-m-knight-gs authored Apr 24, 2024
1 parent 33171fd commit 5f9b21a
Show file tree
Hide file tree
Showing 15 changed files with 305 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,23 @@

package org.finos.legend.pure.m3.tests.elements._class;

import org.eclipse.collections.api.factory.Lists;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.api.tuple.Pair;
import org.eclipse.collections.impl.factory.Lists;
import org.eclipse.collections.impl.tuple.Tuples;
import org.finos.legend.pure.m3.exception.PureExecutionException;
import org.finos.legend.pure.m3.serialization.filesystem.usercodestorage.composite.CompositeCodeStorage;
import org.finos.legend.pure.m3.serialization.filesystem.repository.CodeRepository;
import org.finos.legend.pure.m3.serialization.filesystem.repository.GenericCodeRepository;
import org.finos.legend.pure.m3.serialization.filesystem.usercodestorage.MutableRepositoryCodeStorage;
import org.finos.legend.pure.m3.serialization.filesystem.usercodestorage.classpath.ClassLoaderCodeStorage;
import org.finos.legend.pure.m3.serialization.filesystem.usercodestorage.composite.CompositeCodeStorage;
import org.finos.legend.pure.m3.tests.AbstractPureTestWithCoreCompiled;
import org.finos.legend.pure.m4.exception.PureCompilationException;
import org.finos.legend.pure.m4.serialization.grammar.antlr.PureParserException;
import org.junit.After;
import org.junit.Assert;
import org.junit.Test;

import java.nio.file.Path;

public abstract class AbstractTestConstraints extends AbstractPureTestWithCoreCompiled
{
private static final String EMPLOYEE_SOURCE_NAME = "employee.pure";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@

package org.finos.legend.pure.m3.tests.function.base.io;

import com.sun.net.httpserver.*;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpServer;
import org.finos.legend.pure.m3.tests.AbstractPureTestWithCoreCompiled;
import org.junit.*;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.IOException;
import java.net.InetSocketAddress;
Expand All @@ -34,7 +38,7 @@ public static void beforeClass() throws Exception
}

@AfterClass
public static void stopHttp() throws Exception
public static void stopHttp()
{
httpServer.stop(0);
}
Expand All @@ -49,6 +53,7 @@ private static void handle(HttpExchange exchange) throws IOException
public void cleanRuntime()
{
runtime.delete("testHttp.pure");
runtime.compile();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,19 @@
package org.finos.legend.pure.m3.tests.function.base.io;

import org.finos.legend.pure.m3.tests.AbstractPureTestWithCoreCompiled;
import org.junit.After;
import org.junit.Assert;
import org.junit.Test;

public abstract class AbstractTestPrint extends AbstractPureTestWithCoreCompiled
{
@After
public void cleanRuntime()
{
runtime.delete("fromString.pure");
runtime.compile();
}

@Test
public void testPrint()
{
Expand All @@ -28,7 +36,7 @@ public void testPrint()
" print('Hello World', 1);\n" +
"}\n");
this.execute("testPrint():Nil[0]");
Assert.assertEquals("'Hello World'", this.functionExecution.getConsole().getLine(0));
Assert.assertEquals("'Hello World'", functionExecution.getConsole().getLine(0));
}

@Test
Expand All @@ -39,7 +47,7 @@ public void testArrowWithAFunctionWithNoParameters()
" 'a'->print(1);\n" +
"}\n");
this.execute("testArrowWithFunctionNoParameters():Nil[0]");
Assert.assertEquals("'a'", this.functionExecution.getConsole().getLine(0));
Assert.assertEquals("'a'", functionExecution.getConsole().getLine(0));
}

@Test
Expand All @@ -50,7 +58,7 @@ public void testPrintInteger()
" print(123, 1);\n" +
"}\n");
this.execute("testPrintInteger():Nil[0]");
Assert.assertEquals("123", this.functionExecution.getConsole().getLine(0));
Assert.assertEquals("123", functionExecution.getConsole().getLine(0));
}

@Test
Expand All @@ -61,7 +69,7 @@ public void testPrintFloat()
" print(123.456, 1);\n" +
"}\n");
this.execute("testPrintFloat():Nil[0]");
Assert.assertEquals("123.456", this.functionExecution.getConsole().getLine(0));
Assert.assertEquals("123.456", functionExecution.getConsole().getLine(0));
}

@Test
Expand All @@ -72,7 +80,7 @@ public void testPrintDate()
" print(%2016-07-08, 1);\n" +
"}\n");
this.execute("testPrintDate():Nil[0]");
Assert.assertEquals("2016-07-08", this.functionExecution.getConsole().getLine(0));
Assert.assertEquals("2016-07-08", functionExecution.getConsole().getLine(0));
}

@Test
Expand All @@ -83,7 +91,7 @@ public void testPrintBoolean()
" print(true, 1);\n" +
"}\n");
this.execute("testPrintBoolean():Nil[0]");
Assert.assertEquals("true", this.functionExecution.getConsole().getLine(0));
Assert.assertEquals("true", functionExecution.getConsole().getLine(0));
}

@Test
Expand All @@ -98,7 +106,7 @@ public void testPrintIntegerCollection()
" 1\n" +
" 2\n" +
" 3\n" +
"]", this.functionExecution.getConsole().getLine(0));
"]", functionExecution.getConsole().getLine(0));
}

@Test
Expand All @@ -113,7 +121,7 @@ public void testPrintFloatCollection()
" 1.0\n" +
" 2.5\n" +
" 3.0\n" +
"]", this.functionExecution.getConsole().getLine(0));
"]", functionExecution.getConsole().getLine(0));
}

@Test
Expand All @@ -127,38 +135,38 @@ public void testPrintDateCollection()
Assert.assertEquals("[\n" +
" 1973-11-13T23:09:11+0000\n" +
" 2016-07-08\n" +
"]", this.functionExecution.getConsole().getLine(0));
"]", functionExecution.getConsole().getLine(0));
}

@Test
public void testPrintStringCollection()
{
compileTestSource("fromString.pure","function testPrintStringCollection():Nil[0]\n" +
"{\n" +
" print([\'testString\', \'2.5\', \'%2016-07-08\'], 1);\n" +
" print(['testString', '2.5', '%2016-07-08'], 1);\n" +
"}\n");
this.execute("testPrintStringCollection():Nil[0]");
Assert.assertEquals("[\n" +
" \'testString\'\n" +
" \'2.5\'\n" +
" \'%2016-07-08\'\n" +
"]", this.functionExecution.getConsole().getLine(0));
" 'testString'\n" +
" '2.5'\n" +
" '%2016-07-08'\n" +
"]", functionExecution.getConsole().getLine(0));
}

@Test
public void testPrintMixedCollection()
{
compileTestSource("fromString.pure","function testPrintMixedCollection():Nil[0]\n" +
"{\n" +
" print([1, 2.5, \'testString\', %2016-07-08], 1);\n" +
" print([1, 2.5, 'testString', %2016-07-08], 1);\n" +
"}\n");
this.execute("testPrintMixedCollection():Nil[0]");
Assert.assertEquals("[\n" +
" 1\n" +
" 2.5\n" +
" 'testString'\n" +
" 2016-07-08\n" +
"]", this.functionExecution.getConsole().getLine(0));
"]", functionExecution.getConsole().getLine(0));
}

@Test
Expand Down Expand Up @@ -277,7 +285,7 @@ public void testFunction()
" values(Property):\n" +
" [~>] tst__String_1_ instance ConcreteFunctionDefinition\n" +
" propertyName(Property):\n" +
" values instance String", this.functionExecution.getConsole().getLine(0));
" values instance String", functionExecution.getConsole().getLine(0));
}

@Test
Expand Down Expand Up @@ -309,6 +317,6 @@ public void testPrintObj()
" referenceUsages(Property):\n" +
" [>0] Anonymous_StripedId instance ReferenceUsage\n" +
" [>0] Anonymous_StripedId instance ReferenceUsage\n" +
" [>0] Anonymous_StripedId instance ReferenceUsage", this.functionExecution.getConsole().getLine(0));
" [>0] Anonymous_StripedId instance ReferenceUsage", functionExecution.getConsole().getLine(0));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
// Copyright 2024 Goldman Sachs
//
// Licensed 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.

package org.finos.legend.pure.m3.tests.function.base.io;

import org.eclipse.collections.api.factory.Lists;
import org.eclipse.collections.api.list.MutableList;
import org.finos.legend.pure.m3.navigation.M3Properties;
import org.finos.legend.pure.m3.navigation.PrimitiveUtilities;
import org.finos.legend.pure.m3.serialization.filesystem.repository.CodeRepository;
import org.finos.legend.pure.m3.serialization.filesystem.repository.GenericCodeRepository;
import org.finos.legend.pure.m3.serialization.filesystem.usercodestorage.MutableRepositoryCodeStorage;
import org.finos.legend.pure.m3.serialization.filesystem.usercodestorage.classpath.ClassLoaderCodeStorage;
import org.finos.legend.pure.m3.serialization.filesystem.usercodestorage.composite.CompositeCodeStorage;
import org.finos.legend.pure.m3.tests.AbstractPureTestWithCoreCompiled;
import org.finos.legend.pure.m4.coreinstance.CoreInstance;
import org.finos.legend.pure.m4.serialization.grammar.StringEscape;
import org.junit.After;
import org.junit.Assert;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UncheckedIOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;

public abstract class AbstractTestReadFile extends AbstractPureTestWithCoreCompiled
{
private static final String TEST_FILE_NAME = "/read_file_test/test.pure";
private static final String TEST_TEXT_FILE_NAME = "/read_file_test/io/readFileTestText.txt";

@After
public void cleanRuntime()
{
runtime.delete(TEST_FILE_NAME);
runtime.compile();
}

@Test
public void testReadFileNoLineSeparator()
{
compileTestSource(
TEST_FILE_NAME,
"function readTestFile():String[0..1]\n" +
"{\n" +
" readFile('" + StringEscape.escape(TEST_TEXT_FILE_NAME) + "')\n" +
"}\n"
);
String expected = getTestText();
String actual = executeAndGetStringResult("readTestFile():String[0..1]");
Assert.assertEquals(expected, actual);
}

@Test
public void testReadFileNewLine()
{
testReadFileWithLineSeparator("\n");
}

@Test
public void testReadFileCarriageReturn()
{
testReadFileWithLineSeparator("\r");
}

@Test
public void testReadFileNewLineCarriageReturn()
{
testReadFileWithLineSeparator("\n\r");
}

@Test
public void testReadFileCarriageReturnNewLine()
{
testReadFileWithLineSeparator("\r\n");
}

@Test
public void testReadFileEmptyString()
{
testReadFileWithLineSeparator("");
}

private void testReadFileWithLineSeparator(String lineSeparator)
{
compileTestSource(
TEST_FILE_NAME,
"function readTestFile():String[0..1]\n" +
"{\n" +
" readFile('" + StringEscape.escape(TEST_TEXT_FILE_NAME) + "', '" + StringEscape.escape(lineSeparator) + "')\n" +
"}\n"
);
String expected = getTestText().replaceAll("\\R", lineSeparator);
String actual = executeAndGetStringResult("readTestFile():String[0..1]");
Assert.assertEquals(expected, actual);
}

private String executeAndGetStringResult(String functionDesc)
{
CoreInstance func = runtime.getFunction(functionDesc);
Assert.assertNotNull(functionDesc, func);
CoreInstance result = functionExecution.start(func, Lists.immutable.empty());
return PrimitiveUtilities.getStringValue(result.getValueForMetaPropertyToOne(M3Properties.values));
}

private String getTestText()
{
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
String resourceName = TEST_TEXT_FILE_NAME.substring(1);
URL url = classLoader.getResource(resourceName);
if (url == null)
{
throw new RuntimeException("Could not find resource: " + resourceName);
}
try (Reader reader = new InputStreamReader(url.openStream(), StandardCharsets.UTF_8))
{
StringBuilder builder = new StringBuilder();
char[] buffer = new char[1024];
int read;
while ((read = reader.read(buffer)) != -1)
{
builder.append(buffer, 0, read);
}
return builder.toString();
}
catch (IOException e)
{
throw new UncheckedIOException(e);
}
}

protected static MutableRepositoryCodeStorage getCodeStorage()
{
MutableList<CodeRepository> repositories = Lists.mutable.withAll(AbstractPureTestWithCoreCompiled.getCodeRepositories());
repositories.add(new GenericCodeRepository("read_file_test", null, "platform", "platform_functions"));
return new CompositeCodeStorage(new ClassLoaderCodeStorage(repositories));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2024 Goldman Sachs
//
// Licensed 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.

This is a test
This is only a test
Do not panic
Loading

0 comments on commit 5f9b21a

Please sign in to comment.