Example #1
0
    def test_die_init(self):
        # non-existing file to build a die
        with self.assertRaises(FileNotFoundError):
            Die('imnotafile.txt')

        # empty die
        with self.assertRaises(IndexError):
            Die(path + "dieEmpty.txt")

        # die check
        die = Die(path + "die0.txt")
        expected_faced = [
            'bike', 'moonandstars', 'bag', 'bird', 'crying', 'angry'
        ]
        self.assertEqual(die.faces, expected_faced)
 def test_die_init(self):
     die = Die("./monolith/classes/tests/die0.txt")
     rnd.seed(574891)
     result = die.faces
     print(result)
     self.assertEqual(
         result, ['bike', 'moonandstars', 'bag', 'bird', 'crying', 'angry'])
Example #3
0
    def test_throw_and_serialize_dice_set(self):
        rnd.seed(574891)
        die1 = Die(path + "die0.txt")
        die2 = Die(path + "die1.txt")
        die3 = Die(path + "die2.txt")
        dice = [die1, die2, die3]
        dice_set = DiceSet(dice)

        # throw dice
        expected_res = ['bag', 'clock', 'bus']
        self.assertEqual(dice_set.throw_dice(), expected_res)

        # serialize set
        serialized_set = dice_set.serialize()
        expected_serialized_set = json.dumps(dice_set.pips)
        self.assertEqual(serialized_set, expected_serialized_set)
Example #4
0
def _roll_dice():
    # check dice_number
    try:
        # get number of dice from the form of previous page
        dice_number = int(request.form['dice_number'])
        session['dice_number'] = dice_number

        # check retrieved data
        if dice_number not in range(2, 7):
            raise ValueError

    except BadRequestKeyError:  # i'm here after re-rolling dice
        dice_number = session['dice_number']
    except (KeyError,
            ValueError):  # i'm here directly, have to go from settings before
        flash('Invalid number of dice!', 'error')
        session.pop('dice_number', None)
        return redirect(url_for('dice._settings'))

    # check dice_set
    try:
        # get dice set from the form of previous page
        dice_img_set = str(request.form['dice_img_set'])
        session['dice_img_set'] = dice_img_set

        # check retrieved data
        if dice_img_set not in {'standard', 'animal', 'halloween', 'emptyset'}:
            raise ValueError

    except BadRequestKeyError:  # i'm here after re-rolling dice
        dice_img_set = session['dice_img_set']
    except (KeyError,
            ValueError):  # i'm here directly, have to go from settings before
        flash('Invalid dice set!', 'error')
        session.pop('dice_img_set', None)
        return redirect(url_for('dice._settings'))

    # random sampling dice and throw them
    dice_indexes = rnd.sample(range(0, 6), dice_number)
    dice_list = []
    for i in dice_indexes:
        try:
            dirname = os.path.dirname(os.path.abspath(__file__))
            path = dirname + "/../resources/" + dice_img_set + "/"
            dice_list.append(Die(path + 'die' + str(i) + '.txt'))
        except (FileNotFoundError, IndexError):
            session.pop('dice_number', None)
            session.pop('dice_img_set', None)
            flash("Can't find dice on server", 'error')
            return redirect(url_for('dice._settings'))

    dice_set = DiceSet(dice_list)
    dice_set.throw_dice()
    session['figures'] = dice_set.pips

    context_vars = {
        'dice_number': dice_number,
        'dice_img_set': dice_img_set,
        'dice_indexes': dice_indexes,
        'words': dice_set.pips,
        'home_url': HOME_URL
    }
    return render_template('roll_dice.html', **context_vars)
Example #5
0
 def test_die_pip(self):
     rnd.seed(574891)
     die = Die(RESOURCES_DIR + '/diceset/standard/die0.txt')
     res = die.throw_die()
     self.assertEqual(res, 'bag')
Example #6
0
 def test_die_init(self):
     die = Die(RESOURCES_DIR + '/diceset/standard/die0.txt')
     check = ['bike', 'moonandstars', 'bag', 'bird', 'crying', 'angry']
     self.assertEqual(die.faces, check)
Example #7
0
 def test_throw_die(self):
     rnd.seed(666)
     die = Die(path + "die0.txt")
     res = die.throw_die()
     self.assertEqual(res, 'bird')