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

🧹 Reduce duplication in snippet testing #5345

Merged
merged 24 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
b1d3426
Remove duplication in snippet testing
rix0rrr Apr 1, 2024
d479f8b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 1, 2024
41df944
Slightly nicer code
rix0rrr Apr 1, 2024
36f6ab9
Merge branch 'reduce-snippet-testing-duplication' of github.com:Felie…
rix0rrr Apr 1, 2024
8d8d6c0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 1, 2024
e369df3
Oops, finish Slides
rix0rrr Apr 1, 2024
e5bb564
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 1, 2024
326c8be
Make the locating of snippets in YAML more straightforward
rix0rrr Apr 2, 2024
7feb6a6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 2, 2024
ae6b1b0
Slightly better formatting and variable names
rix0rrr Apr 2, 2024
1558677
Merge branch 'reduce-snippet-testing-duplication' of github.com:Felie…
rix0rrr Apr 2, 2024
8a95014
Automate yaml_path tracking
rix0rrr Apr 2, 2024
6ec3174
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 2, 2024
bd3c9d7
Add documentation
rix0rrr Apr 2, 2024
f9726de
Merge branch 'reduce-snippet-testing-duplication' of github.com:Felie…
rix0rrr Apr 2, 2024
99ef718
Remove some dealing with old formats
rix0rrr Apr 2, 2024
6a197d0
Address review comments
rix0rrr Apr 4, 2024
9f51dbe
Re-add pylint
rix0rrr Apr 4, 2024
67171ad
Merge remote-tracking branch 'origin/main' into reduce-snippet-testin…
rix0rrr Apr 4, 2024
c1a6221
Add version note
rix0rrr Apr 4, 2024
091cb37
Forgot to commit this file
rix0rrr Apr 4, 2024
4042885
Update pylint call to be robust against different libdir locations
rix0rrr Apr 4, 2024
0230e79
Move pylint code to a separate PR
rix0rrr Apr 4, 2024
993d047
Merge branch 'main' into reduce-snippet-testing-duplication
mergify[bot] Apr 5, 2024
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
66 changes: 57 additions & 9 deletions tests/Tester.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import random
import textwrap
from dataclasses import dataclass
import pickle
import hashlib
import hedy
Expand All @@ -12,6 +13,7 @@
import inspect
import unittest
import utils
import typing
from hedy_content import ALL_KEYWORD_LANGUAGES, KEYWORDS

from hedy_sourcemap import SourceRange
Expand Down Expand Up @@ -48,6 +50,52 @@ def __repr__(self):
return f'Snippet({self.name})'


@dataclass
class YamlSnippet:
"""A snippet found in one of the YAML files.

This is a replacement of 'Snippet' with fewer fields, only the fields that
are used in the snippet tests.

`yaml_path` is the path in the YAML where this snippet was found, as an
array of either strings or ints.

For example, in a YAML file that looks like:

```
adventures:
1:
code: |
print hello
```

The `yaml_path` would be `['adventures', 1, 'code']`.
"""
filename: str
yaml_path: typing.List
code: str
language: str
level: int

def __post_init__(self):
# 'code' may be replaced later on when translating keywords
self.original_code = self.code
self.name = f'{self.relative_filename}-{self.yaml_path_str}'

@property
def relative_filename(self):
return os.path.relpath(self.filename, ROOT_DIR)

@property
def yaml_path_str(self):
return '.'.join(str(x) for x in self.yaml_path)

@property
def location(self):
"""Returns a description of the location."""
return f'{self.relative_filename} at {self.yaml_path_str}'


class SkippedMapping:
""" Class used to test if a certain source mapping contains an exception type """

Expand Down Expand Up @@ -481,6 +529,8 @@ def indent(code, spaces_amount=2, skip_first_line=False):

@staticmethod
def translate_keywords_in_snippets(snippets):
"""Mutates the snippets in-place."""

# fill keyword dict for all keyword languages
keyword_dict = {}
for lang in ALL_KEYWORD_LANGUAGES:
Expand All @@ -495,20 +545,18 @@ def translate_keywords_in_snippets(snippets):
# NOTE: .format() instead of safe_format() on purpose!
for snippet in snippets:
# store original code
snippet[1].original_code = snippet[1].code
snippet.original_code = snippet.code
try:
if snippet[1].language in ALL_KEYWORD_LANGUAGES.keys():
snippet[1].code = snippet[1].code.format(**keyword_dict[snippet[1].language])
if snippet.language in ALL_KEYWORD_LANGUAGES.keys():
snippet.code = snippet.code.format(**keyword_dict[snippet.language])
else:
snippet[1].code = snippet[1].code.format(**english_keywords)
snippet.code = snippet.code.format(**english_keywords)
except KeyError:
print("This following snippet contains an invalid placeholder...")
print(snippet[1].code)
print(snippet.code)
except ValueError:
print("This following snippet contains an unclosed invalid placeholder...")
print(snippet[1].code)

return snippets
print(snippet.code)

def format_test_error_md(self, E, snippet: Snippet):
"""Given a snippet and an exception, return a Markdown string describing the problem."""
Expand Down Expand Up @@ -538,7 +586,7 @@ def add_arrow(code):

rel_file = os.path.relpath(snippet.filename, ROOT_DIR)
message.append(f'## {rel_file}')
message.append(f'There was a problem in a level {snippet.level} snippet:')
message.append(f'There was a problem in a level {snippet.level} snippet, at {snippet.yaml_path_str}:')

# Use a 'caution' admonition because it renders in red
message.append('> [!CAUTION]')
Expand Down
Loading
Loading