Skip to content

Commit

Permalink
[Hackathon] Add source range highligh check to test_string_formatting (
Browse files Browse the repository at this point in the history
…pytorch#55491)

Summary: Pull Request resolved: pytorch#55491

Reviewed By: janeyx99

Differential Revision: D27627477

Pulled By: gmagogsfm

fbshipit-source-id: 4586d7c96eae762be53c1155c6c724c6d65f1e7f
  • Loading branch information
gmagogsfm authored and facebook-github-bot committed Apr 7, 2021
1 parent b9326d4 commit 5f90ed5
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions test/jit/test_string_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ def test_string_interpolation_with_percent_in_string(self):
def fn(arg1: str) -> str:
return "%s in template %" % arg1 # noqa: F501

with self.assertRaisesRegex(RuntimeError, "Incomplete format specifier"):
with self.assertRaisesRegexWithHighlight(RuntimeError,
"Incomplete format specifier",
"\"%s in template %\" % arg1"):
fn("foo")

def test_string_interpolation_with_string_placeholder_and_digit_variable(self):
Expand All @@ -64,15 +66,19 @@ def test_string_interpolation_with_digit_placeholder_and_string_variable(self):
def fn(arg1: str) -> str:
return "%d in template" % arg1

with self.assertRaisesRegex(RuntimeError, "%d requires a number for formatting, but got String"):
with self.assertRaisesRegexWithHighlight(RuntimeError,
"%d requires a number for formatting, but got String",
"\"%d in template\" % arg1"):
fn("1")

def test_string_interpolation_with_exponent_placeholder_and_string_variable(self):
@torch.jit.script
def fn(arg1: str) -> str:
return "%e in template" % arg1

with self.assertRaisesRegex(RuntimeError, "%e requires a number for formatting, but got String"):
with self.assertRaisesRegexWithHighlight(RuntimeError,
"%e requires a number for formatting, but got String",
"\"%e in template\" % arg1"):
fn("1")

def test_string_interpolation_with_lowercase_exponent_placeholder_and_digit_variable(self):
Expand Down Expand Up @@ -110,7 +116,9 @@ def test_string_interpolation_with_char_placeholder_and_true_string_variable(sel
def fn(arg1: str) -> str:
return "%c in template" % arg1

with self.assertRaisesRegex(RuntimeError, "%c requires an int or char for formatting, but got String"):
with self.assertRaisesRegexWithHighlight(RuntimeError,
"%c requires an int or char for formatting, but got String",
"\"%c in template\" % arg1"):
fn("foo")

def test_string_interpolation_with_multiple_placeholders(self):
Expand All @@ -128,21 +136,27 @@ def test_string_interpolation_with_too_few_arguments(self):
def fn(arg1: str) -> str:
return "%s %s in template" % arg1

with self.assertRaisesRegex(RuntimeError, "Too few arguments for format string"):
with self.assertRaisesRegexWithHighlight(RuntimeError,
"Too few arguments for format string",
"\"%s %s in template\" % arg1"):
fn("foo")

def test_string_interpolation_with_too_many_arguments(self):
@torch.jit.script
def fn(arg1: str, arg2: str) -> str:
return "%s in template" % (arg1, arg2) # noqa: F507

with self.assertRaisesRegex(RuntimeError, "Too many arguments for format string"):
with self.assertRaisesRegexWithHighlight(RuntimeError,
"Too many arguments for format string",
"\"%s in template\" % (arg1, arg2"):
fn("foo", "bar")

def test_string_interpolation_with_unknown_format_specifier(self):
@torch.jit.script
def fn(arg1: str) -> str:
return "%a in template" % arg1 # noqa: F501

with self.assertRaisesRegex(RuntimeError, "The specifier %a is not supported in TorchScript format strings"):
with self.assertRaisesRegexWithHighlight(RuntimeError,
"The specifier %a is not supported in TorchScript format strings",
"\"%a in template\" % arg1"):
fn("foo")

0 comments on commit 5f90ed5

Please sign in to comment.