|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require_relative "../spec_helper" |
| 4 | + |
| 5 | +# This spec validates that RBS runtime type checking catches actual type violations |
| 6 | +# when enabled during test execution. These tests should only run when RBS is available. |
| 7 | +RSpec.describe "RBS Runtime Type Checking", type: :rbs do |
| 8 | + before do |
| 9 | + skip "RBS gem not available" unless defined?(RBS) |
| 10 | + skip "RBS runtime checking disabled" if ENV["DISABLE_RBS_RUNTIME_CHECKING"] == "true" |
| 11 | + skip "RBS runtime hook not loaded" unless ENV.fetch("RUBYOPT", "").include?("-rrbs/test/setup") |
| 12 | + end |
| 13 | + |
| 14 | + describe "Configuration type checking" do |
| 15 | + it "catches invalid type assignments to configuration" do |
| 16 | + # This test verifies runtime checking actually works by intentionally |
| 17 | + # violating a type signature and expecting RBS to catch it |
| 18 | + expect do |
| 19 | + config = ReactOnRails::Configuration.new( |
| 20 | + server_bundle_js_file: 123 # Invalid: should be String, not Integer |
| 21 | + ) |
| 22 | + config.server_bundle_js_file # Access to trigger type check |
| 23 | + end.to raise_error(RBS::Test::Hook::TypeError) |
| 24 | + end |
| 25 | + |
| 26 | + it "allows valid type assignments to configuration" do |
| 27 | + # This validates that correct types pass through without error |
| 28 | + expect do |
| 29 | + config = ReactOnRails::Configuration.new( |
| 30 | + server_bundle_js_file: "valid-string.js" |
| 31 | + ) |
| 32 | + config.server_bundle_js_file |
| 33 | + end.not_to raise_error |
| 34 | + end |
| 35 | + end |
| 36 | + |
| 37 | + describe "Helper method type checking" do |
| 38 | + # Test that helper methods have their signatures validated |
| 39 | + # This ensures the RBS signatures in sig/ are being used |
| 40 | + it "has RBS signatures loaded for ReactOnRails::Helper" do |
| 41 | + # Verify the helper module has type signatures |
| 42 | + expect(ReactOnRails::Helper).to be_a(Module) |
| 43 | + |
| 44 | + # If RBS runtime checking is active, method calls will be wrapped |
| 45 | + # We can verify this by checking that invalid calls raise type errors |
| 46 | + # (implementation depends on specific helper method signatures) |
| 47 | + end |
| 48 | + end |
| 49 | + |
| 50 | + describe "RBS::Test environment" do |
| 51 | + it "has RBS_TEST_TARGET configured for ReactOnRails" do |
| 52 | + expect(ENV.fetch("RBS_TEST_TARGET", "")).to include("ReactOnRails") |
| 53 | + end |
| 54 | + |
| 55 | + it "has loaded RBS test setup" do |
| 56 | + # Verify the RBS test framework is active |
| 57 | + expect(defined?(RBS::Test::Hook)).to be_truthy |
| 58 | + end |
| 59 | + end |
| 60 | +end |
0 commit comments