test file in subdir and test json import

This commit is contained in:
Bela 2019-08-26 18:02:56 +02:00
parent ef03d48889
commit 2aebd18b45
5 changed files with 33 additions and 3 deletions

View file

@ -2,6 +2,7 @@
import random as r
import os
import configparser
import json
class Config:
"""Provide configuration as object elements.
@ -33,14 +34,26 @@ class Config:
for item in int_items:
setattr(self, item, config.getint("conf", item))
class playcard:
class Playcard:
def __init__(self, name, cost, expansion):
self.name = name
self.cost = cost
self.expansion = expansion
def importCardsfile(path):
"""Import the information about cards from a json file.
Args:
path (string): path to the imported file.
Returns:
list of Playcard: list of cards with the data from the file
"""
with open(path) as cardfile:
return(json.load(cardfile))
if __name__ == "__main__":
(_, _, filenames) = next(os.walk("cards"))
cards = {}

11
testCardImporter.py Executable file
View file

@ -0,0 +1,11 @@
#!/usr/bin/env python3
"""Test the importing of cards from json card files."""
from chooseCards import importCardsfile
import json
cards = importCardsfile("testFile/cards.json")
assert cards["bla"] == [3, 4]
assert cards["blub"] == 3
print("json import tested: OK")

View file

@ -8,7 +8,7 @@ Tests:
from chooseCards import Config
config = Config("config.cfg")
config = Config("testFile/config.cfg")
assert config.numberOfCards is 10
try:
config.nonexistingOption

1
testFile/cards.json Normal file
View file

@ -0,0 +1 @@
{"blub":3, "bla": [3,4]}

5
testFile/config.cfg Normal file
View file

@ -0,0 +1,5 @@
[conf]
# configuration for the dominion set generator
# some configurations are self-explanatory, others are described
numberOfCards = 10