Moodle

Refactoring of regular expressions and unit test best practice

Details

  • Type: Improvement Improvement
  • Status: Open Open
  • Priority: Minor Minor
  • Resolution: Unresolved
  • Affects Version/s: 1.9.5
  • Fix Version/s: 2.0.8
  • Component/s: Unit tests
  • Labels:
    None
  • Affected Branches:
    MOODLE_19_STABLE
  • Fixed Branches:
    MOODLE_20_STABLE

Description

Regular expressions are very powerful, but most of them in Moodle are untested. Ideally we should unit-test every regular expression with a wide range of inputs.

This is difficult for one main reason: often the regular expressions are not accessible to unit tests, they are deeply embedded in the logic and have strong coupling.

One solution is to extract all static regular expressions into constants, and dynamic expressions into functions. Unit tests can then be written for them easily.

Activity

Hide
Nicolas Connault added a comment -

I'm changing the requirement of extracting "ALL" regular expressions. I think that only those that are difficult to read need to be unit-tested.

Show
Nicolas Connault added a comment - I'm changing the requirement of extracting "ALL" regular expressions. I think that only those that are difficult to read need to be unit-tested.
Hide
Tim Hunt added a comment -

It depends.

I don't think a regular-expression is normally the right level of granularity to test. That is, unit test should test the external API of a unit of code, not the implementation details, and a regex is normally an implementation detail.

So, as a simple example, we should not unit test '/[^A-Za-z0-9]/i', we should unit test clean_param(..., PARAM_ALPHANUM);

Of course, if a regex is perfoming an important task all on its own, and that is currently buried in the middle of a big function, then it might be good to factor out the task done by the regex into a new function.

Show
Tim Hunt added a comment - It depends. I don't think a regular-expression is normally the right level of granularity to test. That is, unit test should test the external API of a unit of code, not the implementation details, and a regex is normally an implementation detail. So, as a simple example, we should not unit test '/[^A-Za-z0-9]/i', we should unit test clean_param(..., PARAM_ALPHANUM); Of course, if a regex is perfoming an important task all on its own, and that is currently buried in the middle of a big function, then it might be good to factor out the task done by the regex into a new function.

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated: