forked from kscience/kmath
Merge pull request #289 from mipt-npm/commandertvis/rendering-tests
Move AST rendering tests to common
This commit is contained in:
commit
23d2f8d758
@ -0,0 +1,100 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2018-2021 KMath contributors.
|
||||||
|
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package space.kscience.kmath.ast.rendering
|
||||||
|
|
||||||
|
import space.kscience.kmath.ast.rendering.TestUtils.testLatex
|
||||||
|
import space.kscience.kmath.expressions.MST.Numeric
|
||||||
|
import kotlin.test.Test
|
||||||
|
|
||||||
|
internal class TestFeatures {
|
||||||
|
@Test
|
||||||
|
fun printSymbolic() = testLatex("x", "x")
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun printNumeric() {
|
||||||
|
val num = object : Number() {
|
||||||
|
override fun toByte(): Byte = throw UnsupportedOperationException()
|
||||||
|
override fun toChar(): Char = throw UnsupportedOperationException()
|
||||||
|
override fun toDouble(): Double = throw UnsupportedOperationException()
|
||||||
|
override fun toFloat(): Float = throw UnsupportedOperationException()
|
||||||
|
override fun toInt(): Int = throw UnsupportedOperationException()
|
||||||
|
override fun toLong(): Long = throw UnsupportedOperationException()
|
||||||
|
override fun toShort(): Short = throw UnsupportedOperationException()
|
||||||
|
override fun toString(): String = "foo"
|
||||||
|
}
|
||||||
|
|
||||||
|
testLatex(Numeric(num), "foo")
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun prettyPrintFloats() {
|
||||||
|
testLatex(Numeric(Double.NaN), "NaN")
|
||||||
|
testLatex(Numeric(Double.POSITIVE_INFINITY), "\\infty")
|
||||||
|
testLatex(Numeric(Double.NEGATIVE_INFINITY), "-\\infty")
|
||||||
|
testLatex(Numeric(1.0), "1")
|
||||||
|
testLatex(Numeric(-1.0), "-1")
|
||||||
|
testLatex(Numeric(1.42), "1.42")
|
||||||
|
testLatex(Numeric(-1.42), "-1.42")
|
||||||
|
testLatex(Numeric(1.1e10), "1.1\\times10^{10}")
|
||||||
|
testLatex(Numeric(1.1e-10), "1.1\\times10^{-10}")
|
||||||
|
testLatex(Numeric(-1.1e-10), "-1.1\\times10^{-10}")
|
||||||
|
testLatex(Numeric(-1.1e10), "-1.1\\times10^{10}")
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun prettyPrintIntegers() {
|
||||||
|
testLatex(Numeric(42), "42")
|
||||||
|
testLatex(Numeric(-42), "-42")
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun prettyPrintPi() {
|
||||||
|
testLatex("pi", "\\pi")
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun binaryPlus() = testLatex("2+2", "2+2")
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun binaryMinus() = testLatex("2-2", "2-2")
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun fraction() = testLatex("2/2", "\\frac{2}{2}")
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun binaryOperator() = testLatex("f(x, y)", "\\operatorname{f}\\left(x,y\\right)")
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun unaryOperator() = testLatex("f(x)", "\\operatorname{f}\\,\\left(x\\right)")
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun power() = testLatex("x^y", "x^{y}")
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun squareRoot() = testLatex("sqrt(x)", "\\sqrt{x}")
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun exponential() = testLatex("exp(x)", "e^{x}")
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun multiplication() = testLatex("x*1", "x\\times1")
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun inverseTrigonometry() {
|
||||||
|
testLatex("asin(x)", "\\operatorname{sin}^{-1}\\,\\left(x\\right)")
|
||||||
|
testLatex("asinh(x)", "\\operatorname{sinh}^{-1}\\,\\left(x\\right)")
|
||||||
|
testLatex("acos(x)", "\\operatorname{cos}^{-1}\\,\\left(x\\right)")
|
||||||
|
testLatex("acosh(x)", "\\operatorname{cosh}^{-1}\\,\\left(x\\right)")
|
||||||
|
testLatex("atan(x)", "\\operatorname{tan}^{-1}\\,\\left(x\\right)")
|
||||||
|
testLatex("atanh(x)", "\\operatorname{tanh}^{-1}\\,\\left(x\\right)")
|
||||||
|
}
|
||||||
|
|
||||||
|
// @Test
|
||||||
|
// fun unaryPlus() {
|
||||||
|
// testLatex("+1", "+1")
|
||||||
|
// testLatex("+1", "++1")
|
||||||
|
// }
|
||||||
|
}
|
@ -0,0 +1,73 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2018-2021 KMath contributors.
|
||||||
|
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package space.kscience.kmath.ast.rendering
|
||||||
|
|
||||||
|
import space.kscience.kmath.ast.rendering.TestUtils.testLatex
|
||||||
|
import space.kscience.kmath.expressions.MST
|
||||||
|
import space.kscience.kmath.operations.GroupOperations
|
||||||
|
import kotlin.test.Test
|
||||||
|
|
||||||
|
internal class TestLatex {
|
||||||
|
@Test
|
||||||
|
fun number() = testLatex("42", "42")
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun symbol() = testLatex("x", "x")
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun operatorName() = testLatex("sin(1)", "\\operatorname{sin}\\,\\left(1\\right)")
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun specialSymbol() {
|
||||||
|
testLatex(MST.Numeric(Double.POSITIVE_INFINITY), "\\infty")
|
||||||
|
testLatex("pi", "\\pi")
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun operand() {
|
||||||
|
testLatex("sin(1)", "\\operatorname{sin}\\,\\left(1\\right)")
|
||||||
|
testLatex("1+1", "1+1")
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun unaryOperator() = testLatex("sin(1)", "\\operatorname{sin}\\,\\left(1\\right)")
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun unaryPlus() = testLatex(MST.Unary(GroupOperations.PLUS_OPERATION, MST.Numeric(1)), "+1")
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun unaryMinus() = testLatex("-x", "-x")
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun radical() = testLatex("sqrt(x)", "\\sqrt{x}")
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun superscript() = testLatex("x^y", "x^{y}")
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun subscript() = testLatex(SubscriptSyntax("", SymbolSyntax("x"), NumberSyntax("123")), "x_{123}")
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun binaryOperator() = testLatex("f(x, y)", "\\operatorname{f}\\left(x,y\\right)")
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun binaryPlus() = testLatex("x+x", "x+x")
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun binaryMinus() = testLatex("x-x", "x-x")
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun fraction() = testLatex("x/x", "\\frac{x}{x}")
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun radicalWithIndex() = testLatex(RadicalWithIndexSyntax("", SymbolSyntax("x"), SymbolSyntax("y")), "\\sqrt[x]{y}")
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun multiplication() {
|
||||||
|
testLatex("x*1", "x\\times1")
|
||||||
|
testLatex("1*x", "1\\,x")
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,92 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2018-2021 KMath contributors.
|
||||||
|
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package space.kscience.kmath.ast.rendering
|
||||||
|
|
||||||
|
import space.kscience.kmath.ast.rendering.TestUtils.testMathML
|
||||||
|
import space.kscience.kmath.expressions.MST
|
||||||
|
import space.kscience.kmath.operations.GroupOperations
|
||||||
|
import kotlin.test.Test
|
||||||
|
|
||||||
|
internal class TestMathML {
|
||||||
|
@Test
|
||||||
|
fun number() = testMathML("42", "<mn>42</mn>")
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun symbol() = testMathML("x", "<mi>x</mi>")
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun operatorName() = testMathML(
|
||||||
|
"sin(1)",
|
||||||
|
"<mo>sin</mo><mspace width=\"0.167em\"></mspace><mfenced open=\"(\" close=\")\" separators=\"\"><mn>1</mn></mfenced>",
|
||||||
|
)
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun specialSymbol() {
|
||||||
|
testMathML(MST.Numeric(Double.POSITIVE_INFINITY), "<mo>∞</mo>")
|
||||||
|
testMathML("pi", "<mo>π</mo>")
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun operand() {
|
||||||
|
testMathML(
|
||||||
|
"sin(1)",
|
||||||
|
"<mo>sin</mo><mspace width=\"0.167em\"></mspace><mfenced open=\"(\" close=\")\" separators=\"\"><mn>1</mn></mfenced>",
|
||||||
|
)
|
||||||
|
|
||||||
|
testMathML("1+1", "<mn>1</mn><mo>+</mo><mn>1</mn>")
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun unaryOperator() = testMathML(
|
||||||
|
"sin(1)",
|
||||||
|
"<mo>sin</mo><mspace width=\"0.167em\"></mspace><mfenced open=\"(\" close=\")\" separators=\"\"><mn>1</mn></mfenced>",
|
||||||
|
)
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun unaryPlus() =
|
||||||
|
testMathML(MST.Unary(GroupOperations.PLUS_OPERATION, MST.Numeric(1)), "<mo>+</mo><mn>1</mn>")
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun unaryMinus() = testMathML("-x", "<mo>-</mo><mi>x</mi>")
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun radical() = testMathML("sqrt(x)", "<msqrt><mi>x</mi></msqrt>")
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun superscript() = testMathML("x^y", "<msup><mrow><mi>x</mi></mrow><mrow><mi>y</mi></mrow></msup>")
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun subscript() = testMathML(
|
||||||
|
SubscriptSyntax("", SymbolSyntax("x"), NumberSyntax("123")),
|
||||||
|
"<msub><mrow><mi>x</mi></mrow><mrow><mn>123</mn></mrow></msub>",
|
||||||
|
)
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun binaryOperator() = testMathML(
|
||||||
|
"f(x, y)",
|
||||||
|
"<mo>f</mo><mfenced open=\"(\" close=\")\" separators=\"\"><mi>x</mi><mo>,</mo><mi>y</mi></mfenced>",
|
||||||
|
)
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun binaryPlus() = testMathML("x+x", "<mi>x</mi><mo>+</mo><mi>x</mi>")
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun binaryMinus() = testMathML("x-x", "<mi>x</mi><mo>-</mo><mi>x</mi>")
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun fraction() = testMathML("x/x", "<mfrac><mrow><mi>x</mi></mrow><mrow><mi>x</mi></mrow></mfrac>")
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun radicalWithIndex() =
|
||||||
|
testMathML(RadicalWithIndexSyntax("", SymbolSyntax("x"), SymbolSyntax("y")),
|
||||||
|
"<mroot><mrow><mi>y</mi></mrow><mrow><mi>x</mi></mrow></mroot>")
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun multiplication() {
|
||||||
|
testMathML("x*1", "<mi>x</mi><mo>×</mo><mn>1</mn>")
|
||||||
|
testMathML("1*x", "<mn>1</mn><mspace width=\"0.167em\"></mspace><mi>x</mi>")
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,33 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2018-2021 KMath contributors.
|
||||||
|
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package space.kscience.kmath.ast.rendering
|
||||||
|
|
||||||
|
import space.kscience.kmath.ast.rendering.TestUtils.testLatex
|
||||||
|
import kotlin.test.Test
|
||||||
|
|
||||||
|
internal class TestStages {
|
||||||
|
@Test
|
||||||
|
fun betterMultiplication() {
|
||||||
|
testLatex("a*1", "a\\times1")
|
||||||
|
testLatex("1*(2/3)", "1\\times\\left(\\frac{2}{3}\\right)")
|
||||||
|
testLatex("1*1", "1\\times1")
|
||||||
|
testLatex("2e10", "2\\times10^{10}")
|
||||||
|
testLatex("2*x", "2\\,x")
|
||||||
|
testLatex("2*(x+1)", "2\\,\\left(x+1\\right)")
|
||||||
|
testLatex("x*y", "x\\,y")
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun parentheses() {
|
||||||
|
testLatex("(x+1)", "x+1")
|
||||||
|
testLatex("x*x*x", "x\\,x\\,x")
|
||||||
|
testLatex("(x+x)*x", "\\left(x+x\\right)\\,x")
|
||||||
|
testLatex("x+x*x", "x+x\\,x")
|
||||||
|
testLatex("x+x^x*x+x", "x+x^{x}\\,x+x")
|
||||||
|
testLatex("(x+x)^x+x*x", "\\left(x+x\\right)^{x}+x\\,x")
|
||||||
|
testLatex("x^(x+x)", "x^{x+x}")
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,46 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2018-2021 KMath contributors.
|
||||||
|
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package space.kscience.kmath.ast.rendering
|
||||||
|
|
||||||
|
import space.kscience.kmath.ast.parseMath
|
||||||
|
import space.kscience.kmath.expressions.MST
|
||||||
|
import kotlin.test.assertEquals
|
||||||
|
|
||||||
|
internal object TestUtils {
|
||||||
|
private fun mathSyntax(mst: MST) = FeaturedMathRendererWithPostProcess.Default.render(mst)
|
||||||
|
private fun latex(mst: MST) = LatexSyntaxRenderer.renderWithStringBuilder(mathSyntax(mst))
|
||||||
|
private fun mathML(mst: MST) = MathMLSyntaxRenderer.renderWithStringBuilder(mathSyntax(mst))
|
||||||
|
|
||||||
|
internal fun testLatex(mst: MST, expectedLatex: String) = assertEquals(
|
||||||
|
expected = expectedLatex,
|
||||||
|
actual = latex(mst),
|
||||||
|
)
|
||||||
|
|
||||||
|
internal fun testLatex(expression: String, expectedLatex: String) = assertEquals(
|
||||||
|
expected = expectedLatex,
|
||||||
|
actual = latex(expression.parseMath()),
|
||||||
|
)
|
||||||
|
|
||||||
|
internal fun testLatex(expression: MathSyntax, expectedLatex: String) = assertEquals(
|
||||||
|
expected = expectedLatex,
|
||||||
|
actual = LatexSyntaxRenderer.renderWithStringBuilder(expression),
|
||||||
|
)
|
||||||
|
|
||||||
|
internal fun testMathML(mst: MST, expectedMathML: String) = assertEquals(
|
||||||
|
expected = "<math xmlns=\"http://www.w3.org/1998/Math/MathML\"><mrow>$expectedMathML</mrow></math>",
|
||||||
|
actual = mathML(mst),
|
||||||
|
)
|
||||||
|
|
||||||
|
internal fun testMathML(expression: String, expectedMathML: String) = assertEquals(
|
||||||
|
expected = "<math xmlns=\"http://www.w3.org/1998/Math/MathML\"><mrow>$expectedMathML</mrow></math>",
|
||||||
|
actual = mathML(expression.parseMath()),
|
||||||
|
)
|
||||||
|
|
||||||
|
internal fun testMathML(expression: MathSyntax, expectedMathML: String) = assertEquals(
|
||||||
|
expected = "<math xmlns=\"http://www.w3.org/1998/Math/MathML\"><mrow>$expectedMathML</mrow></math>",
|
||||||
|
actual = MathMLSyntaxRenderer.renderWithStringBuilder(expression),
|
||||||
|
)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user