Fix random value generator, new CLI

This commit fixes a defect in the random value generator introduced when
the random_sets module was refactored. It also introduces three useful
CLI options, for generating roll tables of random trinkets, wild magic
surges, and psychadelic effects.
This commit is contained in:
evilchili
2024-01-16 22:09:09 -08:00
parent 2b3e82114f
commit 1c0bd7357d
7 changed files with 456 additions and 222 deletions
+15 -210
View File
@@ -1,217 +1,22 @@
import pytest
from rolltable import types
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 = """
metadata:
headers:
- option
- choice
option 1:
- choice 1
"""
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
"""
fixture_no_options = """
metadata:
headers:
- headerA
- headerB
B1:
B2:
B3:
"""
fixture_lists_and_dicts = ["""
#
# category one two three four
# Category foo bar baz quz
#
metadata:
headers:
- category
- one
- two
- three
- four
list:
- foo:
- bar
- baz
- quz
""", """
#
# category one two three four
# Category foo bar baz quz
#
metadata:
headers:
- category
- one
- two
- three
- four
dict:
foo:
- bar
- baz
- quz
bar:
- a
- b
- c
"""]
from pathlib import Path
from rolltable.types import RollTable
@pytest.mark.parametrize('fixture', fixture_lists_and_dicts)
def test_lists_and_dicts(fixture):
t = types.RollTable([fixture], die=1)
assert(str(t))
sources = Path(__file__).parent / '..' / 'rolltable' / 'sources'
flat_list = (sources / 'trinkets.yaml').read_text()
dict_of_dicts = (sources / 'wild_magic.yaml').read_text()
dict_of_lists = (sources / 'psychadelic_effects.yaml').read_text()
def test_combined_tables():
combined = types.RollTable([fixture_combined_A, fixture_combined_B], die=6)
assert str(combined)
def test_table_end_to_end():
assert str(types.RollTable([fixture_source]))
def test_table_end_to_end_with_metadata():
assert str(types.RollTable([fixture_metadata + fixture_source]))
def test_table_frequency():
t = types.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
def test_one_option():
t = types.RollTable([fixture_one_choice], die=1)
assert t._values == [['option 1', 'choice 1', 'description 1']]
def test_collapsed():
t = types.RollTable([fixture_repeated_choices], die=6)
assert len(list(t.rows)) == 2 # (+1 for headers)
def test_not_collapsed():
t = types.RollTable([fixture_repeated_choices], die=6)
assert len(list(t.expanded_rows)) == 7 # (+1 for headers)
def test_no_descriptions():
t = types.RollTable([fixture_no_descriptions], die=1)
assert 'd1' in str(t)
assert 'option 1' in str(t)
def test_no_options():
t = types.RollTable([fixture_no_options])
assert str(t)
@pytest.mark.parametrize('table', [
types.RollTable([fixture_no_options]),
types.RollTable([fixture_one_choice]),
types.RollTable([fixture_metadata + fixture_source]),
types.RollTable([fixture_source]),
@pytest.mark.parametrize('data, expected', [
([dict_of_dicts], ['d1000 ', 'A third eye', 'Advantage on perception checks']),
([flat_list], ['d1000 ', 'ivory mimic']),
([dict_of_lists], ['d1000', 'Cosmic', 'mind expands', 'it will become so']),
])
def test_yaml(table):
assert table.as_yaml()
def test_text():
assert repr(types.RollTable([fixture_no_options]))
assert repr(types.RollTable([fixture_one_choice]))
assert repr(types.RollTable([fixture_metadata + fixture_source]))
assert repr(types.RollTable([fixture_source]))
@pytest.mark.parametrize('table', [
types.RollTable([fixture_no_options]),
types.RollTable([fixture_one_choice]),
types.RollTable([fixture_metadata + fixture_source]),
types.RollTable([fixture_source]),
types.RollTable([fixture_no_options]),
types.RollTable([fixture_lists_and_dicts]),
])
def test_as_dict(table):
for src in table.datasources:
assert src.as_dict()
def test_flat(data, expected):
rt = RollTable(data, die=1000)
for txt in expected:
assert txt in str(rt)