Skip to content

Commit

Permalink
fix: wrap elements of underover and subsup in row when many used (#19)
Browse files Browse the repository at this point in the history
* fix: wrap elements of underover and subsup in row when many used

* test: add snap tests for many scripted elements
  • Loading branch information
nfejzic authored Dec 12, 2023
1 parent 14fbf6d commit 8355f4d
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 7 deletions.
24 changes: 22 additions & 2 deletions src/elements/msubsup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
Element, Elements,
};

use super::IntoElements;
use super::{grouping::Row, IntoElements};

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
enum SubSupInner {
Expand Down Expand Up @@ -72,6 +72,12 @@ pub struct SubSupBuilder<T1, T2> {
impl<T1, T2> SubSupBuilder<T1, T2> {
/// Set the base of the [`SubSup`] element.
pub fn base(self, base: impl IntoElements) -> SubSupBuilder<Init, T2> {
let mut base = base.into_elements();

if base.len() > 1 {
base = Row::from(base).into_elements();
}

SubSupBuilder {
base: Some(base.into_elements()),
sub: self.sub,
Expand All @@ -83,6 +89,12 @@ impl<T1, T2> SubSupBuilder<T1, T2> {

/// Set the subscript of the [`SubSup`] element.
pub fn subscript(self, sub: impl IntoElements) -> SubSupBuilder<T1, Init> {
let mut sub = sub.into_elements();

if sub.len() > 1 {
sub = Row::from(sub).into_elements();
}

SubSupBuilder {
base: self.base,
sub: Some(sub.into_elements()),
Expand All @@ -94,6 +106,12 @@ impl<T1, T2> SubSupBuilder<T1, T2> {

/// Set the superscript of the [`SubSup`] element.
pub fn supscript(self, sup: impl IntoElements) -> SubSupBuilder<T1, Init> {
let mut sup = sup.into_elements();

if sup.len() > 1 {
sup = Row::from(sup).into_elements();
}

SubSupBuilder {
base: self.base,
sub: self.sub,
Expand Down Expand Up @@ -132,8 +150,10 @@ impl SubSupBuilder<Init, Init> {
}
};

let base = self.base.expect("Base is guaranteed to be init.");

SubSup {
base: self.base.expect("Base is guaranteed to be init."),
base,
inner,
attributes: self.attr,
}
Expand Down
24 changes: 19 additions & 5 deletions src/elements/munderover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ pub struct UnderOverBuilder<T1, T2> {
impl<T1, T2> UnderOverBuilder<T1, T2> {
/// Set the base expression of the [`UnderOver`] element.
pub fn expr(self, expr: impl IntoElements) -> UnderOverBuilder<Init, T2> {
let mut expr = expr.into_elements();

if expr.len() > 1 {
expr = Row::from(expr).into_elements();
}

UnderOverBuilder {
expr: Some(expr.into_elements()),
under: self.under,
Expand All @@ -111,6 +117,12 @@ impl<T1, T2> UnderOverBuilder<T1, T2> {

/// Set the under script of the [`UnderOver`] element.
pub fn over(self, over: impl IntoElements) -> UnderOverBuilder<T1, Init> {
let mut over = over.into_elements();

if over.len() > 1 {
over = Row::from(over).into_elements();
}

UnderOverBuilder {
expr: self.expr,
under: self.under,
Expand All @@ -122,6 +134,12 @@ impl<T1, T2> UnderOverBuilder<T1, T2> {

/// Set the over script of the [`UnderOver`] element.
pub fn under(self, under: impl IntoElements) -> UnderOverBuilder<T1, Init> {
let mut under = under.into_elements();

if under.len() > 1 {
under = Row::from(under).into_elements();
}

UnderOverBuilder {
expr: self.expr,
under: Some(under.into_elements()),
Expand Down Expand Up @@ -160,11 +178,7 @@ impl UnderOverBuilder<Init, Init> {
}
};

let mut expr = self.expr.expect("Expr is guaranteed to be init.");

if expr.len() > 1 {
expr = Row::from(expr).into_elements();
}
let expr = self.expr.expect("Expr is guaranteed to be init.");

UnderOver {
expr,
Expand Down
44 changes: 44 additions & 0 deletions tests/scripted/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,28 @@ fn subsup() {
crate::snap_test!(out, name: "subsup_subsup");
}

#[test]
fn subsup_many() {
let sub = alemat::children![
Operator::lparens(),
Ident::from("n"),
Operator::eq(),
Num::from(0),
Operator::rparens(),
];

let sup = alemat::children![Ident::from("k"), Operator::plus(), Num::from(1)];

let out = MathMl::with_content(alemat::children![SubSup::builder()
.base(Operator::integral())
.subscript(sub)
.supscript(sup)
.build()])
.render();

crate::snap_test!(out, name: "subsup_many");
}

#[test]
fn subsup_integral() {
let out = MathMl::with_content(alemat::children![
Expand Down Expand Up @@ -66,6 +88,28 @@ fn underover() {
crate::snap_test!(out, name: "underover_underover");
}

#[test]
fn underover_many() {
let under = alemat::children![
Operator::lparens(),
Ident::from("n"),
Operator::eq(),
Num::from(0),
Operator::rparens(),
];

let over = alemat::children![Ident::from("k"), Operator::plus(), Num::from(1)];

let out = MathMl::with_content(alemat::children![UnderOver::builder()
.expr(Operator::sum())
.under(under)
.over(over)
.build()])
.render();

crate::snap_test!(out, name: "underover_many");
}

#[test]
fn underover_integral() {
let out = MathMl::with_content(alemat::children![
Expand Down
40 changes: 40 additions & 0 deletions tests/snapshots/subsup_many.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
source: tests/scripted/mod.rs
expression: input
---
<math>
<msubsup>
<mo>
</mo>
<mrow>
<mo>
(
</mo>
<mi>
n
</mi>
<mo>
=
</mo>
<mn>
0
</mn>
<mo>
)
</mo>
</mrow>
<mrow>
<mi>
k
</mi>
<mo>
+
</mo>
<mn>
1
</mn>
</mrow>
</msubsup>
</math>

40 changes: 40 additions & 0 deletions tests/snapshots/underover_many.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
source: tests/scripted/mod.rs
expression: input
---
<math>
<munderover>
<mo>
</mo>
<mrow>
<mo>
(
</mo>
<mi>
n
</mi>
<mo>
=
</mo>
<mn>
0
</mn>
<mo>
)
</mo>
</mrow>
<mrow>
<mi>
k
</mi>
<mo>
+
</mo>
<mn>
1
</mn>
</mrow>
</munderover>
</math>

0 comments on commit 8355f4d

Please sign in to comment.