Module: RBS::UnitTest::TypeAssertions::ClassMethods

Defined in:
lib/rbs/unit_test/type_assertions.rb,
sig/unit_test/type_assertions.rbs

Constant Summary collapse

@@env_cache =

: Hash[Array[String], RBS::Environment]

{}

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#target[target_type, Definition]? (readonly)

Returns the value of attribute target.

Returns:



6
7
8
# File 'lib/rbs/unit_test/type_assertions.rb', line 6

def target
  @target
end

Instance Method Details

#builderDefinitionBuilder

Returns:



32
33
34
# File 'lib/rbs/unit_test/type_assertions.rb', line 32

def builder
  @builder ||= RBS::DefinitionBuilder.new(env: env)
end

#envEnvironment

Returns:



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rbs/unit_test/type_assertions.rb', line 20

def env
  @env = @@env_cache[@libs] ||=
    begin
      loader = RBS::EnvironmentLoader.new
      (@libs || []).each do |lib|
        loader.add(library: lib, version: nil)
      end

      RBS::Environment.from_loader(loader).resolve_type_names
    end
end

#library(*libs) ⇒ void

This method returns an undefined value.

library is to load RBS type definition of dependencies

The test or implementation already require dependencies, but the RBS type definition of the dependencies are also required for testing.

library "pathname", "securerandom"

Parameters:

  • (String)


76
77
78
79
80
81
82
83
84
# File 'sig/unit_test/type_assertions.rbs', line 76

def library(*libs)
  if @libs
    raise "Multiple #library calls are not allowed"
  end

  @libs = libs
  @env = nil
  @target = nil
end

#testing(type_or_string) ⇒ void

This method returns an undefined value.

testing is to tell the framework which class is being tested

testing "::String"
testing "::Array[Integer]"

testing "singleton(::Integer)"

Parameters:

  • target_type (String)


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'sig/unit_test/type_assertions.rbs', line 66

def testing(type_or_string)
  type = case type_or_string
         when String
           RBS::Parser.parse_type(type_or_string, variables: []) || raise
         else
           type_or_string
         end

  definition = case type
               when RBS::Types::ClassInstance
                 builder.build_instance(type.name)
               when RBS::Types::ClassSingleton
                 builder.build_singleton(type.name)
               else
                 raise "Test target should be class instance or class singleton: #{type}"
               end

  @target = [type, definition] #: [target_type, Definition]
end