|
| 1 | +#!/usr/bin/python3 |
| 2 | + |
| 3 | +"""Tests for README examples""" |
| 4 | + |
| 5 | +from unittest import TestCase |
| 6 | +from lambda_calculus import Variable, Abstraction, Application |
| 7 | +from lambda_calculus.terms import Term |
| 8 | +from lambda_calculus.visitors.normalisation import BetaNormalisingVisitor |
| 9 | + |
| 10 | + |
| 11 | +class ExampleTest(TestCase): |
| 12 | + """Test for README example""" |
| 13 | + |
| 14 | + term: Application[str] |
| 15 | + |
| 16 | + def setUp(self) -> None: |
| 17 | + """create a reference term""" |
| 18 | + term: Term[str] = Application(Variable("+"), Variable("x")) |
| 19 | + term = Application(term, Variable("y")) |
| 20 | + term = Abstraction("y", term) |
| 21 | + term = Abstraction("x", term) |
| 22 | + term = Application(term, Variable("y")) |
| 23 | + term = Application(term, Variable("3")) |
| 24 | + term = Abstraction("y", term) |
| 25 | + self.term = Application(term, Variable("4")) |
| 26 | + |
| 27 | + def test_nesting(self) -> None: |
| 28 | + """test nesting example""" |
| 29 | + term: Term[str] = Application(Variable("+"), Variable("x")) |
| 30 | + term = Application(term, Variable("y")) |
| 31 | + term = Abstraction("y", term) |
| 32 | + term = Abstraction("x", term) |
| 33 | + term = Application(term, Variable("y")) |
| 34 | + term = Application(term, Variable("3")) |
| 35 | + term = Abstraction("y", term) |
| 36 | + term = Application(term, Variable("4")) |
| 37 | + self.assertEqual(term, self.term) |
| 38 | + |
| 39 | + def test_utility_methods(self) -> None: |
| 40 | + """test utility method example""" |
| 41 | + x = Variable.with_valid_name("x") |
| 42 | + y = Variable.with_valid_name("y") |
| 43 | + term: Term[str] = Application.with_arguments(Variable.with_valid_name("+"), (x, y)) |
| 44 | + term = Abstraction.curried(("x", "y"), term) |
| 45 | + term = Application.with_arguments(term, (y, Variable.with_valid_name("3"))) |
| 46 | + term = Abstraction("y", term) |
| 47 | + term = Application(term, Variable.with_valid_name("4")) |
| 48 | + self.assertEqual(term, self.term) |
| 49 | + |
| 50 | + def test_method_chaining(self) -> None: |
| 51 | + """test method chaining example""" |
| 52 | + x = Variable.with_valid_name("x") |
| 53 | + y = Variable.with_valid_name("y") |
| 54 | + term = Variable("+") \ |
| 55 | + .apply_to(x, y) \ |
| 56 | + .abstract("x", "y") \ |
| 57 | + .apply_to(y, Variable("3")) \ |
| 58 | + .abstract("y") \ |
| 59 | + .apply_to(Variable("4")) |
| 60 | + self.assertEqual(term, self.term) |
| 61 | + |
| 62 | + def test_evaluation(self) -> None: |
| 63 | + """test evaluation example""" |
| 64 | + self.assertEqual( |
| 65 | + BetaNormalisingVisitor().skip_intermediate(self.term), |
| 66 | + Application.with_arguments( |
| 67 | + Variable("+"), |
| 68 | + (Variable("4"), Variable("3")) |
| 69 | + ) |
| 70 | + ) |
0 commit comments