"Fossies" - the Fresh Open Source Software Archive

Member "ponyc-0.33.0/test/libponyc/scope.cc" (1 Nov 2019, 8097 Bytes) of package /linux/misc/ponyc-0.33.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 #include <gtest/gtest.h>
    2 #include <platform.h>
    3 
    4 #include <ast/ast.h>
    5 #include <pass/scope.h>
    6 #include <ast/stringtab.h>
    7 #include <pass/pass.h>
    8 #include <pkg/package.h>
    9 #include <pkg/use.h>
   10 
   11 #include "util.h"
   12 
   13 
   14 #define TEST_COMPILE(src) DO(test_compile(src, "scope"))
   15 #define TEST_ERROR(src) DO(test_error(src, "scope"))
   16 
   17 
   18 class ScopeTest : public PassTest
   19 {};
   20 
   21 
   22 TEST_F(ScopeTest, Actor)
   23 {
   24   const char* src = "actor Foo";
   25 
   26   TEST_COMPILE(src);
   27 
   28   ASSERT_ID(TK_ACTOR, lookup_type("Foo"));
   29   ASSERT_EQ(1, ref_count(package, "Foo"));
   30 }
   31 
   32 
   33 TEST_F(ScopeTest, Class)
   34 {
   35   const char* src = "class Foo";
   36 
   37   TEST_COMPILE(src);
   38 
   39   ASSERT_ID(TK_CLASS, lookup_type("Foo"));
   40   ASSERT_EQ(1, ref_count(package, "Foo"));
   41 }
   42 
   43 
   44 TEST_F(ScopeTest, Primitive)
   45 {
   46   const char* src = "primitive Foo";
   47 
   48   TEST_COMPILE(src);
   49 
   50   ASSERT_ID(TK_PRIMITIVE, lookup_type("Foo"));
   51   ASSERT_EQ(1, ref_count(package, "Foo"));
   52 }
   53 
   54 
   55 TEST_F(ScopeTest, Trait)
   56 {
   57   const char* src = "trait Foo";
   58 
   59   TEST_COMPILE(src);
   60 
   61   ASSERT_ID(TK_TRAIT, lookup_type("Foo"));
   62   ASSERT_EQ(1, ref_count(package, "Foo"));
   63 }
   64 
   65 
   66 TEST_F(ScopeTest, Interface)
   67 {
   68   const char* src = "interface Foo";
   69 
   70   TEST_COMPILE(src);
   71 
   72   ASSERT_ID(TK_INTERFACE, lookup_type("Foo"));
   73   ASSERT_EQ(1, ref_count(package, "Foo"));
   74 }
   75 
   76 
   77 TEST_F(ScopeTest, TypeAlias)
   78 {
   79   const char* src = "type Foo is Bar";
   80 
   81   TEST_COMPILE(src);
   82 
   83   ASSERT_ID(TK_TYPE, lookup_type("Foo"));
   84   ASSERT_EQ(1, ref_count(package, "Foo"));
   85 }
   86 
   87 
   88 TEST_F(ScopeTest, VarField)
   89 {
   90   const char* src = "class C var foo: U32";
   91 
   92   TEST_COMPILE(src);
   93 
   94   ASSERT_ID(TK_FVAR, lookup_member("C", "foo"));
   95   ASSERT_EQ(1, ref_count(package, "foo"));
   96 }
   97 
   98 
   99 TEST_F(ScopeTest, LetField)
  100 {
  101   const char* src = "class C let foo: U32";
  102 
  103   TEST_COMPILE(src);
  104 
  105   ASSERT_ID(TK_FLET, lookup_member("C", "foo"));
  106   ASSERT_EQ(1, ref_count(package, "foo"));
  107 }
  108 
  109 
  110 TEST_F(ScopeTest, Be)
  111 {
  112   const char* src = "actor A be foo() => None";
  113 
  114   TEST_COMPILE(src);
  115 
  116   ASSERT_ID(TK_BE, lookup_member("A", "foo"));
  117   ASSERT_EQ(1, ref_count(package, "foo"));
  118 }
  119 
  120 
  121 TEST_F(ScopeTest, New)
  122 {
  123   const char* src = "actor A new foo() => None";
  124 
  125   TEST_COMPILE(src);
  126 
  127   ASSERT_ID(TK_NEW, lookup_member("A", "foo"));
  128   ASSERT_EQ(1, ref_count(package, "foo"));
  129 }
  130 
  131 
  132 TEST_F(ScopeTest, Fun)
  133 {
  134   const char* src = "actor A fun foo() => None";
  135 
  136   TEST_COMPILE(src);
  137 
  138   ASSERT_ID(TK_FUN, lookup_member("A", "foo"));
  139   ASSERT_EQ(1, ref_count(package, "foo"));
  140 }
  141 
  142 
  143 TEST_F(ScopeTest, FunParam)
  144 {
  145   const char* src = "actor A fun foo(bar: U32) => None";
  146 
  147   TEST_COMPILE(src);
  148 
  149   ast_t* foo = lookup_member("A", "foo");
  150   ASSERT_ID(TK_FUN, foo);
  151   ASSERT_ID(TK_PARAM, lookup_in(foo, "bar"));
  152   ASSERT_EQ(1, ref_count(package, "bar"));
  153 }
  154 
  155 
  156 TEST_F(ScopeTest, TypeParam)
  157 {
  158   const char* src = "actor A fun foo[T]() => None";
  159 
  160   TEST_COMPILE(src);
  161 
  162   ast_t* foo = lookup_member("A", "foo");
  163   ASSERT_ID(TK_FUN, foo);
  164   ASSERT_ID(TK_TYPEPARAM, lookup_in(foo, "T"));
  165   ASSERT_EQ(1, ref_count(package, "T"));
  166 }
  167 
  168 
  169 TEST_F(ScopeTest, Local)
  170 {
  171   const char* src = "actor A fun foo() => var bar: U32 = 3";
  172 
  173   TEST_COMPILE(src);
  174 
  175   ast_t* foo = lookup_member("A", "foo");
  176   ASSERT_ID(TK_FUN, foo);
  177   ASSERT_ID(TK_VAR, lookup_in(foo, "bar"));
  178   ASSERT_EQ(1, ref_count(package, "bar"));
  179 }
  180 
  181 
  182 TEST_F(ScopeTest, MultipleLocals)
  183 {
  184   const char* src =
  185     "actor A\n"
  186     "  fun wombat() =>\n"
  187     "    (var foo: Type1, var bar: Type2, let aardvark: Type3)";
  188 
  189   TEST_COMPILE(src);
  190 
  191   ast_t* wombat = lookup_member("A", "wombat");
  192   ASSERT_ID(TK_FUN, wombat);
  193 
  194   ASSERT_ID(TK_VAR, lookup_in(wombat, "foo"));
  195   ASSERT_ID(TK_VAR, lookup_in(wombat, "bar"));
  196   ASSERT_ID(TK_LET, lookup_in(wombat, "aardvark"));
  197 
  198   ASSERT_EQ(1, ref_count(package, "foo"));
  199   ASSERT_EQ(1, ref_count(package, "bar"));
  200   ASSERT_EQ(1, ref_count(package, "aardvark"));
  201 }
  202 
  203 
  204 TEST_F(ScopeTest, NestedLocals)
  205 {
  206   const char* src =
  207     "actor A\n"
  208     "  fun wombat() =>"
  209     "    (var foo: Type1, (var bar: Type2, let aardvark: Type3))";
  210 
  211   TEST_COMPILE(src);
  212 
  213   ast_t* wombat = lookup_member("A", "wombat");
  214   ASSERT_ID(TK_FUN, wombat);
  215 
  216   ASSERT_ID(TK_VAR, lookup_in(wombat, "foo"));
  217   ASSERT_ID(TK_VAR, lookup_in(wombat, "bar"));
  218   ASSERT_ID(TK_LET, lookup_in(wombat, "aardvark"));
  219 
  220   ASSERT_EQ(1, ref_count(package, "foo"));
  221   ASSERT_EQ(1, ref_count(package, "bar"));
  222   ASSERT_EQ(1, ref_count(package, "aardvark"));
  223 }
  224 
  225 
  226 TEST_F(ScopeTest, SameScopeNameClash)
  227 {
  228   const char* src1 =
  229     "actor A\n"
  230     "  fun foo() =>\n"
  231     "    var bar1: U32 = 3\n"
  232     "    var bar2: U32 = 4";
  233 
  234   TEST_COMPILE(src1);
  235 
  236   const char* src2 =
  237     "actor A\n"
  238     "  fun foo() =>\n"
  239     "    var bar: U32 = 3\n"
  240     "    var bar: U32 = 4";
  241 
  242   TEST_ERROR(src2);
  243 }
  244 
  245 
  246 TEST_F(ScopeTest, ParentScopeNameClash)
  247 {
  248   const char* src1 =
  249     "actor A\n"
  250     "  fun foo(foo2:U32) => 3";
  251 
  252   TEST_COMPILE(src1);
  253 
  254   const char* src2 =
  255     "actor A\n"
  256     "  fun foo(foo:U32) => 3";
  257 
  258   TEST_ERROR(src2);
  259 }
  260 
  261 
  262 TEST_F(ScopeTest, SiblingScopeNoClash)
  263 {
  264   const char* src =
  265     "actor A1\n"
  266     "  fun foo(foo2:U32) => 3\n"
  267     "actor A2\n"
  268     "  fun foo(foo2:U32) => 3";
  269 
  270   TEST_COMPILE(src);
  271 }
  272 
  273 
  274 TEST_F(ScopeTest, Package)
  275 {
  276   const char* src = "actor A";
  277 
  278   DO(test_compile(src, "import"));
  279 
  280   // Builtin types go in the module symbol table
  281   ASSERT_ID(TK_PRIMITIVE, lookup_in(module, "U32"));
  282   ASSERT_EQ(1, ref_count(package, "U32"));
  283 }
  284 
  285 
  286 TEST_F(ScopeTest, CanShadowDontcare)
  287 {
  288   const char* src =
  289     "actor A\n"
  290     "  fun foo() =>\n"
  291     "    var _: None\n"
  292     "    var _: None";
  293 
  294   TEST_COMPILE(src);
  295 }
  296 
  297 
  298 TEST_F(ScopeTest, MethodOverloading)
  299 {
  300   const char* src =
  301     "actor A\n"
  302     "  fun foo() => None\n"
  303     "  fun foo(a: None) => None";
  304 
  305   TEST_ERROR(src);
  306 }
  307 
  308 
  309 /*
  310 TEST_F(ScopeTest, Use)
  311 {
  312   const char* src =
  313     "use \n"
  314     "actor A";
  315 
  316   TEST_COMPILE(src);
  317 
  318   // Builtin types go in the module symbol table
  319   WALK_TREE(module);
  320   LOOKUP("U32", TK_PRIMITIVE);
  321 
  322   ASSERT_EQ(1, ref_count(package, "U32"));
  323 }
  324 
  325 
  326 TEST_F(ScopeTest, Use)
  327 {
  328   const char* tree =
  329     "(program{scope} (package{scope} (module{scope}"
  330     "  (use{def start} x \"test\" x))))";
  331 
  332   const char* used_package =
  333     "class Foo";
  334 
  335   package_add_magic("test", used_package);
  336 
  337   DO(build(tree));
  338   ASSERT_EQ(AST_OK, run_scope());
  339 
  340   // Use imported types go in the module symbol table
  341   ast_t* module = find_sub_tree(ast, TK_MODULE);
  342   symtab_t* module_symtab = ast_get_symtab(module);
  343   ASSERT_NE((void*)NULL, symtab_find(module_symtab, stringtab("Foo"), NULL));
  344 }
  345 
  346 
  347 TEST_F(ScopeTest, UseAs)
  348 {
  349   const char* tree =
  350     "(program{scope} (package{scope} (module{scope}"
  351     "  (use{def start} (id bar) \"test\" x))))";
  352 
  353   const char* used_package =
  354     "class Foo";
  355 
  356   package_add_magic("test", used_package);
  357 
  358   DO(build(tree));
  359   ASSERT_EQ(AST_OK, run_scope());
  360 
  361   // Use imported types go in the module symbol table
  362   ast_t* module = find_sub_tree(ast, TK_MODULE);
  363   symtab_t* module_symtab = ast_get_symtab(module);
  364   ASSERT_NE((void*)NULL, symtab_find(module_symtab, stringtab("bar"), NULL));
  365   ASSERT_EQ((void*)NULL, symtab_find(module_symtab, stringtab("Foo"), NULL));
  366 }
  367 
  368 
  369 TEST_F(ScopeTest, UseConditionTrue)
  370 {
  371   const char* tree =
  372     "(program{scope} (package{scope} (module{scope}"
  373     "  (use x \"test\" (reference (id debug))))))";
  374 
  375   const char* used_package =
  376     "class Foo";
  377 
  378   package_add_magic("test", used_package);
  379 
  380   DO(build(tree));
  381   ASSERT_EQ(AST_OK, run_scope());
  382 
  383   // Use imported types go in the module symbol table
  384   ast_t* module = find_sub_tree(ast, TK_MODULE);
  385   symtab_t* module_symtab = ast_get_symtab(module);
  386   ASSERT_NE((void*)NULL, symtab_find(module_symtab, stringtab("Foo"), NULL));
  387 }
  388 
  389 
  390 TEST_F(ScopeTest, UseConditionFalse)
  391 {
  392   const char* tree =
  393     "(program{scope} (package{scope} (module{scope}"
  394     "  (use x \"test\" (call x x (. (reference (id debug)) (id op_not)))))))";
  395 
  396   const char* used_package =
  397     "class Foo";
  398 
  399   package_add_magic("test", used_package);
  400 
  401   DO(build(tree));
  402   ASSERT_EQ(AST_OK, run_scope());
  403 
  404   // Nothing should be imported
  405   ast_t* module = find_sub_tree(ast, TK_MODULE);
  406   symtab_t* module_symtab = ast_get_symtab(module);
  407   ASSERT_EQ((void*)NULL, symtab_find(module_symtab, stringtab("Foo"), NULL));
  408 }
  409 */