import pytest

import examples.basic_usage
import examples.basic_usage_manual
import examples.dataset
import examples.variant_ts_difficulties
import examples.variants


def test_dataset(unihan_options):
    examples.dataset.run()


def test_variants(unihan_options):
    examples.variants.run(unihan_options=unihan_options)


def test_ts_difficulties(unihan_options):
    examples.variant_ts_difficulties.run(unihan_options=unihan_options)


def test_basic_usage(unihan_options, capsys: pytest.CaptureFixture[str]):
    examples.basic_usage.run(unihan_options=unihan_options)

    captured = capsys.readouterr()

    assert "lookup for 好: good" in captured.out
    assert 'matches for "good": 好' in captured.out


def test_basic_usage_manual(unihan_options, capsys: pytest.CaptureFixture[str]):
    examples.basic_usage_manual.run(unihan_options=unihan_options)

    captured = capsys.readouterr()

    assert "lookup for 好: good" in captured.out
    assert 'matches for "good": 好' in captured.out

import importlib
import importlib.util
import sys
import types

import pytest


def load_script(example: str) -> types.ModuleType:
    file_path = f"examples/{example}.py"
    module_name = "run"

    spec = importlib.util.spec_from_file_location(module_name, file_path)
    assert spec is not None
    module = importlib.util.module_from_spec(spec)
    sys.modules[module_name] = module

    assert spec.loader is not None
    spec.loader.exec_module(module)

    return module


def test_dataset(unihan_options):
    example = load_script("dataset")
    example.run()


def test_variants(unihan_options):
    example = load_script("variants")
    example.run()


def test_ts_difficulties(unihan_options):
    example = load_script("variant_ts_difficulties")
    example.run(unihan_options=unihan_options)


def test_basic_usage(unihan_options, capsys: pytest.CaptureFixture[str]):
    example = load_script("basic_usage")
    example.run(unihan_options=unihan_options)

    captured = capsys.readouterr()

    assert "lookup for 好: good" in captured.out
    assert 'matches for "good": 好' in captured.out


def test_basic_usage_manual(unihan_options, capsys: pytest.CaptureFixture[str]):
    example = load_script("basic_usage_manual")
    example.run(unihan_options=unihan_options)

    captured = capsys.readouterr()

    assert "lookup for 好: good" in captured.out
    assert 'matches for "good": 好' in captured.out

Rework for handling of examples/
refactor(tests): Rework for handling of examples/

+ import importlib
+ import importlib.util
+ import sys
+ import types
+ 
  import pytest
  
- import examples.basic_usage
- import examples.basic_usage_manual
- import examples.dataset
- import examples.variant_ts_difficulties
- import examples.variants
+ 
+ def load_script(example: str) -> types.ModuleType:
+     file_path = f"examples/{example}.py"
+     module_name = "run"
+ 
+     spec = importlib.util.spec_from_file_location(module_name, file_path)
+     assert spec is not None
+     module = importlib.util.module_from_spec(spec)
+     sys.modules[module_name] = module
+ 
+     assert spec.loader is not None
+     spec.loader.exec_module(module)
+ 
+     return module
  
  
  def test_dataset(unihan_options):
+     example = load_script("dataset")
-     examples.dataset.run()
+     example.run()
  
  
  def test_variants(unihan_options):
-     examples.variants.run(unihan_options=unihan_options)
+     example = load_script("variants")
+     example.run()
  
  
  def test_ts_difficulties(unihan_options):
+     example = load_script("variant_ts_difficulties")
-     examples.variant_ts_difficulties.run(unihan_options=unihan_options)
+     example.run(unihan_options=unihan_options)
  
  
  def test_basic_usage(unihan_options, capsys: pytest.CaptureFixture[str]):
+     example = load_script("basic_usage")
-     examples.basic_usage.run(unihan_options=unihan_options)
+     example.run(unihan_options=unihan_options)
  
      captured = capsys.readouterr()
  
      assert "lookup for 好: good" in captured.out
      assert 'matches for "good": 好' in captured.out
  
  
  def test_basic_usage_manual(unihan_options, capsys: pytest.CaptureFixture[str]):
+     example = load_script("basic_usage_manual")
-     examples.basic_usage_manual.run(unihan_options=unihan_options)
+     example.run(unihan_options=unihan_options)
  
      captured = capsys.readouterr()
  
      assert "lookup for 好: good" in captured.out
      assert 'matches for "good": 好' in captured.out
  
Rework for handling of examples/
## Code Before:
import pytest

import examples.basic_usage
import examples.basic_usage_manual
import examples.dataset
import examples.variant_ts_difficulties
import examples.variants


def test_dataset(unihan_options):
    examples.dataset.run()


def test_variants(unihan_options):
    examples.variants.run(unihan_options=unihan_options)


def test_ts_difficulties(unihan_options):
    examples.variant_ts_difficulties.run(unihan_options=unihan_options)


def test_basic_usage(unihan_options, capsys: pytest.CaptureFixture[str]):
    examples.basic_usage.run(unihan_options=unihan_options)

    captured = capsys.readouterr()

    assert "lookup for 好: good" in captured.out
    assert 'matches for "good": 好' in captured.out


def test_basic_usage_manual(unihan_options, capsys: pytest.CaptureFixture[str]):
    examples.basic_usage_manual.run(unihan_options=unihan_options)

    captured = capsys.readouterr()

    assert "lookup for 好: good" in captured.out
    assert 'matches for "good": 好' in captured.out

## Instruction:
Rework for handling of examples/
## Code After:
import importlib
import importlib.util
import sys
import types

import pytest


def load_script(example: str) -> types.ModuleType:
    file_path = f"examples/{example}.py"
    module_name = "run"

    spec = importlib.util.spec_from_file_location(module_name, file_path)
    assert spec is not None
    module = importlib.util.module_from_spec(spec)
    sys.modules[module_name] = module

    assert spec.loader is not None
    spec.loader.exec_module(module)

    return module


def test_dataset(unihan_options):
    example = load_script("dataset")
    example.run()


def test_variants(unihan_options):
    example = load_script("variants")
    example.run()


def test_ts_difficulties(unihan_options):
    example = load_script("variant_ts_difficulties")
    example.run(unihan_options=unihan_options)


def test_basic_usage(unihan_options, capsys: pytest.CaptureFixture[str]):
    example = load_script("basic_usage")
    example.run(unihan_options=unihan_options)

    captured = capsys.readouterr()

    assert "lookup for 好: good" in captured.out
    assert 'matches for "good": 好' in captured.out


def test_basic_usage_manual(unihan_options, capsys: pytest.CaptureFixture[str]):
    example = load_script("basic_usage_manual")
    example.run(unihan_options=unihan_options)

    captured = capsys.readouterr()

    assert "lookup for 好: good" in captured.out
    assert 'matches for "good": 好' in captured.out
