/// Tests for table properties
use crate::common::*;
use anyhow::Result;

#[test]
fn array() -> Result<()> {
    let mathml = r#"<math><mfenced open="[" close="]"><mtable intent=":array">
            <mtr><mtd><mn>1</mn></mtd><mtd><mn>2</mn></mtd><mtd><mn>3</mn></mtd></mtr>
            <mtr><mtd><mn>4</mn></mtd><mtd><mn>5</mn></mtd><mtd><mn>6</mn></mtd></mtr>
        </mtable></mfenced></math>"#;
    let intent = " <math data-from-mathml='math'>
                        <array data-from-mathml='mtable' data-intent-property=':array:'>
                            <mtr data-from-mathml='mtr'>
                                <mtd data-from-mathml='mtd'><mn data-from-mathml='mn'>1</mn></mtd>
                                <mtd data-from-mathml='mtd'><mn data-from-mathml='mn'>2</mn></mtd>
                                <mtd data-from-mathml='mtd'><mn data-from-mathml='mn'>3</mn></mtd></mtr>
                            <mtr data-from-mathml='mtr'>
                                <mtd data-from-mathml='mtd'><mn data-from-mathml='mn'>4</mn></mtd>
                                <mtd data-from-mathml='mtd'><mn data-from-mathml='mn'>5</mn></mtd>
                                <mtd data-from-mathml='mtd'><mn data-from-mathml='mn'>6</mn></mtd>
                            </mtr>
                        </array>
                    </math>";
    test_intent(mathml, intent, vec![])?;
    return Ok(());

}

#[test]
fn matrix_mtable_intent() -> Result<()> {
    let mathml = r#"<math><mfenced open="[" close="]"><mtable intent=":matrix">
            <mtr><mtd><mn>1</mn></mtd><mtd><mn>2</mn></mtd><mtd><mn>3</mn></mtd></mtr>
            <mtr><mtd><mn>4</mn></mtd><mtd><mn>5</mn></mtd><mtd><mn>6</mn></mtd></mtr>
        </mtable></mfenced></math>"#;
    let intent = " <math data-from-mathml='math'>
                        <matrix data-from-mathml='mtable' data-intent-property=':matrix:'>
                            <mtr data-from-mathml='mtr'>
                                <mtd data-from-mathml='mtd'><mn data-from-mathml='mn'>1</mn></mtd>
                                <mtd data-from-mathml='mtd'><mn data-from-mathml='mn'>2</mn></mtd>
                                <mtd data-from-mathml='mtd'><mn data-from-mathml='mn'>3</mn></mtd></mtr>
                            <mtr data-from-mathml='mtr'>
                                <mtd data-from-mathml='mtd'><mn data-from-mathml='mn'>4</mn></mtd>
                                <mtd data-from-mathml='mtd'><mn data-from-mathml='mn'>5</mn></mtd>
                                <mtd data-from-mathml='mtd'><mn data-from-mathml='mn'>6</mn></mtd>
                            </mtr>
                        </matrix>
                    </math>";
    test_intent(mathml, intent, vec![])?;
    return Ok(());

}

#[test]
fn matrix_mrow_intent() -> Result<()> {
    let mathml = r#"<math><mfenced open="[" close="]" intent="$t"><mtable arg="t" intent=":matrix">
            <mtr><mtd><mn>1</mn></mtd><mtd><mn>2</mn></mtd><mtd><mn>3</mn></mtd></mtr>
            <mtr><mtd><mn>4</mn></mtd><mtd><mn>5</mn></mtd><mtd><mn>6</mn></mtd></mtr>
        </mtable></mfenced></math>"#;
    let intent = " <math data-from-mathml='math'>
                        <matrix data-from-mathml='mtable' arg='t' data-intent-property=':matrix:'>
                            <mtr data-from-mathml='mtr'>
                                <mtd data-from-mathml='mtd'><mn data-from-mathml='mn'>1</mn></mtd>
                                <mtd data-from-mathml='mtd'><mn data-from-mathml='mn'>2</mn></mtd>
                                <mtd data-from-mathml='mtd'><mn data-from-mathml='mn'>3</mn></mtd></mtr>
                            <mtr data-from-mathml='mtr'>
                                <mtd data-from-mathml='mtd'><mn data-from-mathml='mn'>4</mn></mtd>
                                <mtd data-from-mathml='mtd'><mn data-from-mathml='mn'>5</mn></mtd>
                                <mtd data-from-mathml='mtd'><mn data-from-mathml='mn'>6</mn></mtd>
                            </mtr>
                        </matrix>
                    </math>";
    test_intent(mathml, intent, vec![])?;
    return Ok(());

}

