|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +RSpec.describe Triangulum::Validation do |
| 4 | + let(:data_types) { default_data_types } |
| 5 | + let(:functions) { default_functions } |
| 6 | + |
| 7 | + describe '#validate' do |
| 8 | + it 'validates a simple valid flow' do |
| 9 | + flow = build_flow( |
| 10 | + node_functions: [ |
| 11 | + node( |
| 12 | + id: 1, |
| 13 | + function_id: 'std::math::add', |
| 14 | + parameters: [ |
| 15 | + param(id: 1, runtime_parameter_id: 'a', value: literal_value(1)), |
| 16 | + param(id: 2, runtime_parameter_id: 'b', value: literal_value(0)) |
| 17 | + ] |
| 18 | + ) |
| 19 | + ] |
| 20 | + ) |
| 21 | + |
| 22 | + result = described_class.new(flow, functions, data_types).validate |
| 23 | + |
| 24 | + expect(result.valid?).to be true |
| 25 | + expect(result.diagnostics).to be_empty |
| 26 | + end |
| 27 | + |
| 28 | + it 'detects type errors in parameters' do |
| 29 | + flow = build_flow( |
| 30 | + node_functions: [ |
| 31 | + node( |
| 32 | + id: 1, |
| 33 | + function_id: 'std::math::add', |
| 34 | + parameters: [ |
| 35 | + param(id: 1, runtime_parameter_id: 'a', value: literal_value('not a number')), |
| 36 | + param(id: 2, runtime_parameter_id: 'b', value: literal_value(10)) |
| 37 | + ] |
| 38 | + ) |
| 39 | + ] |
| 40 | + ) |
| 41 | + |
| 42 | + result = described_class.new(flow, functions, data_types).validate |
| 43 | + |
| 44 | + expect(result.valid?).to be false |
| 45 | + expect(result.diagnostics).not_to be_empty |
| 46 | + expect(result.diagnostics.first.message).to include('number') |
| 47 | + end |
| 48 | + |
| 49 | + it 'validates a flow with references between nodes' do |
| 50 | + flow = build_flow( |
| 51 | + node_functions: [ |
| 52 | + node( |
| 53 | + id: 1, |
| 54 | + function_id: 'std::math::add', |
| 55 | + next_node_id: 2, |
| 56 | + parameters: [ |
| 57 | + param(id: 1, runtime_parameter_id: 'a', value: literal_value(1)), |
| 58 | + param(id: 2, runtime_parameter_id: 'b', value: literal_value(2)) |
| 59 | + ] |
| 60 | + ), |
| 61 | + node( |
| 62 | + id: 2, |
| 63 | + function_id: 'std::math::add', |
| 64 | + parameters: [ |
| 65 | + param(id: 3, runtime_parameter_id: 'a', value: reference_node(node_id: 1)), |
| 66 | + param(id: 4, runtime_parameter_id: 'b', value: literal_value(3)) |
| 67 | + ] |
| 68 | + ) |
| 69 | + ] |
| 70 | + ) |
| 71 | + |
| 72 | + result = described_class.new(flow, functions, data_types).validate |
| 73 | + |
| 74 | + expect(result.valid?).to be true |
| 75 | + expect(result.diagnostics).to be_empty |
| 76 | + end |
| 77 | + |
| 78 | + it 'validates a flow with nested scopes' do |
| 79 | + flow = build_flow( |
| 80 | + node_functions: [ |
| 81 | + node( |
| 82 | + id: 1, |
| 83 | + function_id: 'std::control::for_each', |
| 84 | + parameters: [ |
| 85 | + param(id: 1, runtime_parameter_id: 'list', value: literal_value([1, 2, 3])), |
| 86 | + param(id: 2, runtime_parameter_id: 'consumer', value: node_function_value(2)) |
| 87 | + ] |
| 88 | + ), |
| 89 | + node( |
| 90 | + id: 2, |
| 91 | + function_id: 'std::math::add', |
| 92 | + parameters: [ |
| 93 | + param(id: 3, runtime_parameter_id: 'a', value: literal_value(1)), |
| 94 | + param(id: 4, runtime_parameter_id: 'b', value: literal_value(2)) |
| 95 | + ] |
| 96 | + ) |
| 97 | + ] |
| 98 | + ) |
| 99 | + |
| 100 | + result = described_class.new(flow, functions, data_types).validate |
| 101 | + |
| 102 | + expect(result.valid?).to be true |
| 103 | + end |
| 104 | + |
| 105 | + it 'returns diagnostics with node_id and parameter_index' do |
| 106 | + flow = build_flow( |
| 107 | + node_functions: [ |
| 108 | + node( |
| 109 | + id: 1, |
| 110 | + function_id: 'std::math::add', |
| 111 | + parameters: [ |
| 112 | + param(id: 1, runtime_parameter_id: 'a', value: literal_value('string')), |
| 113 | + param(id: 2, runtime_parameter_id: 'b', value: literal_value(10)) |
| 114 | + ] |
| 115 | + ) |
| 116 | + ] |
| 117 | + ) |
| 118 | + |
| 119 | + result = described_class.new(flow, functions, data_types).validate |
| 120 | + |
| 121 | + expect(result.valid?).to be false |
| 122 | + diagnostic = result.diagnostics.find { |d| d.parameter_index == 0 } |
| 123 | + expect(diagnostic).not_to be_nil |
| 124 | + expect(diagnostic.node_id).not_to be_nil |
| 125 | + end |
| 126 | + |
| 127 | + it 'returns the return type' do |
| 128 | + flow = build_flow( |
| 129 | + node_functions: [ |
| 130 | + node( |
| 131 | + id: 1, |
| 132 | + function_id: 'std::math::add', |
| 133 | + parameters: [ |
| 134 | + param(id: 1, runtime_parameter_id: 'a', value: literal_value(1)), |
| 135 | + param(id: 2, runtime_parameter_id: 'b', value: literal_value(2)) |
| 136 | + ] |
| 137 | + ) |
| 138 | + ] |
| 139 | + ) |
| 140 | + |
| 141 | + result = described_class.new(flow, functions, data_types).validate |
| 142 | + |
| 143 | + expect(result.return_type).to eq('void') |
| 144 | + end |
| 145 | + end |
| 146 | +end |
0 commit comments