Skip to content

Commit

Permalink
Support fine grained python error comparison (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
JosephTLyons authored Nov 12, 2024
1 parent f5981fd commit a32c9b1
Show file tree
Hide file tree
Showing 12 changed files with 973 additions and 844 deletions.
692 changes: 363 additions & 329 deletions test/data/float/invalid_float_data.gleam

Large diffs are not rendered by default.

1,012 changes: 532 additions & 480 deletions test/data/integer/invalid_integer_data.gleam

Large diffs are not rendered by default.

22 changes: 18 additions & 4 deletions test/helpers.gleam
Original file line number Diff line number Diff line change
@@ -1,22 +1,36 @@
import gleam/string

pub fn to_printable_text(text: String) -> String {
do_to_printable_text(text |> string.to_graphemes, "")
pub fn to_printable_text(text: String, python_output: Bool) -> String {
do_to_printable_text(
characters: text |> string.to_graphemes,
python_output: python_output,
acc: "",
)
}

fn do_to_printable_text(characters: List(String), acc: String) -> String {
fn do_to_printable_text(
characters characters: List(String),
python_output python_output: Bool,
acc acc: String,
) -> String {
case characters {
[] -> acc
[first, ..rest] -> {
let printable = case first {
"\t" -> "\\t"
"\n" -> "\\n"
"\r" -> "\\r"
// Python weirdly converts "\f" to "\x0c"
"\f" if python_output -> "\\x0c"
"\f" -> "\\f"
"\r\n" -> "\\r\\n"
_ -> first
}
do_to_printable_text(rest, acc <> printable)
do_to_printable_text(
characters: rest,
python_output: python_output,
acc: acc <> printable,
)
}
}
}
22 changes: 21 additions & 1 deletion test/helpers_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,27 @@ pub fn to_printable_text_tests() {
|> list.map(fn(tuple) {
let #(input, output) = tuple
use <- it("\"" <> output <> "\"")
input |> to_printable_text |> expect.to_equal(output)
input |> to_printable_text(False) |> expect.to_equal(output)
}),
)
}

pub fn to_printable_text_python_tests() {
describe(
"should_be_printable_python_text",
[
#("\t", "\\t"),
#("\n", "\\n"),
#("\r", "\\r"),
#("\f", "\\x0c"),
#("\r\n", "\\r\\n"),
#("\t\nabc123\r", "\\t\\nabc123\\r"),
#("abc123\r\n", "abc123\\r\\n"),
]
|> list.map(fn(tuple) {
let #(input, output) = tuple
use <- it("\"" <> output <> "\"")
input |> to_printable_text(True) |> expect.to_equal(output)
}),
)
}
8 changes: 4 additions & 4 deletions test/python/parse_floats.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
output_values = []

for item in data_list:
text = item["input"]
input = item["input"]

try:
value = float(text)
except ValueError:
value = "ValueError"
value = float(input)
except ValueError as e:
value = f"ValueError: {e}"

output_values.append(str(value))

Expand Down
8 changes: 4 additions & 4 deletions test/python/parse_ints.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
output_values = []

for item in data_list:
text = item["input"]
input = item["input"]
base = int(item["base"])

try:
value = int(text, base=base)
except ValueError:
value = "ValueError"
value = int(input, base=base)
except ValueError as e:
value = f"ValueError: {e}"

output_values.append(str(value))

Expand Down
3 changes: 3 additions & 0 deletions test/python/python_error.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub type PythonError {
ValueError(message: String)
}
9 changes: 5 additions & 4 deletions test/python/python_parse.gleam
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import gleam/dynamic
import gleam/json
import gleam/list
import python/python_error.{type PythonError, ValueError}
import shellout
import test_data.{type FloatTestData, type IntegerTestData}

pub fn to_floats(
float_test_data float_test_data: List(FloatTestData),
) -> List(Result(String, Nil)) {
) -> List(Result(String, PythonError)) {
float_test_data
|> json.array(fn(float_data) {
json.object([#("input", json.string(float_data.input))])
Expand All @@ -17,7 +18,7 @@ pub fn to_floats(

pub fn to_ints(
integer_test_data integer_test_data: List(IntegerTestData),
) -> List(Result(String, Nil)) {
) -> List(Result(String, PythonError)) {
integer_test_data
|> json.array(fn(integer_data) {
json.object([
Expand All @@ -32,7 +33,7 @@ pub fn to_ints(
fn parse(
input_json_string input_json_string: String,
program_name program_name: String,
) -> List(Result(String, Nil)) {
) -> List(Result(String, PythonError)) {
let arguments = [
"run",
"-p",
Expand All @@ -51,7 +52,7 @@ fn parse(
parsed_strings
|> list.map(fn(value) {
case value {
"ValueError" -> Error(Nil)
"ValueError: " <> error_message -> Error(ValueError(error_message))
_ -> Ok(value)
}
})
Expand Down
28 changes: 16 additions & 12 deletions test/python_parse_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub fn check_against_python_tests() {
let expected_python_output = { data.0 }.expected_python_output
let actual_python_output = data.1

let input_printable_text = input |> helpers.to_printable_text
let input_printable_text = input |> helpers.to_printable_text(False)

let message = case expected_program_output, expected_python_output {
Ok(_), Ok(python_output) -> {
Expand All @@ -28,16 +28,18 @@ pub fn check_against_python_tests() {
<> python_output
<> "\""
}
Error(_), Error(_) -> {
Error(_), Error(python_error) -> {
"should_not_parse: \""
<> input_printable_text
<> "\" -> \"Error\""
<> "\" -> \""
<> python_error.message
<> "\""
}
Ok(output), Error(_) -> {
Ok(output), Error(python_error) -> {
panic as form_panic_message(
input_printable_text,
output |> float.to_string,
"Error",
python_error.message,
)
}
Error(output), Ok(python_output) -> {
Expand Down Expand Up @@ -65,11 +67,11 @@ pub fn check_against_python_tests() {
let expected_python_output = { data.0 }.expected_python_output
let actual_python_output = data.1

let input_printable_text = input |> helpers.to_printable_text
let input_printable_text = input |> helpers.to_printable_text(False)

let base_text = case base {
10 -> ""
_ -> "(base: " <> base |> int.to_string <> ")"
_ -> "(base: " <> base |> int.to_string <> ") "
}

let message = case expected_program_output, expected_python_output {
Expand All @@ -78,22 +80,24 @@ pub fn check_against_python_tests() {
<> input_printable_text
<> "\" "
<> base_text
<> " -> \""
<> "-> \""
<> python_output
<> "\""
}
Error(_), Error(_) -> {
Error(_), Error(python_error) -> {
"should_not_parse: \""
<> input_printable_text
<> "\" "
<> base_text
<> " -> \"Error\""
<> "-> \""
<> python_error.message
<> "\""
}
Ok(output), Error(_) -> {
Ok(output), Error(python_error) -> {
panic as form_panic_message(
input_printable_text,
output |> int.to_string,
"Error",
python_error.message,
)
}
Error(output), Ok(python_output) -> {
Expand Down
5 changes: 3 additions & 2 deletions test/test_data.gleam
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import parse_error.{type ParseError}
import python/python_error.{type PythonError}

pub type FloatTestData {
FloatTestData(
input: String,
expected_program_output: Result(Float, ParseError),
expected_python_output: Result(String, Nil),
expected_python_output: Result(String, PythonError),
)
}

Expand All @@ -13,6 +14,6 @@ pub type IntegerTestData {
input: String,
base: Int,
expected_program_output: Result(Int, ParseError),
expected_python_output: Result(String, Nil),
expected_python_output: Result(String, PythonError),
)
}
2 changes: 1 addition & 1 deletion test/to_float_parse_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub fn to_float_tests() {
data.float_test_data()
|> list.map(fn(data) {
let input = data.input
let input_printable_text = input |> helpers.to_printable_text
let input_printable_text = input |> helpers.to_printable_text(False)
let expected_program_output = data.expected_program_output

let message = case expected_program_output {
Expand Down
6 changes: 3 additions & 3 deletions test/to_int_parse_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ pub fn to_int_tests() {
data.integer_test_data()
|> list.map(fn(data) {
let input = data.input
let input_printable_text = input |> helpers.to_printable_text
let input_printable_text = input |> helpers.to_printable_text(False)
let expected_program_output = data.expected_program_output
let base = data.base

let base_text = case base {
10 -> ""
_ -> "(base: " <> base |> int.to_string <> ")"
_ -> "(base: " <> base |> int.to_string <> ") "
}

let message = case expected_program_output {
Expand All @@ -28,7 +28,7 @@ pub fn to_int_tests() {
<> input_printable_text
<> "\" "
<> base_text
<> " -> "
<> "-> "
<> output |> int.to_string
}
Error(error) -> {
Expand Down

0 comments on commit a32c9b1

Please sign in to comment.