|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +Unit tests from mathics.builtin.string. |
| 4 | +""" |
| 5 | + |
| 6 | +from test.helper import check_evaluation |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | + |
| 11 | +@pytest.mark.parametrize( |
| 12 | + ("str_expr", "msgs", "str_expected", "fail_msg"), |
| 13 | + [ |
| 14 | + ('ToCharacterCode[{"ab"}]', None, "{{97, 98}}", None), |
| 15 | + (r'ToCharacterCode[{"\(A\)"}]', None, "{{63433, 65, 63424}}", None), |
| 16 | + ( |
| 17 | + 'ToCharacterCode[{{"ab"}}]', |
| 18 | + ( |
| 19 | + "String or list of strings expected at position 1 in ToCharacterCode[{{ab}}].", |
| 20 | + ), |
| 21 | + "ToCharacterCode[{{ab}}]", |
| 22 | + None, |
| 23 | + ), |
| 24 | + ( |
| 25 | + "ToCharacterCode[x]", |
| 26 | + ( |
| 27 | + "String or list of strings expected at position 1 in ToCharacterCode[x].", |
| 28 | + ), |
| 29 | + "ToCharacterCode[x]", |
| 30 | + None, |
| 31 | + ), |
| 32 | + ('ToCharacterCode[""]', None, "{}", None), |
| 33 | + ( |
| 34 | + "#1 == ToCharacterCode[FromCharacterCode[#1]] & [RandomInteger[{0, 65535}, 100]]", |
| 35 | + None, |
| 36 | + "True", |
| 37 | + None, |
| 38 | + ), |
| 39 | + ( |
| 40 | + r"ToCharacterCode[]", |
| 41 | + ["ToCharacterCode called with 0 arguments; 1 or 2 arguments are expected."], |
| 42 | + "ToCharacterCode[]", |
| 43 | + "ToCharacterCode argument checking", |
| 44 | + ), |
| 45 | + ], |
| 46 | +) |
| 47 | +def test_to_character_code(str_expr, msgs, str_expected, fail_msg): |
| 48 | + """ """ |
| 49 | + check_evaluation( |
| 50 | + str_expr, |
| 51 | + str_expected, |
| 52 | + to_string_expr=True, |
| 53 | + to_string_expected=True, |
| 54 | + hold_expected=True, |
| 55 | + failure_message=fail_msg, |
| 56 | + expected_messages=msgs, |
| 57 | + ) |
| 58 | + |
| 59 | + |
| 60 | +@pytest.mark.parametrize( |
| 61 | + ("str_expr", "msgs", "str_expected", "fail_msg"), |
| 62 | + [ |
| 63 | + ("FromCharacterCode[{}] // InputForm", None, '""', None), |
| 64 | + ( |
| 65 | + "FromCharacterCode[65536]", |
| 66 | + ( |
| 67 | + "A character code, which should be a non-negative integer less than 65536, is expected at position 1 in {65536}.", |
| 68 | + ), |
| 69 | + "FromCharacterCode[65536]", |
| 70 | + None, |
| 71 | + ), |
| 72 | + ( |
| 73 | + "FromCharacterCode[-1]", |
| 74 | + ( |
| 75 | + "Non-negative machine-sized integer expected at position 1 in FromCharacterCode[-1].", |
| 76 | + ), |
| 77 | + "FromCharacterCode[-1]", |
| 78 | + None, |
| 79 | + ), |
| 80 | + ( |
| 81 | + "FromCharacterCode[444444444444444444444444444444444444]", |
| 82 | + ( |
| 83 | + "Non-negative machine-sized integer expected at position 1 in FromCharacterCode[444444444444444444444444444444444444].", |
| 84 | + ), |
| 85 | + "FromCharacterCode[444444444444444444444444444444444444]", |
| 86 | + None, |
| 87 | + ), |
| 88 | + ( |
| 89 | + "FromCharacterCode[{100, 101, -1}]", |
| 90 | + ( |
| 91 | + "A character code, which should be a non-negative integer less than 65536, is expected at position 3 in {100, 101, -1}.", |
| 92 | + ), |
| 93 | + "FromCharacterCode[{100, 101, -1}]", |
| 94 | + None, |
| 95 | + ), |
| 96 | + ( |
| 97 | + "FromCharacterCode[{100, 101, 65536}]", |
| 98 | + ( |
| 99 | + "A character code, which should be a non-negative integer less than 65536, is expected at position 3 in {100, 101, 65536}.", |
| 100 | + ), |
| 101 | + "FromCharacterCode[{100, 101, 65536}]", |
| 102 | + None, |
| 103 | + ), |
| 104 | + ( |
| 105 | + "FromCharacterCode[{100, 101, x}]", |
| 106 | + ( |
| 107 | + "A character code, which should be a non-negative integer less than 65536, is expected at position 3 in {100, 101, x}.", |
| 108 | + ), |
| 109 | + "FromCharacterCode[{100, 101, x}]", |
| 110 | + None, |
| 111 | + ), |
| 112 | + ( |
| 113 | + "FromCharacterCode[{100, {101}}]", |
| 114 | + ( |
| 115 | + "A character code, which should be a non-negative integer less than 65536, is expected at position 2 in {100, {101}}.", |
| 116 | + ), |
| 117 | + "FromCharacterCode[{100, {101}}]", |
| 118 | + None, |
| 119 | + ), |
| 120 | + ( |
| 121 | + "FromCharacterCode[{{97, 98, 99}, {100, 101, x}}]", |
| 122 | + ( |
| 123 | + "A character code, which should be a non-negative integer less than 65536, is expected at position 3 in {100, 101, x}.", |
| 124 | + ), |
| 125 | + "FromCharacterCode[{{97, 98, 99}, {100, 101, x}}]", |
| 126 | + None, |
| 127 | + ), |
| 128 | + ( |
| 129 | + "FromCharacterCode[{{97, 98, x}, {100, 101, x}}]", |
| 130 | + ( |
| 131 | + "A character code, which should be a non-negative integer less than 65536, is expected at position 3 in {97, 98, x}.", |
| 132 | + ), |
| 133 | + "FromCharacterCode[{{97, 98, x}, {100, 101, x}}]", |
| 134 | + None, |
| 135 | + ), |
| 136 | + ], |
| 137 | +) |
| 138 | +def test_from_character_code(str_expr, msgs, str_expected, fail_msg): |
| 139 | + """ """ |
| 140 | + check_evaluation( |
| 141 | + str_expr, |
| 142 | + str_expected, |
| 143 | + to_string_expr=True, |
| 144 | + to_string_expected=True, |
| 145 | + hold_expected=True, |
| 146 | + failure_message=fail_msg, |
| 147 | + expected_messages=msgs, |
| 148 | + ) |
0 commit comments