File: module_language/import_declaration_test.rb

Overview
Module Structure
Class Hierarchy
Code

Overview

Module Structure

  module: <Toplevel Module>
  module: Umu#6
  module: Test#8
  module: Grammar#10
  module: ModuleLanguage#12
  module: Declaration#14
  class: ImportTest#16
inherits from
  Test ( Minitest )
has properties
method: setup #43
method: test_value #48
method: test_function #61
method: test_structure #74
method: test_identifier_value_should_be_a_struct_type #93
method: test_single_field #104
method: test_some_fields #117
method: test_rename #134
method: test_single_group_field_single_ident #151
method: test_single_group_field_some_idents #164
method: test_some_group_fields #181

Code

   1  # frozen_string_literal: true
   2 
   3  require "test_helper"
   4 
   5 
   6  module Umu
   7 
   8  module Test
   9 
  10  module Grammar
  11 
  12  module ModuleLanguage
  13 
  14  module Declaration
  15 
  16  class ImportTest < Minitest::Test
  17  =begin
  18  <md-structure-declaration> ::=
  19      IMPORT <me-long-id> [
  20          "{"
  21              { <md-import-field> }
  22          "}"
  23      ]
  24      ;
  25 
  26  <md-import-field> ::=
  27      VAL       <md-import-field-contents>
  28    | FUN       <md-import-field-contents>
  29    | STRUCTURE <md-import-field-contents>
  30    ;
  31 
  32  <md-import-field-contents> ::=
  33      <md-atomic-import-field>
  34    | "("
  35          <md-atomic-import-field>
  36          { "," <md-atomic-import-field> }
  37      ")"
  38    ;
  39 
  40  <md-atomic-import-field> ::=
  41      <variable-pattern> [ "=" <me-long-id> ] ;
  42  =end
  43      def setup
  44          @interp = Api.setup_interpreter
  45      end
  46 
  47 
  48      def test_value
  49          interp = Api.eval_decls @interp, <<-EOS
  50              structure M = struct { val x = 3 }
  51 
  52              import M
  53              EOS
  54 
  55          value = Api.eval_expr interp, "x"
  56          assert_instance_of VCAN::Int, value
  57          assert_equal       3,         value.val
  58      end
  59 
  60 
  61      def test_function
  62          interp = Api.eval_decls @interp, <<-EOS
  63              structure M = struct { fun add = x y -> x + y }
  64 
  65              import M
  66              EOS
  67 
  68          value = Api.eval_expr interp, "add 3 4"
  69          assert_instance_of VCAN::Int, value
  70          assert_equal       7,         value.val
  71      end
  72 
  73 
  74      def test_structure
  75          interp = Api.eval_decls @interp, <<-EOS
  76              structure M = struct {
  77                  structure N = struct { val x = 3 }
  78              }
  79 
  80              import M
  81              EOS
  82 
  83          value = Api.eval_expr interp, "N::x"
  84          assert_instance_of VCAN::Int, value
  85          assert_equal       3,         value.val
  86 
  87          assert_raises(X::NameError) do
  88              Api.eval_expr interp, "x"
  89          end
  90      end
  91 
  92 
  93      def test_identifier_value_should_be_a_struct_type
  94          assert_raises(X::TypeError) do
  95              Api.eval_decls @interp, <<-EOS
  96                  val M = 1
  97 
  98                  import M
  99                  EOS
 100          end
 101      end
 102 
 103 
 104      def test_single_field
 105          interp = Api.eval_decls @interp, <<-EOS
 106              structure M = struct { val x = 3 }
 107 
 108              import M { val x }
 109              EOS
 110 
 111          value = Api.eval_expr interp, "x"
 112          assert_instance_of VCAN::Int, value
 113          assert_equal       3,         value.val
 114      end
 115 
 116 
 117      def test_some_fields
 118          interp = Api.eval_decls @interp, <<-EOS
 119              structure M = struct { val x = 3 val y = 4}
 120 
 121              import M { val x val y }
 122              EOS
 123 
 124          value_1 = Api.eval_expr interp, "x"
 125          assert_instance_of VCAN::Int, value_1
 126          assert_equal       3,         value_1.val
 127 
 128          value_2 = Api.eval_expr interp, "y"
 129          assert_instance_of VCAN::Int, value_2
 130          assert_equal       4,         value_2.val
 131      end
 132 
 133 
 134      def test_rename
 135          interp = Api.eval_decls @interp, <<-EOS
 136              structure M = struct { val x = 3 }
 137 
 138              import M { val x' = x }
 139              EOS
 140 
 141          value = Api.eval_expr interp, "x'"
 142          assert_instance_of VCAN::Int, value
 143          assert_equal       3,         value.val
 144 
 145          assert_raises(X::NameError) do
 146              Api.eval_expr interp, "x"
 147          end
 148      end
 149 
 150 
 151      def test_single_group_field_single_ident
 152          interp = Api.eval_decls @interp, <<-EOS
 153              structure M = struct { val x = 3 }
 154 
 155              import M { val (x) }
 156              EOS
 157 
 158          value = Api.eval_expr interp, "x"
 159          assert_instance_of VCAN::Int, value
 160          assert_equal       3,         value.val
 161      end
 162 
 163 
 164      def test_single_group_field_some_idents
 165          interp = Api.eval_decls @interp, <<-EOS
 166              structure M = struct { val x = 3 val y = 4}
 167 
 168              import M { val (x, y) }
 169              EOS
 170 
 171          value_1 = Api.eval_expr interp, "x"
 172          assert_instance_of VCAN::Int, value_1
 173          assert_equal       3,         value_1.val
 174 
 175          value_2 = Api.eval_expr interp, "y"
 176          assert_instance_of VCAN::Int, value_2
 177          assert_equal       4,         value_2.val
 178      end
 179 
 180 
 181      def test_some_group_fields
 182          interp = Api.eval_decls @interp, <<-EOS
 183              structure M = struct { val x = 3 val y = 4 val z = 5}
 184 
 185              import M { val (x) val (y, z) }
 186              EOS
 187 
 188          value_1 = Api.eval_expr interp, "x"
 189          assert_instance_of VCAN::Int, value_1
 190          assert_equal       3,         value_1.val
 191 
 192          value_2 = Api.eval_expr interp, "y"
 193          assert_instance_of VCAN::Int, value_2
 194          assert_equal       4,         value_2.val
 195 
 196          value_3 = Api.eval_expr interp, "z"
 197          assert_instance_of VCAN::Int, value_3
 198          assert_equal       5,         value_3.val
 199      end
 200  end
 201 
 202  end # Umu::Test::Grammar::ModuleLanguage::Declaration
 203 
 204  end # Umu::Test::Grammar::ModuleLanguage
 205 
 206  end # Umu::Test::Grammar
 207 
 208  end # Umu::Test
 209 
 210  end # Umu