コード例 #1
0
def roll_dices(_, update):
    try:
        dices = parse(update.message.text)

        value, minimal, maximal = calculate(dices)
        update.message.reply_text('%d (%d..%d)' % (value, minimal, maximal))

    except InvalidFormat as exc:
        update.message.reply_text('Error: Invalid format %s' % exc.format)

    except InvalidDicesCount as exc:
        update.message.reply_text(
            'Error: Invalid dices count %d (min %d, max %d)' %
            (exc.dices, minimal_dices_count, maximal_dices_count))

    except InvalidFacesCount as exc:
        update.message.reply_text(
            'Error: Invalid faces count %d (min %d, max %d)' %
            (exc.faces, minimal_faces_count, maximal_faces_count))

    except:
        update.message.reply_text('Error: Unknown')
コード例 #2
0
ファイル: test_parse.py プロジェクト: vasalvit/rpg_dices_bot
 def test_fail_for_empty_string(self):
     with self.assertRaises(InvalidFormat):
         _ = parse('')
コード例 #3
0
ファイル: test_parse.py プロジェクト: vasalvit/rpg_dices_bot
 def test_fail_for_maximal_faces(self):
     with self.assertRaises(InvalidFacesCount):
         parse('2d' + str(maximal_faces_count + 1) + '+6')
コード例 #4
0
ファイル: test_parse.py プロジェクト: vasalvit/rpg_dices_bot
 def test_fail_for_maximal_count(self):
     with self.assertRaises(InvalidDicesCount):
         _ = parse(str(maximal_dices_count + 1) + 'd4+6')
コード例 #5
0
ファイル: test_parse.py プロジェクト: vasalvit/rpg_dices_bot
 def test_fail_for_negative_faces(self):
     with self.assertRaises(InvalidFormat):
         _ = parse('2d+4+6')
コード例 #6
0
ファイル: test_parse.py プロジェクト: vasalvit/rpg_dices_bot
 def test_fail_for_faces_with_sign(self):
     with self.assertRaises(InvalidFormat):
         _ = parse('2d+4+6')
コード例 #7
0
ファイル: test_parse.py プロジェクト: vasalvit/rpg_dices_bot
 def test_fail_if_count_is_not_number(self):
     with self.assertRaises(InvalidFormat):
         _ = parse('x')
     with self.assertRaises(InvalidFormat):
         _ = parse('2x')
コード例 #8
0
ファイル: test_parse.py プロジェクト: vasalvit/rpg_dices_bot
 def test_success_with_D(self):
     (count, faces, modifier) = parse('2d4+6')
     self.assertEqual(2, count)
     self.assertEqual(4, faces)
     self.assertEqual(6, modifier)
コード例 #9
0
ファイル: test_parse.py プロジェクト: vasalvit/rpg_dices_bot
 def test_fail_if_there_is_tail(self):
     with self.assertRaises(InvalidFormat):
         _ = parse('2d4+6x')
コード例 #10
0
ファイル: test_parse.py プロジェクト: vasalvit/rpg_dices_bot
 def test_fail_if_modifier_is_not_number(self):
     with self.assertRaises(InvalidFormat):
         _ = parse('2d4+x')
コード例 #11
0
ファイル: test_parse.py プロジェクト: vasalvit/rpg_dices_bot
 def test_fail_without_modifier_sign(self):
     with self.assertRaises(InvalidFormat):
         _ = parse('2d4x')
コード例 #12
0
ファイル: test_parse.py プロジェクト: vasalvit/rpg_dices_bot
 def test_fail_if_faces_is_not_number(self):
     with self.assertRaises(InvalidFormat):
         _ = parse('2dx')
コード例 #13
0
ファイル: test_parse.py プロジェクト: vasalvit/rpg_dices_bot
 def test_fail_without_faces(self):
     with self.assertRaises(InvalidFormat):
         _ = parse('2d')
コード例 #14
0
ファイル: test_parse.py プロジェクト: vasalvit/rpg_dices_bot
 def test_success_with_negative_modifier(self):
     (count, faces, modifier) = parse('2d4-6')
     self.assertEqual(2, count)
     self.assertEqual(4, faces)
     self.assertEqual(-6, modifier)
コード例 #15
0
ファイル: test_parse.py プロジェクト: vasalvit/rpg_dices_bot
 def test_ignore_spaces(self):
     (count, faces, modifier) = parse(' 2 d 4 + 6 ')
     self.assertEqual(2, count)
     self.assertEqual(4, faces)
     self.assertEqual(6, modifier)
コード例 #16
0
ファイル: test_parse.py プロジェクト: vasalvit/rpg_dices_bot
 def test_use_1_without_count(self):
     (count, _, _) = parse('d4')
     self.assertEqual(1, count)
コード例 #17
0
ファイル: test_parse.py プロジェクト: vasalvit/rpg_dices_bot
 def test_fail_for_count_with_sign(self):
     with self.assertRaises(InvalidFormat):
         _ = parse('+2d4+6')
コード例 #18
0
ファイル: test_parse.py プロジェクト: vasalvit/rpg_dices_bot
 def test_success_without_modifier(self):
     (count, faces, modifier) = parse('2d4')
     self.assertEqual(2, count)
     self.assertEqual(4, faces)
     self.assertEqual(0, modifier)
コード例 #19
0
ファイル: test_parse.py プロジェクト: vasalvit/rpg_dices_bot
 def test_fail_for_negative_count(self):
     with self.assertRaises(InvalidFormat):
         _ = parse('-2d4+6')