|
| 1 | +// |
| 2 | +// JSONTreeParserTests.swift |
| 3 | +// TableProTests |
| 4 | +// |
| 5 | + |
| 6 | +import Foundation |
| 7 | +import Testing |
| 8 | + |
| 9 | +@testable import TablePro |
| 10 | + |
| 11 | +@Suite("JSONTreeParser") |
| 12 | +struct JSONTreeParserTests { |
| 13 | + @Test("Long string nodes keep the full display value") |
| 14 | + func longStringNodesKeepFullDisplayValue() { |
| 15 | + let longString = String(repeating: "abcdefghij", count: 12) |
| 16 | + let json = "{\"message\":\"\(longString)\"}" |
| 17 | + |
| 18 | + let result = JSONTreeParser.parse(json) |
| 19 | + guard case .success(let root) = result else { |
| 20 | + Issue.record("Expected JSONTreeParser.parse to succeed") |
| 21 | + return |
| 22 | + } |
| 23 | + |
| 24 | + guard let messageNode = root.children.first else { |
| 25 | + Issue.record("Expected a child node for message") |
| 26 | + return |
| 27 | + } |
| 28 | + |
| 29 | + #expect(messageNode.valueType == .string) |
| 30 | + #expect(messageNode.rawValue == longString) |
| 31 | + #expect(messageNode.displayValue == "\"\(longString)\"") |
| 32 | + #expect(!messageNode.displayValue.contains("...")) |
| 33 | + } |
| 34 | + |
| 35 | + @Test("Tree parser still rejects oversized documents") |
| 36 | + func oversizedDocumentStillRejected() { |
| 37 | + let oversizedValue = String(repeating: "a", count: 100_001) |
| 38 | + let json = "{\"message\":\"\(oversizedValue)\"}" |
| 39 | + |
| 40 | + let result = JSONTreeParser.parse(json) |
| 41 | + guard case .failure(let error) = result else { |
| 42 | + Issue.record("Expected JSONTreeParser.parse to fail for oversized input") |
| 43 | + return |
| 44 | + } |
| 45 | + |
| 46 | + switch error { |
| 47 | + case .tooLarge: |
| 48 | + break |
| 49 | + case .invalidJSON: |
| 50 | + Issue.record("Expected oversized input to hit the tooLarge guard first") |
| 51 | + } |
| 52 | + } |
| 53 | +} |
0 commit comments