forked from google/pigweed
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pw_compilation_testing: Relax PW_NC_EXPECT syntax; add tests
Allow PW_NC_EXPECT() statements to span multiple lines. The statements must contain only string literals or //-style comments. This prevents clang-format from breaking PW_NC_EXPECT() statements when it reflows string literals. With this change, statements like the following are permitted: PW_NC_EXPECT( // This is a comment "This is a \"very\" long string that didn't " // Comment "fit on one line,\nokay?"); Also added tests for negative compilation test parsing. Change-Id: Ie7ad157c817eaf7f0f576bcd841f00426623b635 Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/107678 Reviewed-by: Armando Montanez <[email protected]> Commit-Queue: Auto-Submit <[email protected]> Pigweed-Auto-Submit: Wyatt Hepler <[email protected]>
- Loading branch information
Showing
4 changed files
with
230 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
# Copyright 2022 The Pigweed Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
# use this file except in compliance with the License. You may obtain a copy of | ||
# the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
# License for the specific language governing permissions and limitations under | ||
# the License. | ||
"""Tests for the negative compilation test generator.""" | ||
|
||
from pathlib import Path | ||
import re | ||
import tempfile | ||
import unittest | ||
|
||
from pw_compilation_testing.generator import (Compiler, Expectation, | ||
ParseError, TestCase, | ||
enumerate_tests) | ||
|
||
SOURCE = r''' | ||
#if PW_NC_TEST(FirstTest) | ||
PW_NC_EXPECT("abcdef"); | ||
SomeSourceCode(); | ||
#endif // PW_NC_TEST | ||
#if PW_NC_TEST(SecondTest) | ||
PW_NC_EXPECT("\"\"abc123" // Include " and other escapes in the string | ||
"def'456\"" | ||
// Goodbye | ||
"ghi\n\t789" // ??? | ||
); // abc | ||
#endif // PW_NC_TEST | ||
''' | ||
|
||
ILLEGAL_COMMENT = ''' | ||
#if PW_NC_TEST(FirstTest) | ||
PW_NC_EXPECT("abcdef" /* illegal comment */); | ||
#endif // PW_NC_TEST | ||
''' | ||
|
||
UNTERMINATED_EXPECTATION = '#if PW_NC_TEST(FirstTest)\nPW_NC_EXPECT("abcdef"\n' | ||
|
||
|
||
def _write_to_temp_file(contents: str) -> Path: | ||
file = tempfile.NamedTemporaryFile('w', delete=False) | ||
file.write(contents) | ||
file.close() | ||
return Path(file.name) | ||
|
||
|
||
class ParserTest(unittest.TestCase): | ||
"""Tests parsing negative compilation tests from a file.""" | ||
def test_successful(self) -> None: | ||
try: | ||
path = _write_to_temp_file(SOURCE) | ||
|
||
self.assertEqual([ | ||
TestCase( | ||
'TestSuite', | ||
'FirstTest', | ||
(Expectation(Compiler.ANY, re.compile('abcdef'), 3), ), | ||
path, | ||
2, | ||
), | ||
TestCase( | ||
'TestSuite', | ||
'SecondTest', | ||
(Expectation(Compiler.ANY, | ||
re.compile('""abc123def\'456"ghi\\n\\t789'), | ||
10), ), | ||
path, | ||
9, | ||
) | ||
], list(enumerate_tests('TestSuite', [path]))) | ||
finally: | ||
path.unlink() | ||
|
||
def test_illegal_comment(self) -> None: | ||
try: | ||
path = _write_to_temp_file(ILLEGAL_COMMENT) | ||
with self.assertRaises(ParseError): | ||
list(enumerate_tests('TestSuite', [path])) | ||
finally: | ||
path.unlink() | ||
|
||
def test_unterminated_expectation(self) -> None: | ||
try: | ||
path = _write_to_temp_file(UNTERMINATED_EXPECTATION) | ||
with self.assertRaises(ParseError) as err: | ||
list(enumerate_tests('TestSuite', [path])) | ||
finally: | ||
path.unlink() | ||
|
||
self.assertIn('Unterminated', str(err.exception)) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters