Skip to content

Commit

Permalink
Add tests for files in DSL
Browse files Browse the repository at this point in the history
  • Loading branch information
niknetniko committed Aug 13, 2024
1 parent 7ec058f commit 43a00e5
Show file tree
Hide file tree
Showing 24 changed files with 91 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hallo
7 changes: 7 additions & 0 deletions tests/exercises/echo-function-file-output/evaluation/one.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
- tab: "Test"
testcases:
- statement: echo_function("result.txt", "Hallo")
file:
content: "contents.txt"
location: "result.txt"
oracle: builtin
7 changes: 7 additions & 0 deletions tests/exercises/echo-function-file-output/solution/correct.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <stdio.h>

void echo_function(const char *filename, const char *string) {
FILE *file = fopen(filename, "w");
fprintf(file, "%s\n", string);
fclose(file);
}
12 changes: 12 additions & 0 deletions tests/exercises/echo-function-file-output/solution/correct.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.IO;

class Submission
{
public static void EchoFunction(string filename, string stringToWrite)
{
using (StreamWriter writer = new StreamWriter(filename))
{
writer.WriteLine(stringToWrite);
}
}
}
7 changes: 7 additions & 0 deletions tests/exercises/echo-function-file-output/solution/correct.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import System.IO (writeFile)

echoFunction :: FilePath -> String -> IO ()
echoFunction filename content = do
let contentWithNewline = content ++ "\n"
writeFile filename contentWithNewline
return ()
15 changes: 15 additions & 0 deletions tests/exercises/echo-function-file-output/solution/correct.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import java.io.FileWriter;
import java.io.IOException;

public class Submission {

public static void echoFunction(String filename, String stringToWrite) {
try (FileWriter writer = new FileWriter(filename)) {
writer.write(stringToWrite);
writer.write(System.lineSeparator()); // Add a newline
} catch (IOException e) {
System.err.println("Error writing to file: " + e.getMessage());
}
}
}

5 changes: 5 additions & 0 deletions tests/exercises/echo-function-file-output/solution/correct.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const fs = require('fs');

function echoFunction(filename, stringToWrite) {
fs.writeFileSync(filename, stringToWrite + '\n', { flag: 'w' });
}
5 changes: 5 additions & 0 deletions tests/exercises/echo-function-file-output/solution/correct.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import java.io.File

fun echoFunction(filename: String, stringToWrite: String) {
File(filename).writeText(stringToWrite + "\n")
}
3 changes: 3 additions & 0 deletions tests/exercises/echo-function-file-output/solution/correct.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def echo_function(filename, string_to_write):
with open(filename, 'w') as file:
file.write(string_to_write + '\n') # Write the string and a newline)
4 changes: 4 additions & 0 deletions tests/exercises/echo-function-file-output/solution/correct.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
echo_function() {
echo "$2" > "$1"
}

Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@
- testcases:
- expression: 'echo("input-1")'
return: !object {}

26 changes: 24 additions & 2 deletions tests/test_io_exercises.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,16 @@ def test_io_function_exercise(


@pytest.mark.parametrize("language", ALL_LANGUAGES)
def test_io_function_file_exercise(
def test_io_function_file_input_exercise(
language: str, tmp_path: Path, pytestconfig: pytest.Config
):
conf = configuration(
pytestconfig, "echo-function-file", language, tmp_path, "one.tson", "correct"
pytestconfig,
"echo-function-file-input",
language,
tmp_path,
"one.tson",
"correct",
)
shutil.copytree(
Path(conf.resources).parent / "workdir", tmp_path, dirs_exist_ok=True
Expand All @@ -58,6 +63,23 @@ def test_io_function_file_exercise(
assert updates.find_status_enum() == ["correct"]


@pytest.mark.parametrize("language", ALL_LANGUAGES)
def test_io_function_file_output_exercise(
language: str, tmp_path: Path, pytestconfig: pytest.Config
):
conf = configuration(
pytestconfig,
"echo-function-file-output",
language,
tmp_path,
"one.yaml",
"correct",
)
result = execute_config(conf)
updates = assert_valid_output(result, pytestconfig)
assert updates.find_status_enum() == ["correct"]


@pytest.mark.parametrize("language", ALL_LANGUAGES)
def test_io_function_additional_source_files(
language: str, tmp_path: Path, pytestconfig: pytest.Config
Expand Down
2 changes: 1 addition & 1 deletion tests/test_language_quircks.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def test_javascript_exception_missing_message(
assert updates.find_status_enum() == ["wrong"]


@pytest.mark.parametrize("exercise", ["echo-function-file", "echo-function"])
@pytest.mark.parametrize("exercise", ["echo-function-file-input", "echo-function"])
def test_javascript_async(exercise: str, tmp_path: Path, pytestconfig: pytest.Config):
conf = configuration(
pytestconfig, exercise, "javascript", tmp_path, "one.tson", "correct-async"
Expand Down

0 comments on commit 43a00e5

Please sign in to comment.