Skip to content

Commit

Permalink
Fixed the problem name bug (fitbenchmarking#1355)
Browse files Browse the repository at this point in the history
  • Loading branch information
RabiyaF authored Nov 5, 2024
1 parent cbbfa71 commit 2527cad
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
6 changes: 5 additions & 1 deletion fitbenchmarking/parsing/fitbenchmark_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,11 @@ def _get_data_problem_entries(self):
continue

lhs, rhs = line.split("=", 1)
entries[lhs.strip()] = rhs.strip().strip('"').strip("'")
key = lhs.strip()
value = rhs.strip().strip('"').strip("'")
if key == 'name':
value = re.sub(r"[\\/]", "", value)
entries[key] = value

return entries

Expand Down
36 changes: 36 additions & 0 deletions fitbenchmarking/parsing/tests/test_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@
from inspect import getmembers, isabstract, isclass
from json import load
from unittest import TestCase
from unittest.mock import MagicMock, patch

import numpy as np
from parameterized import parameterized
from pytest import test_type as TEST_TYPE # pylint: disable=no-name-in-module
from scipy.sparse import issparse

from conftest import run_for_test_types
from fitbenchmarking.parsing.base_parser import Parser
from fitbenchmarking.parsing.fitbenchmark_parser import FitbenchmarkParser
from fitbenchmarking.parsing.fitting_problem import FittingProblem
from fitbenchmarking.parsing.parser_factory import (ParserFactory,
parse_problem_file)
Expand Down Expand Up @@ -556,3 +559,36 @@ def test_mantid_jac_when_no_func_by_user(self):

actual = fitting_problem.jacobian(x, r[1])
assert np.isclose(actual, r[2]).all()


class TestFitbenchmarkParser(TestCase):
"""
Tests the FitbenchmarkParser.
"""

@parameterized.expand([
(["name = '\\sample_problem'"],
{"name": "sample_problem"}),
(["name = 'sample_problem/'"],
{"name": "sample_problem"}),
(["name = '\\sample_problem/////'"],
{"name": "sample_problem"}),
(["#fitbenchmarking"], {}),
(["name = 'sample_problem' # my important comment"],
{"name": "sample_problem"}),
(["function = 'Userdefined: a/4 ** 6'"],
{"function": "Userdefined: a/4 ** 6"}),
(["function = 'Userdefined = a/4 ** 6'"],
{"function": "Userdefined = a/4 ** 6"}),
])
@patch.object(FitbenchmarkParser, "__init__", lambda a, b, c: None)
def test_get_data_problem_entries(self, file_data, expected):
"""
Verifies the output of _get_data_problem_entries() method.
"""
parser = FitbenchmarkParser('test_file.txt', {"parse"})
parser.file = MagicMock()
parser.file.readlines.return_value = file_data
# pylint: disable=protected-access
result = parser._get_data_problem_entries()
assert result == expected

0 comments on commit 2527cad

Please sign in to comment.