SPB Git

Repository Public

OffsetMapTests.swift 2.2 KB 45 lines swift

1// ORIX Sonata2// File: Packages/SonataUI/Tests/SonataUITests/OffsetMapTests.swift3// Description: UTF-8 to UTF-16 offset conversion used between the compiler and the editor.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import Foundation8import SonataBridge9import Testing10@testable import SonataUI1112@Test func offsetsRoundTripOnMixedText() {13    let text = "tempo 120bpm // é𝄞x\nkey D minor — ok"14    let map = OffsetMap(text)15    #expect(map.utf8Count == text.utf8.count && map.utf16Count == text.utf16.count)16    var u8 = 0, u16 = 017    for scalar in text.unicodeScalars {18        #expect(map.utf16(fromUTF8: u8) == u16, "at byte \(u8)")19        #expect(map.utf8(fromUTF16: u16) == u8, "at unit \(u16)")20        u8 += UTF8.width(scalar)21        u16 += UTF16.width(scalar)22    }23    #expect(map.utf16(fromUTF8: 10_000) == text.utf16.count)24    let x = text.utf8.firstIndex(of: UInt8(ascii: "x"))!25    let r = map.nsRange(utf8Start: text.utf8.distance(from: text.utf8.startIndex, to: x), utf8End: text.utf8.distance(from: text.utf8.startIndex, to: x) + 1)26    #expect((text as NSString).substring(with: r) == "x")27}2829@Test func asciiIsIdentity() {30    let map = OffsetMap("play A4 duration 1/4")31    #expect(map.utf16(fromUTF8: 7) == 7 && map.utf8(fromUTF16: 12) == 12)32}3334@Test @MainActor func barBeatReadout() throws {35    let json = #"{"title":"t","duration_sec":4,"key":null,"seed":0,"tempo":[],"signatures":[],"markers":[],"tracks":[],"buses":[],"bars":[{"number":1,"at_sec":0,"beats":4,"beat_sec":[0,0.5,1,1.5],"position":{"num":0,"den":1},"beat":{"num":1,"den":4}},{"number":2,"at_sec":2,"beats":4,"beat_sec":[2,2.5,3,3.5],"position":{"num":1,"den":1},"beat":{"num":1,"den":4}}],"master":{"sample_rate":48000,"gain_db":0,"peak_limit_db":-1,"effects":[]}}"#36    let decoder = JSONDecoder()37    decoder.keyDecodingStrategy = .convertFromSnakeCase38    let t = try decoder.decode(SonataBridge.Timeline.self, from: Data(json.utf8))39    #expect(PositionReadout.barBeat(at: 0, in: t) == "1.1")40    #expect(PositionReadout.barBeat(at: 1.2, in: t) == "1.3")41    #expect(PositionReadout.barBeat(at: 2.0, in: t) == "2.1")42    #expect(PositionReadout.barBeat(at: 99, in: t) == "2.4")43    #expect(PositionReadout.barBeat(at: 0, in: nil) == "–.–")44}45
Files main
Menu

Theme