|
1 | 1 | """CmdStanModel tests""" |
2 | 2 |
|
| 3 | +import contextlib |
| 4 | +import io |
3 | 5 | import logging |
4 | 6 | import os |
5 | 7 | import shutil |
6 | 8 | import tempfile |
7 | 9 | import unittest |
| 10 | +from glob import glob |
8 | 11 | from test import CustomTestCase |
| 12 | +from unittest.mock import MagicMock, patch |
9 | 13 |
|
| 14 | +import pytest |
10 | 15 | from testfixtures import LogCapture, StringComparison |
11 | 16 |
|
12 | 17 | from cmdstanpy.model import CmdStanModel |
13 | | -from cmdstanpy.utils import EXTENSION |
| 18 | +from cmdstanpy.utils import EXTENSION, cmdstan_version_before |
14 | 19 |
|
15 | 20 | HERE = os.path.dirname(os.path.abspath(__file__)) |
16 | 21 | DATAFILES_PATH = os.path.join(HERE, 'data') |
|
34 | 39 | BERN_BASENAME = 'bernoulli' |
35 | 40 |
|
36 | 41 |
|
| 42 | +# pylint: disable=too-many-public-methods |
37 | 43 | class CmdStanModelTest(CustomTestCase): |
38 | 44 | def test_model_good(self): |
39 | 45 | # compile on instantiation, override model name |
@@ -374,6 +380,88 @@ def test_model_includes_implicit(self): |
374 | 380 | model2 = CmdStanModel(stan_file=stan) |
375 | 381 | self.assertPathsEqual(model2.exe_file, exe) |
376 | 382 |
|
| 383 | + @pytest.mark.skipif( |
| 384 | + not cmdstan_version_before(2, 32), |
| 385 | + reason="Deprecated syntax removed in Stan 2.32", |
| 386 | + ) |
| 387 | + def test_model_format_deprecations(self): |
| 388 | + stan = os.path.join(DATAFILES_PATH, 'format_me_deprecations.stan') |
| 389 | + |
| 390 | + model = CmdStanModel(stan_file=stan, compile=False) |
| 391 | + |
| 392 | + sys_stdout = io.StringIO() |
| 393 | + with contextlib.redirect_stdout(sys_stdout): |
| 394 | + model.format() |
| 395 | + |
| 396 | + formatted = sys_stdout.getvalue() |
| 397 | + self.assertIn("//", formatted) |
| 398 | + self.assertNotIn("#", formatted) |
| 399 | + self.assertEqual(formatted.count('('), 5) |
| 400 | + |
| 401 | + sys_stdout = io.StringIO() |
| 402 | + with contextlib.redirect_stdout(sys_stdout): |
| 403 | + model.format(canonicalize=True) |
| 404 | + |
| 405 | + formatted = sys_stdout.getvalue() |
| 406 | + print(formatted) |
| 407 | + self.assertNotIn("<-", formatted) |
| 408 | + self.assertEqual(formatted.count('('), 0) |
| 409 | + |
| 410 | + shutil.copy(stan, stan + '.testbak') |
| 411 | + try: |
| 412 | + model.format(overwrite_file=True, canonicalize=True) |
| 413 | + self.assertEqual(len(glob(stan + '.bak-*')), 1) |
| 414 | + finally: |
| 415 | + shutil.copy(stan + '.testbak', stan) |
| 416 | + |
| 417 | + @pytest.mark.skipif( |
| 418 | + cmdstan_version_before(2, 29), reason='Options only available later' |
| 419 | + ) |
| 420 | + def test_model_format_options(self): |
| 421 | + stan = os.path.join(DATAFILES_PATH, 'format_me.stan') |
| 422 | + |
| 423 | + model = CmdStanModel(stan_file=stan, compile=False) |
| 424 | + |
| 425 | + sys_stdout = io.StringIO() |
| 426 | + with contextlib.redirect_stdout(sys_stdout): |
| 427 | + model.format(max_line_length=10) |
| 428 | + formatted = sys_stdout.getvalue() |
| 429 | + self.assertGreater(len(formatted.splitlines()), 11) |
| 430 | + |
| 431 | + sys_stdout = io.StringIO() |
| 432 | + with contextlib.redirect_stdout(sys_stdout): |
| 433 | + model.format(canonicalize='braces') |
| 434 | + formatted = sys_stdout.getvalue() |
| 435 | + self.assertEqual(formatted.count('{'), 3) |
| 436 | + self.assertEqual(formatted.count('('), 4) |
| 437 | + |
| 438 | + sys_stdout = io.StringIO() |
| 439 | + with contextlib.redirect_stdout(sys_stdout): |
| 440 | + model.format(canonicalize=['parentheses']) |
| 441 | + formatted = sys_stdout.getvalue() |
| 442 | + self.assertEqual(formatted.count('{'), 1) |
| 443 | + self.assertEqual(formatted.count('('), 1) |
| 444 | + |
| 445 | + sys_stdout = io.StringIO() |
| 446 | + with contextlib.redirect_stdout(sys_stdout): |
| 447 | + model.format(canonicalize=True) |
| 448 | + formatted = sys_stdout.getvalue() |
| 449 | + self.assertEqual(formatted.count('{'), 3) |
| 450 | + self.assertEqual(formatted.count('('), 1) |
| 451 | + |
| 452 | + @patch('cmdstanpy.utils.cmdstan_version', MagicMock(return_value=(2, 27))) |
| 453 | + def test_format_old_version(self): |
| 454 | + self.assertTrue(cmdstan_version_before(2, 28)) |
| 455 | + |
| 456 | + stan = os.path.join(DATAFILES_PATH, 'format_me.stan') |
| 457 | + model = CmdStanModel(stan_file=stan, compile=False) |
| 458 | + with self.assertRaisesRegexNested(RuntimeError, r"--canonicalize"): |
| 459 | + model.format(canonicalize='braces') |
| 460 | + with self.assertRaisesRegexNested(RuntimeError, r"--max-line"): |
| 461 | + model.format(max_line_length=88) |
| 462 | + |
| 463 | + model.format(canonicalize=True) |
| 464 | + |
377 | 465 |
|
378 | 466 | if __name__ == '__main__': |
379 | 467 | unittest.main() |
0 commit comments