Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions CSharpMath.Editor.Tests/MathKeyboardTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using CSharpMath.Editor;
using CSharpMath.FrontEnd;
using CSharpMath.Tests.FrontEnd;
using Xunit;

public class MathKeyboardTests {

public const float FontSize = 20;
public static readonly TestFont Font = new TestFont(FontSize);
private static readonly TypesettingContext<TestFont, char> context = TestTypesettingContexts.Instance;

[Fact]
// https://github.com/verybadcat/CSharpMath/issues/39
public void SlashInsideOfPowerTest() {
var keyboard = new MathKeyboard<TestFont, char>(context);
keyboard.KeyPress(MathKeyboardInput.SmallX);
keyboard.KeyPress(MathKeyboardInput.Power);
keyboard.KeyPress(MathKeyboardInput.D2);
keyboard.KeyPress(MathKeyboardInput.Slash);
Assert.Equal("x^{\\frac{2}{■}}", keyboard.LaTeX);
}

[Fact]
public void SlashInsideOfPowerTest2() {
var keyboard = new MathKeyboard<TestFont, char>(context);
keyboard.KeyPress(MathKeyboardInput.SmallX);
keyboard.KeyPress(MathKeyboardInput.Power);
keyboard.KeyPress(MathKeyboardInput.D1);
keyboard.KeyPress(MathKeyboardInput.D2);
keyboard.KeyPress(MathKeyboardInput.D3);
keyboard.KeyPress(MathKeyboardInput.Slash);
Assert.Equal("x^{\\frac{123}{■}}", keyboard.LaTeX);
}

[Fact]
public void DefaultNumeratorTest() {
var keyboard = new MathKeyboard<TestFont, char>(context);
keyboard.KeyPress(MathKeyboardInput.Slash);
Assert.Equal("\\frac{1}{■}", keyboard.LaTeX);
}

[Fact]
// https://github.com/kostub/MathEditor/issues/18
public void SlashAfterSlashTest() {
var keyboard = new MathKeyboard<TestFont, char>(context);
keyboard.KeyPress(MathKeyboardInput.D4);
keyboard.KeyPress(MathKeyboardInput.Slash);
keyboard.KeyPress(MathKeyboardInput.D4);
keyboard.KeyPress(MathKeyboardInput.Slash);
Assert.Equal("\\frac{4}{\\frac{4}{■}}", keyboard.LaTeX);
}
}
7 changes: 3 additions & 4 deletions CSharpMath.Editor/Keyboards/MathKeyboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ void HandleSlashButton() {
//Add the number to the beginning of the list
numerator.Insert(0, a);
}
if (current.AtomIndex == _insertionIndex.AtomIndex) {
if (numerator.Count == 0) {
// so we didn't really find any numbers before this, so make the numerator 1
numerator.Add(MathAtoms.ForCharacter('1'));
if (!current.AtBeginningOfLine) {
Expand All @@ -259,9 +259,8 @@ void HandleSlashButton() {
}
}
} else
// delete stuff in the Mathlist from current to insertionIndex
MathList.RemoveAtoms(
new MathListRange(current, _insertionIndex.AtomIndex - current.AtomIndex));
// delete stuff in the Mathlist
MathList.RemoveAtoms(new MathListRange(current, numerator.Count));

//Create the fraction
var frac = new Fraction { Numerator = numerator, Denominator = MathAtoms.PlaceholderList };
Expand Down