def test_from_card_spec_malformed(self): fc = FlashCard.from_card_spec("") self.assertEqual(None, fc) fc = FlashCard.from_card_spec("front : back # abc") self.assertEqual(None, fc) fc = FlashCard.from_card_spec("front : back # 3 @ ab") self.assertEqual(None, fc) fc = FlashCard.from_card_spec("front : back # 123@") self.assertEqual(None, fc)
def test_from_card_spec_corner_cases(self): fc = FlashCard.from_card_spec("front:") self.assertEqual("front:", fc.front) self.assertEqual("", fc.back) fc = FlashCard.from_card_spec("front :") self.assertEqual("front :", fc.front) self.assertEqual("", fc.back) fc = FlashCard.from_card_spec("front : ") self.assertEqual("front", fc.front) self.assertEqual("", fc.back) fc = FlashCard.from_card_spec("front : back @ 123") self.assertEqual("front", fc.front) self.assertEqual("back @ 123", fc.back) fc = FlashCard.from_card_spec("front : back #") self.assertEqual("front", fc.front) self.assertEqual("back #", fc.back) self.assertEqual(0, fc.box) self.assertEqual(0, fc.timestamp) fc = FlashCard.from_card_spec("front : back #123") self.assertEqual("front", fc.front) self.assertEqual("back #123", fc.back) self.assertEqual(0, fc.box) self.assertEqual(0, fc.timestamp) fc = FlashCard.from_card_spec("front : # 7 @ 4711") self.assertEqual(fc.front, "front") self.assertEqual(fc.back, "") self.assertEqual(fc.box, 7) self.assertEqual(fc.timestamp, 4711)
def test_from_card_spec_happy_path(self): fc = FlashCard.from_card_spec("front") self.assertEqual("front", fc.front) self.assertEqual("", fc.back) self.assertEqual(0, fc.box) self.assertEqual(0, fc.timestamp) fc = FlashCard.from_card_spec("front : back") self.assertEqual("front", fc.front) self.assertEqual("back", fc.back) self.assertEqual(0, fc.box) self.assertEqual(0, fc.timestamp) fc = FlashCard.from_card_spec("front : back # 3") self.assertEqual("front", fc.front) self.assertEqual("back", fc.back) self.assertEqual(3, fc.box) self.assertEqual(0, fc.timestamp) fc = FlashCard.from_card_spec("front : back # 3 @ 1000") self.assertEqual("front", fc.front) self.assertEqual("back", fc.back) self.assertEqual(3, fc.box) self.assertEqual(1000, fc.timestamp)
def load_from_specs(self, card_specs): for line in card_specs: card_spec = line.rstrip() # Handle empty lines and comments. if not card_spec or card_spec.startswith(Deck.comment_leader): self.deckfile_lines.append(card_spec) else: card = FlashCard.from_card_spec(card_spec) if card: if 0 <= card.box <= self.max_box_num: self.boxes[card.box].append(card) else: raise Deck.CardSpecError("Box number out of range: " + card_spec) self.deckfile_lines.append(card) else: raise Deck.CardSpecError("Malformed card spec: " + card_spec)