Example #1
0
import tests.helpers as Helpers
from limp.standard_library.comparisons import *
from tests.syntax import *

t0 = Helpers.evaluation_fixture("test_conditionals", [
    (conditional((boolean(True), string("yes"))), "yes"),
    (conditional((boolean(True), string("aye")),
                 (boolean(True), string("no"))), "aye"),
    (conditional((boolean(False), string("aye")),
                 (boolean(True), string("no"))), "no"),
    (conditional((boolean(False), string("aye")),
                 (boolean(True), string("nah"))), "nah"),
    (conditional((invoke(GREATER_THAN, integer(0),
                         integer(10)), string("Are you dumb?")),
                 (boolean(True), string("You smart."))), "You smart."),
    (conditional((boolean(True), integer(0))), 0),
    (conditional((boolean(True), integer(1))), 1),
    (conditional((boolean(False), integer(0)),
                 (boolean(True), integer(1))), 1),
    (conditional(
        (invoke(ARE_EQUAL, integer(0), integer(1)), string("NotThisOne")),
        (invoke(ARE_EQUAL, integer(0), integer(2)), string("NorThisOne")),
        (invoke(ARE_EQUAL, integer(0), integer(0)), string("This one!")),
        (invoke(ARE_EQUAL, integer(0), integer(3)), string("NorNorThisOne"))),
     "This one!"),
])

