def test_get_value_string_is_defined_by_expression(self): self.bd.strings['name'] = 'string' exp = BibDataStringExpression(['this is a ', BibDataString(self.bd, 'name')]) self.bd.strings['exp'] = exp bds = BibDataString(self.bd, 'exp') self.assertEqual(bds.get_value(), 'this is a string')
def test_get_value_string_is_defined_by_expression(self): self.bd.strings['name'] = 'string' exp = BibDataStringExpression( ['this is a ', BibDataString(self.bd, 'name')]) self.bd.strings['exp'] = exp bds = BibDataString(self.bd, 'exp') self.assertEqual(bds.get_value(), 'this is a string')
def test_equations_are_equal_iif_same(self): a1 = BibDataString(self.bd, 'a') a2 = BibDataString(self.bd, 'a') exp = BibDataStringExpression([a1, self.bds, 'text']) self.assertEqual(exp, BibDataStringExpression([a2, self.bds, 'text'])) self.assertNotEqual(exp, BibDataStringExpression(['foo', self.bds, 'text'])) self.assertNotEqual(exp, 'foovaluetext')
def test_strings_are_equal_iif_name_is_equal(self): self.bd.strings['a'] = 'foo' self.bd.strings['b'] = 'foo' a1 = BibDataString(self.bd, 'a') a2 = BibDataString(self.bd, 'a') b = BibDataString(self.bd, 'b') self.assertEqual(a1, a2) self.assertNotEqual(a1, b) self.assertNotEqual(a1, b) self.assertNotEqual(a1, "foo")
def _init_expressions(self): """ Defines all parser expressions used internally. """ self._expr = BibtexExpression() # Handle string as BibDataString object self._expr.set_string_name_parse_action( lambda s, l, t: BibDataString(self.bib_database, t[0])) self._expr.set_string_expression_parse_action( lambda s, l, t: self._interpolate_string_expression(t)) # Add notice to logger self._expr.add_log_function(logger.debug) # Set actions self._expr.entry.addParseAction( lambda s, l, t: self._add_entry( t.get('EntryType'), t.get('Key'), t.get('Fields')) ) self._expr.implicit_comment.addParseAction( lambda s, l, t: self._add_comment(t[0]) ) self._expr.explicit_comment.addParseAction( lambda s, l, t: self._add_comment(t[0]) ) self._expr.preamble_decl.addParseAction( lambda s, l, t: self._add_preamble(t[0]) ) self._expr.string_def.addParseAction( lambda s, l, t: self._add_string(t['StringName'].name, t['StringValue']) )
def test_write_dependent_strings(self): bib_database = BibDatabase() bib_database.strings['title'] = 'Mr' expr = BibDataStringExpression( [BibDataString(bib_database, 'title'), 'Smith']) bib_database.strings['name'] = expr result = bibtexparser.dumps(bib_database) expected = '@string{title = {Mr}}\n\n@string{name = title # {Smith}}\n\n' self.assertEqual(result, expected)
def test_expand_string(self): bds = BibDataString(self.bd, 'name') self.bd.strings['name'] = 'value' self.assertEqual(BibDataString.expand_string('name'), 'name') self.assertEqual(BibDataString.expand_string(bds), 'value')
def test_get_value(self): bds = BibDataString(self.bd, 'name') self.bd.strings['name'] = 'value' self.assertEqual(bds.get_value(), 'value')
def test_raises_KeyError(self): bds = BibDataString(self.bd, 'name') with self.assertRaises(KeyError): bds.get_value()
def test_raises_KeyError(self): bds = BibDataString(self.bd, 'unknown') exp = BibDataStringExpression([bds, self.bds, 'text']) with self.assertRaises(KeyError): exp.get_value()
def setUp(self): self.bd = BibDatabase() self.bd.strings['name'] = 'value' self.bds = BibDataString(self.bd, 'name')
def test_name_is_lower(self): bds = BibDataString(self.bd, 'nAmE') self.assertTrue(bds.name.islower())