Skip to content

Commit

Permalink
Fix error message for unit conversions involving unit "1" (#462)
Browse files Browse the repository at this point in the history
* Fix error message for unit conversions involving unit "1"

* Add unit tests for function string_to_python_identifier()
  • Loading branch information
mkavulich authored Mar 10, 2023
1 parent 1b6352f commit 60295ad
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
3 changes: 3 additions & 0 deletions scripts/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ def string_to_python_identifier(string):
string = string.replace("-","_minus_")
# Replace plus signs with _plus_
string = string.replace("+","_plus_")
# "1" is a valid unit
if string == "1":
string = "one"
# Test that the resulting string is a valid Python identifier
if re.match("[_A-Za-z][_a-zA-Z0-9]*$", string) and not keyword.iskeyword(string):
return string
Expand Down
52 changes: 52 additions & 0 deletions test/unit_tests/test_common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#! /usr/bin/env python3
"""
-----------------------------------------------------------------------
Description: Contains unit tests for functions in common.py
Assumptions:
Command line arguments: none
Usage: python test_common.py # run the unit tests
-----------------------------------------------------------------------
"""
import sys
import os
import logging
import unittest

TEST_DIR = os.path.dirname(os.path.abspath(__file__))
SCRIPTS_DIR = os.path.abspath(os.path.join(TEST_DIR, os.pardir, os.pardir, "scripts"))

if not os.path.exists(SCRIPTS_DIR):
raise ImportError("Cannot find scripts directory")

sys.path.append(SCRIPTS_DIR)

# pylint: disable=wrong-import-position
import common
# pylint: enable=wrong-import-position

class CommonTestCase(unittest.TestCase):

"""Tests functionality of functions in common.py"""

def test_string_to_python_identifier(self):
"""Test string_to_python_identifier() function"""

# Test various successful combinations
self.assertEqual(common.string_to_python_identifier("Test 1"),"Test_1")
self.assertEqual(common.string_to_python_identifier("Test.2"),"Test_p_2")
self.assertEqual(common.string_to_python_identifier("Test-3"),"Test_minus_3")
self.assertEqual(common.string_to_python_identifier("Test+4"),"Test_plus_4")
self.assertEqual(common.string_to_python_identifier("1"),"one")
self.assertEqual(common.string_to_python_identifier(" Test all--even +."),
"_Test_all_minus__minus_even__plus__p_")
# Test expected failures
self.assertRaises(Exception,common.string_to_python_identifier,"else")
self.assertRaises(Exception,common.string_to_python_identifier,"1 ")
self.assertRaises(Exception,common.string_to_python_identifier,"0")
self.assertRaises(Exception,common.string_to_python_identifier,"Disallowed character!")

if __name__ == '__main__':
unittest.main()

0 comments on commit 60295ad

Please sign in to comment.