From 78f6301e272d8664e1261e57f20026e11d5261bd Mon Sep 17 00:00:00 2001 From: Nicolas Dandrimont Date: Thu, 9 Jan 2014 03:34:29 +0100 Subject: [PATCH] Add tests for the attribute access DSL --- tests/compilers/test_ast.py | 11 +++++++++++ tests/native_tests/language.hy | 20 ++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/tests/compilers/test_ast.py b/tests/compilers/test_ast.py index 716f2bb..e13cf53 100644 --- a/tests/compilers/test_ast.py +++ b/tests/compilers/test_ast.py @@ -464,3 +464,14 @@ def test_for_compile_error(): assert(e.message == "`for' requires a body to evaluate") else: assert(False) + + +def test_attribute_access(): + """Ensure attribute access compiles correctly""" + can_compile("(. foo bar baz)") + can_compile("(. foo [bar] baz)") + can_compile("(. foo bar [baz] [0] quux [frob])") + can_compile("(. foo bar [(+ 1 2 3 4)] quux [frob])") + cant_compile("(. foo bar :baz [0] quux [frob])") + cant_compile("(. foo bar baz (0) quux [frob])") + cant_compile("(. foo bar baz [0] quux {frob})") diff --git a/tests/native_tests/language.hy b/tests/native_tests/language.hy index b188811..f116233 100644 --- a/tests/native_tests/language.hy +++ b/tests/native_tests/language.hy @@ -888,3 +888,23 @@ (setv stdout (.getvalue sys.stdout)) (setv sys.stdout prev-stdout) (assert (= stdout "leaky()\nleaky()\nmacros()\n"))) + + +(defn test-attribute-access [] + "NATIVE: Test the attribute access DSL" + (defclass mycls [object]) + + (setv foo [(mycls) (mycls) (mycls)]) + (assert (is (. foo) foo)) + (assert (is (. foo [0]) (get foo 0))) + (assert (is (. foo [0] --class--) mycls)) + (assert (is (. foo [1] --class--) mycls)) + (assert (is (. foo [(+ 1 1)] --class--) mycls)) + (assert (= (. foo [(+ 1 1)] --class-- --name-- [0]) "m")) + (assert (= (. foo [(+ 1 1)] --class-- --name-- [1]) "y")) + + (setv bar (mycls)) + (setv (. foo [1]) bar) + (assert (is bar (get foo 1))) + (setv (. foo [1] test) "hello") + (assert (= (getattr (. foo [1]) "test") "hello")))