#[test]
fn matrix_infer_intent() -> Result<()> {
    let mathml = r#"<math><mfenced open="[" close="]" ><mtable>
            <mtr><mtd><mn>1</mn></mtd><mtd><mn>2</mn></mtd><mtd><mn>3</mn></mtd></mtr>
            <mtr><mtd><mn>4</mn></mtd><mtd><mn>5</mn></mtd><mtd><mn>6</mn></mtd></mtr>
        </mtable></mfenced></math>"#;
    let intent = " <math data-from-mathml='math'>
                        <matrix data-from-mathml='mtable'>
                            <mtr data-from-mathml='mtr'>
                                <mtd data-from-mathml='mtd'><mn data-from-mathml='mn'>1</mn></mtd>
                                <mtd data-from-mathml='mtd'><mn data-from-mathml='mn'>2</mn></mtd>
                                <mtd data-from-mathml='mtd'><mn data-from-mathml='mn'>3</mn></mtd></mtr>
                            <mtr data-from-mathml='mtr'>
                                <mtd data-from-mathml='mtd'><mn data-from-mathml='mn'>4</mn></mtd>
                                <mtd data-from-mathml='mtd'><mn data-from-mathml='mn'>5</mn></mtd>
                                <mtd data-from-mathml='mtd'><mn data-from-mathml='mn'>6</mn></mtd>
                            </mtr>
                        </matrix>
                    </math>";
    test_intent(mathml, intent, vec![])?;
    return Ok(());

}

#[test]
fn determinant_not_matrix() -> Result<()> {
    let mathml = r#"<math><mfenced open="[" close="]" ><mtable intent=":determinant">
            <mtr><mtd><mn>1</mn></mtd><mtd><mn>2</mn></mtd></mtr>
            <mtr><mtd><mn>4</mn></mtd><mtd><mn>5</mn></mtd></mtr>
        </mtable></mfenced></math>"#;
    let intent = " <math data-from-mathml='math'>
                        <determinant data-from-mathml='mtable' data-intent-property=':determinant:'>
                            <mtr data-from-mathml='mtr'>
                                <mtd data-from-mathml='mtd'><mn data-from-mathml='mn'>1</mn></mtd>
                                <mtd data-from-mathml='mtd'><mn data-from-mathml='mn'>2</mn></mtd>
                            </mtr>
                            <mtr data-from-mathml='mtr'>
                                <mtd data-from-mathml='mtd'><mn data-from-mathml='mn'>4</mn></mtd>
                                <mtd data-from-mathml='mtd'><mn data-from-mathml='mn'>5</mn></mtd>
                            </mtr>
                        </determinant>
                    </math>";
    test_intent(mathml, intent, vec![])?;
    return Ok(());

}

#[test]
fn determinant_infer_intent() -> Result<()> {
    let mathml = r#"<math><mfenced open="|" close="|" ><mtable>
            <mtr><mtd><mn>1</mn></mtd><mtd><mn>2</mn></mtd><mtd><mn>3</mn></mtd></mtr>
            <mtr><mtd><mn>4</mn></mtd><mtd><mn>5</mn></mtd><mtd><mn>6</mn></mtd></mtr>
        </mtable></mfenced></math>"#;
    let intent = " <math data-from-mathml='math'>
                        <determinant data-from-mathml='mtable'>
                            <mtr data-from-mathml='mtr'>
                                <mtd data-from-mathml='mtd'><mn data-from-mathml='mn'>1</mn></mtd>
                                <mtd data-from-mathml='mtd'><mn data-from-mathml='mn'>2</mn></mtd>
                                <mtd data-from-mathml='mtd'><mn data-from-mathml='mn'>3</mn></mtd></mtr>
                            <mtr data-from-mathml='mtr'>
                                <mtd data-from-mathml='mtd'><mn data-from-mathml='mn'>4</mn></mtd>
                                <mtd data-from-mathml='mtd'><mn data-from-mathml='mn'>5</mn></mtd>
                                <mtd data-from-mathml='mtd'><mn data-from-mathml='mn'>6</mn></mtd>
                            </mtr>
                        </determinant>
                    </math>";
    test_intent(mathml, intent, vec![])?;
    return Ok(());

}

