Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error message for unit conversions involving unit "1" #462

Merged
merged 2 commits into from
Mar 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()