2023-12-08 14:52:40 -08:00
|
|
|
import pytest
|
|
|
|
|
2022-07-30 14:20:26 -07:00
|
|
|
from rolltable import tables
|
|
|
|
|
|
|
|
fixture_metadata = """
|
|
|
|
metadata:
|
|
|
|
headers:
|
|
|
|
- Header 1
|
|
|
|
- Header 2
|
|
|
|
- Header 3
|
|
|
|
die: 10
|
|
|
|
frequencies:
|
|
|
|
default:
|
|
|
|
Option 1: 0.3
|
|
|
|
Option 2: 0.5
|
|
|
|
Option 3: 0.2
|
|
|
|
nondefault:
|
|
|
|
Option 1: 0.0
|
|
|
|
Option 2: 0.1
|
|
|
|
Option 3: 0.9
|
|
|
|
"""
|
|
|
|
|
|
|
|
fixture_source = """
|
|
|
|
Option 1:
|
|
|
|
- choice 1: description 1
|
|
|
|
- choice 2: description 2
|
|
|
|
- choice 3: description 3
|
|
|
|
Option 2:
|
|
|
|
- choice 1: description 4
|
|
|
|
- choice 2: description 5
|
|
|
|
- choice 3: description 6
|
|
|
|
Option 3:
|
|
|
|
- choice 1: description 7
|
|
|
|
- choice 2: description 8
|
|
|
|
- choice 3: description 9
|
|
|
|
"""
|
|
|
|
|
|
|
|
fixture_one_choice = """
|
|
|
|
option 1:
|
|
|
|
- choice 1: description 1
|
|
|
|
"""
|
|
|
|
|
|
|
|
fixture_repeated_choices = """
|
|
|
|
option 1:
|
|
|
|
- choice 1: description 1
|
|
|
|
- choice 1: description 1
|
|
|
|
- choice 1: description 1
|
|
|
|
"""
|
|
|
|
|
|
|
|
fixture_no_descriptions = """
|
2022-07-31 15:03:19 -07:00
|
|
|
metadata:
|
|
|
|
headers:
|
|
|
|
- option
|
|
|
|
- choice
|
2022-07-30 14:20:26 -07:00
|
|
|
option 1:
|
|
|
|
- choice 1
|
|
|
|
"""
|
|
|
|
|
2022-07-30 20:44:16 -07:00
|
|
|
fixture_combined_A = """
|
|
|
|
A1:
|
|
|
|
- A choice 1
|
|
|
|
- A choice 2
|
|
|
|
- A choice 3
|
|
|
|
A2:
|
|
|
|
- A choice 4
|
|
|
|
- A choice 5
|
|
|
|
- A choice 6
|
|
|
|
A3:
|
|
|
|
- A choice 7
|
|
|
|
- A choice 8
|
|
|
|
- A choice 9
|
|
|
|
"""
|
|
|
|
|
|
|
|
fixture_combined_B = """
|
|
|
|
metadata:
|
|
|
|
headers:
|
|
|
|
- HeaderB
|
|
|
|
- HeaderB_Choice
|
|
|
|
B1:
|
|
|
|
- B choice 1
|
|
|
|
B2:
|
|
|
|
- B choice 2
|
|
|
|
B3:
|
|
|
|
- B choice 3
|
|
|
|
"""
|
|
|
|
|
2022-08-06 16:58:28 -07:00
|
|
|
fixture_no_options = """
|
|
|
|
metadata:
|
|
|
|
headers:
|
|
|
|
- headerA
|
|
|
|
- headerB
|
|
|
|
B1:
|
|
|
|
B2:
|
|
|
|
B3:
|
|
|
|
"""
|
|
|
|
|
2023-12-08 14:28:07 -08:00
|
|
|
fixture_lists_and_dicts = ["""
|
2022-08-13 12:32:52 -07:00
|
|
|
#
|
2023-12-08 14:28:07 -08:00
|
|
|
# category one two three four
|
|
|
|
# Category foo bar baz quz
|
2022-08-13 12:32:52 -07:00
|
|
|
#
|
|
|
|
metadata:
|
|
|
|
headers:
|
2023-12-08 14:28:07 -08:00
|
|
|
- category
|
2022-08-13 12:32:52 -07:00
|
|
|
- one
|
|
|
|
- two
|
|
|
|
- three
|
|
|
|
- four
|
2023-12-08 14:52:40 -08:00
|
|
|
list:
|
2023-12-08 14:28:07 -08:00
|
|
|
- foo:
|
|
|
|
- bar
|
|
|
|
- baz
|
|
|
|
- quz
|
|
|
|
""", """
|
|
|
|
#
|
|
|
|
# category one two three four
|
|
|
|
# Category foo bar baz quz
|
|
|
|
#
|
|
|
|
metadata:
|
|
|
|
headers:
|
|
|
|
- category
|
|
|
|
- one
|
|
|
|
- two
|
|
|
|
- three
|
|
|
|
- four
|
2023-12-08 14:52:40 -08:00
|
|
|
dict:
|
2023-12-08 14:28:07 -08:00
|
|
|
foo:
|
|
|
|
- bar
|
2022-08-13 12:32:52 -07:00
|
|
|
- baz
|
|
|
|
- quz
|
2023-12-08 14:28:07 -08:00
|
|
|
bar:
|
|
|
|
- a
|
|
|
|
- b
|
|
|
|
- c
|
|
|
|
"""]
|
2022-08-13 12:32:52 -07:00
|
|
|
|
|
|
|
|
2023-12-08 14:52:40 -08:00
|
|
|
@pytest.mark.parametrize('fixture', fixture_lists_and_dicts)
|
|
|
|
def test_lists_and_dicts(fixture):
|
|
|
|
t = tables.RollTable([fixture], die=1)
|
|
|
|
assert(str(t))
|
2022-08-13 12:32:52 -07:00
|
|
|
|
2022-07-30 20:44:16 -07:00
|
|
|
|
|
|
|
def test_combined_tables():
|
2022-07-31 15:03:19 -07:00
|
|
|
combined = tables.RollTable([fixture_combined_A, fixture_combined_B], die=6)
|
|
|
|
assert str(combined)
|
2022-07-30 20:44:16 -07:00
|
|
|
|
2022-07-30 14:20:26 -07:00
|
|
|
|
|
|
|
def test_table_end_to_end():
|
2022-07-31 15:03:19 -07:00
|
|
|
assert str(tables.RollTable([fixture_source]))
|
2022-07-30 14:20:26 -07:00
|
|
|
|
|
|
|
|
|
|
|
def test_table_end_to_end_with_metadata():
|
2022-07-31 15:03:19 -07:00
|
|
|
assert str(tables.RollTable([fixture_metadata + fixture_source]))
|
2022-07-30 14:20:26 -07:00
|
|
|
|
|
|
|
|
|
|
|
def test_table_frequency():
|
2022-07-31 15:03:19 -07:00
|
|
|
t = tables.RollTable([fixture_metadata + fixture_source], frequency='nondefault')
|
|
|
|
assert t._data[0].frequencies['Option 1'] == 0.0
|
|
|
|
assert t._data[0].frequencies['Option 2'] == 0.1
|
|
|
|
assert t._data[0].frequencies['Option 3'] == 0.9
|
2022-07-30 14:20:26 -07:00
|
|
|
|
|
|
|
|
|
|
|
def test_one_option():
|
2022-07-31 15:03:19 -07:00
|
|
|
t = tables.RollTable([fixture_one_choice], die=1)
|
|
|
|
assert t._values == [['option 1', 'choice 1', 'description 1']]
|
2022-07-30 14:20:26 -07:00
|
|
|
|
|
|
|
|
|
|
|
def test_collapsed():
|
2022-07-31 15:03:19 -07:00
|
|
|
t = tables.RollTable([fixture_repeated_choices], die=6)
|
|
|
|
assert len(list(t.rows)) == 2 # (+1 for headers)
|
2022-07-30 14:20:26 -07:00
|
|
|
|
|
|
|
|
|
|
|
def test_not_collapsed():
|
2022-07-31 15:03:19 -07:00
|
|
|
t = tables.RollTable([fixture_repeated_choices], die=6)
|
|
|
|
assert len(list(t.expanded_rows)) == 7 # (+1 for headers)
|
2022-07-30 14:20:26 -07:00
|
|
|
|
|
|
|
|
|
|
|
def test_no_descriptions():
|
2022-07-31 15:03:19 -07:00
|
|
|
t = tables.RollTable([fixture_no_descriptions], die=1)
|
2022-07-30 14:20:26 -07:00
|
|
|
assert 'd1' in str(t)
|
|
|
|
assert 'option 1' in str(t)
|
2022-08-06 13:32:34 -07:00
|
|
|
|
|
|
|
|
2022-08-13 12:32:52 -07:00
|
|
|
def test_no_options():
|
2022-08-06 16:58:28 -07:00
|
|
|
t = tables.RollTable([fixture_no_options])
|
2022-08-13 12:32:52 -07:00
|
|
|
assert str(t)
|
|
|
|
|
|
|
|
|
2023-12-22 14:44:50 -08:00
|
|
|
@pytest.mark.parametrize('table', [
|
|
|
|
tables.RollTable([fixture_no_options]),
|
|
|
|
tables.RollTable([fixture_one_choice]),
|
|
|
|
tables.RollTable([fixture_metadata + fixture_source]),
|
|
|
|
tables.RollTable([fixture_source]),
|
|
|
|
])
|
|
|
|
def test_yaml(table):
|
|
|
|
assert table.as_yaml()
|
2023-04-12 10:37:31 -07:00
|
|
|
|
|
|
|
|
|
|
|
def test_text():
|
|
|
|
assert repr(tables.RollTable([fixture_no_options]))
|
|
|
|
assert repr(tables.RollTable([fixture_one_choice]))
|
|
|
|
assert repr(tables.RollTable([fixture_metadata + fixture_source]))
|
|
|
|
assert repr(tables.RollTable([fixture_source]))
|