#[test]
fn system_of_equations() -> Result<()> {
    let mathml = r#"<math>
            <mtable intent=':system-of-equations' columnalign="right left" columnspacing="0em" rowspacing="3pt">
                <mtr>
                    <mtd><mi>x</mi><mo>+</mo><mi>y</mi></mtd>
                    <mtd><mi/><mo>is equal to</mo><mi>y</mi><mo>+</mo><mn>1</mn></mtd>
                </mtr>
                <mtr>
                    <mtd><mi>x</mi><mo>&#x2212;</mo><mi>y</mi></mtd>
                    <mtd><mi/><mo>is equal to</mo><mn>4</mn></mtd>
                </mtr>
            </mtable>
            </math>"#;
    let intent = " <math data-from-mathml='math'>
        <system-of-equations data-from-mathml='mtable' columnalign='right left' columnspacing='0em' rowspacing='3pt' data-intent-property=':system-of-equations:'>
            <mtr data-from-mathml='mtr'>
                <mtd data-from-mathml='mtr'>
                    <mrow data-from-mathml='mtr'>
                    <mrow data-from-mathml='mrow' data-changed='added'>
                        <mi data-from-mathml='mi'>x</mi>
                        <mo data-from-mathml='mo'>+</mo>
                        <mi data-from-mathml='mi'>y</mi>
                    </mrow>
                    <mrow data-from-mathml='mrow' data-changed='added'>
                        <mo data-from-mathml='mo'>is equal to</mo>
                        <mrow data-from-mathml='mrow' data-changed='added'>
                        <mi data-from-mathml='mi'>y</mi>
                        <mo data-from-mathml='mo'>+</mo>
                        <mn data-from-mathml='mn'>1</mn>
                        </mrow>
                    </mrow>
                    </mrow>
                </mtd>
                </mtr>
                <mtr data-from-mathml='mtr'>
                <mtd data-from-mathml='mtr'>
                    <mrow data-from-mathml='mtr'>
                    <mrow data-from-mathml='mrow' data-changed='added'>
                        <mi data-from-mathml='mi'>x</mi>
                        <mo data-from-mathml='mo'>-</mo>
                        <mi data-from-mathml='mi'>y</mi>
                    </mrow>
                    <mrow data-from-mathml='mrow' data-changed='added'>
                        <mo data-from-mathml='mo'>is equal to</mo>
                        <mn data-from-mathml='mn'>4</mn>
                    </mrow>
                    </mrow>
                </mtd>
            </mtr>
        </system-of-equations>
        </math>";
    test_intent(mathml, intent, vec![])?;
    return Ok(());

}

#[test]
fn system_of_equations_infer_intent() -> Result<()> {
    let mathml = r#"<math>
            <mtable intent=":system-of-equations" columnalign="right left" columnspacing="0em" rowspacing="3pt">
                <mtr>
                    <mtd><mi>x</mi><mo>+</mo><mi>y</mi></mtd>
                    <mtd><mi/><mo>=</mo><mi>y</mi><mo>+</mo><mn>1</mn></mtd>
                </mtr>
                <mtr>
                    <mtd><mi>x</mi><mo>&#x2212;</mo><mi>y</mi></mtd>
                    <mtd><mi/><mo>=</mo><mn>4</mn></mtd>
                </mtr>
            </mtable>
            </math>"#;
    let intent = "<math data-from-mathml='math'>
        <system-of-equations data-from-mathml='mtable' columnalign='right left' columnspacing='0em' rowspacing='3pt' data-intent-property=':system-of-equations:'>
            <mtr data-from-mathml='mtr'>
                <mtd data-from-mathml='mtr'>
                    <mrow data-from-mathml='mtr'>
                    <mrow data-from-mathml='mrow' data-changed='added'>
                        <mi data-from-mathml='mi'>x</mi>
                        <mo data-from-mathml='mo'>+</mo>
                        <mi data-from-mathml='mi'>y</mi>
                    </mrow>
                    <mrow data-from-mathml='mrow' data-changed='added'>
                        <mo data-from-mathml='mo'>=</mo>
                        <mrow data-from-mathml='mrow' data-changed='added'>
                        <mi data-from-mathml='mi'>y</mi>
                        <mo data-from-mathml='mo'>+</mo>
                        <mn data-from-mathml='mn'>1</mn>
                        </mrow>
                    </mrow>
                    </mrow>
                </mtd>
                </mtr>
                <mtr data-from-mathml='mtr'>
                <mtd data-from-mathml='mtr'>
                    <mrow data-from-mathml='mtr'>
                    <mrow data-from-mathml='mrow' data-changed='added'>
                        <mi data-from-mathml='mi'>x</mi>
                        <mo data-from-mathml='mo'>-</mo>
                        <mi data-from-mathml='mi'>y</mi>
                    </mrow>
                    <mrow data-from-mathml='mrow' data-changed='added'>
                        <mo data-from-mathml='mo'>=</mo>
                        <mn data-from-mathml='mn'>4</mn>
                    </mrow>
                </mrow>
            </mtd>
            </mtr>
        </system-of-equations>
        </math>";
    test_intent(mathml, intent, vec![])?;
    return Ok(());

}

