def test_sum_template_transform(self): values = self.sum_template.transform({ 'get_value': 1, 'get_nonexisting_value': 0, }) self.maxDiff = None self.assertEqual(values, { 'base': list(parse(""" (do (defn agg_sub [tally value] (- tally value)) (defn agg_add [tally value] (+ tally value)) (defn agg_trans [value] value)) """))[0], 'handle_change': list(parse(""" (-> tally (agg_sub (agg_trans old_value)) (agg_add (agg_trans new_value))) """))[0], 'get_value': 1, 'get_nonexisting_value': 0, 'get_tally': 0, })
def test_parse_spread(self): self.assertEqual( list(parse('[&foo 1 2 3 &bar 4 5 6 &baz]'))[0], [ KW('into_list'), KW('foo'), [KW('list'), 1, 2, 3], KW('bar'), [KW('list'), 4, 5, 6], KW('baz'), ], )
def test_parse_operators(self): self.assertEqual( list(parse('* / + - = != <= >= < > ->')), [ KW('*'), KW('/'), KW('+'), KW('-'), KW('='), KW('!='), KW('<='), KW('>='), KW('<'), KW('>'), KW('->'), ], )
AGGREGATE_TEMPLATE = list(parse(""" (do (def agg_base '(do (defn agg_sub [tally value] (unquote sub)) (defn agg_add [tally value] (unquote add)) (defn agg_trans [value] (unquote (if (def? transform) transform 'value))))) (if (def? base) (def agg_base (cat '(do (unquote base)) (slice agg_base 1 null)))) (def res #{ 'base agg_base 'handle_change '(-> tally (agg_sub (agg_trans old_value)) (agg_add (agg_trans new_value)))}) (for key [ 'get_tally 'get_value 'get_nonexisting_value 'filter_value 'get_group] (if (eval '(def? (unquote key))) (put res key (eval key)))) res) """))[0]
def test_parse_string(self): self.assertEqual(next(parse('"foobar\\"\\n\\t\\r"')), 'foobar"\n\t\r')
def test_parse_float(self): self.assertEqual(next(parse('0.123')), 0.123)
def test_unexpected_eof(self): with self.assertRaises(ValueError) as cm: list(parse('(')) self.assertEqual(str(cm.exception), 'Unexpected EOF')
def test_unexpected_closing_paren(self): with self.assertRaises(ValueError) as cm: list(parse(')')) self.assertEqual(str(cm.exception), 'Unexpected )')
def test_final_error(self): with self.assertRaises(ValueError) as cm: list(parse('foo %')) self.assertEqual(str(cm.exception), 'Invalid body: %')
def test_intermediate_error(self): with self.assertRaises(ValueError) as cm: list(parse('% foo')) self.assertEqual(str(cm.exception), 'Invalid body: %')
def test_serialize(self): self.assertEqual(list(parse(serialize(body, many=True))), body)
def test_parse(self): self.assertEqual(list(parse(source)), body)
def test_parse_col_spread_without_expression(self): with self.assertRaises(ValueError) as cm: list(parse('[&]')) self.assertEqual(str(cm.exception), 'Expected expression to spread')
def test_parse_constants(self): self.assertEqual( list(parse('null true false')), [None, True, False], )