|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "interscript/isc" |
| 4 | + |
| 5 | +RSpec.describe "ISC ↔ YAML round-trip", type: :integration do |
| 6 | + def parse_isc(src, filename = "test.isc") |
| 7 | + tree = Interscript::Isc::Parser.parse(src, filename: filename) |
| 8 | + Interscript::Isc::DocumentBuilder.build(tree, filename: filename) |
| 9 | + end |
| 10 | + |
| 11 | + def round_trip(doc_hash) |
| 12 | + yaml = Interscript::Isc::YamlBridge.to_yaml(doc_hash) |
| 13 | + doc_back = Interscript::Isc::YamlBridge.from_yaml(yaml) |
| 14 | + isc = Interscript::Isc::Serializer.serialize(doc_back) |
| 15 | + parse_isc(isc, "round-trip.isc") |
| 16 | + end |
| 17 | + |
| 18 | + def comparable(hash) |
| 19 | + { |
| 20 | + system_code: hash[:systemCode], |
| 21 | + test_count: hash[:tests]&.size || 0, |
| 22 | + stage_count: hash[:stages]&.size || 0, |
| 23 | + } |
| 24 | + end |
| 25 | + |
| 26 | + it "round-trips a minimal map" do |
| 27 | + src = <<~ISC |
| 28 | + system "TEST:eng-Latn:Latn:2026" { |
| 29 | +
|
| 30 | + metadata { |
| 31 | + authority_id test |
| 32 | + name "Test Map" |
| 33 | + } |
| 34 | +
|
| 35 | + tests { |
| 36 | + "hello" -> "world" |
| 37 | + } |
| 38 | +
|
| 39 | + stage main { |
| 40 | + parallel { |
| 41 | + sub "a" "b" |
| 42 | + sub "c" "d" |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + ISC |
| 47 | + doc1 = parse_isc(src) |
| 48 | + doc2 = round_trip(doc1) |
| 49 | + expect(comparable(doc1)).to eq(comparable(doc2)) |
| 50 | + expect(doc2[:tests].size).to eq(1) |
| 51 | + expect(doc2[:stages].first[:body].first[:rules].size).to eq(2) |
| 52 | + end |
| 53 | + |
| 54 | + it "preserves StringValue items" do |
| 55 | + src = %(system "T:e-L:Latn:1" {\nstage main {\nsub "abc" "def"\n}\n}) |
| 56 | + doc1 = parse_isc(src) |
| 57 | + doc2 = round_trip(doc1) |
| 58 | + rule = doc2[:stages].first[:body].first[:rule] |
| 59 | + expect(rule[:from]).to be_a(Interscript::Isc::Items::StringValue) |
| 60 | + expect(rule[:from].value).to eq("abc") |
| 61 | + end |
| 62 | + |
| 63 | + it "preserves Set items" do |
| 64 | + src = %(system "T:e-L:Latn:1" {\nstage main {\nsub any("abc") "x"\n}\n}) |
| 65 | + doc1 = parse_isc(src) |
| 66 | + doc2 = round_trip(doc1) |
| 67 | + rule = doc2[:stages].first[:body].first[:rule] |
| 68 | + expect(rule[:from]).to be_a(Interscript::Isc::Items::Set) |
| 69 | + expect(rule[:from].chars).to eq(%w[a b c]) |
| 70 | + end |
| 71 | + |
| 72 | + it "preserves Capture and CaptureRef items" do |
| 73 | + src = %(system "T:e-L:Latn:1" {\nstage main {\nsub capture("x") ref(1)\n}\n}) |
| 74 | + doc1 = parse_isc(src) |
| 75 | + doc2 = round_trip(doc1) |
| 76 | + rule = doc2[:stages].first[:body].first[:rule] |
| 77 | + expect(rule[:from]).to be_a(Interscript::Isc::Items::CaptureGroup) |
| 78 | + expect(rule[:to]).to be_a(Interscript::Isc::Items::Capture) |
| 79 | + end |
| 80 | + |
| 81 | + it "preserves Concat items" do |
| 82 | + src = <<~ISC |
| 83 | + system "T:e-L:Latn:1" { |
| 84 | + stage main { |
| 85 | + sub { |
| 86 | + from "a" + "b" |
| 87 | + to "c" |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + ISC |
| 92 | + doc1 = parse_isc(src) |
| 93 | + doc2 = round_trip(doc1) |
| 94 | + rule = doc2[:stages].first[:body].first[:rule] |
| 95 | + expect(rule[:from]).to be_a(Interscript::Isc::Items::Concat) |
| 96 | + end |
| 97 | + |
| 98 | + it "preserves constraints" do |
| 99 | + src = <<~ISC |
| 100 | + system "T:e-L:Latn:1" { |
| 101 | + stage main { |
| 102 | + sub "a" "b" |
| 103 | + before "c" |
| 104 | + after "d" |
| 105 | + } |
| 106 | + } |
| 107 | + ISC |
| 108 | + doc1 = parse_isc(src) |
| 109 | + doc2 = round_trip(doc1) |
| 110 | + rule = doc2[:stages].first[:body].first[:rule] |
| 111 | + expect(rule[:constraints].size).to eq(2) |
| 112 | + expect(rule[:constraints].first[:kind]).to eq(:before) |
| 113 | + end |
| 114 | + |
| 115 | + it "preserves run directives" do |
| 116 | + src = %(system "T:e-L:Latn:1" {\nstage main {\nrun map.dep.stage.main\n}\n}) |
| 117 | + doc1 = parse_isc(src) |
| 118 | + doc2 = round_trip(doc1) |
| 119 | + run_item = doc2[:stages].first[:body].first |
| 120 | + expect(run_item[:kind]).to eq(:run) |
| 121 | + expect(run_item[:dependency]).to eq("dep") |
| 122 | + expect(run_item[:stage]).to eq("main") |
| 123 | + end |
| 124 | + |
| 125 | + it "preserves compose and string_case directives" do |
| 126 | + src = <<~ISC |
| 127 | + system "T:e-L:Latn:1" { |
| 128 | + stage main { |
| 129 | + title_case |
| 130 | + compose |
| 131 | + } |
| 132 | + } |
| 133 | + ISC |
| 134 | + doc1 = parse_isc(src) |
| 135 | + doc2 = round_trip(doc1) |
| 136 | + items = doc2[:stages].first[:body] |
| 137 | + expect(items.map { |i| i[:kind] }).to include(:string_case, :compose) |
| 138 | + end |
| 139 | + |
| 140 | + it "preserves aliases" do |
| 141 | + src = <<~ISC |
| 142 | + system "T:e-L:Latn:1" { |
| 143 | +
|
| 144 | + aliases { |
| 145 | + vowels = any("aeiou") |
| 146 | + } |
| 147 | +
|
| 148 | + stage main { |
| 149 | + sub vowels "x" |
| 150 | + } |
| 151 | + } |
| 152 | + ISC |
| 153 | + doc1 = parse_isc(src) |
| 154 | + doc2 = round_trip(doc1) |
| 155 | + expect(doc2[:aliases].size).to eq(1) |
| 156 | + expect(doc2[:aliases].first[:name]).to eq("vowels") |
| 157 | + expect(doc2[:aliases].first[:value]).to be_a(Interscript::Isc::Items::Set) |
| 158 | + end |
| 159 | + |
| 160 | + it "round-trips real maps from /tmp/isc-verify" do |
| 161 | + maps_dir = "/tmp/isc-verify" |
| 162 | + skip "isc-verify not found" unless Dir.exist?(maps_dir) |
| 163 | + |
| 164 | + tested = 0 |
| 165 | + failed = [] |
| 166 | + |
| 167 | + Dir.glob("#{maps_dir}/*.isc").sort.first(20).each do |path| |
| 168 | + base = File.basename(path, ".isc") |
| 169 | + begin |
| 170 | + src = File.read(path) |
| 171 | + doc1 = parse_isc(src, base) |
| 172 | + doc2 = round_trip(doc1) |
| 173 | + # Verify structural equivalence |
| 174 | + expect(doc1[:tests].size).to eq(doc2[:tests].size) |
| 175 | + expect(doc1[:stages].size).to eq(doc2[:stages].size) |
| 176 | + tested += 1 |
| 177 | + rescue => e |
| 178 | + failed << "#{base}: #{e.message[0..60]}" |
| 179 | + end |
| 180 | + end |
| 181 | + |
| 182 | + expect(failed).to be_empty, "#{failed.size}/#{tested} maps failed:\n#{failed.join("\n")}" |
| 183 | + end |
| 184 | +end |
0 commit comments