1
$\begingroup$

I'm a strong proponent of unit tests and as such I'm using the MUnit framework from Wolfram Workbench on a regular basis. Many of the codes I'm working on also use TraditionalForm formatting in order to make objects look nicer in the Mathematica notebooks. Some of the formatting,, rules are pretty complicated which means that it would be nice to have unit tests for them as well.

However, I don't see a simple way to implement unit tests to check the TraditionalForm output of particular expression. For example, let us look at

MyObject/:MakeBoxes[MyObject[x_,y_],TraditionalForm]:=
If[x===y,SuperscriptBox["\[Alpha]",1],SuperscriptBox["\[Alpha]",2]]

Now I would like to write two tests like (pseudocode):

Representation[MyObject[1,1]] === SuperscriptBox[\[Alpha],1]
Representation[MyObject[1,2]] === SuperscriptBox[\[Alpha],2]

The best thing I was able to come up with is

TraditionalForm[MyObject[1,1]] === TraditionalForm[SuperscriptBox[\[Alpha],1]//DisplayForm]
TraditionalForm[MyObject[1,2]] === TraditionalForm[SuperscriptBox[\[Alpha],2]//DisplayForm]

Which however still doesn't return True. When I run Mathematica kernel without the frontend I see that both

TraditionalForm[MyObject[1,1]]

and

TraditionalForm[SuperscriptBox[\[Alpha],1]//DisplayForm]

evaluate to

//TraditionalForm= α^1

but for some reason Mathematica doesn't consider them to be equal.

Is there some handy function to output the TraditionalForm of MyObject[1,1] and MyObject[1,2] in terms of SuperscriptBox and \[Alpha]?

Or may be there is some standard way to write unit tests for the TraditionalForm representation that I'm completely missing?

$\endgroup$

1 Answer 1

1
$\begingroup$

TraditionalForm and DisplayForm are just "wrappers". If you look at FullForm of expressions compared in question, you'll see that although they look the same, internally they are represented differently:

MyObject /: MakeBoxes[MyObject[x_, y_], TraditionalForm] :=
    If[x === y, SuperscriptBox["α", 1], SuperscriptBox["α", 2]]

TraditionalForm[MyObject[1, 1]]
TraditionalForm[MyObject[1, 1]] // FullForm
(*
Out[3]//TraditionalForm=
    α^1
Out[4]//FullForm=
    TraditionalForm[MyObject[1, 1]]
*)

TraditionalForm[SuperscriptBox["α", 1] // DisplayForm]
TraditionalForm[SuperscriptBox["α", 1] // DisplayForm] // FullForm
(*
Out[5]//TraditionalForm=
    α^1
Out[6]//FullForm=
    TraditionalForm[DisplayForm[SuperscriptBox["\[Alpha]", 1]]]
*)

If you want to compare box representation of expressions, you should explicitly convert them to boxes.

MakeBoxes[MyObject[1, 1], TraditionalForm] === SuperscriptBox["α", 1]
(* True *)

ToBoxes[MyObject[1, 1], TraditionalForm] === FormBox[SuperscriptBox["α", 1], TraditionalForm]
(* True *)
$\endgroup$
1
  • $\begingroup$ Thanks, this actually solves my problem! $\endgroup$
    – vsht
    Dec 5, 2014 at 9:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.