#[test]
fn lines() -> Result<()> {
    let mathml = r#"<math>
            <mtable intent=":lines">
                <mtr>
                    <mtd><mi>x</mi><mo>+</mo><mi>y</mi></mtd>
                    <mtd><mi/><mo>=</mo><mi>y</mi><mo>+</mo><mn>1</mn></mtd>
                </mtr>
                <mtr>
                    <mtd><mi>x</mi><mo>&#x2212;</mo><mi>y</mi></mtd>
                    <mtd><mi/><mo>=</mo><mn>4</mn></mtd>
                </mtr>
            </mtable>
            </math>"#;
    let intent = " <math data-from-mathml='math'>
        <lines data-from-mathml='mtable' data-intent-property=':lines:'>
            <mtr data-from-mathml='mtr'>
            <mtd data-from-mathml='mtr'>
                <mrow data-from-mathml='mtr'>
                <mrow data-from-mathml='mrow' data-changed='added'>
                    <mi data-from-mathml='mi'>x</mi>
                    <mo data-from-mathml='mo'>+</mo>
                    <mi data-from-mathml='mi'>y</mi>
                </mrow>
                <mrow data-from-mathml='mrow' data-changed='added'>
                    <mo data-from-mathml='mo'>=</mo>
                    <mrow data-from-mathml='mrow' data-changed='added'>
                    <mi data-from-mathml='mi'>y</mi>
                    <mo data-from-mathml='mo'>+</mo>
                    <mn data-from-mathml='mn'>1</mn>
                    </mrow>
                </mrow>
                </mrow>
            </mtd>
            </mtr>
            <mtr data-from-mathml='mtr'>
            <mtd data-from-mathml='mtr'>
                <mrow data-from-mathml='mtr'>
                <mrow data-from-mathml='mrow' data-changed='added'>
                    <mi data-from-mathml='mi'>x</mi>
                    <mo data-from-mathml='mo'>-</mo>
                    <mi data-from-mathml='mi'>y</mi>
                </mrow>
                <mrow data-from-mathml='mrow' data-changed='added'>
                    <mo data-from-mathml='mo'>=</mo>
                    <mn data-from-mathml='mn'>4</mn>
                </mrow>
                </mrow>
            </mtd>
            </mtr>
        </lines>
        </math>";
    test_intent(mathml, intent, vec![])?;
    return Ok(());
}


#[test]
fn mlabelledtr_bug_526() -> Result<()> {
    let mathml = r#"<math><mtable>
            <mlabeledtr>
                <mtd>
                <mtext>foo</mtext>
                </mtd>
                <mtd>
                    <mi>m</mi>
                </mtd>
            </mlabeledtr>
            </mtable></math>"#;
    let intent = "<math data-from-mathml='math'>
    <lines data-from-mathml='mtable'>
      <mlabeledtr data-from-mathml='mlabeledtr'>
        <TEMP_NAME>
          <mtext data-from-mathml='mtext'>foo</mtext>
        </TEMP_NAME>
        <mtd data-from-mathml='mlabeledtr'>
          <mrow data-from-mathml='mlabeledtr'>
            <mi data-from-mathml='mi'>m</mi>
          </mrow>
        </mtd>
      </mlabeledtr>
    </lines>
   </math>";
    test_intent(mathml, intent, vec![])?;
    return Ok(());
}