t1 = Helpers.evaluation_fixture("test_default_return_values", [
    (conditional(
        (boolean(False), string("nope")), string("default")), "default"),
    (conditional(
Example #2
0
t0 = Helpers.evaluation_fixture("test", [
    (invoke(ADD, integer(1), integer(1)), 2),
    (invoke(ADD, integer(2), integer(3)), 5),
    (invoke(ADD, integer(1), integer(2), integer(3)), 6),
    (invoke(SUBTRACT, integer(1), integer(1)), 0),
    (invoke(SUBTRACT, integer(4), integer(2)), 2),
    (invoke(SUBTRACT, integer(9), integer(8), integer(7)), -6),
    (invoke(MULTIPLY, integer(2), integer(3)), 6),
    (invoke(MULTIPLY, integer(4), integer(1)), 4),
    (invoke(MULTIPLY, integer(2), integer(3), integer(4)), 24),
    (invoke(DIVIDE, integer(4), integer(2)), 2),
    (invoke(DIVIDE, integer(1), integer(2)), 0.5),
    (invoke(DIVIDE, integer(1), integer(2), integer(3)), ((1 / 2) / 3)),
    (invoke(INTEGER_DIVIDE, integer(1), integer(2)), 0),
    (invoke(INTEGER_DIVIDE, integer(5), integer(2)), 2),
    (invoke(INTEGER_DIVIDE, integer(99), integer(2), integer(2)),
     ((99 // 2) // 2)),
    (invoke(EXPONENT, integer(2), integer(3)), 8),
    (invoke(EXPONENT, integer(0), integer(0)), 1),
    (invoke(SQUARE_ROOT, integer(144)), 12),
    (invoke(SQUARE_ROOT, integer(0)), 0),
    (invoke(SQUARE_ROOT, integer(1)), 1),
    (invoke(FACTORIAL, integer(0)), 1),
    (invoke(FACTORIAL, integer(1)), 1),
    (invoke(FACTORIAL, integer(4)), 24),
    (invoke(FACTORIAL, integer(7)), 5040),
    (invoke(MODULO, integer(4), integer(4)), 0),
    (invoke(MODULO, integer(5), integer(6)), 5),
    (invoke(MODULO, integer(3), integer(1)), 0),
    (invoke(IS_DIVISOR, integer(3), integer(9)), True),
    (invoke(IS_DIVISOR, integer(3), integer(99)), True),
    (invoke(IS_DIVISOR, integer(2), integer(44)), True),
    (invoke(IS_DIVISOR, integer(2), integer(45)), False),
    (invoke(IS_DIVISOR, integer(44), integer(45)), False),
    (invoke(IS_EVEN, integer(0)), True),
    (invoke(IS_EVEN, integer(1)), False),
    (invoke(IS_EVEN, integer(2)), True),
    (invoke(IS_EVEN, integer(3)), False),
    (invoke(IS_EVEN, integer(4)), True),
    (invoke(IS_EVEN, integer(5)), False),
    (invoke(IS_ODD, integer(0)), False),
    (invoke(IS_ODD, integer(1)), True),
    (invoke(IS_ODD, integer(2)), False),
    (invoke(IS_ODD, integer(3)), True),
    (invoke(IS_ODD, integer(4)), False),
    (invoke(IS_ODD, integer(5)), True),
])
Example #3
0

SQUARE_ROOT = function(['n'], invoke(SQUARE_ROOT, 'n'))
SUBTRACT = function(['a', 'b'], invoke(SUBTRACT, 'a', 'b'))
SELF_REFERENCING_FUNCTION = function(
    ['n'],
    conditional(
        (invoke(GREATER_THAN, 'n', integer(0)),
         invoke(ADD, integer(1), invoke(self_reference(), invoke(SUBTRACT, 'n', integer(1))))),

        (boolean(True),
         integer(0))
    )
)


t0 = Helpers.evaluation_fixture("test_invoking_anonymous_functions", [
    (invoke(function([], integer(10))),        10),
    (invoke(function([], integer(20))),        20),
    (invoke(SQUARE_ROOT, integer(16)),         4),
    (invoke(SQUARE_ROOT, integer(9)),          3),
    (invoke(SUBTRACT, integer(5), integer(3)), 2),
])


t1 = Helpers.evaluation_fixture("test_self_referencing_functions", [
    (invoke(SELF_REFERENCING_FUNCTION, integer(0)), 0),
    (invoke(SELF_REFERENCING_FUNCTION, integer(1)), 1),
    (invoke(SELF_REFERENCING_FUNCTION, integer(2)), 2),
])
Example #4
0
import tests.helpers as Helpers
from tests.syntax import *
from limp.standard_library.booleans import *

_ = Helpers.evaluation_fixture("test", [
    (invoke(NOT, boolean(True)), False),
    (invoke(NOT, boolean(False)), True),
    (invoke(AND, boolean(True), boolean(True)), True),
    (invoke(AND, boolean(True), boolean(False)), False),
    (invoke(AND, boolean(False), boolean(True)), False),
    (invoke(AND, boolean(False), boolean(False)), False),
    (invoke(OR, boolean(True), boolean(True)), True),
    (invoke(OR, boolean(True), boolean(False)), True),
    (invoke(OR, boolean(False), boolean(True)), True),
    (invoke(OR, boolean(False), boolean(False)), False),
    (invoke(XOR, boolean(True), boolean(True)), False),
    (invoke(XOR, boolean(True), boolean(False)), True),
    (invoke(XOR, boolean(False), boolean(True)), True),
    (invoke(XOR, boolean(False), boolean(False)), False),
])
Example #5
0
import tests.helpers as Helpers
from tests.syntax import *

t0 = Helpers.evaluation_fixture("test", [
    (list_of(integer(1), integer(2), integer(3)), [1, 2, 3]),
    (list_of(string("celery"), string("man")), ["celery", "man"]),
])
Example #6
0
    EXCLUSIONS = [
        Syntax.STRING_DELIMITER,
        Syntax.ESCAPE_SEQUENCE,
    ]

    for ordinal in ORDINALS():
        character = chr(ordinal)
        if not character in EXCLUSIONS:
            source_code = string(character)
            yield assert_equal, character, limp.evaluate(source_code)


def test_miscellaneous_strings_remain_unmodified():
    data = [
        "[]",
        "()",
        "[ ]",
    ]
    for string_ in data:
        source_code = string(string_)
        yield assert_equal, string_, limp.evaluate(source_code)


t0 = Helpers.evaluation_fixture('test_escape_sequences_are_recognised', [
    (string('\\n'), "\n"),
    (string('hello\\nworld!'), "hello\nworld!"),
    (string('\\n\\n\\n'), "\n\n\n"),
    (string('\\\"'), "\""),
    (string('\\\\'), "\\"),
])
Example #7
0
t0 = Helpers.evaluation_fixture("test", [
    (invoke(CONCATENATE, string("foo"), string("bar")), "foobar"),
    (invoke(CONCATENATE, string("hello"), string(" "),
            string("there")), "hello there"),
    (invoke(STRIP, string(" x ")), "x"),
    (invoke(STRIP, string(" abc ")), "abc"),
    (invoke(STRIP, string(" x \t\n")), "x"),
    (invoke(STRIP, string("hello \n\t\n there")), "hello \n\t\n there"),
    (invoke(LENGTH, string("")), 0),
    (invoke(LENGTH, string("a")), 1),
    (invoke(LENGTH, string("ab")), 2),
    (invoke(LENGTH, string("abc")), 3),
    (invoke(LENGTH, string("tayne")), 5),
    (invoke(CONTAINS, string(""), string("")), True),
    (invoke(CONTAINS, string("a"), string("a")), True),
    (invoke(CONTAINS, string("abcde"), string("abc")), True),
    (invoke(CONTAINS, string("abcde"), string("bc")), True),
    (invoke(CONTAINS, string("car is red"), string("car")), True),
    (invoke(CONTAINS, string("b"), string("a")), False),
    (invoke(CONTAINS, string("b"), string("abacus")), False),
    (invoke(CONTAINS, string("php-programmers"),
            string("self-respect")), False),
    (invoke(EMPTY, string("")), True),
    (invoke(EMPTY, string(".")), False),
    (invoke(EMPTY, string("hello!")), False),
    (invoke(REPEAT, string(""), integer(10)), ""),
    (invoke(REPEAT, string("a"), integer(5)), "aaaaa"),
    (invoke(REPEAT, string("hey"), integer(3)), "heyheyhey"),
    (invoke(REPEAT, string("racecar"), integer(3)), "racecarracecarracecar"),
    (invoke(REVERSE, string("")), ""),
    (invoke(REVERSE, string("abc")), "cba"),
    (invoke(REVERSE, string("lol")), "lol"),
    (invoke(REVERSE, string("jesus")), "susej"),
    (invoke(LOWERCASE, string("abc")), "abc"),
    (invoke(LOWERCASE, string("AbC")), "abc"),
    (invoke(LOWERCASE, string("LIMP 2017")), "limp 2017"),
    (invoke(LOWERCASE, string("Byxor")), "byxor"),
    (invoke(UPPERCASE, string("abc")), "ABC"),
    (invoke(UPPERCASE, string("AbC")), "ABC"),
    (invoke(UPPERCASE, string("LIMP 2017")), "LIMP 2017"),
    (invoke(UPPERCASE, string("Byxor")), "BYXOR"),
    (invoke(SPLIT, string(" "), string("a b c")), ['a', 'b', 'c']),
    (invoke(SPLIT, string(", "),
            string("test, 1, 2, 3")), ['test', '1', '2', '3']),
    (invoke(SPLIT, string("-"),
            string("cars-the-movie")), ['cars', 'the', 'movie']),
    (invoke(JOIN, string(" "), list_of(string("1"), string("2"),
                                       string("3"))), "1 2 3"),
    (invoke(JOIN, string(" "), list_of(string("4"), string("5"),
                                       string("6"))), "4 5 6"),
    (invoke(JOIN, string("_::_"), list_of(string("tayne"),
                                          string("brain"))), "tayne_::_brain"),
    (invoke(JOIN, string(", "), list_of(integer(1), integer(2),
                                        integer(3))), "1, 2, 3"),
    (invoke(FIRST_ELEMENT, string("ruby da cherry")), "r"),
    (invoke(FIRST_ELEMENT, string("love my parents")), "l"),
    (invoke(FIRST_ELEMENT, string("2017 is alright")), "2"),
    (invoke(LAST_ELEMENT, string("hot chocolate")), "e"),
    (invoke(LAST_ELEMENT, string("soothes a sunday")), "y"),
    (invoke(LAST_ELEMENT, string("from monday blues")), "s"),
    (invoke(ALL_BUT_FIRST, string("You will")), "ou will"),
    (invoke(ALL_BUT_FIRST, string("improve with")), "mprove with"),
    (invoke(ALL_BUT_FIRST, string("the passage")), "he passage"),
    (invoke(ALL_BUT_FIRST, string("of time.")), "f time."),
    (invoke(ALL_BUT_LAST, string("Remember to")), "Remember t"),
    (invoke(ALL_BUT_LAST, string("surround yourself")), "surround yoursel"),
    (invoke(ALL_BUT_LAST, string("with everything")), "with everythin"),
    (invoke(ALL_BUT_LAST, string("that brings")), "that bring"),
    (invoke(ALL_BUT_LAST, string("you happiness,")), "you happiness"),
    (invoke(GET_ELEMENT, string("especially family"), integer(0)), "e"),
    (invoke(GET_ELEMENT, string("before the chance"), integer(2)), "f"),
    (invoke(GET_ELEMENT, string("is lost."), integer(3)), "l"),
    (invoke(MAP, DOUBLE_CHARACTER,
            string("hello again")), "hheelllloo  aaggaaiinn"),
    (invoke(FILTER, IS_UPPERCASE,
            string("look_at_THis_UGly_suPeR_Onion")), "__TH_UG_PR_O"),
])
Example #8
0
t0 = Helpers.evaluation_fixture("test", [
    (invoke(invoke(PARTIAL, ADD, integer(10)), integer(20)), 30),
    (invoke(invoke(PARTIAL, MAP, IS_EVEN),
            TEMPORARY_NUMBERS), [False, True, False, True]),
    (invoke(invoke(PARTIAL, ADD, integer(10), integer(1), integer(2)),
            integer(20)), 33),
    (invoke(
        invoke(
            PARTIAL, MAP,
            function([TEMPORARY_NUMBER],
                     invoke(MULTIPLY, TEMPORARY_NUMBER, integer(2)))),
        TEMPORARY_NUMBERS), [2, 4, 6, 8]),
    (invoke(
        invoke(
            PARTIAL, FILTER,
            function([TEMPORARY_NUMBER],
                     invoke(ARE_EQUAL, TEMPORARY_NUMBER, integer(1)))),
        TEMPORARY_NUMBERS), [1]),
    (invoke(
        CHAIN, integer(0),
        function([TEMPORARY_NUMBER], invoke(ADD, TEMPORARY_NUMBER,
                                            integer(10))),
        function([TEMPORARY_NUMBER],
                 invoke(INTEGER_DIVIDE, TEMPORARY_NUMBER, integer(2)))), 5),
    (invoke(
        CHAIN, TEMPORARY_NUMBERS,
        invoke(
            PARTIAL, MAP,
            function([TEMPORARY_NUMBER],
                     invoke(MULTIPLY, TEMPORARY_NUMBER, integer(2)))),
        invoke(
            PARTIAL, MAP,
            function([TEMPORARY_NUMBER],
                     invoke(ADD, TEMPORARY_NUMBER, integer(1)))),
        invoke(
            PARTIAL, MAP,
            function([TEMPORARY_NUMBER],
                     invoke(SUBTRACT, TEMPORARY_NUMBER, integer(1)))),
        invoke(
            PARTIAL, FILTER,
            function([TEMPORARY_NUMBER],
                     invoke(ARE_EQUAL,
                            invoke(MODULO, TEMPORARY_NUMBER, integer(4)),
                            integer(0)))), invoke(PARTIAL, REDUCE, ADD)), 12),
    (invoke(RANGE, integer(0)), []),
    (invoke(RANGE, integer(1)), [0]),
    (invoke(RANGE, integer(5)), [0, 1, 2, 3, 4]),
])
Example #9
0
t0 = Helpers.evaluation_fixture("test_numeric_types", [
    (integer(0), 0),
    (integer(10), 10),
    (integer(23), 23),
    (negative(integer(4)), -4),
    (positive(integer(4)), +4),
    (hexadecimal(0x0), 0x0),
    (hexadecimal(0x123), 0x123),
    (hexadecimal(0xDEADA55), 0xDEADA55),
    (negative(hexadecimal(0x1)), -0x1),
    (negative(hexadecimal(0xBEEF)), -0xBEEF),
    (positive(hexadecimal(0xB00B5)), +0xB00B5),
    (binary(0b0), 0b0),
    (binary(0b1), 0b1),
    (binary(0b101), 0b101),
    (negative(binary(0b1)), -0b1),
    (negative(binary(0b111)), -0b111),
    (positive(binary(0b10)), +0b10),
    (octal(0o0), 0o0),
    (octal(0o1), 0o1),
    (octal(0o4234), 0o4234),
    (negative(octal(0o1)), -0o1),
    (negative(octal(0o4536)), -0o4536),
    (positive(octal(0o42)), +0o42),
    (float_(0), 0.0),
    (float_(0.5), 0.5),
    (float_(2.3), 2.3),
    (float_(1.23456789), 1.23456789),
    (negative(float_(2.5)), -2.5),
    (negative(float_(60.12)), -60.12),
    (positive(float_(20.3)), +20.3),
])
Example #10
0
    address: {
        house-number: 420
        street: "earth street"
    }
    job: "professional door knocker"
}
'''

t0 = Helpers.evaluation_fixture("test_object_literals", [
    ('{}', {}),
    ('{age: 90}', {
        "age": 90
    }),
    (object_code, {
        "name": "Blarg Smith",
        "age": 1337,
        "interests": ["food", "limp", "THPS"],
        "address": {
            "house-number": 420,
            "street": "earth street"
        },
        "job": "professional door knocker"
    }),
])


def test_accessing_attributes():
    environment = Environment.create_standard()
    environment.define('object', limp.evaluate(object_code))

    data = [
        ('{age: 90}.age', 90),
Example #11
0
import tests.helpers as Helpers
from tests.syntax import *
from limp.standard_library.comparisons import *


_ = Helpers.evaluation_fixture("test", [
    (invoke(GREATER_THAN, integer(1), integer(0)),      True),
    (invoke(GREATER_THAN, integer(1), integer(1)),      False),
    (invoke(GREATER_THAN, integer(9999), integer(321)), True),
    (invoke(GREATER_THAN, integer(-10), integer(-20)),  True),

    (invoke(LESS_THAN, integer(10), integer(11)),    True),
    (invoke(LESS_THAN, integer(11), integer(11)),    False),
    (invoke(LESS_THAN, integer(12), integer(10)),    False),
    (invoke(LESS_THAN, integer(100), integer(4321)), True),
    (invoke(LESS_THAN, integer(-99), integer(-12)),  True),

    (invoke(GREATER_THAN_OR_EQUAL, integer(1), integer(0)),      True),
    (invoke(GREATER_THAN_OR_EQUAL, integer(1), integer(1)),      True),
    (invoke(GREATER_THAN_OR_EQUAL, integer(0), integer(1)),      False),
    (invoke(GREATER_THAN_OR_EQUAL, integer(9999), integer(321)), True),
    (invoke(GREATER_THAN_OR_EQUAL, integer(-10), integer(-20)), True),

    (invoke(LESS_THAN_OR_EQUAL, integer(10), integer(11)),    True),
    (invoke(LESS_THAN_OR_EQUAL, integer(11), integer(11)),    True),
    (invoke(LESS_THAN_OR_EQUAL, integer(12), integer(10)),    False),
    (invoke(LESS_THAN_OR_EQUAL, integer(100), integer(4321)), True),
    (invoke(LESS_THAN_OR_EQUAL, integer(-99), integer(-12)),  True),
])
Example #12
0
import tests.helpers as Helpers
from tests.syntax import *
from limp.standard_library.conversions import *

t0 = Helpers.evaluation_fixture("test", [
    (invoke(STRING, integer(123)), "123"),
    (invoke(STRING, float_(0.1)), "0.1"),
    (invoke(STRING, boolean(True)), "True"),
    (invoke(STRING, string("foo")), "foo"),
    (invoke(INTEGER, string("32")), 32),
    (invoke(INTEGER, string("-1")), -1),
    (invoke(INTEGER, integer(10)), 10),
    (invoke(FLOAT, string("5.2")), 5.2),
    (invoke(FLOAT, string("0.3")), 0.3),
    (invoke(FLOAT, float_(9.99)), 9.99),
    (invoke(BOOLEAN, string("false")), False),
    (invoke(BOOLEAN, string("true")), True),
    (invoke(BOOLEAN, integer(1)), True),
    (invoke(BOOLEAN, integer(0)), False),
    (invoke(BOOLEAN, boolean(True)), True),
    (invoke(BOOLEAN, boolean(False)), False),
])
Example #13
0
t0 = Helpers.evaluation_fixture("test", [
    (invoke(MAP, function([VARIABLE], invoke(MULTIPLY, VARIABLE, integer(2))),
            NUMBERS), [2, 4, 6, 8]),
    (invoke(MAP, function([VARIABLE], invoke(ADD, VARIABLE, integer(1))),
            NUMBERS), [2, 3, 4, 5]),
    (invoke(MAP, function([VARIABLE], invoke(DIVIDE, VARIABLE, integer(2))),
            list_of(integer(10), integer(20))), [5, 10]),
    (invoke(
        FILTER,
        function([VARIABLE],
                 invoke(ARE_EQUAL, invoke(MODULO, VARIABLE, integer(2)),
                        integer(0))), NUMBERS), [2, 4]),
    (invoke(
        FILTER,
        function([VARIABLE],
                 invoke(ARE_EQUAL, invoke(MODULO, VARIABLE, integer(2)),
                        integer(1))), NUMBERS), [1, 3]),
    (invoke(FILTER,
            function([VARIABLE], invoke(ARE_EQUAL, VARIABLE, integer(1))),
            NUMBERS), [1]),
    (invoke(REDUCE, ADD, NUMBERS), 10),
    (invoke(REDUCE, SUBTRACT, NUMBERS), -8),
    (invoke(GET_ELEMENT, NUMBERS, integer(0)), 1),
    (invoke(GET_ELEMENT, NUMBERS, integer(1)), 2),
    (invoke(GET_ELEMENT, NUMBERS, integer(2)), 3),
    (invoke(GET_ELEMENT, NUMBERS, integer(3)), 4),
    (invoke(GET_ELEMENT, STRINGS, integer(0)), "foo"),
    (invoke(GET_ELEMENT, STRINGS, integer(1)), "bar"),
    (invoke(GET_ELEMENT, STRINGS, integer(2)), "baz"),
    (invoke(APPEND_ELEMENT, EMPTY_LIST, integer(1)), [1]),
    (invoke(APPEND_ELEMENT, STRINGS,
            string("qux")), ["foo", "bar", "baz", "qux"]),
    (invoke(APPEND_ELEMENT, NUMBERS, integer(5)), [1, 2, 3, 4, 5]),
    (invoke(PREPEND_ELEMENT, EMPTY_LIST, integer(1)), [1]),
    (invoke(PREPEND_ELEMENT, STRINGS,
            string("hi")), ["hi", "foo", "bar", "baz"]),
    (invoke(PREPEND_ELEMENT, NUMBERS, integer(0)), [0, 1, 2, 3, 4]),
    (invoke(CONCATENATE, NUMBERS, NUMBERS), [1, 2, 3, 4, 1, 2, 3, 4]),
    (invoke(CONCATENATE, NUMBERS, integer(0), integer(0),
            integer(1)), [1, 2, 3, 4, 0, 0, 1]),
    (invoke(FIRST_ELEMENT, NUMBERS), 1),
    (invoke(FIRST_ELEMENT, STRINGS), "foo"),
    (invoke(LAST_ELEMENT, NUMBERS), 4),
    (invoke(LAST_ELEMENT, STRINGS), "baz"),
    (invoke(ALL_BUT_FIRST, NUMBERS), [2, 3, 4]),
    (invoke(ALL_BUT_FIRST, STRINGS), ["bar", "baz"]),
    (invoke(ALL_BUT_LAST, NUMBERS), [1, 2, 3]),
    (invoke(ALL_BUT_LAST, STRINGS), ["foo", "bar"]),
    (invoke(LENGTH, EMPTY_LIST), 0),
    (invoke(LENGTH, NUMBERS), 4),
    (invoke(LENGTH, STRINGS), 3),
    (invoke(CONTAINS, NUMBERS, integer(1)), True),
    (invoke(CONTAINS, NUMBERS, integer(2)), True),
    (invoke(CONTAINS, NUMBERS, integer(3)), True),
    (invoke(CONTAINS, NUMBERS, integer(4)), True),
    (invoke(CONTAINS, NUMBERS, integer(5)), False),
    (invoke(REVERSE, NUMBERS), [4, 3, 2, 1]),
    (invoke(REVERSE, STRINGS), ["baz", "bar", "foo"]),
    (invoke(EMPTY, NUMBERS), False),
    (invoke(EMPTY, STRINGS), False),
    (invoke(EMPTY, EMPTY_LIST), True),
])
Example #14
0
import tests.helpers as Helpers
import meta

t0 = Helpers.evaluation_fixture("test_that_version_number_is_accessible",
                                [("version", meta.VERSION)])