Skip to content

Commit

Permalink
Merge pull request #4 from AndreDouzette/main
Browse files Browse the repository at this point in the history
Fix crash when sqrt has no radicand
  • Loading branch information
mgriebling authored Jul 6, 2023
2 parents 695c8a6 + a482791 commit 71ff06c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Sources/SwiftMath/MathRender/MTMathListBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,10 @@ public struct MTMathListBuilder {
} else if command == "sqrt" {
// A sqrt command with one argument
let rad = MTRadical()
guard self.hasCharacters else {
rad.radicand = self.buildInternal(true)
return rad
}
let ch = self.getNextCharacter()
if (ch == "[") {
// special handling for sqrt[degree]{radicand}
Expand Down
37 changes: 37 additions & 0 deletions Tests/SwiftMathTests/MTMathListBuilderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,43 @@ final class MTMathListBuilderTests: XCTestCase {
XCTAssertEqual(latex, "\\sqrt[3]{2}");
}

func testSqrtWithoutRadicand() throws {
let str = "\\sqrt"
let list = try XCTUnwrap(MTMathListBuilder.build(fromString: str))

XCTAssertEqual(list.atoms.count, 1)
let rad = try XCTUnwrap(list.atoms.first as? MTRadical)
XCTAssertEqual(rad.type, .radical)
XCTAssertEqual(rad.nucleus, "")

XCTAssertEqual(rad.radicand?.atoms.isEmpty, true)
XCTAssertNil(rad.degree)

let latex = MTMathListBuilder.mathListToString(list)
XCTAssertEqual(latex, "\\sqrt{}")
}

func testSqrtWithDegreeWithoutRadicand() throws {
let str = "\\sqrt[3]"
let list = try XCTUnwrap(MTMathListBuilder.build(fromString: str))

XCTAssertEqual(list.atoms.count, 1)
let rad = try XCTUnwrap(list.atoms.first as? MTRadical)
XCTAssertEqual(rad.type, .radical)
XCTAssertEqual(rad.nucleus, "")

XCTAssertEqual(rad.radicand?.atoms.isEmpty, true)

let subList = try XCTUnwrap(rad.degree)
XCTAssertEqual(subList.atoms.count, 1)
let atom = try XCTUnwrap(subList.atoms.first)
XCTAssertEqual(atom.type, .number)
XCTAssertEqual(atom.nucleus, "3")

let latex = MTMathListBuilder.mathListToString(list)
XCTAssertEqual(latex, "\\sqrt[3]{}");
}

func testLeftRight() throws {
let data = getTestDataLeftRight()
for testCase in data {
Expand Down

0 comments on commit 71ff06c

Please sign in to comment.